mirror of
https://github.com/CumulusNetworks/ifupdown2.git
synced 2024-05-06 15:54:50 +00:00
Ticket: CM-3346 Reviewed By: Testing Done: Sanity test + test new bridge format There are a bunch of open issues with `vlan` interface handling. Below is the format. auto swp1 iface swp1 bridge-access 300 mstpctl-pathcost 0 mstpctl-adminedge yes mstpctl-autoedge yes mstpctl-p2p yes mstpctl-bpduguard yes mstpctl-treeprio 64 mstpctl-network yes mstpctl-bpdufilter yes auto swp2 iface swp2 bridge-vids 301 bridge-pvid 302 bridge-pathcost 10 bridge-priority 10 bridge-multicast-router 0 bridge-multicast-fast-leave 1 auto br0 iface br0 bridge-vlan-aware yes bridge-stp on bridge-ports swp1 swp2 bridge-vids 2001 auto br0.2001 iface br0.2001 address 10.0.14.2 hwaddress 00:03:00:00:00:12 address-virtual 00:00:5e:00:01:01 11.0.4.1/24 auto br0.2001 vlan br0.2001 bridge-igmp-querier-src 172.16.101.1
108 lines
3.3 KiB
Python
108 lines
3.3 KiB
Python
#!/usr/bin/env python
|
|
#
|
|
# Copyright 2014 Cumulus Networks, Inc. All rights reserved.
|
|
#
|
|
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
|
|
#
|
|
#
|
|
|
|
from os import getpid
|
|
from socket import AF_UNSPEC
|
|
from iff import IFF_UP
|
|
from rtnetlink import *
|
|
import os
|
|
import ifupdownmain
|
|
|
|
class rtnetlinkApi(RtNetlink):
|
|
|
|
bind_done = False
|
|
|
|
def __init__(self, pid):
|
|
RtNetlink.__init__(self, pid)
|
|
self.logger = logging.getLogger('ifupdown.' +
|
|
self.__class__.__name__)
|
|
self.bind(0, None)
|
|
self.bind_done = True
|
|
self.ifindexmap = {}
|
|
|
|
def do_bind(self):
|
|
if self.bind_done:
|
|
return True
|
|
self.bind(0, None)
|
|
self.bind_done = True
|
|
|
|
def get_ifindex(self, ifname):
|
|
ifindex = self.ifindexmap.get(ifname)
|
|
if not ifindex:
|
|
with open('/sys/class/net/%s/ifindex' %ifname, 'r') as f:
|
|
ifindex = int(f.read())
|
|
self.ifindexmap[ifname] = ifindex
|
|
return ifindex
|
|
|
|
def create_vlan(self, link, ifname, vlanid):
|
|
self.logger.info('rtnetlink: creating vlan %s' %ifname)
|
|
if ifupdownmain.ifupdownFlags.DRYRUN:
|
|
return
|
|
try:
|
|
ifindex = self.get_ifindex(link)
|
|
except Exception, e:
|
|
raise Exception('cannot determine ifindex for link %s (%s)'
|
|
%(link, str(e)))
|
|
|
|
ifm = Ifinfomsg(AF_UNSPEC)
|
|
rtas = {IFLA_IFNAME: ifname,
|
|
IFLA_LINK : ifindex,
|
|
IFLA_LINKINFO : {
|
|
IFLA_INFO_KIND : 'vlan',
|
|
IFLA_INFO_DATA : {
|
|
IFLA_VLAN_ID : vlanid,
|
|
}
|
|
}
|
|
}
|
|
token = self.request(RTM_NEWLINK,
|
|
NLM_F_CREATE | NLM_F_REQUEST | NLM_F_ACK, ifm, rtas)
|
|
self.process_wait([token])
|
|
|
|
def create_macvlan(self, ifname, link, mode='private'):
|
|
self.logger.info('rtnetlink: creating macvlan %s' %ifname)
|
|
if ifupdownmain.ifupdownFlags.DRYRUN:
|
|
return
|
|
try:
|
|
ifindex = self.get_ifindex(link)
|
|
except Exception, e:
|
|
raise Exception('cannot determine ifindex for link %s (%s)'
|
|
%(link, str(e)))
|
|
|
|
ifm = Ifinfomsg(AF_UNSPEC)
|
|
rtas = {IFLA_IFNAME: ifname,
|
|
IFLA_LINK : ifindex,
|
|
IFLA_LINKINFO : {
|
|
IFLA_INFO_KIND : 'macvlan',
|
|
IFLA_INFO_DATA : {
|
|
IFLA_MACVLAN_MODE : MACVLAN_MODE_PRIVATE,
|
|
}
|
|
}
|
|
}
|
|
token = self.request(RTM_NEWLINK, NLM_F_CREATE | NLM_F_REQUEST |
|
|
NLM_F_ACK, ifm, rtas)
|
|
self.process_wait([token])
|
|
|
|
def link_set(self, ifname, state):
|
|
flags = 0
|
|
self.logger.info('rtnetlink: setting link %s %s' %(ifname, state))
|
|
if ifupdownmain.ifupdownFlags.DRYRUN:
|
|
return
|
|
|
|
if state == "up":
|
|
flags |= IFF_UP
|
|
else:
|
|
flags &= ~IFF_UP
|
|
|
|
ifm = Ifinfomsg(AF_UNSPEC, ifi_change=IFF_UP, ifi_flags=flags)
|
|
rtas = {IFLA_IFNAME: ifname}
|
|
|
|
token = self.request(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK, ifm, rtas)
|
|
self.process_wait([token])
|
|
|
|
rtnl_api = rtnetlinkApi(os.getpid())
|