2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-29 15:28:56 +00:00

Replace most uses of and references to "ifconfig" by "ip".

It's becoming more common that OSes include "ip" but not "ifconfig", so
it's best to avoid using the latter.  This commit removes most references
to "ifconfig" and replaces them by "ip".  It also adds a build-time check
to make it harder to introduce new uses of "ifconfig".

There are important differences between "ifconfig" and "ip":

- An "ifconfig" command that sets an IP address also brings the interface
  up, but a similar "ip addr add" command does not, so it is often necessary
  (or at least precautionary) to add an "ip link set <dev> up" command.

- "ifconfig" can infer a netmask from an IP adddress, but "ip" always
  assumes /32 if none is given.

- "ifconfig" with address 0.0.0.0 removes any configured IP address, but
  "ip addr add" does not, so "ifconfig <dev> 0.0.0.0" must be replaced by
  "ip addr del" or "ip addr flush".

Signed-off-by: Ben Pfaff <blp@ovn.org>
This commit is contained in:
Ben Pfaff
2017-06-01 07:21:41 -07:00
parent e2d12c07df
commit 0b2c7e690a
18 changed files with 123 additions and 71 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2011, 2012 Nicira, Inc.
# Copyright (c) 2011, 2012, 2017 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -107,23 +107,42 @@ def interface_up(iface):
"""
This function brings given iface up.
"""
ret, _out, _err = start_process(["ifconfig", iface, "up"])
ret, _out, _err = start_process(["ip", "link", "set", iface, "up"])
return ret
def interface_assign_ip(iface, ip_addr, mask):
"""
This function allows to assign IP address to an interface. If mask is an
empty string then ifconfig will decide what kind of mask to use. The
caller can also specify the mask by using CIDR notation in ip argument by
leaving the mask argument as an empty string. In case of success this
function returns 0.
This function adds an IP address to an interface. If mask is None
then a mask will be selected automatically. In case of success
this function returns 0.
"""
args = ["ifconfig", iface, ip_addr]
interface_ip_op(iface, ip_addr, mask, "add")
def interface_remove_ip(iface, ip_addr, mask):
"""
This function removes an IP address from an interface. If mask is
None then a mask will be selected automatically. In case of
success this function returns 0.
"""
interface_ip_op(iface, ip_addr, mask, "del")
def interface_ip_op(iface, ip_addr, mask, op):
if mask is not None:
args.append("netmask")
args.append(mask)
ret, _out, _err = start_process(args)
arg = "%s/%s" % (ip_addr, mask)
elif '/' in ip_addr:
arg = ip_addr
else:
(x1, x2, x3, x4) = struct.unpack("BBBB", socket.inet_aton(ip_addr))
if x1 < 128:
arg = "%s/8" % ip_addr
elif x1 < 192:
arg = "%s/16" % ip_addr
else:
arg = "%s/24" % ip_addr
ret, _out, _err = start_process(["ip", "addr", op, arg, "dev", iface])
return ret
@@ -132,14 +151,13 @@ def interface_get_ip(iface):
This function returns tuple - ip and mask that was assigned to the
interface.
"""
args = ["ifconfig", iface]
args = ["ip", "addr", "show", iface]
ret, out, _err = start_process(args)
if ret == 0:
ip = re.search(r'inet addr:(\S+)', out)
mask = re.search(r'Mask:(\S+)', out)
if ip is not None and mask is not None:
return (ip.group(1), mask.group(1))
ip = re.search(r'inet (\S+)/(\S+)', out)
if ip is not None:
return (ip.group(1), ip.group(2))
else:
return ret