1
0
mirror of https://github.com/CumulusNetworks/ifupdown2.git synced 2024-05-06 15:54:50 +00:00
Roopa Prabhu a070c90ec3 Multiple fixes and cleanup
Ticket: CM-3346
Reviewed By:
Testing Done: Tested ifupdown2 sanity

- moved 'admin up' delays that we introduced recently to be
configurable via two ifupdown2.conf attributes
    # Let link master (bridges, bonds) own the link state of slaves
    link_master_slave=1

    # Delay admin state change till the end
    delay_admin_state_change=0

- reduced some redundant traversal of dependency trees

- fixed a few bugs in query check
2014-12-17 12:39:38 -08:00

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 interface %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 interface %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())