From: Ben Pfaff Date: Mon, 14 Jul 2008 20:03:37 +0000 (-0700) Subject: netdev: Add more functions for manipulating device flags. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=24eb1d1546852c698c412e2214d1994f8e4b6a1c;p=openvswitch netdev: Add more functions for manipulating device flags. These are convenient for use in secchan and elsewhere. --- diff --git a/include/netdev.h b/include/netdev.h index 108f12c7..2d1c3d74 100644 --- a/include/netdev.h +++ b/include/netdev.h @@ -74,6 +74,8 @@ bool netdev_get_in4(const struct netdev *, struct in_addr *); bool netdev_get_in6(const struct netdev *, struct in6_addr *); int netdev_get_flags(const struct netdev *, enum netdev_flags *); int netdev_set_flags(struct netdev *, enum netdev_flags); +int netdev_turn_flags_on(struct netdev *, enum netdev_flags); +int netdev_turn_flags_off(struct netdev *, enum netdev_flags); int netdev_arp_lookup(const struct netdev *, uint32_t ip, uint8_t mac[6]); #endif /* netdev.h */ diff --git a/lib/netdev.c b/lib/netdev.c index 82325616..bb641366 100644 --- a/lib/netdev.c +++ b/lib/netdev.c @@ -575,10 +575,24 @@ netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp) return 0; } -/* Sets the flags for 'netdev' to 'nd_flags'. - * Returns 0 if successful, otherwise a positive errno value. */ -int -netdev_set_flags(struct netdev *netdev, enum netdev_flags nd_flags) +static int +nd_to_iff_flags(enum netdev_flags nd) +{ + int iff = 0; + if (nd & NETDEV_UP) { + iff |= IFF_UP; + } + if (nd & NETDEV_PROMISC) { + iff |= IFF_PROMISC; + } + return iff; +} + +/* On 'netdev', turns off the flags in 'off' and then turns on the flags in + * 'on'. Returns 0 if successful, otherwise a positive errno value. */ +static int +do_update_flags(struct netdev *netdev, enum netdev_flags off, + enum netdev_flags on) { int old_flags, new_flags; int error; @@ -588,19 +602,37 @@ netdev_set_flags(struct netdev *netdev, enum netdev_flags nd_flags) return error; } - new_flags = old_flags & ~(IFF_UP | IFF_PROMISC); - if (nd_flags & NETDEV_UP) { - new_flags |= IFF_UP; - } - if (nd_flags & NETDEV_PROMISC) { - new_flags |= IFF_PROMISC; - } + new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on); if (new_flags != old_flags) { error = set_flags(netdev, new_flags); } return error; } +/* Sets the flags for 'netdev' to 'flags'. + * Returns 0 if successful, otherwise a positive errno value. */ +int +netdev_set_flags(struct netdev *netdev, enum netdev_flags flags) +{ + return do_update_flags(netdev, -1, flags); +} + +/* Turns on the specified 'flags' on 'netdev'. + * Returns 0 if successful, otherwise a positive errno value. */ +int +netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags) +{ + return do_update_flags(netdev, 0, flags); +} + +/* Turns off the specified 'flags' on 'netdev'. + * Returns 0 if successful, otherwise a positive errno value. */ +int +netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags) +{ + return do_update_flags(netdev, flags, 0); +} + /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be * successfully retrieved, it stores the corresponding MAC address in 'mac' and * returns 0. Otherwise, it returns a positive errno value; in particular,