diff options
author | Nicolas PLANEL <nicolas.planel@enovance.com> | 2013-10-10 22:27:44 (GMT) |
---|---|---|
committer | Thomas Graf <tgraf@suug.ch> | 2013-10-22 12:20:41 (GMT) |
commit | defebe30c657aee9a40c4cf331659e7a285c2f65 (patch) | |
tree | a7391061e015ab599724ebd281bee740a7b74f92 /python | |
parent | cb034e12a42d95c25b9029e577e4581c3a1e4399 (diff) | |
download | libnl-defebe30c657aee9a40c4cf331659e7a285c2f65.zip libnl-defebe30c657aee9a40c4cf331659e7a285c2f65.tar.gz libnl-defebe30c657aee9a40c4cf331659e7a285c2f65.tar.bz2 |
python: add basic netlink protocol bridge interface support
Implements basic bridge interface support using netlink protocol
Signed-off-by: Nicolas PLANEL <nicolas.planel@enovance.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>
Diffstat (limited to 'python')
-rw-r--r-- | python/netlink/route/capi.i | 22 | ||||
-rw-r--r-- | python/netlink/route/link.py | 12 | ||||
-rw-r--r-- | python/netlink/route/links/bridge.py | 84 |
3 files changed, 118 insertions, 0 deletions
diff --git a/python/netlink/route/capi.i b/python/netlink/route/capi.i index 356df12..d4c4b6a 100644 --- a/python/netlink/route/capi.i +++ b/python/netlink/route/capi.i @@ -5,6 +5,7 @@ #include <netlink/route/link/vlan.h> #include <netlink/route/link/macvlan.h> #include <netlink/route/link/vxlan.h> +#include <netlink/route/link/bridge.h> #include <netlink/route/link/inet.h> #include <netlink/route/tc.h> @@ -144,6 +145,9 @@ extern int rtnl_link_set_stat(struct rtnl_link *, const unsigned int, const uint extern int rtnl_link_set_type(struct rtnl_link *, const char *); extern char *rtnl_link_get_type(struct rtnl_link *); +extern int rtnl_link_enslave(struct nl_sock * sock, struct rtnl_link * master, struct rtnl_link * slave); +extern int rtnl_link_release(struct nl_sock * sock, struct rtnl_link * slave); + /* <netlink/route/link/vlan.h> */ struct vlan_map @@ -248,6 +252,24 @@ extern int rtnl_link_vxlan_get_l3miss(struct rtnl_link *); extern int rtnl_link_vxlan_enable_l3miss(struct rtnl_link *); extern int rtnl_link_vxlan_disable_l3miss(struct rtnl_link *); +/* <netlink/route/link/bridge.h> */ + +extern int rtnl_link_is_bridge(struct rtnl_link *); +extern int rtnl_link_bridge_has_ext_info(struct rtnl_link *); + +extern int rtnl_link_bridge_set_port_state(struct rtnl_link *, uint8_t ); +extern int rtnl_link_bridge_get_port_state(struct rtnl_link *); + +extern int rtnl_link_bridge_set_priority(struct rtnl_link *, uint16_t); +extern int rtnl_link_bridge_get_priority(struct rtnl_link *); + +extern int rtnl_link_bridge_set_cost(struct rtnl_link *, uint32_t); +extern int rtnl_link_bridge_get_cost(struct rtnl_link *, uint32_t *); + +extern int rtnl_link_bridge_unset_flags(struct rtnl_link *, unsigned int); +extern int rtnl_link_bridge_set_flags(struct rtnl_link *, unsigned int); +extern int rtnl_link_bridge_get_flags(struct rtnl_link *); + /* <netlink/route/link/inet.h> */ %cstring_output_maxsize(char *buf, size_t len) extern const char *rtnl_link_inet_devconf2str(int, char *buf, size_t len); diff --git a/python/netlink/route/link.py b/python/netlink/route/link.py index 36d0e9d..5ec14b2 100644 --- a/python/netlink/route/link.py +++ b/python/netlink/route/link.py @@ -374,6 +374,18 @@ class Link(netlink.Object): return capi.rtnl_link_get_stat(self._rtnl_link, stat) + def enslave(self, slave, sock=None): + if not sock: + sock = netlink.lookup_socket(netlink.NETLINK_ROUTE) + + return capi.rtnl_link_enslave(sock._sock, self._rtnl_link, slave._rtnl_link) + + def release(self, slave, sock=None): + if not sock: + sock = netlink.lookup_socket(netlink.NETLINK_ROUTE) + + return capi.rtnl_link_release(sock._sock, self._rtnl_link, slave._rtnl_link) + def add(self, sock=None, flags=None): if not sock: sock = netlink.lookup_socket(netlink.NETLINK_ROUTE) diff --git a/python/netlink/route/links/bridge.py b/python/netlink/route/links/bridge.py new file mode 100644 index 0000000..09d9334 --- /dev/null +++ b/python/netlink/route/links/bridge.py @@ -0,0 +1,84 @@ +# +# Copyright (c) 2013 Nicolas PLANEL <nicolas.planel@enovance.com> +# + +"""BRIDGE network link + +""" + +from __future__ import absolute_import + +from ... import core as netlink +from .. import capi as capi + +class BRIDGELink(object): + def __init__(self, link): + self._link = link + self._has_ext_info = capi.rtnl_link_bridge_has_ext_info(self._link) + self._port_state_values = ['disabled','listening','learning','forwarding','blocking'] + + def bridge_assert_ext_info(self): + if self._has_ext_info == False: + print """ + Please update your kernel to be able to call this method. + Your current kernel bridge version is too old to support this extention. + """ + raise RuntimeWarning() + + def port_state2str(self, state): + return self._port_state_values[state] + + def str2port_state(self, str): + for value, port in enumerate(self._port_state_values): + if str.lower() == port: + return value + raise ValueError() + + @property + @netlink.nlattr(type=int) + def port_state(self): + """bridge state : + %s + """ % (self.port_state) + return capi.rtnl_link_bridge_get_state(self._link) + + @port_state.setter + def port_state(self, state): + capi.rtnl_link_bridge_set_state(self._link, int(state)) + + @property + @netlink.nlattr(type=int) + def priority(self): + """bridge prio + """ + bridge_assert_ext_info() + return capi.rtnl_link_bridge_get_prio(self._link) + + @priority.setter + def priority(self, prio): + bridge_assert_ext_info() + if prio < 0 or prio >= 2**16: + raise ValueError() + capi.rtnl_link_bridge_set_prio(self._link, int(prio)) + + @property + @netlink.nlattr(type=int) + def cost(self): + """bridge prio + """ + bridge_assert_ext_info() + return capi.rtnl_link_bridge_get_cost(self._link) + + @cost.setter + def cost(self, cost): + bridge_assert_ext_info() + if cost < 0 or cost >= 2**32: + raise ValueError() + capi.rtnl_link_bridge_set_cost(self._link, int(cost)) + + def brief(self): + return 'bridge-has-ext-info {0}'.format(self._has_ext_info) + +def init(link): + link.bridge = BRIDGELink(link._rtnl_link) + return link.bridge |