core.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. *******************
  2. Netlink Core Module
  3. *******************
  4. .. py:module:: netlink.core
  5. Examples::
  6. import netlink.core as netlink
  7. ===============
  8. Object
  9. ===============
  10. .. py:class:: Object
  11. Base class for all classes representing a cacheable object
  12. Example::
  13. obj = netlink.Object("route/link", "link")
  14. .. py:method:: clone
  15. Clone the object and return a duplicate (used for COW)
  16. .. py:method:: dump([params=None])
  17. Call the libnl internal dump mechanism to dump the object
  18. according to the parameters specified.
  19. .. py:method:: apply(attr, val)
  20. Applies a attribute=value pair and modifies the object accordingly.
  21. Example::
  22. obj.apply("mtu", 1200) # Sets attribute mtu to 1200 (link obj)
  23. :raises: KeyError if attribute is unknown
  24. :raises: ImmutableError if attribute is not mutable
  25. .. py:attribute:: mark
  26. True if the object is marked, otherwise False.
  27. .. py:attribute:: shared
  28. True if the object is used by multiple parties, otherwise False.
  29. .. py:attribute:: refcnt
  30. Number of users sharing a reference to the object
  31. :rtype: int
  32. .. py:attribute:: attrs
  33. List of attributes
  34. :rtype: list of strings
  35. ===============
  36. Cache
  37. ===============
  38. .. py:class:: Cache
  39. Base class for all cache implementations.
  40. A cache is a collection of cacheable objects which is typically used
  41. by netlink protocols which handle any kind of object, e.g. network
  42. links, network addresses, neighbours, ...
  43. .. py:method:: subset(filter)
  44. Returns a new cache containing the subset which matches the
  45. provided filter.
  46. :raises: ValueError if no filter is specified
  47. :rtype: :py:class:`Cache`
  48. .. py:method:: dump([params=None, filter=None])
  49. Calls the libnl internal dump mechanism to dump the cache according
  50. to the parameters and filter specified.
  51. .. py:method:: clear()
  52. Remove and possibly destroy all objects in the cache
  53. .. py:method:: refill([socket=None]) -> :py:class:`Cache`
  54. Clears and refills the cache with the content which is provided by
  55. the kernel, e.g. for a link cache this would mean refilling the
  56. cache with all configured network links.
  57. .. py:method:: provide()
  58. Caches which have been "provided" are made available to other users
  59. (of the same application context) which "require" it. F.e. a link
  60. cache is generally provided to allow others to translate interface
  61. indexes to link names
  62. .. py:method:: unprovide()
  63. No longer make the cache available to others. If the cache has been
  64. handed out already, that reference will still be valid.
  65. ===============
  66. AbstractAddress
  67. ===============
  68. .. py:class:: AbstractAddress
  69. Abstract representation of an address. This class is not to be mistaken
  70. with :py:class:`route.Address` which represents a configured network
  71. address. This class represents the actual address in a family independent
  72. way::
  73. addr = netlink.AbstractAddress('127.0.0.1/8')
  74. print addr # => '127.0.0.1/8'
  75. print addr.prefixlen # => '8'
  76. print addr.family # => 'inet'
  77. print len(addr) # => '4' (32bit ipv4 address)
  78. a = netlink.AbstractAddress('10.0.0.1/24')
  79. b = netlink.AbstractAddress('10.0.0.2/24')
  80. print a == b # => False
  81. .. py:attribute:: prefixlen
  82. Length of prefix in number of bits.
  83. :rtype: int
  84. .. py:attribute:: family
  85. The family type of the address. Setting the address family can be
  86. done with a string or a :py:class:`AddressFamily` object.
  87. :rtype: :py:class:`AddressFamily`
  88. .. py:attribute:: shared
  89. True if address is in use by multiple callers, otherwise False
  90. :rtype: bool
  91. ===============
  92. AddressFamily
  93. ===============
  94. .. py:class:: AddressFamily
  95. Address family representation::
  96. af = netlink.AddressFamily('inet6')
  97. # raises:
  98. # - ValueError if family name is not known
  99. # - TypeError if invalid type is specified for family
  100. print af # => 'inet6' (string representation)
  101. print int(af) # => 10 (numeric representation)
  102. print repr(af) # => AddressFamily('inet6')
  103. ===============
  104. Exceptions
  105. ===============
  106. .. py:exception:: NetlinkError
  107. Generic exception raised by netlink modules.
  108. .. py:exception:: KernelError
  109. Raised if an error occured while communicating with the kernel. Contains
  110. the error code returning which is automatically included in the error
  111. message.
  112. .. py:exception:: ImmutableError
  113. Raised if an attribute is modified which is marked immutable.
  114. ===============
  115. Socket
  116. ===============
  117. .. py:class:: Socket
  118. Netlink socket.
  119. Note: It is not required to manually create and connect netlink sockets
  120. when using caches. The caches will automatically lookup or create a
  121. socket as needed.
  122. .. py:attribute:: local_port
  123. Local port (address) of netlink socket
  124. .. py:attribute:: peer_port
  125. Peer port (remote address) of netlink socket. If set, all messages
  126. will be sent to that peer.
  127. .. py:method:: connect(proto)
  128. Connect the netlink socket using the specified netlink protocol::
  129. sock.connect(netlink.NETLINK_ROUTE)
  130. .. py:method:: disconnect()
  131. Disconnect the socket
  132. .. py:method:: set_bufsize(rx, tx)
  133. Sets the size of the socket buffer