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=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¶
-
class
pyroute2.netlink.nlsocket.
Marshal
¶ Generic marshalling class
-
parse
(data, seq=None, callback=None)¶ Parse string data.
At this moment all transport, except of the native Netlink is deprecated in this library, so we should not support any defragmentation on that level
-
-
class
pyroute2.netlink.nlsocket.
NetlinkMixin
(family=16, port=None, pid=None, fileno=None, all_ns=False)¶ Generic netlink socket
-
get
(bufsize=16384, msg_seq=0, terminate=None, callback=None)¶ Get parsed messages list. If msg_seq is given, return only messages with that msg[‘header’][‘sequence_number’], saving all other messages into self.backlog.
The routine is thread-safe.
The bufsize parameter can be:
- -1: bufsize will be calculated from the first 4 bytes of
- the network data
- 0: bufsize will be calculated from SO_RCVBUF sockopt
- int >= 0: just a bufsize
-
get_policy_map
(policy=None)¶ Return policy for a given message type or for all message types. Policy parameter can be either int, or a list of ints. Always return dictionary.
-
put
(msg, msg_type, msg_flags=1, addr=(0, 0), msg_seq=0, msg_pid=None)¶ Construct a message from a dictionary and send it to the socket. Parameters:
- msg – the message in the dictionary format
- msg_type – the message type
- msg_flags – the message flags to use in the request
- addr – sendto() addr, default (0, 0)
- msg_seq – sequence number to use
- msg_pid – pid to use, if None – use os.getpid()
Example:
s = IPRSocket() s.bind() s.put({'index': 1}, RTM_GETLINK) s.get() s.close()
Please notice, that the return value of s.get() can be not the result of s.put(), but any broadcast message. To fix that, use msg_seq – the response must contain the same msg[‘header’][‘sequence_number’] value.
-
register_callback
(callback, predicate=<function NetlinkMixin.<lambda>>, args=None)¶ Register a callback to run on a message arrival.
Callback is the function that will be called with the message as the first argument. Predicate is the optional callable object, that returns True or False. Upon True, the callback will be called. Upon False it will not. Args is a list or tuple of arguments.
Simplest example, assume ipr is the IPRoute() instance:
# create a simplest callback that will print messages def cb(msg): print(msg) # register callback for any message: ipr.register_callback(cb)
More complex example, with filtering:
# Set object's attribute after the message key def cb(msg, obj): obj.some_attr = msg["some key"] # Register the callback only for the loopback device, index 1: ipr.register_callback(cb, lambda x: x.get('index', None) == 1, (self, ))
Please note: you do not need to register the default 0 queue to invoke callbacks on broadcast messages. Callbacks are iterated before messages get enqueued.
-
register_policy
(policy, msg_class=None)¶ Register netlink encoding/decoding policy. Can be specified in two ways: nlsocket.register_policy(MSG_ID, msg_class) to register one particular rule, or nlsocket.register_policy({MSG_ID1: msg_class}) to register several rules at once. E.g.:
policy = {RTM_NEWLINK: ifinfmsg, RTM_DELLINK: ifinfmsg, RTM_NEWADDR: ifaddrmsg, RTM_DELADDR: ifaddrmsg} nlsocket.register_policy(policy)
One can call register_policy() as many times, as one want to – it will just extend the current policy scheme, not replace it.
-
unregister_callback
(callback)¶ Remove the first reference to the function from the callback register
-
unregister_policy
(policy)¶ Unregister policy. Policy can be:
- int – then it will just remove one policy
- list or tuple of ints – remove all given
- dict – remove policies by keys from dict
In the last case the routine will ignore dict values, it is implemented so just to make it compatible with get_policy_map() return value.
-