X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fnetdev.c;h=eff6e4cb7259372c63f528ced1c322d113723c09;hb=999fba59afd9c8eef30d30a6fd2f490b85c24665;hp=9fba077b6982c9f31e5db6cd1b5c54b1c61ed93f;hpb=9b02078077b62e4277e84c7f39382ce09986cf6b;p=openvswitch diff --git a/lib/netdev.c b/lib/netdev.c index 9fba077b..eff6e4cb 100644 --- a/lib/netdev.c +++ b/lib/netdev.c @@ -332,31 +332,23 @@ netdev_is_open(const char *name) return !!shash_find_data(&netdev_dev_shash, name); } -/* Clears 'sset' and enumerates the names of all known network devices. */ -int -netdev_enumerate(struct sset *sset) +/* Parses 'netdev_name_', which is of the form [type@]name into its component + * pieces. 'name' and 'type' must be freed by the caller. */ +void +netdev_parse_name(const char *netdev_name_, char **name, char **type) { - struct shash_node *node; - int error = 0; + char *netdev_name = xstrdup(netdev_name_); + char *separator; - netdev_initialize(); - sset_clear(sset); - - SHASH_FOR_EACH(node, &netdev_classes) { - const struct netdev_class *netdev_class = node->data; - if (netdev_class->enumerate) { - int retval = netdev_class->enumerate(sset); - if (retval) { - VLOG_WARN("failed to enumerate %s network devices: %s", - netdev_class->type, strerror(retval)); - if (!error) { - error = retval; - } - } - } + separator = strchr(netdev_name, '@'); + if (separator) { + *separator = '\0'; + *type = netdev_name; + *name = xstrdup(separator + 1); + } else { + *name = netdev_name; + *type = xstrdup("system"); } - - return error; } /* Attempts to set up 'netdev' for receiving packets with netdev_recv(). @@ -512,14 +504,21 @@ netdev_get_name(const struct netdev *netdev) * * If successful, returns 0 and stores the MTU size in '*mtup'. Returns * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not). - * On other failure, returns a positive errno value. */ + * On other failure, returns a positive errno value. On failure, sets '*mtup' + * to 0. */ int netdev_get_mtu(const struct netdev *netdev, int *mtup) { - int error = netdev_get_dev(netdev)->netdev_class->get_mtu(netdev, mtup); - if (error && error != EOPNOTSUPP) { - VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: %s", - netdev_get_name(netdev), strerror(error)); + const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class; + int error; + + error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP; + if (error) { + *mtup = 0; + if (error != EOPNOTSUPP) { + VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: " + "%s", netdev_get_name(netdev), strerror(error)); + } } return error; } @@ -533,8 +532,10 @@ netdev_get_mtu(const struct netdev *netdev, int *mtup) int netdev_set_mtu(const struct netdev *netdev, int mtu) { - int error = netdev_get_dev(netdev)->netdev_class->set_mtu(netdev, mtu); + const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class; + int error; + error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP; if (error && error != EOPNOTSUPP) { VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: %s", netdev_get_name(netdev), strerror(error)); @@ -691,6 +692,26 @@ netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask) : EOPNOTSUPP); } +/* Obtains ad IPv4 address from device name and save the address in + * in4. Returns 0 if successful, otherwise a positive errno value. + */ +int +netdev_get_in4_by_name(const char *device_name, struct in_addr *in4) +{ + struct netdev *netdev; + int error; + + error = netdev_open(device_name, "system", &netdev); + if (error) { + in4->s_addr = htonl(0); + return error; + } + + error = netdev_get_in4(netdev, in4, NULL); + netdev_close(netdev); + return error; +} + /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds * to 'netdev'. */ int @@ -891,6 +912,15 @@ netdev_get_carrier(const struct netdev *netdev) return carrier; } +/* Returns the number of times 'netdev''s carrier has changed. */ +long long int +netdev_get_carrier_resets(const struct netdev *netdev) +{ + return (netdev_get_dev(netdev)->netdev_class->get_carrier_resets + ? netdev_get_dev(netdev)->netdev_class->get_carrier_resets(netdev) + : 0); +} + /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for * link status instead of checking 'netdev''s carrier. 'netdev''s MII * registers will be polled once ever 'interval' milliseconds. If 'netdev' @@ -1240,51 +1270,6 @@ netdev_change_seq(const struct netdev *netdev) { return netdev_get_dev(netdev)->netdev_class->change_seq(netdev); } - -/* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)), - * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0. - * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the - * name of a network device that is not a VLAN device) and sets '*vlan_vid' to - * -1. */ -int -netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid) -{ - int error = (netdev_get_dev(netdev)->netdev_class->get_vlan_vid - ? netdev_get_dev(netdev)->netdev_class->get_vlan_vid(netdev, - vlan_vid) - : ENOENT); - if (error) { - *vlan_vid = 0; - } - return error; -} - -/* Returns a network device that has 'in4' as its IP address, if one exists, - * otherwise a null pointer. */ -struct netdev * -netdev_find_dev_by_in4(const struct in_addr *in4) -{ - struct netdev *netdev; - struct sset dev_list = SSET_INITIALIZER(&dev_list); - const char *name; - - netdev_enumerate(&dev_list); - SSET_FOR_EACH (name, &dev_list) { - struct in_addr dev_in4; - - if (!netdev_open(name, "system", &netdev) - && !netdev_get_in4(netdev, &dev_in4, NULL) - && dev_in4.s_addr == in4->s_addr) { - goto exit; - } - netdev_close(netdev); - } - netdev = NULL; - -exit: - sset_destroy(&dev_list); - return netdev; -} /* Initializes 'netdev_dev' as a netdev device named 'name' of the specified * 'netdev_class'. This function is ordinarily called from a netdev provider's