File: //opt/alt/python37/lib/python3.7/site-packages/pyroute2/netlink/__pycache__/nlsocket.cpython-37.pyc
B
��f=� � @ sR d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
Z
ddlZddlm
Z
ddlmZmZmZmZmZmZmZ ddlmZ ddlmZmZ ddlmZ ddlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ dd l0m1Z1m2Z2m3Z3m4Z4m5Z5 ydd
l6m6Z6 W n" e7k
�rR dd
l8m6Z6 Y nX e�9e:�Z;e�<dd�Z=d
Z>G dd� d�Z?G dd� d�Z@edddd�ZAG dd� d�ZBG dd� d�ZCG dd� d�ZDG dd� deD�ZEG dd� deD�ZFG dd � d �ZGG d!d"� d"�ZHG d#d$� d$eI�ZJG d%d&� d&eK�ZLG d'd(� d(eG�ZMG d)d*� d*eG�ZNG d+d,� d,eN�ZOdS )-a�
Base netlink socket and marshal
===============================
All the netlink providers are derived from the socket
class, so they provide normal socket API, including
`getsockopt()`, `setsockopt()`, they can be used in
poll/select I/O loops etc.
asynchronous I/O
----------------
To run async reader thread, one should call
`NetlinkSocket.bind(async_cache=True)`. In that case
a background thread will be launched. The thread will
automatically collect all the messages and store
into a userspace buffer.
.. note::
There is no need to turn on async I/O, if you
don't plan to receive broadcast messages.
ENOBUF and async I/O
--------------------
When Netlink messages arrive faster than a program
reads then from the socket, the messages overflow
the socket buffer and one gets ENOBUF on `recv()`::
... self.recv(bufsize)
error: [Errno 105] No buffer space available
One way to avoid ENOBUF, is to use async I/O. Then the
library not only reads and buffers all the messages, but
also re-prioritizes threads. Suppressing the parser
activity, the library increases the response delay, but
spares CPU to read and enqueue arriving messages as
fast, as it is possible.
With logging level DEBUG you can notice messages, that
the library started to calm down the parser thread::
DEBUG:root:Packet burst: the reader thread priority
is increased, beware of delays on netlink calls
Counters: delta=25 qsize=25 delay=0.1
This state requires no immediate action, but just some
more attention. When the delay between messages on the
parser thread exceeds 1 second, DEBUG messages become
WARNING ones::
WARNING:root:Packet burst: the reader thread priority
is increased, beware of delays on netlink calls
Counters: delta=2525 qsize=213536 delay=3
This state means, that almost all the CPU resources are
dedicated to the reader thread. It doesn't mean, that
the reader thread consumes 100% CPU -- it means, that the
CPU is reserved for the case of more intensive bursts. The
library will return to the normal state only when the
broadcast storm will be over, and then the CPU will be
100% loaded with the parser for some time, when it will
process all the messages queued so far.
when async I/O doesn't help
---------------------------
Sometimes, even turning async I/O doesn't fix ENOBUF.
Mostly it means, that in this particular case the Python
performance is not enough even to read and store the raw
data from the socket. There is no workaround for such
cases, except of using something *not* Python-based.
One can still play around with SO_RCVBUF socket option,
but it doesn't help much. So keep it in mind, and if you
expect massive broadcast Netlink storms, perform stress
testing prior to deploy a solution in the production.
classes
-------
� N)�partial)�MSG_DONTWAIT�MSG_PEEK� MSG_TRUNC� SO_RCVBUF� SO_SNDBUF�
SOCK_DGRAM�
SOL_SOCKET)�config)�DEFAULT_RCVBUF�AddrPool)�
AF_NETLINK)�NETLINK_ADD_MEMBERSHIP�NETLINK_DROP_MEMBERSHIP�NETLINK_EXT_ACK�NETLINK_GENERIC�NETLINK_GET_STRICT_CHK�NETLINK_LISTEN_ALL_NSID� NLM_F_ACK�NLM_F_ACK_TLVS�
NLM_F_DUMP�NLM_F_DUMP_INTR�NLM_F_MULTI�
NLM_F_REQUEST�
NLMSG_DONE�NLMSG_ERROR�SOL_NETLINK�mtypes�nlmsg�nlmsgerr)�ChaoticException�NetlinkDecodeError�NetlinkDumpInterrupted�NetlinkError�NetlinkHeaderDecodeError)�Queue�Stats)�qsize�delta�delayi � c @ s, e Zd Zdd� Zdd� Zdd� Zdd� Zd S )
�CompileContextc C s || _ g | j _d S )N)�netlink_socket�compiled)�selfr+ � r. �J/opt/alt/python37/lib/python3.7/site-packages/pyroute2/netlink/nlsocket.py�__init__� s zCompileContext.__init__c C s | S )Nr. )r- r. r. r/ � __enter__� s zCompileContext.__enter__c C s | � � d S )N)�close)r- �exc_type� exc_value� tracebackr. r. r/ �__exit__� s zCompileContext.__exit__c C s d | j _d S )N)r+ r, )r- r. r. r/ r2 � s zCompileContext.closeN)�__name__�
__module__�__qualname__r0 r1 r6 r2 r. r. r. r/ r* � s r* c @ sZ e Zd ZdZi ZdZdZdZdZdZ e
ZeZ
dd� Zdd� Zdd � Zdd
d�Zdd
� ZdS )�Marshalz#
Generic marshalling class
NFc C s&