Skip to content

Commit

Permalink
Refactoring get_primary_interface to use get_list_of_routes (#1377)
Browse files Browse the repository at this point in the history
* refactoring get_primary_interface to use get_list_of_routes

* following jasonzio advice to determine default route and primary values

* converting candidates to list for python 3.4; primary_route should be the candidate with the lowest metric

* converting candidates to list during generation instead of multiple later conversion

* using get_metric instead of lambda to assign primary_route

* cleaning module from unused variables; expliciting primary_interface; we don't actually need primary_metric
  • Loading branch information
elfosardo authored and vrdmr committed Nov 1, 2018
1 parent c8182b3 commit 3c8084e
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions azurelinuxagent/common/osutil/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,8 @@ def _build_route_list(proc_net_route):
route_list.append(route_obj)
return route_list

def read_route_table(self):
@staticmethod
def read_route_table():
"""
Return a list of strings comprising the route table, including column headers. Each line is stripped of leading
or trailing whitespace but is otherwise unmolested.
Expand All @@ -863,7 +864,8 @@ def read_route_table(self):

return []

def get_list_of_routes(self, route_table):
@staticmethod
def get_list_of_routes(route_table):
"""
Construct a list of all network routes known to this system.
Expand Down Expand Up @@ -893,43 +895,26 @@ def get_primary_interface(self):
RTF_GATEWAY = 0x02
DEFAULT_DEST = "00000000"

hdr_iface = "Iface"
hdr_dest = "Destination"
hdr_flags = "Flags"
hdr_metric = "Metric"

idx_iface = -1
idx_dest = -1
idx_flags = -1
idx_metric = -1
primary = None
primary_metric = None
primary_interface = None

if not self.disable_route_warning:
logger.info("Examine /proc/net/route for primary interface")
with open('/proc/net/route') as routing_table:
idx = 0
for header in filter(lambda h: len(h) > 0, routing_table.readline().strip(" \n").split("\t")):
if header == hdr_iface:
idx_iface = idx
elif header == hdr_dest:
idx_dest = idx
elif header == hdr_flags:
idx_flags = idx
elif header == hdr_metric:
idx_metric = idx
idx = idx + 1
for entry in routing_table.readlines():
route = entry.strip(" \n").split("\t")
if route[idx_dest] == DEFAULT_DEST and int(route[idx_flags]) & RTF_GATEWAY == RTF_GATEWAY:
metric = int(route[idx_metric])
iface = route[idx_iface]
if primary is None or metric < primary_metric:
primary = iface
primary_metric = metric

if primary is None:
primary = ''

route_table = DefaultOSUtil.read_route_table()

def is_default(route):
return route.destination == DEFAULT_DEST and int(route.flags) & RTF_GATEWAY == RTF_GATEWAY

candidates = list(filter(is_default, DefaultOSUtil.get_list_of_routes(route_table)))

if len(candidates) > 0:
def get_metric(route):
return int(route.metric)
primary_route = min(candidates, key=get_metric)
primary_interface = primary_route.interface

if primary_interface is None:
primary_interface = ''
if not self.disable_route_warning:
with open('/proc/net/route') as routing_table_fh:
routing_table_text = routing_table_fh.read()
Expand All @@ -939,9 +924,9 @@ def get_primary_interface(self):
logger.warn('Primary interface examination will retry silently')
self.disable_route_warning = True
else:
logger.info('Primary interface is [{0}]'.format(primary))
logger.info('Primary interface is [{0}]'.format(primary_interface))
self.disable_route_warning = False
return primary
return primary_interface

def is_primary_interface(self, ifname):
"""
Expand Down

0 comments on commit 3c8084e

Please sign in to comment.