X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=lib%2Fnetdev-linux.c;h=d58c1b1c56d7691f30d8fc1db086bcdf488e7c0d;hb=d0a46cb4608e632f5028034762f0adde2ce947a0;hp=c0e0c40c5d2df63ef950da1b27192c150c9d43fc;hpb=c4c7a3d7a1c278548c92e5d317e4be4a8525f8c7;p=cascardo%2Fovs.git diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index c0e0c40c5..d58c1b1c5 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. + * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -32,10 +33,10 @@ #include #include #include -#include #include #include #include +#include #include #include #include @@ -48,7 +49,9 @@ #include #include "coverage.h" -#include "dpif-linux.h" +#include "dp-packet.h" +#include "dpif-netlink.h" +#include "dpif-netdev.h" #include "dynamic-string.h" #include "fatal-signal.h" #include "hash.h" @@ -60,15 +63,16 @@ #include "netlink.h" #include "ofpbuf.h" #include "openflow/openflow.h" +#include "ovs-atomic.h" #include "packets.h" #include "poll-loop.h" -#include "rtnetlink-link.h" +#include "rtnetlink.h" #include "shash.h" #include "socket-util.h" #include "sset.h" #include "timer.h" #include "unaligned.h" -#include "vlog.h" +#include "openvswitch/vlog.h" VLOG_DEFINE_THIS_MODULE(netdev_linux); @@ -105,8 +109,113 @@ COVERAGE_DEFINE(netdev_set_ethtool); #define TC_RTAB_SIZE 1024 #endif -static struct nln_notifier *netdev_linux_cache_notifier = NULL; -static int cache_notifier_refcount; +/* Linux 2.6.21 introduced struct tpacket_auxdata. + * Linux 2.6.27 added the tp_vlan_tci member. + * Linux 3.0 defined TP_STATUS_VLAN_VALID. + * Linux 3.13 repurposed a padding member for tp_vlan_tpid and defined + * TP_STATUS_VLAN_TPID_VALID. + * + * With all this churn it's easiest to unconditionally define a replacement + * structure that has everything we want. + */ +#ifndef PACKET_AUXDATA +#define PACKET_AUXDATA 8 +#endif +#ifndef TP_STATUS_VLAN_VALID +#define TP_STATUS_VLAN_VALID (1 << 4) +#endif +#ifndef TP_STATUS_VLAN_TPID_VALID +#define TP_STATUS_VLAN_TPID_VALID (1 << 6) +#endif +#undef tpacket_auxdata +#define tpacket_auxdata rpl_tpacket_auxdata +struct tpacket_auxdata { + uint32_t tp_status; + uint32_t tp_len; + uint32_t tp_snaplen; + uint16_t tp_mac; + uint16_t tp_net; + uint16_t tp_vlan_tci; + uint16_t tp_vlan_tpid; +}; + +/* Linux 2.6.27 introduced ethtool_cmd_speed + * + * To avoid revisiting problems reported with using configure to detect + * compatibility (see report at + * http://openvswitch.org/pipermail/dev/2014-October/047978.html) + * unconditionally replace ethtool_cmd_speed. */ +#define ethtool_cmd_speed rpl_ethtool_cmd_speed +static inline uint32_t rpl_ethtool_cmd_speed(const struct ethtool_cmd *ep) +{ + return ep->speed | (ep->speed_hi << 16); +} + +/* Linux 2.6.30 introduced supported and advertised flags for + * 1G base KX, and 10G base KX4, KR and R. */ +#ifndef SUPPORTED_1000baseKX_Full +#define SUPPORTED_1000baseKX_Full (1 << 17) +#define SUPPORTED_10000baseKX4_Full (1 << 18) +#define SUPPORTED_10000baseKR_Full (1 << 19) +#define SUPPORTED_10000baseR_FEC (1 << 20) +#define ADVERTISED_1000baseKX_Full (1 << 17) +#define ADVERTISED_10000baseKX4_Full (1 << 18) +#define ADVERTISED_10000baseKR_Full (1 << 19) +#define ADVERTISED_10000baseR_FEC (1 << 20) +#endif + +/* Linux 3.5 introduced supported and advertised flags for + * 40G base KR4, CR4, SR4 and LR4. */ +#ifndef SUPPORTED_40000baseKR4_Full +#define SUPPORTED_40000baseKR4_Full (1 << 23) +#define SUPPORTED_40000baseCR4_Full (1 << 24) +#define SUPPORTED_40000baseSR4_Full (1 << 25) +#define SUPPORTED_40000baseLR4_Full (1 << 26) +#define ADVERTISED_40000baseKR4_Full (1 << 23) +#define ADVERTISED_40000baseCR4_Full (1 << 24) +#define ADVERTISED_40000baseSR4_Full (1 << 25) +#define ADVERTISED_40000baseLR4_Full (1 << 26) +#endif + +/* Linux 2.6.35 introduced IFLA_STATS64 and rtnl_link_stats64. + * + * Tests for rtnl_link_stats64 don't seem to consistently work, e.g. on + * 2.6.32-431.29.2.el6.x86_64 (see report at + * http://openvswitch.org/pipermail/dev/2014-October/047978.html). Maybe + * if_link.h is not self-contained on those kernels. It is easiest to + * unconditionally define a replacement. */ +#ifndef IFLA_STATS64 +#define IFLA_STATS64 23 +#endif +#define rtnl_link_stats64 rpl_rtnl_link_stats64 +struct rtnl_link_stats64 { + uint64_t rx_packets; + uint64_t tx_packets; + uint64_t rx_bytes; + uint64_t tx_bytes; + uint64_t rx_errors; + uint64_t tx_errors; + uint64_t rx_dropped; + uint64_t tx_dropped; + uint64_t multicast; + uint64_t collisions; + + uint64_t rx_length_errors; + uint64_t rx_over_errors; + uint64_t rx_crc_errors; + uint64_t rx_frame_errors; + uint64_t rx_fifo_errors; + uint64_t rx_missed_errors; + + uint64_t tx_aborted_errors; + uint64_t tx_carrier_errors; + uint64_t tx_fifo_errors; + uint64_t tx_heartbeat_errors; + uint64_t tx_window_errors; + + uint64_t rx_compressed; + uint64_t tx_compressed; +}; enum { VALID_IFINDEX = 1 << 0, @@ -119,11 +228,6 @@ enum { VALID_DRVINFO = 1 << 7, VALID_FEATURES = 1 << 8, }; - -struct tap_state { - int fd; - bool opened; -}; /* Traffic control. */ @@ -139,6 +243,8 @@ struct tc { * Written only by TC implementation. */ }; +#define TC_INITIALIZER(TC, OPS) { OPS, HMAP_INITIALIZER(&(TC)->queues) } + /* One traffic control queue. * * Each TC implementation subclasses this with whatever additional data it @@ -146,6 +252,7 @@ struct tc { struct tc_queue { struct hmap_node hmap_node; /* In struct tc's "queues" hmap. */ unsigned int queue_id; /* OpenFlow queue ID. */ + long long int created; /* Time queue was created, in msecs. */ }; /* A particular kind of traffic control. Each implementation generally maps to @@ -308,12 +415,18 @@ tc_destroy(struct tc *tc) static const struct tc_ops tc_ops_htb; static const struct tc_ops tc_ops_hfsc; +static const struct tc_ops tc_ops_codel; +static const struct tc_ops tc_ops_fqcodel; +static const struct tc_ops tc_ops_sfq; static const struct tc_ops tc_ops_default; static const struct tc_ops tc_ops_other; -static const struct tc_ops *tcs[] = { +static const struct tc_ops *const tcs[] = { &tc_ops_htb, /* Hierarchy token bucket (see tc-htb(8)). */ &tc_ops_hfsc, /* Hierarchical fair service curve. */ + &tc_ops_codel, /* Controlled delay */ + &tc_ops_fqcodel, /* Fair queue controlled delay */ + &tc_ops_sfq, /* Stochastic fair queueing */ &tc_ops_default, /* Default qdisc (see tc-pfifo_fast(8)). */ &tc_ops_other, /* Some other qdisc. */ NULL @@ -331,8 +444,8 @@ static struct tcmsg *tc_make_request(const struct netdev *, int type, unsigned int flags, struct ofpbuf *); static int tc_transact(struct ofpbuf *request, struct ofpbuf **replyp); static int tc_add_del_ingress_qdisc(struct netdev *netdev, bool add); -static int tc_add_policer(struct netdev *netdev, int kbits_rate, - int kbits_burst); +static int tc_add_policer(struct netdev *, + uint32_t kbits_rate, uint32_t kbits_burst); static int tc_parse_qdisc(const struct ofpbuf *, const char **kind, struct nlattr **options); @@ -353,12 +466,13 @@ static void tc_put_rtab(struct ofpbuf *, uint16_t type, const struct tc_ratespec *rate); static int tc_calc_buffer(unsigned int Bps, int mtu, uint64_t burst_bytes); -struct netdev_dev_linux { - struct netdev_dev netdev_dev; +struct netdev_linux { + struct netdev up; + + /* Protects all members below. */ + struct ovs_mutex mutex; - struct shash_node *shash_node; unsigned int cache_valid; - unsigned int change_seq; bool miimon; /* Link status of last poll. */ long long int miimon_interval; /* Miimon Poll rate. Disabled if <= 0. */ @@ -367,7 +481,7 @@ struct netdev_dev_linux { /* The following are figured out "on demand" only. They are only valid * when the corresponding VALID_* bit in 'cache_valid' is set. */ int ifindex; - uint8_t etheraddr[ETH_ADDR_LEN]; + struct eth_addr etheraddr; struct in_addr address, netmask; struct in6_addr in6; int mtu; @@ -382,130 +496,221 @@ struct netdev_dev_linux { int netdev_policing_error; /* Cached error code from set policing. */ int get_features_error; /* Cached error code from ETHTOOL_GSET. */ int get_ifindex_error; /* Cached error code from SIOCGIFINDEX. */ + int in4_error; /* Cached error code from reading in4 addr. */ + int in6_error; /* Cached error code from reading in6 addr. */ enum netdev_features current; /* Cached from ETHTOOL_GSET. */ enum netdev_features advertised; /* Cached from ETHTOOL_GSET. */ enum netdev_features supported; /* Cached from ETHTOOL_GSET. */ - enum netdev_features peer; /* Cached from ETHTOOL_GSET. */ struct ethtool_drvinfo drvinfo; /* Cached from ETHTOOL_GDRVINFO. */ struct tc *tc; - union { - struct tap_state tap; - } state; + /* For devices of class netdev_tap_class only. */ + int tap_fd; }; -struct netdev_linux { - struct netdev netdev; +struct netdev_rxq_linux { + struct netdev_rxq up; + bool is_tap; int fd; }; -/* Sockets used for ioctl operations. */ -static int af_inet_sock = -1; /* AF_INET, SOCK_DGRAM. */ - -/* A Netlink routing socket that is not subscribed to any multicast groups. */ -static struct nl_sock *rtnl_sock; - /* This is set pretty low because we probably won't learn anything from the * additional log messages. */ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); -static int netdev_linux_init(void); +/* Polling miimon status for all ports causes performance degradation when + * handling a large number of ports. If there are no devices using miimon, then + * we skip netdev_linux_miimon_run() and netdev_linux_miimon_wait(). + * + * Readers do not depend on this variable synchronizing with the related + * changes in the device miimon status, so we can use atomic_count. */ +static atomic_count miimon_cnt = ATOMIC_COUNT_INIT(0); + +static void netdev_linux_run(void); static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *, int cmd, const char *cmd_name); -static int netdev_linux_do_ioctl(const char *name, struct ifreq *, int cmd, - const char *cmd_name); static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *, int cmd, const char *cmd_name); -static int get_flags(const struct netdev_dev *, unsigned int *flags); -static int set_flags(struct netdev *, unsigned int flags); +static int get_flags(const struct netdev *, unsigned int *flags); +static int set_flags(const char *, unsigned int flags); +static int update_flags(struct netdev_linux *netdev, enum netdev_flags off, + enum netdev_flags on, enum netdev_flags *old_flagsp) + OVS_REQUIRES(netdev->mutex); static int do_get_ifindex(const char *netdev_name); static int get_ifindex(const struct netdev *, int *ifindexp); static int do_set_addr(struct netdev *netdev, int ioctl_nr, const char *ioctl_name, struct in_addr addr); -static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]); -static int set_etheraddr(const char *netdev_name, const uint8_t[ETH_ADDR_LEN]); -static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats); -static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats); +static int get_etheraddr(const char *netdev_name, struct eth_addr *ea); +static int set_etheraddr(const char *netdev_name, const struct eth_addr); +static int get_stats_via_netlink(const struct netdev *, struct netdev_stats *); static int af_packet_sock(void); +static bool netdev_linux_miimon_enabled(void); static void netdev_linux_miimon_run(void); static void netdev_linux_miimon_wait(void); +static int netdev_linux_get_mtu__(struct netdev_linux *netdev, int *mtup); static bool is_netdev_linux_class(const struct netdev_class *netdev_class) { - return netdev_class->init == netdev_linux_init; + return netdev_class->run == netdev_linux_run; } -static struct netdev_dev_linux * -netdev_dev_linux_cast(const struct netdev_dev *netdev_dev) +static bool +is_tap_netdev(const struct netdev *netdev) { - const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev); - ovs_assert(is_netdev_linux_class(netdev_class)); - - return CONTAINER_OF(netdev_dev, struct netdev_dev_linux, netdev_dev); + return netdev_get_class(netdev) == &netdev_tap_class; } static struct netdev_linux * netdev_linux_cast(const struct netdev *netdev) { - struct netdev_dev *netdev_dev = netdev_get_dev(netdev); - const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev); - ovs_assert(is_netdev_linux_class(netdev_class)); + ovs_assert(is_netdev_linux_class(netdev_get_class(netdev))); + + return CONTAINER_OF(netdev, struct netdev_linux, up); +} - return CONTAINER_OF(netdev, struct netdev_linux, netdev); +static struct netdev_rxq_linux * +netdev_rxq_linux_cast(const struct netdev_rxq *rx) +{ + ovs_assert(is_netdev_linux_class(netdev_get_class(rx->netdev))); + return CONTAINER_OF(rx, struct netdev_rxq_linux, up); } -static int -netdev_linux_init(void) -{ - static int status = -1; - if (status < 0) { - /* Create AF_INET socket. */ - af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0); - status = af_inet_sock >= 0 ? 0 : errno; - if (status) { - VLOG_ERR("failed to create inet socket: %s", strerror(status)); - } +static void netdev_linux_update(struct netdev_linux *netdev, + const struct rtnetlink_change *) + OVS_REQUIRES(netdev->mutex); +static void netdev_linux_changed(struct netdev_linux *netdev, + unsigned int ifi_flags, unsigned int mask) + OVS_REQUIRES(netdev->mutex); + +/* Returns a NETLINK_ROUTE socket listening for RTNLGRP_LINK, + * RTNLGRP_IPV4_IFADDR and RTNLGRP_IPV6_IFADDR changes, or NULL + * if no such socket could be created. */ +static struct nl_sock * +netdev_linux_notify_sock(void) +{ + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; + static struct nl_sock *sock; + unsigned int mcgroups[3] = {RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, + RTNLGRP_IPV6_IFADDR}; + + if (ovsthread_once_start(&once)) { + int error; + + error = nl_sock_create(NETLINK_ROUTE, &sock); + if (!error) { + size_t i; - /* Create rtnetlink socket. */ - if (!status) { - status = nl_sock_create(NETLINK_ROUTE, &rtnl_sock); - if (status) { - VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s", - strerror(status)); + for (i = 0; i < ARRAY_SIZE(mcgroups); i++) { + error = nl_sock_join_mcgroup(sock, mcgroups[i]); + if (error) { + nl_sock_destroy(sock); + sock = NULL; + break; + } } } + ovsthread_once_done(&once); } - return status; + + return sock; +} + +static bool +netdev_linux_miimon_enabled(void) +{ + return atomic_count_get(&miimon_cnt) > 0; } static void netdev_linux_run(void) { - rtnetlink_link_run(); - netdev_linux_miimon_run(); + struct nl_sock *sock; + int error; + + if (netdev_linux_miimon_enabled()) { + netdev_linux_miimon_run(); + } + + sock = netdev_linux_notify_sock(); + if (!sock) { + return; + } + + do { + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + uint64_t buf_stub[4096 / 8]; + struct ofpbuf buf; + + ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); + error = nl_sock_recv(sock, &buf, false); + if (!error) { + struct rtnetlink_change change; + + if (rtnetlink_parse(&buf, &change)) { + struct netdev *netdev_ = netdev_from_name(change.ifname); + if (netdev_ && is_netdev_linux_class(netdev_->netdev_class)) { + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + + ovs_mutex_lock(&netdev->mutex); + netdev_linux_update(netdev, &change); + ovs_mutex_unlock(&netdev->mutex); + } + netdev_close(netdev_); + } + } else if (error == ENOBUFS) { + struct shash device_shash; + struct shash_node *node; + + nl_sock_drain(sock); + + shash_init(&device_shash); + netdev_get_devices(&netdev_linux_class, &device_shash); + SHASH_FOR_EACH (node, &device_shash) { + struct netdev *netdev_ = node->data; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + unsigned int flags; + + ovs_mutex_lock(&netdev->mutex); + get_flags(netdev_, &flags); + netdev_linux_changed(netdev, flags, 0); + ovs_mutex_unlock(&netdev->mutex); + + netdev_close(netdev_); + } + shash_destroy(&device_shash); + } else if (error != EAGAIN) { + VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)", + ovs_strerror(error)); + } + ofpbuf_uninit(&buf); + } while (!error); } static void netdev_linux_wait(void) { - rtnetlink_link_wait(); - netdev_linux_miimon_wait(); + struct nl_sock *sock; + + if (netdev_linux_miimon_enabled()) { + netdev_linux_miimon_wait(); + } + sock = netdev_linux_notify_sock(); + if (sock) { + nl_sock_wait(sock, POLLIN); + } } static void -netdev_dev_linux_changed(struct netdev_dev_linux *dev, - unsigned int ifi_flags, - unsigned int mask) +netdev_linux_changed(struct netdev_linux *dev, + unsigned int ifi_flags, unsigned int mask) + OVS_REQUIRES(dev->mutex) { - dev->change_seq++; - if (!dev->change_seq) { - dev->change_seq++; - } + netdev_change_seq_changed(&dev->up); if ((dev->ifi_flags ^ ifi_flags) & IFF_RUNNING) { dev->carrier_resets++; @@ -516,117 +721,79 @@ netdev_dev_linux_changed(struct netdev_dev_linux *dev, } static void -netdev_dev_linux_update(struct netdev_dev_linux *dev, - const struct rtnetlink_link_change *change) -{ - if (change->nlmsg_type == RTM_NEWLINK) { - /* Keep drv-info */ - netdev_dev_linux_changed(dev, change->ifi_flags, VALID_DRVINFO); - - /* Update netdev from rtnl-change msg. */ - if (change->mtu) { - dev->mtu = change->mtu; - dev->cache_valid |= VALID_MTU; - dev->netdev_mtu_error = 0; - } - - if (!eth_addr_is_zero(change->addr)) { - memcpy(dev->etheraddr, change->addr, ETH_ADDR_LEN); - dev->cache_valid |= VALID_ETHERADDR; - dev->ether_addr_error = 0; - } - - dev->ifindex = change->ifi_index; - dev->cache_valid |= VALID_IFINDEX; - dev->get_ifindex_error = 0; - - } else { - netdev_dev_linux_changed(dev, change->ifi_flags, 0); - } -} - -static void -netdev_linux_cache_cb(const struct rtnetlink_link_change *change, - void *aux OVS_UNUSED) -{ - struct netdev_dev_linux *dev; - if (change) { - struct netdev_dev *base_dev = netdev_dev_from_name(change->ifname); - if (base_dev) { - const struct netdev_class *netdev_class = - netdev_dev_get_class(base_dev); - - if (is_netdev_linux_class(netdev_class)) { - dev = netdev_dev_linux_cast(base_dev); - netdev_dev_linux_update(dev, change); +netdev_linux_update(struct netdev_linux *dev, + const struct rtnetlink_change *change) + OVS_REQUIRES(dev->mutex) +{ + if (rtnetlink_type_is_rtnlgrp_link(change->nlmsg_type)){ + if (change->nlmsg_type == RTM_NEWLINK) { + /* Keep drv-info, in4, in6. */ + netdev_linux_changed(dev, change->ifi_flags, + VALID_DRVINFO | VALID_IN4 | VALID_IN6); + + /* Update netdev from rtnl-change msg. */ + if (change->mtu) { + dev->mtu = change->mtu; + dev->cache_valid |= VALID_MTU; + dev->netdev_mtu_error = 0; } - } - } else { - struct shash device_shash; - struct shash_node *node; - shash_init(&device_shash); - netdev_dev_get_devices(&netdev_linux_class, &device_shash); - SHASH_FOR_EACH (node, &device_shash) { - unsigned int flags; - - dev = node->data; + if (!eth_addr_is_zero(change->mac)) { + dev->etheraddr = change->mac; + dev->cache_valid |= VALID_ETHERADDR; + dev->ether_addr_error = 0; + } - get_flags(&dev->netdev_dev, &flags); - netdev_dev_linux_changed(dev, flags, 0); + dev->ifindex = change->if_index; + dev->cache_valid |= VALID_IFINDEX; + dev->get_ifindex_error = 0; + } else { + netdev_linux_changed(dev, change->ifi_flags, 0); } - shash_destroy(&device_shash); + } else if (rtnetlink_type_is_rtnlgrp_addr(change->nlmsg_type)) { + /* Invalidates in4, in6. */ + netdev_linux_changed(dev, dev->ifi_flags, + ~(VALID_IN4 | VALID_IN6)); + } else { + OVS_NOT_REACHED(); } } -static int -cache_notifier_ref(void) +static struct netdev * +netdev_linux_alloc(void) { - if (!cache_notifier_refcount) { - ovs_assert(!netdev_linux_cache_notifier); - - netdev_linux_cache_notifier = - rtnetlink_link_notifier_create(netdev_linux_cache_cb, NULL); - - if (!netdev_linux_cache_notifier) { - return EINVAL; - } - } - cache_notifier_refcount++; - - return 0; + struct netdev_linux *netdev = xzalloc(sizeof *netdev); + return &netdev->up; } static void -cache_notifier_unref(void) +netdev_linux_common_construct(struct netdev_linux *netdev) { - ovs_assert(cache_notifier_refcount > 0); - if (!--cache_notifier_refcount) { - ovs_assert(netdev_linux_cache_notifier); - rtnetlink_link_notifier_destroy(netdev_linux_cache_notifier); - netdev_linux_cache_notifier = NULL; - } + ovs_mutex_init(&netdev->mutex); } /* Creates system and internal devices. */ static int -netdev_linux_create(const struct netdev_class *class, const char *name, - struct netdev_dev **netdev_devp) +netdev_linux_construct(struct netdev *netdev_) { - struct netdev_dev_linux *netdev_dev; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - error = cache_notifier_ref(); - if (error) { - return error; - } + netdev_linux_common_construct(netdev); - netdev_dev = xzalloc(sizeof *netdev_dev); - netdev_dev->change_seq = 1; - netdev_dev_init(&netdev_dev->netdev_dev, name, class); - get_flags(&netdev_dev->netdev_dev, &netdev_dev->ifi_flags); + error = get_flags(&netdev->up, &netdev->ifi_flags); + if (error == ENODEV) { + if (netdev->up.netdev_class != &netdev_internal_class) { + /* The device does not exist, so don't allow it to be opened. */ + return ENODEV; + } else { + /* "Internal" netdevs have to be created as netdev objects before + * they exist in the kernel, because creating them in the kernel + * happens by passing a netdev object to dpif_port_add(). + * Therefore, ignore the error. */ + } + } - *netdev_devp = &netdev_dev->netdev_dev; return 0; } @@ -637,255 +804,344 @@ netdev_linux_create(const struct netdev_class *class, const char *name, * buffers, across all readers. Therefore once data is read it will * be unavailable to other reads for tap devices. */ static int -netdev_linux_create_tap(const struct netdev_class *class OVS_UNUSED, - const char *name, struct netdev_dev **netdev_devp) +netdev_linux_construct_tap(struct netdev *netdev_) { - struct netdev_dev_linux *netdev_dev; - struct tap_state *state; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); static const char tap_dev[] = "/dev/net/tun"; + const char *name = netdev_->name; struct ifreq ifr; int error; - netdev_dev = xzalloc(sizeof *netdev_dev); - state = &netdev_dev->state.tap; - - error = cache_notifier_ref(); - if (error) { - goto error; - } + netdev_linux_common_construct(netdev); /* Open tap device. */ - state->fd = open(tap_dev, O_RDWR); - if (state->fd < 0) { + netdev->tap_fd = open(tap_dev, O_RDWR); + if (netdev->tap_fd < 0) { error = errno; - VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error)); - goto error_unref_notifier; + VLOG_WARN("opening \"%s\" failed: %s", tap_dev, ovs_strerror(error)); + return error; } /* Create tap device. */ ifr.ifr_flags = IFF_TAP | IFF_NO_PI; ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name); - if (ioctl(state->fd, TUNSETIFF, &ifr) == -1) { + if (ioctl(netdev->tap_fd, TUNSETIFF, &ifr) == -1) { VLOG_WARN("%s: creating tap device failed: %s", name, - strerror(errno)); + ovs_strerror(errno)); error = errno; - goto error_unref_notifier; + goto error_close; } /* Make non-blocking. */ - error = set_nonblocking(state->fd); + error = set_nonblocking(netdev->tap_fd); if (error) { - goto error_unref_notifier; + goto error_close; } - netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_tap_class); - *netdev_devp = &netdev_dev->netdev_dev; return 0; -error_unref_notifier: - cache_notifier_unref(); -error: - free(netdev_dev); +error_close: + close(netdev->tap_fd); return error; } static void -destroy_tap(struct netdev_dev_linux *netdev_dev) +netdev_linux_destruct(struct netdev *netdev_) { - struct tap_state *state = &netdev_dev->state.tap; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); - if (state->fd >= 0) { - close(state->fd); + if (netdev->tc && netdev->tc->ops->tc_destroy) { + netdev->tc->ops->tc_destroy(netdev->tc); } -} -/* Destroys the netdev device 'netdev_dev_'. */ -static void -netdev_linux_destroy(struct netdev_dev *netdev_dev_) -{ - struct netdev_dev_linux *netdev_dev = netdev_dev_linux_cast(netdev_dev_); - const struct netdev_class *class = netdev_dev_get_class(netdev_dev_); - - if (netdev_dev->tc && netdev_dev->tc->ops->tc_destroy) { - netdev_dev->tc->ops->tc_destroy(netdev_dev->tc); + if (netdev_get_class(netdev_) == &netdev_tap_class + && netdev->tap_fd >= 0) + { + close(netdev->tap_fd); } - if (class == &netdev_tap_class) { - destroy_tap(netdev_dev); + if (netdev->miimon_interval > 0) { + atomic_count_dec(&miimon_cnt); } - free(netdev_dev); - cache_notifier_unref(); + ovs_mutex_destroy(&netdev->mutex); +} + +static void +netdev_linux_dealloc(struct netdev *netdev_) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + free(netdev); +} + +static struct netdev_rxq * +netdev_linux_rxq_alloc(void) +{ + struct netdev_rxq_linux *rx = xzalloc(sizeof *rx); + return &rx->up; } static int -netdev_linux_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp) +netdev_linux_rxq_construct(struct netdev_rxq *rxq_) { - struct netdev_linux *netdev; - enum netdev_flags flags; + struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_); + struct netdev *netdev_ = rx->up.netdev; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - /* Allocate network device. */ - netdev = xzalloc(sizeof *netdev); - netdev->fd = -1; - netdev_init(&netdev->netdev, netdev_dev_); + ovs_mutex_lock(&netdev->mutex); + rx->is_tap = is_tap_netdev(netdev_); + if (rx->is_tap) { + rx->fd = netdev->tap_fd; + } else { + struct sockaddr_ll sll; + int ifindex, val; + /* Result of tcpdump -dd inbound */ + static const struct sock_filter filt[] = { + { 0x28, 0, 0, 0xfffff004 }, /* ldh [0] */ + { 0x15, 0, 1, 0x00000004 }, /* jeq #4 jt 2 jf 3 */ + { 0x6, 0, 0, 0x00000000 }, /* ret #0 */ + { 0x6, 0, 0, 0x0000ffff } /* ret #65535 */ + }; + static const struct sock_fprog fprog = { + ARRAY_SIZE(filt), (struct sock_filter *) filt + }; + + /* Create file descriptor. */ + rx->fd = socket(PF_PACKET, SOCK_RAW, 0); + if (rx->fd < 0) { + error = errno; + VLOG_ERR("failed to create raw socket (%s)", ovs_strerror(error)); + goto error; + } - /* Verify that the device really exists, by attempting to read its flags. - * (The flags might be cached, in which case this won't actually do an - * ioctl.) - * - * Don't do this for "internal" netdevs, though, because those have to be - * created as netdev objects before they exist in the kernel, because - * creating them in the kernel happens by passing a netdev object to - * dpif_port_add(). */ - if (netdev_dev_get_class(netdev_dev_) != &netdev_internal_class) { - error = netdev_get_flags(&netdev->netdev, &flags); - if (error == ENODEV) { + val = 1; + if (setsockopt(rx->fd, SOL_PACKET, PACKET_AUXDATA, &val, sizeof val)) { + error = errno; + VLOG_ERR("%s: failed to mark socket for auxdata (%s)", + netdev_get_name(netdev_), ovs_strerror(error)); + goto error; + } + + /* Set non-blocking mode. */ + error = set_nonblocking(rx->fd); + if (error) { + goto error; + } + + /* Get ethernet device index. */ + error = get_ifindex(&netdev->up, &ifindex); + if (error) { + goto error; + } + + /* Bind to specific ethernet device. */ + memset(&sll, 0, sizeof sll); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = ifindex; + sll.sll_protocol = htons(ETH_P_ALL); + if (bind(rx->fd, (struct sockaddr *) &sll, sizeof sll) < 0) { + error = errno; + VLOG_ERR("%s: failed to bind raw socket (%s)", + netdev_get_name(netdev_), ovs_strerror(error)); + goto error; + } + + /* Filter for only inbound packets. */ + error = setsockopt(rx->fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, + sizeof fprog); + if (error) { + error = errno; + VLOG_ERR("%s: failed to attach filter (%s)", + netdev_get_name(netdev_), ovs_strerror(error)); goto error; } } + ovs_mutex_unlock(&netdev->mutex); - *netdevp = &netdev->netdev; return 0; error: - netdev_uninit(&netdev->netdev, true); + if (rx->fd >= 0) { + close(rx->fd); + } + ovs_mutex_unlock(&netdev->mutex); return error; } -/* Closes and destroys 'netdev'. */ static void -netdev_linux_close(struct netdev *netdev_) +netdev_linux_rxq_destruct(struct netdev_rxq *rxq_) { - struct netdev_linux *netdev = netdev_linux_cast(netdev_); + struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_); - if (netdev->fd > 0 && strcmp(netdev_get_type(netdev_), "tap")) { - close(netdev->fd); + if (!rx->is_tap) { + close(rx->fd); } - free(netdev); } -static int -netdev_linux_listen(struct netdev *netdev_) +static void +netdev_linux_rxq_dealloc(struct netdev_rxq *rxq_) { - struct netdev_linux *netdev = netdev_linux_cast(netdev_); - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); - struct sockaddr_ll sll; - int ifindex; - int error; - int fd; + struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_); - if (netdev->fd >= 0) { - return 0; - } + free(rx); +} - if (!strcmp(netdev_get_type(netdev_), "tap") - && !netdev_dev->state.tap.opened) { - netdev->fd = netdev_dev->state.tap.fd; - netdev_dev->state.tap.opened = true; - return 0; +static ovs_be16 +auxdata_to_vlan_tpid(const struct tpacket_auxdata *aux) +{ + if (aux->tp_status & TP_STATUS_VLAN_TPID_VALID) { + return htons(aux->tp_vlan_tpid); + } else { + return htons(ETH_TYPE_VLAN); } +} - /* Create file descriptor. */ - fd = socket(PF_PACKET, SOCK_RAW, 0); - if (fd < 0) { - error = errno; - VLOG_ERR("failed to create raw socket (%s)", strerror(error)); - goto error; - } +static bool +auxdata_has_vlan_tci(const struct tpacket_auxdata *aux) +{ + return aux->tp_vlan_tci || aux->tp_status & TP_STATUS_VLAN_VALID; +} - /* Set non-blocking mode. */ - error = set_nonblocking(fd); - if (error) { - goto error; +static int +netdev_linux_rxq_recv_sock(int fd, struct dp_packet *buffer) +{ + size_t size; + ssize_t retval; + struct iovec iov; + struct cmsghdr *cmsg; + union { + struct cmsghdr cmsg; + char buffer[CMSG_SPACE(sizeof(struct tpacket_auxdata))]; + } cmsg_buffer; + struct msghdr msgh; + + /* Reserve headroom for a single VLAN tag */ + dp_packet_reserve(buffer, VLAN_HEADER_LEN); + size = dp_packet_tailroom(buffer); + + iov.iov_base = dp_packet_data(buffer); + iov.iov_len = size; + msgh.msg_name = NULL; + msgh.msg_namelen = 0; + msgh.msg_iov = &iov; + msgh.msg_iovlen = 1; + msgh.msg_control = &cmsg_buffer; + msgh.msg_controllen = sizeof cmsg_buffer; + msgh.msg_flags = 0; + + do { + retval = recvmsg(fd, &msgh, MSG_TRUNC); + } while (retval < 0 && errno == EINTR); + + if (retval < 0) { + return errno; + } else if (retval > size) { + return EMSGSIZE; } - /* Get ethernet device index. */ - error = get_ifindex(&netdev->netdev, &ifindex); - if (error) { - goto error; - } + dp_packet_set_size(buffer, dp_packet_size(buffer) + retval); - /* Bind to specific ethernet device. */ - memset(&sll, 0, sizeof sll); - sll.sll_family = AF_PACKET; - sll.sll_ifindex = ifindex; - sll.sll_protocol = (OVS_FORCE unsigned short int) htons(ETH_P_ALL); - if (bind(fd, (struct sockaddr *) &sll, sizeof sll) < 0) { - error = errno; - VLOG_ERR("%s: failed to bind raw socket (%s)", - netdev_get_name(netdev_), strerror(error)); - goto error; + for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg; cmsg = CMSG_NXTHDR(&msgh, cmsg)) { + const struct tpacket_auxdata *aux; + + if (cmsg->cmsg_level != SOL_PACKET + || cmsg->cmsg_type != PACKET_AUXDATA + || cmsg->cmsg_len < CMSG_LEN(sizeof(struct tpacket_auxdata))) { + continue; + } + + aux = ALIGNED_CAST(struct tpacket_auxdata *, CMSG_DATA(cmsg)); + if (auxdata_has_vlan_tci(aux)) { + if (retval < ETH_HEADER_LEN) { + return EINVAL; + } + + eth_push_vlan(buffer, auxdata_to_vlan_tpid(aux), + htons(aux->tp_vlan_tci)); + break; + } } - netdev->fd = fd; return 0; +} -error: - if (fd >= 0) { - close(fd); +static int +netdev_linux_rxq_recv_tap(int fd, struct dp_packet *buffer) +{ + ssize_t retval; + size_t size = dp_packet_tailroom(buffer); + + do { + retval = read(fd, dp_packet_data(buffer), size); + } while (retval < 0 && errno == EINTR); + + if (retval < 0) { + return errno; } - return error; + + dp_packet_set_size(buffer, dp_packet_size(buffer) + retval); + return 0; } static int -netdev_linux_recv(struct netdev *netdev_, void *data, size_t size) +netdev_linux_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet **packets, + int *c) { - struct netdev_linux *netdev = netdev_linux_cast(netdev_); + struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_); + struct netdev *netdev = rx->up.netdev; + struct dp_packet *buffer; + ssize_t retval; + int mtu; - if (netdev->fd < 0) { - /* Device is not listening. */ - return -EAGAIN; + if (netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu)) { + mtu = ETH_PAYLOAD_MAX; } - for (;;) { - ssize_t retval; + buffer = dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu, + DP_NETDEV_HEADROOM); + retval = (rx->is_tap + ? netdev_linux_rxq_recv_tap(rx->fd, buffer) + : netdev_linux_rxq_recv_sock(rx->fd, buffer)); - retval = (netdev_->netdev_dev->netdev_class == &netdev_tap_class - ? read(netdev->fd, data, size) - : recv(netdev->fd, data, size, MSG_TRUNC)); - if (retval >= 0) { - return retval <= size ? retval : -EMSGSIZE; - } else if (errno != EINTR) { - if (errno != EAGAIN) { - VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s", - strerror(errno), netdev_get_name(netdev_)); - } - return -errno; + if (retval) { + if (retval != EAGAIN && retval != EMSGSIZE) { + VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s", + netdev_rxq_get_name(rxq_), ovs_strerror(errno)); } + dp_packet_delete(buffer); + } else { + dp_packet_pad(buffer); + dp_packet_rss_invalidate(buffer); + packets[0] = buffer; + *c = 1; } + + return retval; } -/* Registers with the poll loop to wake up from the next call to poll_block() - * when a packet is ready to be received with netdev_recv() on 'netdev'. */ static void -netdev_linux_recv_wait(struct netdev *netdev_) +netdev_linux_rxq_wait(struct netdev_rxq *rxq_) { - struct netdev_linux *netdev = netdev_linux_cast(netdev_); - if (netdev->fd >= 0) { - poll_fd_wait(netdev->fd, POLLIN); - } + struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_); + poll_fd_wait(rx->fd, POLLIN); } -/* Discards all packets waiting to be received from 'netdev'. */ static int -netdev_linux_drain(struct netdev *netdev_) +netdev_linux_rxq_drain(struct netdev_rxq *rxq_) { - struct netdev_linux *netdev = netdev_linux_cast(netdev_); - if (netdev->fd < 0) { - return 0; - } else if (!strcmp(netdev_get_type(netdev_), "tap")) { + struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_); + if (rx->is_tap) { struct ifreq ifr; - int error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr, - SIOCGIFTXQLEN, "SIOCGIFTXQLEN"); + int error = af_inet_ifreq_ioctl(netdev_rxq_get_name(rxq_), &ifr, + SIOCGIFTXQLEN, "SIOCGIFTXQLEN"); if (error) { return error; } - drain_fd(netdev->fd, ifr.ifr_qlen); + drain_fd(rx->fd, ifr.ifr_qlen); return 0; } else { - return drain_rcvbuf(netdev->fd); + return drain_rcvbuf(rx->fd); } } @@ -899,19 +1155,24 @@ netdev_linux_drain(struct netdev *netdev_) * The kernel maintains a packet transmission queue, so the caller is not * expected to do additional queuing of packets. */ static int -netdev_linux_send(struct netdev *netdev_, const void *data, size_t size) +netdev_linux_send(struct netdev *netdev_, int qid OVS_UNUSED, + struct dp_packet **pkts, int cnt, bool may_steal) { - struct netdev_linux *netdev = netdev_linux_cast(netdev_); - for (;;) { + int i; + int error = 0; + + /* 'i' is incremented only if there's no error */ + for (i = 0; i < cnt;) { + const void *data = dp_packet_data(pkts[i]); + size_t size = dp_packet_size(pkts[i]); ssize_t retval; - if (netdev->fd < 0) { + if (!is_tap_netdev(netdev_)) { /* Use our AF_PACKET socket to send to this device. */ struct sockaddr_ll sll; struct msghdr msg; struct iovec iov; int ifindex; - int error; int sock; sock = af_packet_sock(); @@ -919,9 +1180,9 @@ netdev_linux_send(struct netdev *netdev_, const void *data, size_t size) return -sock; } - error = get_ifindex(netdev_, &ifindex); - if (error) { - return error; + ifindex = netdev_get_ifindex(netdev_); + if (ifindex < 0) { + return -ifindex; } /* We don't bother setting most fields in sockaddr_ll because the @@ -943,34 +1204,51 @@ netdev_linux_send(struct netdev *netdev_, const void *data, size_t size) retval = sendmsg(sock, &msg, 0); } else { - /* Use the netdev's own fd to send to this device. This is - * essential for tap devices, because packets sent to a tap device - * with an AF_PACKET socket will loop back to be *received* again - * on the tap device. */ - retval = write(netdev->fd, data, size); + /* Use the tap fd to send to this device. This is essential for + * tap devices, because packets sent to a tap device with an + * AF_PACKET socket will loop back to be *received* again on the + * tap device. This doesn't occur on other interface types + * because we attach a socket filter to the rx socket. */ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + + retval = write(netdev->tap_fd, data, size); } if (retval < 0) { /* The Linux AF_PACKET implementation never blocks waiting for room * for packets, instead returning ENOBUFS. Translate this into * EAGAIN for the caller. */ - if (errno == ENOBUFS) { - return EAGAIN; - } else if (errno == EINTR) { + error = errno == ENOBUFS ? EAGAIN : errno; + if (error == EINTR) { + /* continue without incrementing 'i', i.e. retry this packet */ continue; - } else if (errno != EAGAIN) { - VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s", - netdev_get_name(netdev_), strerror(errno)); } - return errno; + break; } else if (retval != size) { - VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of " - "%zu) on %s", retval, size, netdev_get_name(netdev_)); - return EMSGSIZE; - } else { - return 0; + VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE" bytes" + " of %"PRIuSIZE") on %s", retval, size, + netdev_get_name(netdev_)); + error = EMSGSIZE; + break; + } + + /* Process the next packet in the batch */ + i++; + } + + if (may_steal) { + for (i = 0; i < cnt; i++) { + dp_packet_delete(pkts[i]); } } + + if (error && error != EAGAIN) { + VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s", + netdev_get_name(netdev_), ovs_strerror(error)); + } + + return error; + } /* Registers with the poll loop to wake up from the next call to poll_block() @@ -981,14 +1259,9 @@ netdev_linux_send(struct netdev *netdev_, const void *data, size_t size) * expected to do additional queuing of packets. Thus, this function is * unlikely to ever be used. It is included for completeness. */ static void -netdev_linux_send_wait(struct netdev *netdev_) +netdev_linux_send_wait(struct netdev *netdev, int qid OVS_UNUSED) { - struct netdev_linux *netdev = netdev_linux_cast(netdev_); - if (netdev->fd < 0) { - /* Nothing to do. */ - } else if (strcmp(netdev_get_type(netdev_), "tap")) { - poll_fd_wait(netdev->fd, POLLOUT); - } else { + if (is_tap_netdev(netdev)) { /* TAP device always accepts packets.*/ poll_immediate_wake(); } @@ -997,70 +1270,87 @@ netdev_linux_send_wait(struct netdev *netdev_) /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful, * otherwise a positive errno value. */ static int -netdev_linux_set_etheraddr(struct netdev *netdev_, - const uint8_t mac[ETH_ADDR_LEN]) +netdev_linux_set_etheraddr(struct netdev *netdev_, const struct eth_addr mac) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + enum netdev_flags old_flags = 0; int error; - bool up_again = false; - if (netdev_dev->cache_valid & VALID_ETHERADDR) { - if (netdev_dev->ether_addr_error) { - return netdev_dev->ether_addr_error; - } - if (eth_addr_equals(netdev_dev->etheraddr, mac)) { - return 0; + ovs_mutex_lock(&netdev->mutex); + + if (netdev->cache_valid & VALID_ETHERADDR) { + error = netdev->ether_addr_error; + if (error || eth_addr_equals(netdev->etheraddr, mac)) { + goto exit; } - netdev_dev->cache_valid &= ~VALID_ETHERADDR; + netdev->cache_valid &= ~VALID_ETHERADDR; } /* Tap devices must be brought down before setting the address. */ - if (!strcmp(netdev_get_type(netdev_), "tap")) { - enum netdev_flags flags; - - if (!netdev_get_flags(netdev_, &flags) && (flags & NETDEV_UP)) { - netdev_turn_flags_off(netdev_, NETDEV_UP, false); - up_again = true; - } + if (is_tap_netdev(netdev_)) { + update_flags(netdev, NETDEV_UP, 0, &old_flags); } error = set_etheraddr(netdev_get_name(netdev_), mac); if (!error || error == ENODEV) { - netdev_dev->ether_addr_error = error; - netdev_dev->cache_valid |= VALID_ETHERADDR; + netdev->ether_addr_error = error; + netdev->cache_valid |= VALID_ETHERADDR; if (!error) { - memcpy(netdev_dev->etheraddr, mac, ETH_ADDR_LEN); + netdev->etheraddr = mac; } } - if (up_again) { - netdev_turn_flags_on(netdev_, NETDEV_UP, false); + if (is_tap_netdev(netdev_) && old_flags & NETDEV_UP) { + update_flags(netdev, 0, NETDEV_UP, &old_flags); } +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } /* Copies 'netdev''s MAC address to 'mac' which is passed as param. */ static int -netdev_linux_get_etheraddr(const struct netdev *netdev_, - uint8_t mac[ETH_ADDR_LEN]) +netdev_linux_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + int error; + + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_ETHERADDR)) { + netdev->ether_addr_error = get_etheraddr(netdev_get_name(netdev_), + &netdev->etheraddr); + netdev->cache_valid |= VALID_ETHERADDR; + } + + error = netdev->ether_addr_error; + if (!error) { + *mac = netdev->etheraddr; + } + ovs_mutex_unlock(&netdev->mutex); + + return error; +} + +static int +netdev_linux_get_mtu__(struct netdev_linux *netdev, int *mtup) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + int error; - if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) { - int error = get_etheraddr(netdev_get_name(netdev_), - netdev_dev->etheraddr); + if (!(netdev->cache_valid & VALID_MTU)) { + struct ifreq ifr; - netdev_dev->ether_addr_error = error; - netdev_dev->cache_valid |= VALID_ETHERADDR; + netdev->netdev_mtu_error = af_inet_ifreq_ioctl( + netdev_get_name(&netdev->up), &ifr, SIOCGIFMTU, "SIOCGIFMTU"); + netdev->mtu = ifr.ifr_mtu; + netdev->cache_valid |= VALID_MTU; } - if (!netdev_dev->ether_addr_error) { - memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN); + error = netdev->netdev_mtu_error; + if (!error) { + *mtup = netdev->mtu; } - return netdev_dev->ether_addr_error; + return error; } /* Returns the maximum size of transmitted (and received) packets on 'netdev', @@ -1069,24 +1359,14 @@ netdev_linux_get_etheraddr(const struct netdev *netdev_, static int netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); - if (!(netdev_dev->cache_valid & VALID_MTU)) { - struct ifreq ifr; - int error; - - error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr, - SIOCGIFMTU, "SIOCGIFMTU"); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + int error; - netdev_dev->netdev_mtu_error = error; - netdev_dev->mtu = ifr.ifr_mtu; - netdev_dev->cache_valid |= VALID_MTU; - } + ovs_mutex_lock(&netdev->mutex); + error = netdev_linux_get_mtu__(netdev, mtup); + ovs_mutex_unlock(&netdev->mutex); - if (!netdev_dev->netdev_mtu_error) { - *mtup = netdev_dev->mtu; - } - return netdev_dev->netdev_mtu_error; + return error; } /* Sets the maximum size of transmitted (MTU) for given device using linux @@ -1095,61 +1375,73 @@ netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup) static int netdev_linux_set_mtu(const struct netdev *netdev_, int mtu) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct ifreq ifr; int error; - if (netdev_dev->cache_valid & VALID_MTU) { - if (netdev_dev->netdev_mtu_error) { - return netdev_dev->netdev_mtu_error; + ovs_mutex_lock(&netdev->mutex); + if (netdev->cache_valid & VALID_MTU) { + error = netdev->netdev_mtu_error; + if (error || netdev->mtu == mtu) { + goto exit; } - if (netdev_dev->mtu == mtu) { - return 0; - } - netdev_dev->cache_valid &= ~VALID_MTU; + netdev->cache_valid &= ~VALID_MTU; } ifr.ifr_mtu = mtu; - error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr, - SIOCSIFMTU, "SIOCSIFMTU"); + error = af_inet_ifreq_ioctl(netdev_get_name(netdev_), &ifr, + SIOCSIFMTU, "SIOCSIFMTU"); if (!error || error == ENODEV) { - netdev_dev->netdev_mtu_error = error; - netdev_dev->mtu = ifr.ifr_mtu; - netdev_dev->cache_valid |= VALID_MTU; + netdev->netdev_mtu_error = error; + netdev->mtu = ifr.ifr_mtu; + netdev->cache_valid |= VALID_MTU; } +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } /* Returns the ifindex of 'netdev', if successful, as a positive number. * On failure, returns a negative errno value. */ static int -netdev_linux_get_ifindex(const struct netdev *netdev) +netdev_linux_get_ifindex(const struct netdev *netdev_) { + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int ifindex, error; - error = get_ifindex(netdev, &ifindex); + ovs_mutex_lock(&netdev->mutex); + error = get_ifindex(netdev_, &ifindex); + ovs_mutex_unlock(&netdev->mutex); + return error ? -error : ifindex; } static int netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); - if (netdev_dev->miimon_interval > 0) { - *carrier = netdev_dev->miimon; + ovs_mutex_lock(&netdev->mutex); + if (netdev->miimon_interval > 0) { + *carrier = netdev->miimon; } else { - *carrier = (netdev_dev->ifi_flags & IFF_RUNNING) != 0; + *carrier = (netdev->ifi_flags & IFF_RUNNING) != 0; } + ovs_mutex_unlock(&netdev->mutex); return 0; } static long long int -netdev_linux_get_carrier_resets(const struct netdev *netdev) +netdev_linux_get_carrier_resets(const struct netdev *netdev_) { - return netdev_dev_linux_cast(netdev_get_dev(netdev))->carrier_resets; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + long long int carrier_resets; + + ovs_mutex_lock(&netdev->mutex); + carrier_resets = netdev->carrier_resets; + ovs_mutex_unlock(&netdev->mutex); + + return carrier_resets; } static int @@ -1161,7 +1453,7 @@ netdev_linux_do_miimon(const char *name, int cmd, const char *cmd_name, memset(&ifr, 0, sizeof ifr); memcpy(&ifr.ifr_data, data, sizeof *data); - error = netdev_linux_do_ioctl(name, &ifr, cmd, cmd_name); + error = af_inet_ifreq_ioctl(name, &ifr, cmd, cmd_name); memcpy(data, &ifr.ifr_data, sizeof *data); return error; @@ -1215,15 +1507,21 @@ static int netdev_linux_set_miimon_interval(struct netdev *netdev_, long long int interval) { - struct netdev_dev_linux *netdev_dev; - - netdev_dev = netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + ovs_mutex_lock(&netdev->mutex); interval = interval > 0 ? MAX(interval, 100) : 0; - if (netdev_dev->miimon_interval != interval) { - netdev_dev->miimon_interval = interval; - timer_set_expired(&netdev_dev->miimon_timer); + if (netdev->miimon_interval != interval) { + if (interval && !netdev->miimon_interval) { + atomic_count_inc(&miimon_cnt); + } else if (!interval && netdev->miimon_interval) { + atomic_count_dec(&miimon_cnt); + } + + netdev->miimon_interval = interval; + timer_set_expired(&netdev->miimon_timer); } + ovs_mutex_unlock(&netdev->mutex); return 0; } @@ -1235,22 +1533,24 @@ netdev_linux_miimon_run(void) struct shash_node *node; shash_init(&device_shash); - netdev_dev_get_devices(&netdev_linux_class, &device_shash); + netdev_get_devices(&netdev_linux_class, &device_shash); SHASH_FOR_EACH (node, &device_shash) { - struct netdev_dev_linux *dev = node->data; + struct netdev *netdev = node->data; + struct netdev_linux *dev = netdev_linux_cast(netdev); bool miimon; - if (dev->miimon_interval <= 0 || !timer_expired(&dev->miimon_timer)) { - continue; - } + ovs_mutex_lock(&dev->mutex); + if (dev->miimon_interval > 0 && timer_expired(&dev->miimon_timer)) { + netdev_linux_get_miimon(dev->up.name, &miimon); + if (miimon != dev->miimon) { + dev->miimon = miimon; + netdev_linux_changed(dev, dev->ifi_flags, 0); + } - netdev_linux_get_miimon(dev->netdev_dev.name, &miimon); - if (miimon != dev->miimon) { - dev->miimon = miimon; - netdev_dev_linux_changed(dev, dev->ifi_flags, 0); + timer_set_duration(&dev->miimon_timer, dev->miimon_interval); } - - timer_set_duration(&dev->miimon_timer, dev->miimon_interval); + ovs_mutex_unlock(&dev->mutex); + netdev_close(netdev); } shash_destroy(&device_shash); @@ -1263,45 +1563,21 @@ netdev_linux_miimon_wait(void) struct shash_node *node; shash_init(&device_shash); - netdev_dev_get_devices(&netdev_linux_class, &device_shash); + netdev_get_devices(&netdev_linux_class, &device_shash); SHASH_FOR_EACH (node, &device_shash) { - struct netdev_dev_linux *dev = node->data; + struct netdev *netdev = node->data; + struct netdev_linux *dev = netdev_linux_cast(netdev); + ovs_mutex_lock(&dev->mutex); if (dev->miimon_interval > 0) { timer_wait(&dev->miimon_timer); } + ovs_mutex_unlock(&dev->mutex); + netdev_close(netdev); } shash_destroy(&device_shash); } -/* Check whether we can we use RTM_GETLINK to get network device statistics. - * In pre-2.6.19 kernels, this was only available if wireless extensions were - * enabled. */ -static bool -check_for_working_netlink_stats(void) -{ - /* Decide on the netdev_get_stats() implementation to use. Netlink is - * preferable, so if that works, we'll use it. */ - int ifindex = do_get_ifindex("lo"); - if (ifindex < 0) { - VLOG_WARN("failed to get ifindex for lo, " - "obtaining netdev stats from proc"); - return false; - } else { - struct netdev_stats stats; - int error = get_stats_via_netlink(ifindex, &stats); - if (!error) { - VLOG_DBG("obtaining netdev stats via rtnetlink"); - return true; - } else { - VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats " - "via proc (you are probably running a pre-2.6.19 " - "kernel)", strerror(error)); - return false; - } - } -} - static void swap_uint64(uint64_t *a, uint64_t *b) { @@ -1317,14 +1593,14 @@ static void netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst, const struct ovs_vport_stats *src) { - dst->rx_packets = get_unaligned_u64(&src->rx_packets); - dst->tx_packets = get_unaligned_u64(&src->tx_packets); - dst->rx_bytes = get_unaligned_u64(&src->rx_bytes); - dst->tx_bytes = get_unaligned_u64(&src->tx_bytes); - dst->rx_errors = get_unaligned_u64(&src->rx_errors); - dst->tx_errors = get_unaligned_u64(&src->tx_errors); - dst->rx_dropped = get_unaligned_u64(&src->rx_dropped); - dst->tx_dropped = get_unaligned_u64(&src->tx_dropped); + dst->rx_packets = get_32aligned_u64(&src->rx_packets); + dst->tx_packets = get_32aligned_u64(&src->tx_packets); + dst->rx_bytes = get_32aligned_u64(&src->rx_bytes); + dst->tx_bytes = get_32aligned_u64(&src->tx_bytes); + dst->rx_errors = get_32aligned_u64(&src->rx_errors); + dst->tx_errors = get_32aligned_u64(&src->tx_errors); + dst->rx_dropped = get_32aligned_u64(&src->rx_dropped); + dst->tx_dropped = get_32aligned_u64(&src->tx_dropped); dst->multicast = 0; dst->collisions = 0; dst->rx_length_errors = 0; @@ -1343,11 +1619,11 @@ netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst, static int get_stats_via_vport__(const struct netdev *netdev, struct netdev_stats *stats) { - struct dpif_linux_vport reply; + struct dpif_netlink_vport reply; struct ofpbuf *buf; int error; - error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf); + error = dpif_netlink_vport_get(netdev_get_name(netdev), &reply, &buf); if (error) { return error; } else if (!reply.stats) { @@ -1366,51 +1642,21 @@ static void get_stats_via_vport(const struct netdev *netdev_, struct netdev_stats *stats) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); - if (!netdev_dev->vport_stats_error || - !(netdev_dev->cache_valid & VALID_VPORT_STAT_ERROR)) { + if (!netdev->vport_stats_error || + !(netdev->cache_valid & VALID_VPORT_STAT_ERROR)) { int error; error = get_stats_via_vport__(netdev_, stats); - if (error && error != ENOENT) { + if (error && error != ENOENT && error != ENODEV) { VLOG_WARN_RL(&rl, "%s: obtaining netdev stats via vport failed " - "(%s)", netdev_get_name(netdev_), strerror(error)); - } - netdev_dev->vport_stats_error = error; - netdev_dev->cache_valid |= VALID_VPORT_STAT_ERROR; - } -} - -static int -netdev_linux_sys_get_stats(const struct netdev *netdev_, - struct netdev_stats *stats) -{ - static int use_netlink_stats = -1; - int error; - - if (use_netlink_stats < 0) { - use_netlink_stats = check_for_working_netlink_stats(); - } - - if (use_netlink_stats) { - int ifindex; - - error = get_ifindex(netdev_, &ifindex); - if (!error) { - error = get_stats_via_netlink(ifindex, stats); + "(%s)", + netdev_get_name(netdev_), ovs_strerror(error)); } - } else { - error = get_stats_via_proc(netdev_get_name(netdev_), stats); + netdev->vport_stats_error = error; + netdev->cache_valid |= VALID_VPORT_STAT_ERROR; } - - if (error) { - VLOG_WARN_RL(&rl, "%s: linux-sys get stats failed %d", - netdev_get_name(netdev_), error); - } - return error; - } /* Retrieves current device stats for 'netdev-linux'. */ @@ -1418,27 +1664,29 @@ static int netdev_linux_get_stats(const struct netdev *netdev_, struct netdev_stats *stats) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct netdev_stats dev_stats; int error; + ovs_mutex_lock(&netdev->mutex); get_stats_via_vport(netdev_, stats); - - error = netdev_linux_sys_get_stats(netdev_, &dev_stats); - + error = get_stats_via_netlink(netdev_, &dev_stats); if (error) { - if (netdev_dev->vport_stats_error) { - return error; - } else { - return 0; + if (!netdev->vport_stats_error) { + error = 0; } - } - - if (netdev_dev->vport_stats_error) { - /* stats not available from OVS then use ioctl stats. */ + } else if (netdev->vport_stats_error) { + /* stats not available from OVS then use netdev stats. */ *stats = dev_stats; } else { + /* Use kernel netdev's packet and byte counts since vport's counters + * do not reflect packet counts on the wire when GSO, TSO or GRO are + * enabled. */ + stats->rx_packets = dev_stats.rx_packets; + stats->rx_bytes = dev_stats.rx_bytes; + stats->tx_packets = dev_stats.tx_packets; + stats->tx_bytes = dev_stats.tx_bytes; + stats->rx_errors += dev_stats.rx_errors; stats->tx_errors += dev_stats.tx_errors; stats->rx_dropped += dev_stats.rx_dropped; @@ -1457,38 +1705,34 @@ netdev_linux_get_stats(const struct netdev *netdev_, stats->tx_heartbeat_errors += dev_stats.tx_heartbeat_errors; stats->tx_window_errors += dev_stats.tx_window_errors; } - return 0; + ovs_mutex_unlock(&netdev->mutex); + + return error; } /* Retrieves current device stats for 'netdev-tap' netdev or * netdev-internal. */ static int -netdev_tap_get_stats(const struct netdev *netdev_, - struct netdev_stats *stats) +netdev_tap_get_stats(const struct netdev *netdev_, struct netdev_stats *stats) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct netdev_stats dev_stats; int error; + ovs_mutex_lock(&netdev->mutex); get_stats_via_vport(netdev_, stats); - - error = netdev_linux_sys_get_stats(netdev_, &dev_stats); + error = get_stats_via_netlink(netdev_, &dev_stats); if (error) { - if (netdev_dev->vport_stats_error) { - return error; - } else { - return 0; + if (!netdev->vport_stats_error) { + error = 0; } - } + } else if (netdev->vport_stats_error) { + /* Transmit and receive stats will appear to be swapped relative to the + * other ports since we are the one sending the data, not a remote + * computer. For consistency, we swap them back here. This does not + * apply if we are getting stats from the vport layer because it always + * tracks stats from the perspective of the switch. */ - /* If this port is an internal port then the transmit and receive stats - * will appear to be swapped relative to the other ports since we are the - * one sending the data, not a remote computer. For consistency, we swap - * them back here. This does not apply if we are getting stats from the - * vport layer because it always tracks stats from the perspective of the - * switch. */ - if (netdev_dev->vport_stats_error) { *stats = dev_stats; swap_uint64(&stats->rx_packets, &stats->tx_packets); swap_uint64(&stats->rx_bytes, &stats->tx_bytes); @@ -1506,6 +1750,14 @@ netdev_tap_get_stats(const struct netdev *netdev_, stats->tx_heartbeat_errors = 0; stats->tx_window_errors = 0; } else { + /* Use kernel netdev's packet and byte counts since vport counters + * do not reflect packet counts on the wire when GSO, TSO or GRO + * are enabled. */ + stats->rx_packets = dev_stats.tx_packets; + stats->rx_bytes = dev_stats.tx_bytes; + stats->tx_packets = dev_stats.rx_packets; + stats->tx_bytes = dev_stats.rx_bytes; + stats->rx_dropped += dev_stats.tx_dropped; stats->tx_dropped += dev_stats.rx_dropped; @@ -1515,194 +1767,181 @@ netdev_tap_get_stats(const struct netdev *netdev_, stats->multicast += dev_stats.multicast; stats->collisions += dev_stats.collisions; } - return 0; + ovs_mutex_unlock(&netdev->mutex); + + return error; } static int netdev_internal_get_stats(const struct netdev *netdev_, struct netdev_stats *stats) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + int error; + ovs_mutex_lock(&netdev->mutex); get_stats_via_vport(netdev_, stats); - return netdev_dev->vport_stats_error; -} - -static int -netdev_internal_set_stats(struct netdev *netdev, - const struct netdev_stats *stats) -{ - struct ovs_vport_stats vport_stats; - struct dpif_linux_vport vport; - int err; - - vport_stats.rx_packets = stats->rx_packets; - vport_stats.tx_packets = stats->tx_packets; - vport_stats.rx_bytes = stats->rx_bytes; - vport_stats.tx_bytes = stats->tx_bytes; - vport_stats.rx_errors = stats->rx_errors; - vport_stats.tx_errors = stats->tx_errors; - vport_stats.rx_dropped = stats->rx_dropped; - vport_stats.tx_dropped = stats->tx_dropped; - - dpif_linux_vport_init(&vport); - vport.cmd = OVS_VPORT_CMD_SET; - vport.name = netdev_get_name(netdev); - vport.stats = &vport_stats; - - err = dpif_linux_vport_transact(&vport, NULL, NULL); + error = netdev->vport_stats_error; + ovs_mutex_unlock(&netdev->mutex); - /* If the vport layer doesn't know about the device, that doesn't mean it - * doesn't exist (after all were able to open it when netdev_open() was - * called), it just means that it isn't attached and we'll be getting - * stats a different way. */ - if (err == ENODEV) { - err = EOPNOTSUPP; - } - - return err; + return error; } static void -netdev_linux_read_features(struct netdev_dev_linux *netdev_dev) +netdev_linux_read_features(struct netdev_linux *netdev) { struct ethtool_cmd ecmd; uint32_t speed; int error; - if (netdev_dev->cache_valid & VALID_FEATURES) { + if (netdev->cache_valid & VALID_FEATURES) { return; } COVERAGE_INC(netdev_get_ethtool); memset(&ecmd, 0, sizeof ecmd); - error = netdev_linux_do_ethtool(netdev_dev->netdev_dev.name, &ecmd, + error = netdev_linux_do_ethtool(netdev->up.name, &ecmd, ETHTOOL_GSET, "ETHTOOL_GSET"); if (error) { goto out; } /* Supported features. */ - netdev_dev->supported = 0; + netdev->supported = 0; if (ecmd.supported & SUPPORTED_10baseT_Half) { - netdev_dev->supported |= NETDEV_F_10MB_HD; + netdev->supported |= NETDEV_F_10MB_HD; } if (ecmd.supported & SUPPORTED_10baseT_Full) { - netdev_dev->supported |= NETDEV_F_10MB_FD; + netdev->supported |= NETDEV_F_10MB_FD; } if (ecmd.supported & SUPPORTED_100baseT_Half) { - netdev_dev->supported |= NETDEV_F_100MB_HD; + netdev->supported |= NETDEV_F_100MB_HD; } if (ecmd.supported & SUPPORTED_100baseT_Full) { - netdev_dev->supported |= NETDEV_F_100MB_FD; + netdev->supported |= NETDEV_F_100MB_FD; } if (ecmd.supported & SUPPORTED_1000baseT_Half) { - netdev_dev->supported |= NETDEV_F_1GB_HD; + netdev->supported |= NETDEV_F_1GB_HD; + } + if ((ecmd.supported & SUPPORTED_1000baseT_Full) || + (ecmd.supported & SUPPORTED_1000baseKX_Full)) { + netdev->supported |= NETDEV_F_1GB_FD; } - if (ecmd.supported & SUPPORTED_1000baseT_Full) { - netdev_dev->supported |= NETDEV_F_1GB_FD; + if ((ecmd.supported & SUPPORTED_10000baseT_Full) || + (ecmd.supported & SUPPORTED_10000baseKX4_Full) || + (ecmd.supported & SUPPORTED_10000baseKR_Full) || + (ecmd.supported & SUPPORTED_10000baseR_FEC)) { + netdev->supported |= NETDEV_F_10GB_FD; } - if (ecmd.supported & SUPPORTED_10000baseT_Full) { - netdev_dev->supported |= NETDEV_F_10GB_FD; + if ((ecmd.supported & SUPPORTED_40000baseKR4_Full) || + (ecmd.supported & SUPPORTED_40000baseCR4_Full) || + (ecmd.supported & SUPPORTED_40000baseSR4_Full) || + (ecmd.supported & SUPPORTED_40000baseLR4_Full)) { + netdev->supported |= NETDEV_F_40GB_FD; } if (ecmd.supported & SUPPORTED_TP) { - netdev_dev->supported |= NETDEV_F_COPPER; + netdev->supported |= NETDEV_F_COPPER; } if (ecmd.supported & SUPPORTED_FIBRE) { - netdev_dev->supported |= NETDEV_F_FIBER; + netdev->supported |= NETDEV_F_FIBER; } if (ecmd.supported & SUPPORTED_Autoneg) { - netdev_dev->supported |= NETDEV_F_AUTONEG; + netdev->supported |= NETDEV_F_AUTONEG; } if (ecmd.supported & SUPPORTED_Pause) { - netdev_dev->supported |= NETDEV_F_PAUSE; + netdev->supported |= NETDEV_F_PAUSE; } if (ecmd.supported & SUPPORTED_Asym_Pause) { - netdev_dev->supported |= NETDEV_F_PAUSE_ASYM; + netdev->supported |= NETDEV_F_PAUSE_ASYM; } /* Advertised features. */ - netdev_dev->advertised = 0; + netdev->advertised = 0; if (ecmd.advertising & ADVERTISED_10baseT_Half) { - netdev_dev->advertised |= NETDEV_F_10MB_HD; + netdev->advertised |= NETDEV_F_10MB_HD; } if (ecmd.advertising & ADVERTISED_10baseT_Full) { - netdev_dev->advertised |= NETDEV_F_10MB_FD; + netdev->advertised |= NETDEV_F_10MB_FD; } if (ecmd.advertising & ADVERTISED_100baseT_Half) { - netdev_dev->advertised |= NETDEV_F_100MB_HD; + netdev->advertised |= NETDEV_F_100MB_HD; } if (ecmd.advertising & ADVERTISED_100baseT_Full) { - netdev_dev->advertised |= NETDEV_F_100MB_FD; + netdev->advertised |= NETDEV_F_100MB_FD; } if (ecmd.advertising & ADVERTISED_1000baseT_Half) { - netdev_dev->advertised |= NETDEV_F_1GB_HD; + netdev->advertised |= NETDEV_F_1GB_HD; } - if (ecmd.advertising & ADVERTISED_1000baseT_Full) { - netdev_dev->advertised |= NETDEV_F_1GB_FD; + if ((ecmd.advertising & ADVERTISED_1000baseT_Full) || + (ecmd.advertising & ADVERTISED_1000baseKX_Full)) { + netdev->advertised |= NETDEV_F_1GB_FD; } - if (ecmd.advertising & ADVERTISED_10000baseT_Full) { - netdev_dev->advertised |= NETDEV_F_10GB_FD; + if ((ecmd.advertising & ADVERTISED_10000baseT_Full) || + (ecmd.advertising & ADVERTISED_10000baseKX4_Full) || + (ecmd.advertising & ADVERTISED_10000baseKR_Full) || + (ecmd.advertising & ADVERTISED_10000baseR_FEC)) { + netdev->advertised |= NETDEV_F_10GB_FD; + } + if ((ecmd.advertising & ADVERTISED_40000baseKR4_Full) || + (ecmd.advertising & ADVERTISED_40000baseCR4_Full) || + (ecmd.advertising & ADVERTISED_40000baseSR4_Full) || + (ecmd.advertising & ADVERTISED_40000baseLR4_Full)) { + netdev->advertised |= NETDEV_F_40GB_FD; } if (ecmd.advertising & ADVERTISED_TP) { - netdev_dev->advertised |= NETDEV_F_COPPER; + netdev->advertised |= NETDEV_F_COPPER; } if (ecmd.advertising & ADVERTISED_FIBRE) { - netdev_dev->advertised |= NETDEV_F_FIBER; + netdev->advertised |= NETDEV_F_FIBER; } if (ecmd.advertising & ADVERTISED_Autoneg) { - netdev_dev->advertised |= NETDEV_F_AUTONEG; + netdev->advertised |= NETDEV_F_AUTONEG; } if (ecmd.advertising & ADVERTISED_Pause) { - netdev_dev->advertised |= NETDEV_F_PAUSE; + netdev->advertised |= NETDEV_F_PAUSE; } if (ecmd.advertising & ADVERTISED_Asym_Pause) { - netdev_dev->advertised |= NETDEV_F_PAUSE_ASYM; + netdev->advertised |= NETDEV_F_PAUSE_ASYM; } /* Current settings. */ - speed = ecmd.speed; + speed = ethtool_cmd_speed(&ecmd); if (speed == SPEED_10) { - netdev_dev->current = ecmd.duplex ? NETDEV_F_10MB_FD : NETDEV_F_10MB_HD; + netdev->current = ecmd.duplex ? NETDEV_F_10MB_FD : NETDEV_F_10MB_HD; } else if (speed == SPEED_100) { - netdev_dev->current = ecmd.duplex ? NETDEV_F_100MB_FD : NETDEV_F_100MB_HD; + netdev->current = ecmd.duplex ? NETDEV_F_100MB_FD : NETDEV_F_100MB_HD; } else if (speed == SPEED_1000) { - netdev_dev->current = ecmd.duplex ? NETDEV_F_1GB_FD : NETDEV_F_1GB_HD; + netdev->current = ecmd.duplex ? NETDEV_F_1GB_FD : NETDEV_F_1GB_HD; } else if (speed == SPEED_10000) { - netdev_dev->current = NETDEV_F_10GB_FD; + netdev->current = NETDEV_F_10GB_FD; } else if (speed == 40000) { - netdev_dev->current = NETDEV_F_40GB_FD; + netdev->current = NETDEV_F_40GB_FD; } else if (speed == 100000) { - netdev_dev->current = NETDEV_F_100GB_FD; + netdev->current = NETDEV_F_100GB_FD; } else if (speed == 1000000) { - netdev_dev->current = NETDEV_F_1TB_FD; + netdev->current = NETDEV_F_1TB_FD; } else { - netdev_dev->current = 0; + netdev->current = 0; } if (ecmd.port == PORT_TP) { - netdev_dev->current |= NETDEV_F_COPPER; + netdev->current |= NETDEV_F_COPPER; } else if (ecmd.port == PORT_FIBRE) { - netdev_dev->current |= NETDEV_F_FIBER; + netdev->current |= NETDEV_F_FIBER; } if (ecmd.autoneg) { - netdev_dev->current |= NETDEV_F_AUTONEG; + netdev->current |= NETDEV_F_AUTONEG; } - /* Peer advertisements. */ - netdev_dev->peer = 0; /* XXX */ - out: - netdev_dev->cache_valid |= VALID_FEATURES; - netdev_dev->get_features_error = error; + netdev->cache_valid |= VALID_FEATURES; + netdev->get_features_error = error; } -/* Stores the features supported by 'netdev' into each of '*current', - * '*advertised', '*supported', and '*peer' that are non-null. Each value is a - * bitmap of NETDEV_* bits. Returns 0 if successful, otherwise a positive - * errno value. */ +/* Stores the features supported by 'netdev' into of '*current', '*advertised', + * '*supported', and '*peer'. Each value is a bitmap of NETDEV_* bits. + * Returns 0 if successful, otherwise a positive errno value. */ static int netdev_linux_get_features(const struct netdev *netdev_, enum netdev_features *current, @@ -1710,34 +1949,40 @@ netdev_linux_get_features(const struct netdev *netdev_, enum netdev_features *supported, enum netdev_features *peer) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); - - netdev_linux_read_features(netdev_dev); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + int error; - if (!netdev_dev->get_features_error) { - *current = netdev_dev->current; - *advertised = netdev_dev->advertised; - *supported = netdev_dev->supported; - *peer = netdev_dev->peer; + ovs_mutex_lock(&netdev->mutex); + netdev_linux_read_features(netdev); + if (!netdev->get_features_error) { + *current = netdev->current; + *advertised = netdev->advertised; + *supported = netdev->supported; + *peer = 0; /* XXX */ } - return netdev_dev->get_features_error; + error = netdev->get_features_error; + ovs_mutex_unlock(&netdev->mutex); + + return error; } /* Set the features advertised by 'netdev' to 'advertise'. */ static int -netdev_linux_set_advertisements(struct netdev *netdev, +netdev_linux_set_advertisements(struct netdev *netdev_, enum netdev_features advertise) { + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct ethtool_cmd ecmd; int error; + ovs_mutex_lock(&netdev->mutex); + COVERAGE_INC(netdev_get_ethtool); memset(&ecmd, 0, sizeof ecmd); - error = netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd, + error = netdev_linux_do_ethtool(netdev_get_name(netdev_), &ecmd, ETHTOOL_GSET, "ETHTOOL_GSET"); if (error) { - return error; + goto exit; } ecmd.advertising = 0; @@ -1778,72 +2023,73 @@ netdev_linux_set_advertisements(struct netdev *netdev, ecmd.advertising |= ADVERTISED_Asym_Pause; } COVERAGE_INC(netdev_set_ethtool); - return netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd, - ETHTOOL_SSET, "ETHTOOL_SSET"); + error = netdev_linux_do_ethtool(netdev_get_name(netdev_), &ecmd, + ETHTOOL_SSET, "ETHTOOL_SSET"); + +exit: + ovs_mutex_unlock(&netdev->mutex); + return error; } /* Attempts to set input rate limiting (policing) policy. Returns 0 if * successful, otherwise a positive errno value. */ static int -netdev_linux_set_policing(struct netdev *netdev, +netdev_linux_set_policing(struct netdev *netdev_, uint32_t kbits_rate, uint32_t kbits_burst) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); - const char *netdev_name = netdev_get_name(netdev); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + const char *netdev_name = netdev_get_name(netdev_); int error; - kbits_burst = (!kbits_rate ? 0 /* Force to 0 if no rate specified. */ : !kbits_burst ? 1000 /* Default to 1000 kbits if 0. */ : kbits_burst); /* Stick with user-specified value. */ - if (netdev_dev->cache_valid & VALID_POLICING) { - if (netdev_dev->netdev_policing_error) { - return netdev_dev->netdev_policing_error; - } - - if (netdev_dev->kbits_rate == kbits_rate && - netdev_dev->kbits_burst == kbits_burst) { + ovs_mutex_lock(&netdev->mutex); + if (netdev->cache_valid & VALID_POLICING) { + error = netdev->netdev_policing_error; + if (error || (netdev->kbits_rate == kbits_rate && + netdev->kbits_burst == kbits_burst)) { /* Assume that settings haven't changed since we last set them. */ - return 0; + goto out; } - netdev_dev->cache_valid &= ~VALID_POLICING; + netdev->cache_valid &= ~VALID_POLICING; } COVERAGE_INC(netdev_set_policing); /* Remove any existing ingress qdisc. */ - error = tc_add_del_ingress_qdisc(netdev, false); + error = tc_add_del_ingress_qdisc(netdev_, false); if (error) { VLOG_WARN_RL(&rl, "%s: removing policing failed: %s", - netdev_name, strerror(error)); + netdev_name, ovs_strerror(error)); goto out; } if (kbits_rate) { - error = tc_add_del_ingress_qdisc(netdev, true); + error = tc_add_del_ingress_qdisc(netdev_, true); if (error) { VLOG_WARN_RL(&rl, "%s: adding policing qdisc failed: %s", - netdev_name, strerror(error)); + netdev_name, ovs_strerror(error)); goto out; } - error = tc_add_policer(netdev, kbits_rate, kbits_burst); + error = tc_add_policer(netdev_, kbits_rate, kbits_burst); if (error){ VLOG_WARN_RL(&rl, "%s: adding policing action failed: %s", - netdev_name, strerror(error)); + netdev_name, ovs_strerror(error)); goto out; } } - netdev_dev->kbits_rate = kbits_rate; - netdev_dev->kbits_burst = kbits_burst; + netdev->kbits_rate = kbits_rate; + netdev->kbits_burst = kbits_burst; out: if (!error || error == ENODEV) { - netdev_dev->netdev_policing_error = error; - netdev_dev->cache_valid |= VALID_POLICING; + netdev->netdev_policing_error = error; + netdev->cache_valid |= VALID_POLICING; } + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -1851,7 +2097,7 @@ static int netdev_linux_get_qos_types(const struct netdev *netdev OVS_UNUSED, struct sset *types) { - const struct tc_ops **opsp; + const struct tc_ops *const *opsp; for (opsp = tcs; *opsp != NULL; opsp++) { const struct tc_ops *ops = *opsp; @@ -1865,7 +2111,7 @@ netdev_linux_get_qos_types(const struct netdev *netdev OVS_UNUSED, static const struct tc_ops * tc_lookup_ovs_name(const char *name) { - const struct tc_ops **opsp; + const struct tc_ops *const *opsp; for (opsp = tcs; *opsp != NULL; opsp++) { const struct tc_ops *ops = *opsp; @@ -1879,7 +2125,7 @@ tc_lookup_ovs_name(const char *name) static const struct tc_ops * tc_lookup_linux_name(const char *name) { - const struct tc_ops **opsp; + const struct tc_ops *const *opsp; for (opsp = tcs; *opsp != NULL; opsp++) { const struct tc_ops *ops = *opsp; @@ -1891,14 +2137,13 @@ tc_lookup_linux_name(const char *name) } static struct tc_queue * -tc_find_queue__(const struct netdev *netdev, unsigned int queue_id, +tc_find_queue__(const struct netdev *netdev_, unsigned int queue_id, size_t hash) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct tc_queue *queue; - HMAP_FOR_EACH_IN_BUCKET (queue, hmap_node, hash, &netdev_dev->tc->queues) { + HMAP_FOR_EACH_IN_BUCKET (queue, hmap_node, hash, &netdev->tc->queues) { if (queue->queue_id == queue_id) { return queue; } @@ -1926,30 +2171,30 @@ netdev_linux_get_qos_capabilities(const struct netdev *netdev OVS_UNUSED, } static int -netdev_linux_get_qos(const struct netdev *netdev, +netdev_linux_get_qos(const struct netdev *netdev_, const char **typep, struct smap *details) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - error = tc_query_qdisc(netdev); - if (error) { - return error; + ovs_mutex_lock(&netdev->mutex); + error = tc_query_qdisc(netdev_); + if (!error) { + *typep = netdev->tc->ops->ovs_name; + error = (netdev->tc->ops->qdisc_get + ? netdev->tc->ops->qdisc_get(netdev_, details) + : 0); } + ovs_mutex_unlock(&netdev->mutex); - *typep = netdev_dev->tc->ops->ovs_name; - return (netdev_dev->tc->ops->qdisc_get - ? netdev_dev->tc->ops->qdisc_get(netdev, details) - : 0); + return error; } static int -netdev_linux_set_qos(struct netdev *netdev, +netdev_linux_set_qos(struct netdev *netdev_, const char *type, const struct smap *details) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); const struct tc_ops *new_ops; int error; @@ -1958,111 +2203,131 @@ netdev_linux_set_qos(struct netdev *netdev, return EOPNOTSUPP; } - error = tc_query_qdisc(netdev); + ovs_mutex_lock(&netdev->mutex); + error = tc_query_qdisc(netdev_); if (error) { - return error; + goto exit; } - if (new_ops == netdev_dev->tc->ops) { - return new_ops->qdisc_set ? new_ops->qdisc_set(netdev, details) : 0; + if (new_ops == netdev->tc->ops) { + error = new_ops->qdisc_set ? new_ops->qdisc_set(netdev_, details) : 0; } else { /* Delete existing qdisc. */ - error = tc_del_qdisc(netdev); + error = tc_del_qdisc(netdev_); if (error) { - return error; + goto exit; } - ovs_assert(netdev_dev->tc == NULL); + ovs_assert(netdev->tc == NULL); /* Install new qdisc. */ - error = new_ops->tc_install(netdev, details); - ovs_assert((error == 0) == (netdev_dev->tc != NULL)); - - return error; + error = new_ops->tc_install(netdev_, details); + ovs_assert((error == 0) == (netdev->tc != NULL)); } + +exit: + ovs_mutex_unlock(&netdev->mutex); + return error; } static int -netdev_linux_get_queue(const struct netdev *netdev, +netdev_linux_get_queue(const struct netdev *netdev_, unsigned int queue_id, struct smap *details) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - error = tc_query_qdisc(netdev); - if (error) { - return error; - } else { - struct tc_queue *queue = tc_find_queue(netdev, queue_id); - return (queue - ? netdev_dev->tc->ops->class_get(netdev, queue, details) + ovs_mutex_lock(&netdev->mutex); + error = tc_query_qdisc(netdev_); + if (!error) { + struct tc_queue *queue = tc_find_queue(netdev_, queue_id); + error = (queue + ? netdev->tc->ops->class_get(netdev_, queue, details) : ENOENT); } + ovs_mutex_unlock(&netdev->mutex); + + return error; } static int -netdev_linux_set_queue(struct netdev *netdev, +netdev_linux_set_queue(struct netdev *netdev_, unsigned int queue_id, const struct smap *details) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - error = tc_query_qdisc(netdev); - if (error) { - return error; - } else if (queue_id >= netdev_dev->tc->ops->n_queues - || !netdev_dev->tc->ops->class_set) { - return EINVAL; + ovs_mutex_lock(&netdev->mutex); + error = tc_query_qdisc(netdev_); + if (!error) { + error = (queue_id < netdev->tc->ops->n_queues + && netdev->tc->ops->class_set + ? netdev->tc->ops->class_set(netdev_, queue_id, details) + : EINVAL); } + ovs_mutex_unlock(&netdev->mutex); - return netdev_dev->tc->ops->class_set(netdev, queue_id, details); + return error; } static int -netdev_linux_delete_queue(struct netdev *netdev, unsigned int queue_id) +netdev_linux_delete_queue(struct netdev *netdev_, unsigned int queue_id) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - error = tc_query_qdisc(netdev); - if (error) { - return error; - } else if (!netdev_dev->tc->ops->class_delete) { - return EINVAL; - } else { - struct tc_queue *queue = tc_find_queue(netdev, queue_id); - return (queue - ? netdev_dev->tc->ops->class_delete(netdev, queue) - : ENOENT); + ovs_mutex_lock(&netdev->mutex); + error = tc_query_qdisc(netdev_); + if (!error) { + if (netdev->tc->ops->class_delete) { + struct tc_queue *queue = tc_find_queue(netdev_, queue_id); + error = (queue + ? netdev->tc->ops->class_delete(netdev_, queue) + : ENOENT); + } else { + error = EINVAL; + } } + ovs_mutex_unlock(&netdev->mutex); + + return error; } static int -netdev_linux_get_queue_stats(const struct netdev *netdev, +netdev_linux_get_queue_stats(const struct netdev *netdev_, unsigned int queue_id, struct netdev_queue_stats *stats) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - error = tc_query_qdisc(netdev); - if (error) { - return error; - } else if (!netdev_dev->tc->ops->class_get_stats) { - return EOPNOTSUPP; - } else { - const struct tc_queue *queue = tc_find_queue(netdev, queue_id); - return (queue - ? netdev_dev->tc->ops->class_get_stats(netdev, queue, stats) - : ENOENT); + ovs_mutex_lock(&netdev->mutex); + error = tc_query_qdisc(netdev_); + if (!error) { + if (netdev->tc->ops->class_get_stats) { + const struct tc_queue *queue = tc_find_queue(netdev_, queue_id); + if (queue) { + stats->created = queue->created; + error = netdev->tc->ops->class_get_stats(netdev_, queue, + stats); + } else { + error = ENOENT; + } + } else { + error = EOPNOTSUPP; + } } + ovs_mutex_unlock(&netdev->mutex); + + return error; } +struct queue_dump_state { + struct nl_dump dump; + struct ofpbuf buf; +}; + static bool -start_queue_dump(const struct netdev *netdev, struct nl_dump *dump) +start_queue_dump(const struct netdev *netdev, struct queue_dump_state *state) { struct ofpbuf request; struct tcmsg *tcmsg; @@ -2072,127 +2337,192 @@ start_queue_dump(const struct netdev *netdev, struct nl_dump *dump) return false; } tcmsg->tcm_parent = 0; - nl_dump_start(dump, rtnl_sock, &request); + nl_dump_start(&state->dump, NETLINK_ROUTE, &request); ofpbuf_uninit(&request); + + ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE); return true; } static int -netdev_linux_dump_queues(const struct netdev *netdev, - netdev_dump_queues_cb *cb, void *aux) -{ - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); - struct tc_queue *queue, *next_queue; - struct smap details; - int last_error; +finish_queue_dump(struct queue_dump_state *state) +{ + ofpbuf_uninit(&state->buf); + return nl_dump_done(&state->dump); +} + +struct netdev_linux_queue_state { + unsigned int *queues; + size_t cur_queue; + size_t n_queues; +}; + +static int +netdev_linux_queue_dump_start(const struct netdev *netdev_, void **statep) +{ + const struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - error = tc_query_qdisc(netdev); - if (error) { - return error; - } else if (!netdev_dev->tc->ops->class_get) { - return EOPNOTSUPP; + ovs_mutex_lock(&netdev->mutex); + error = tc_query_qdisc(netdev_); + if (!error) { + if (netdev->tc->ops->class_get) { + struct netdev_linux_queue_state *state; + struct tc_queue *queue; + size_t i; + + *statep = state = xmalloc(sizeof *state); + state->n_queues = hmap_count(&netdev->tc->queues); + state->cur_queue = 0; + state->queues = xmalloc(state->n_queues * sizeof *state->queues); + + i = 0; + HMAP_FOR_EACH (queue, hmap_node, &netdev->tc->queues) { + state->queues[i++] = queue->queue_id; + } + } else { + error = EOPNOTSUPP; + } } + ovs_mutex_unlock(&netdev->mutex); - last_error = 0; - smap_init(&details); - HMAP_FOR_EACH_SAFE (queue, next_queue, hmap_node, - &netdev_dev->tc->queues) { - smap_clear(&details); + return error; +} - error = netdev_dev->tc->ops->class_get(netdev, queue, &details); - if (!error) { - (*cb)(queue->queue_id, &details, aux); - } else { - last_error = error; +static int +netdev_linux_queue_dump_next(const struct netdev *netdev_, void *state_, + unsigned int *queue_idp, struct smap *details) +{ + const struct netdev_linux *netdev = netdev_linux_cast(netdev_); + struct netdev_linux_queue_state *state = state_; + int error = EOF; + + ovs_mutex_lock(&netdev->mutex); + while (state->cur_queue < state->n_queues) { + unsigned int queue_id = state->queues[state->cur_queue++]; + struct tc_queue *queue = tc_find_queue(netdev_, queue_id); + + if (queue) { + *queue_idp = queue_id; + error = netdev->tc->ops->class_get(netdev_, queue, details); + break; } } - smap_destroy(&details); + ovs_mutex_unlock(&netdev->mutex); + + return error; +} + +static int +netdev_linux_queue_dump_done(const struct netdev *netdev OVS_UNUSED, + void *state_) +{ + struct netdev_linux_queue_state *state = state_; - return last_error; + free(state->queues); + free(state); + return 0; } static int -netdev_linux_dump_queue_stats(const struct netdev *netdev, +netdev_linux_dump_queue_stats(const struct netdev *netdev_, netdev_dump_queue_stats_cb *cb, void *aux) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); - struct nl_dump dump; - struct ofpbuf msg; - int last_error; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; - error = tc_query_qdisc(netdev); - if (error) { - return error; - } else if (!netdev_dev->tc->ops->class_dump_stats) { - return EOPNOTSUPP; - } + ovs_mutex_lock(&netdev->mutex); + error = tc_query_qdisc(netdev_); + if (!error) { + struct queue_dump_state state; - last_error = 0; - if (!start_queue_dump(netdev, &dump)) { - return ENODEV; - } - while (nl_dump_next(&dump, &msg)) { - error = netdev_dev->tc->ops->class_dump_stats(netdev, &msg, cb, aux); - if (error) { - last_error = error; + if (!netdev->tc->ops->class_dump_stats) { + error = EOPNOTSUPP; + } else if (!start_queue_dump(netdev_, &state)) { + error = ENODEV; + } else { + struct ofpbuf msg; + int retval; + + while (nl_dump_next(&state.dump, &msg, &state.buf)) { + retval = netdev->tc->ops->class_dump_stats(netdev_, &msg, + cb, aux); + if (retval) { + error = retval; + } + } + + retval = finish_queue_dump(&state); + if (retval) { + error = retval; + } } } + ovs_mutex_unlock(&netdev->mutex); - error = nl_dump_done(&dump); - return error ? error : last_error; + return error; } static int netdev_linux_get_in4(const struct netdev *netdev_, struct in_addr *address, struct in_addr *netmask) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); - - if (!(netdev_dev->cache_valid & VALID_IN4)) { - int error; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + int error; - error = netdev_linux_get_ipv4(netdev_, &netdev_dev->address, + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_IN4)) { + error = netdev_linux_get_ipv4(netdev_, &netdev->address, SIOCGIFADDR, "SIOCGIFADDR"); - if (error) { - return error; + if (!error) { + error = netdev_linux_get_ipv4(netdev_, &netdev->netmask, + SIOCGIFNETMASK, "SIOCGIFNETMASK"); } + netdev->in4_error = error; + netdev->cache_valid |= VALID_IN4; + } else { + error = netdev->in4_error; + } - error = netdev_linux_get_ipv4(netdev_, &netdev_dev->netmask, - SIOCGIFNETMASK, "SIOCGIFNETMASK"); - if (error) { - return error; + if (!error) { + if (netdev->address.s_addr != INADDR_ANY) { + *address = netdev->address; + *netmask = netdev->netmask; + } else { + error = EADDRNOTAVAIL; } - - netdev_dev->cache_valid |= VALID_IN4; } - *address = netdev_dev->address; - *netmask = netdev_dev->netmask; - return address->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0; + ovs_mutex_unlock(&netdev->mutex); + + return error; } static int netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address, struct in_addr netmask) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; + ovs_mutex_lock(&netdev->mutex); error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address); if (!error) { - netdev_dev->cache_valid |= VALID_IN4; - netdev_dev->address = address; - netdev_dev->netmask = netmask; + netdev->address = address; + netdev->netmask = netmask; if (address.s_addr != INADDR_ANY) { error = do_set_addr(netdev_, SIOCSIFNETMASK, "SIOCSIFNETMASK", netmask); } } + + if (!error) { + netdev->cache_valid |= VALID_IN4; + netdev->in4_error = 0; + } else { + netdev->cache_valid &= ~VALID_IN4; + } + ovs_mutex_unlock(&netdev->mutex); + return error; } @@ -2202,28 +2532,32 @@ parse_if_inet6_line(const char *line, { uint8_t *s6 = in6->s6_addr; #define X8 "%2"SCNx8 - return sscanf(line, - " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 - "%*x %*x %*x %*x %16s\n", - &s6[0], &s6[1], &s6[2], &s6[3], - &s6[4], &s6[5], &s6[6], &s6[7], - &s6[8], &s6[9], &s6[10], &s6[11], - &s6[12], &s6[13], &s6[14], &s6[15], - ifname) == 17; -} - -/* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if - * 'in6' is non-null) and returns true. Otherwise, returns false. */ + return ovs_scan(line, + " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 + "%*x %*x %*x %*x %16s\n", + &s6[0], &s6[1], &s6[2], &s6[3], + &s6[4], &s6[5], &s6[6], &s6[7], + &s6[8], &s6[9], &s6[10], &s6[11], + &s6[12], &s6[13], &s6[14], &s6[15], + ifname); +} + +/* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address. + * Otherwise, sets '*in6' to 'in6addr_any' and returns the corresponding + * error. */ static int netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); - if (!(netdev_dev->cache_valid & VALID_IN6)) { + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + int error; + + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_IN6)) { FILE *file; char line[128]; - netdev_dev->in6 = in6addr_any; + netdev->in6 = in6addr_any; + netdev->in6_error = EADDRNOTAVAIL; file = fopen("/proc/net/if_inet6", "r"); if (file != NULL) { @@ -2234,16 +2568,22 @@ netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6) if (parse_if_inet6_line(line, &in6_tmp, ifname) && !strcmp(name, ifname)) { - netdev_dev->in6 = in6_tmp; + netdev->in6 = in6_tmp; + netdev->in6_error = 0; break; } } fclose(file); + } else { + netdev->in6_error = EOPNOTSUPP; } - netdev_dev->cache_valid |= VALID_IN6; + netdev->cache_valid |= VALID_IN6; } - *in6 = netdev_dev->in6; - return 0; + *in6 = netdev->in6; + error = netdev->in6_error; + ovs_mutex_unlock(&netdev->mutex); + + return error; } static void @@ -2264,11 +2604,10 @@ do_set_addr(struct netdev *netdev, int ioctl_nr, const char *ioctl_name, struct in_addr addr) { struct ifreq ifr; - ovs_strzcpy(ifr.ifr_name, netdev_get_name(netdev), sizeof ifr.ifr_name); - make_in4_sockaddr(&ifr.ifr_addr, addr); - return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr, - ioctl_name); + make_in4_sockaddr(&ifr.ifr_addr, addr); + return af_inet_ifreq_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr, + ioctl_name); } /* Adds 'router' as a default IP gateway. */ @@ -2284,9 +2623,9 @@ netdev_linux_add_router(struct netdev *netdev OVS_UNUSED, struct in_addr router) make_in4_sockaddr(&rt.rt_gateway, router); make_in4_sockaddr(&rt.rt_genmask, any); rt.rt_flags = RTF_UP | RTF_GATEWAY; - error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0; + error = af_inet_ioctl(SIOCADDRT, &rt); if (error) { - VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error)); + VLOG_WARN("ioctl(SIOCADDRT): %s", ovs_strerror(error)); } return error; } @@ -2303,7 +2642,7 @@ netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop, *netdev_name = NULL; stream = fopen(fn, "r"); if (stream == NULL) { - VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno)); + VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, ovs_strerror(errno)); return errno; } @@ -2315,12 +2654,11 @@ netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop, int refcnt, metric, mtu; unsigned int flags, use, window, irtt; - if (sscanf(line, - "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32 - " %d %u %u\n", - iface, &dest, &gateway, &flags, &refcnt, - &use, &metric, &mask, &mtu, &window, &irtt) != 11) { - + if (!ovs_scan(line, + "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32 + " %d %u %u\n", + iface, &dest, &gateway, &flags, &refcnt, + &use, &metric, &mask, &mtu, &window, &irtt)) { VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s", fn, ln, line); continue; @@ -2353,31 +2691,33 @@ netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop, } static int -netdev_linux_get_status(const struct netdev *netdev, struct smap *smap) +netdev_linux_get_status(const struct netdev *netdev_, struct smap *smap) { - struct netdev_dev_linux *netdev_dev; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error = 0; - netdev_dev = netdev_dev_linux_cast(netdev_get_dev(netdev)); - if (!(netdev_dev->cache_valid & VALID_DRVINFO)) { - struct ethtool_cmd *cmd = (struct ethtool_cmd *) &netdev_dev->drvinfo; + ovs_mutex_lock(&netdev->mutex); + if (!(netdev->cache_valid & VALID_DRVINFO)) { + struct ethtool_cmd *cmd = (struct ethtool_cmd *) &netdev->drvinfo; COVERAGE_INC(netdev_get_ethtool); - memset(&netdev_dev->drvinfo, 0, sizeof netdev_dev->drvinfo); - error = netdev_linux_do_ethtool(netdev_dev->netdev_dev.name, + memset(&netdev->drvinfo, 0, sizeof netdev->drvinfo); + error = netdev_linux_do_ethtool(netdev->up.name, cmd, ETHTOOL_GDRVINFO, "ETHTOOL_GDRVINFO"); if (!error) { - netdev_dev->cache_valid |= VALID_DRVINFO; + netdev->cache_valid |= VALID_DRVINFO; } } if (!error) { - smap_add(smap, "driver_name", netdev_dev->drvinfo.driver); - smap_add(smap, "driver_version", netdev_dev->drvinfo.version); - smap_add(smap, "firmware_version", netdev_dev->drvinfo.fw_version); + smap_add(smap, "driver_name", netdev->drvinfo.driver); + smap_add(smap, "driver_version", netdev->drvinfo.version); + smap_add(smap, "firmware_version", netdev->drvinfo.fw_version); } + ovs_mutex_unlock(&netdev->mutex); + return error; } @@ -2395,7 +2735,7 @@ netdev_internal_get_status(const struct netdev *netdev OVS_UNUSED, * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */ static int netdev_linux_arp_lookup(const struct netdev *netdev, - ovs_be32 ip, uint8_t mac[ETH_ADDR_LEN]) + ovs_be32 ip, struct eth_addr *mac) { struct arpreq r; struct sockaddr_in sin; @@ -2411,162 +2751,837 @@ netdev_linux_arp_lookup(const struct netdev *netdev, r.arp_flags = 0; ovs_strzcpy(r.arp_dev, netdev_get_name(netdev), sizeof r.arp_dev); COVERAGE_INC(netdev_arp_lookup); - retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0; + retval = af_inet_ioctl(SIOCGARP, &r); if (!retval) { memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN); } else if (retval != ENXIO) { VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s", - netdev_get_name(netdev), IP_ARGS(ip), strerror(retval)); + netdev_get_name(netdev), IP_ARGS(ip), + ovs_strerror(retval)); + } + return retval; +} + +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; + } + if (nd & NETDEV_LOOPBACK) { + iff |= IFF_LOOPBACK; + } + return iff; +} + +static int +iff_to_nd_flags(int iff) +{ + enum netdev_flags nd = 0; + if (iff & IFF_UP) { + nd |= NETDEV_UP; + } + if (iff & IFF_PROMISC) { + nd |= NETDEV_PROMISC; + } + if (iff & IFF_LOOPBACK) { + nd |= NETDEV_LOOPBACK; + } + return nd; +} + +static int +update_flags(struct netdev_linux *netdev, enum netdev_flags off, + enum netdev_flags on, enum netdev_flags *old_flagsp) + OVS_REQUIRES(netdev->mutex) +{ + int old_flags, new_flags; + int error = 0; + + old_flags = netdev->ifi_flags; + *old_flagsp = iff_to_nd_flags(old_flags); + new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on); + if (new_flags != old_flags) { + error = set_flags(netdev_get_name(&netdev->up), new_flags); + get_flags(&netdev->up, &netdev->ifi_flags); + } + + return error; +} + +static int +netdev_linux_update_flags(struct netdev *netdev_, enum netdev_flags off, + enum netdev_flags on, enum netdev_flags *old_flagsp) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + int error; + + ovs_mutex_lock(&netdev->mutex); + error = update_flags(netdev, off, on, old_flagsp); + ovs_mutex_unlock(&netdev->mutex); + + return error; +} + +#define NETDEV_LINUX_CLASS(NAME, CONSTRUCT, GET_STATS, \ + GET_FEATURES, GET_STATUS) \ +{ \ + NAME, \ + \ + NULL, \ + netdev_linux_run, \ + netdev_linux_wait, \ + \ + netdev_linux_alloc, \ + CONSTRUCT, \ + netdev_linux_destruct, \ + netdev_linux_dealloc, \ + NULL, /* get_config */ \ + NULL, /* set_config */ \ + NULL, /* get_tunnel_config */ \ + NULL, /* build header */ \ + NULL, /* push header */ \ + NULL, /* pop header */ \ + NULL, /* get_numa_id */ \ + NULL, /* set_multiq */ \ + \ + netdev_linux_send, \ + netdev_linux_send_wait, \ + \ + netdev_linux_set_etheraddr, \ + netdev_linux_get_etheraddr, \ + netdev_linux_get_mtu, \ + netdev_linux_set_mtu, \ + netdev_linux_get_ifindex, \ + netdev_linux_get_carrier, \ + netdev_linux_get_carrier_resets, \ + netdev_linux_set_miimon_interval, \ + GET_STATS, \ + \ + GET_FEATURES, \ + netdev_linux_set_advertisements, \ + \ + netdev_linux_set_policing, \ + netdev_linux_get_qos_types, \ + netdev_linux_get_qos_capabilities, \ + netdev_linux_get_qos, \ + netdev_linux_set_qos, \ + netdev_linux_get_queue, \ + netdev_linux_set_queue, \ + netdev_linux_delete_queue, \ + netdev_linux_get_queue_stats, \ + netdev_linux_queue_dump_start, \ + netdev_linux_queue_dump_next, \ + netdev_linux_queue_dump_done, \ + netdev_linux_dump_queue_stats, \ + \ + netdev_linux_get_in4, \ + netdev_linux_set_in4, \ + netdev_linux_get_in6, \ + netdev_linux_add_router, \ + netdev_linux_get_next_hop, \ + GET_STATUS, \ + netdev_linux_arp_lookup, \ + \ + netdev_linux_update_flags, \ + \ + netdev_linux_rxq_alloc, \ + netdev_linux_rxq_construct, \ + netdev_linux_rxq_destruct, \ + netdev_linux_rxq_dealloc, \ + netdev_linux_rxq_recv, \ + netdev_linux_rxq_wait, \ + netdev_linux_rxq_drain, \ +} + +const struct netdev_class netdev_linux_class = + NETDEV_LINUX_CLASS( + "system", + netdev_linux_construct, + netdev_linux_get_stats, + netdev_linux_get_features, + netdev_linux_get_status); + +const struct netdev_class netdev_tap_class = + NETDEV_LINUX_CLASS( + "tap", + netdev_linux_construct_tap, + netdev_tap_get_stats, + netdev_linux_get_features, + netdev_linux_get_status); + +const struct netdev_class netdev_internal_class = + NETDEV_LINUX_CLASS( + "internal", + netdev_linux_construct, + netdev_internal_get_stats, + NULL, /* get_features */ + netdev_internal_get_status); + + +#define CODEL_N_QUEUES 0x0000 + +/* In sufficiently new kernel headers these are defined as enums in + * . Define them here as macros to help out with older + * kernels. (This overrides any enum definition in the header file but that's + * harmless.) */ +#define TCA_CODEL_TARGET 1 +#define TCA_CODEL_LIMIT 2 +#define TCA_CODEL_INTERVAL 3 + +struct codel { + struct tc tc; + uint32_t target; + uint32_t limit; + uint32_t interval; +}; + +static struct codel * +codel_get__(const struct netdev *netdev_) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + return CONTAINER_OF(netdev->tc, struct codel, tc); +} + +static void +codel_install__(struct netdev *netdev_, uint32_t target, uint32_t limit, + uint32_t interval) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + struct codel *codel; + + codel = xmalloc(sizeof *codel); + tc_init(&codel->tc, &tc_ops_codel); + codel->target = target; + codel->limit = limit; + codel->interval = interval; + + netdev->tc = &codel->tc; +} + +static int +codel_setup_qdisc__(struct netdev *netdev, uint32_t target, uint32_t limit, + uint32_t interval) +{ + size_t opt_offset; + struct ofpbuf request; + struct tcmsg *tcmsg; + uint32_t otarget, olimit, ointerval; + int error; + + tc_del_qdisc(netdev); + + tcmsg = tc_make_request(netdev, RTM_NEWQDISC, + NLM_F_EXCL | NLM_F_CREATE, &request); + if (!tcmsg) { + return ENODEV; + } + tcmsg->tcm_handle = tc_make_handle(1, 0); + tcmsg->tcm_parent = TC_H_ROOT; + + otarget = target ? target : 5000; + olimit = limit ? limit : 10240; + ointerval = interval ? interval : 100000; + + nl_msg_put_string(&request, TCA_KIND, "codel"); + opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS); + nl_msg_put_u32(&request, TCA_CODEL_TARGET, otarget); + nl_msg_put_u32(&request, TCA_CODEL_LIMIT, olimit); + nl_msg_put_u32(&request, TCA_CODEL_INTERVAL, ointerval); + nl_msg_end_nested(&request, opt_offset); + + error = tc_transact(&request, NULL); + if (error) { + VLOG_WARN_RL(&rl, "failed to replace %s qdisc, " + "target %u, limit %u, interval %u error %d(%s)", + netdev_get_name(netdev), + otarget, olimit, ointerval, + error, ovs_strerror(error)); + } + return error; +} + +static void +codel_parse_qdisc_details__(struct netdev *netdev OVS_UNUSED, + const struct smap *details, struct codel *codel) +{ + const char *target_s; + const char *limit_s; + const char *interval_s; + + target_s = smap_get(details, "target"); + limit_s = smap_get(details, "limit"); + interval_s = smap_get(details, "interval"); + + codel->target = target_s ? strtoull(target_s, NULL, 10) : 0; + codel->limit = limit_s ? strtoull(limit_s, NULL, 10) : 0; + codel->interval = interval_s ? strtoull(interval_s, NULL, 10) : 0; + + if (!codel->target) { + codel->target = 5000; + } + if (!codel->limit) { + codel->limit = 10240; + } + if (!codel->interval) { + codel->interval = 100000; + } +} + +static int +codel_tc_install(struct netdev *netdev, const struct smap *details) +{ + int error; + struct codel codel; + + codel_parse_qdisc_details__(netdev, details, &codel); + error = codel_setup_qdisc__(netdev, codel.target, codel.limit, + codel.interval); + if (!error) { + codel_install__(netdev, codel.target, codel.limit, codel.interval); + } + return error; +} + +static int +codel_parse_tca_options__(struct nlattr *nl_options, struct codel *codel) +{ + static const struct nl_policy tca_codel_policy[] = { + [TCA_CODEL_TARGET] = { .type = NL_A_U32 }, + [TCA_CODEL_LIMIT] = { .type = NL_A_U32 }, + [TCA_CODEL_INTERVAL] = { .type = NL_A_U32 } + }; + + struct nlattr *attrs[ARRAY_SIZE(tca_codel_policy)]; + + if (!nl_parse_nested(nl_options, tca_codel_policy, + attrs, ARRAY_SIZE(tca_codel_policy))) { + VLOG_WARN_RL(&rl, "failed to parse CoDel class options"); + return EPROTO; + } + + codel->target = nl_attr_get_u32(attrs[TCA_CODEL_TARGET]); + codel->limit = nl_attr_get_u32(attrs[TCA_CODEL_LIMIT]); + codel->interval = nl_attr_get_u32(attrs[TCA_CODEL_INTERVAL]); + return 0; +} + +static int +codel_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg) +{ + struct nlattr *nlattr; + const char * kind; + int error; + struct codel codel; + + error = tc_parse_qdisc(nlmsg, &kind, &nlattr); + if (error != 0) { + return error; + } + + error = codel_parse_tca_options__(nlattr, &codel); + if (error != 0) { + return error; + } + + codel_install__(netdev, codel.target, codel.limit, codel.interval); + return 0; +} + + +static void +codel_tc_destroy(struct tc *tc) +{ + struct codel *codel = CONTAINER_OF(tc, struct codel, tc); + tc_destroy(tc); + free(codel); +} + +static int +codel_qdisc_get(const struct netdev *netdev, struct smap *details) +{ + const struct codel *codel = codel_get__(netdev); + smap_add_format(details, "target", "%u", codel->target); + smap_add_format(details, "limit", "%u", codel->limit); + smap_add_format(details, "interval", "%u", codel->interval); + return 0; +} + +static int +codel_qdisc_set(struct netdev *netdev, const struct smap *details) +{ + struct codel codel; + + codel_parse_qdisc_details__(netdev, details, &codel); + codel_install__(netdev, codel.target, codel.limit, codel.interval); + codel_get__(netdev)->target = codel.target; + codel_get__(netdev)->limit = codel.limit; + codel_get__(netdev)->interval = codel.interval; + return 0; +} + +static const struct tc_ops tc_ops_codel = { + "codel", /* linux_name */ + "linux-codel", /* ovs_name */ + CODEL_N_QUEUES, /* n_queues */ + codel_tc_install, + codel_tc_load, + codel_tc_destroy, + codel_qdisc_get, + codel_qdisc_set, + NULL, + NULL, + NULL, + NULL, + NULL +}; + +/* FQ-CoDel traffic control class. */ + +#define FQCODEL_N_QUEUES 0x0000 + +/* In sufficiently new kernel headers these are defined as enums in + * . Define them here as macros to help out with older + * kernels. (This overrides any enum definition in the header file but that's + * harmless.) */ +#define TCA_FQ_CODEL_TARGET 1 +#define TCA_FQ_CODEL_LIMIT 2 +#define TCA_FQ_CODEL_INTERVAL 3 +#define TCA_FQ_CODEL_ECN 4 +#define TCA_FQ_CODEL_FLOWS 5 +#define TCA_FQ_CODEL_QUANTUM 6 + +struct fqcodel { + struct tc tc; + uint32_t target; + uint32_t limit; + uint32_t interval; + uint32_t flows; + uint32_t quantum; +}; + +static struct fqcodel * +fqcodel_get__(const struct netdev *netdev_) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + return CONTAINER_OF(netdev->tc, struct fqcodel, tc); +} + +static void +fqcodel_install__(struct netdev *netdev_, uint32_t target, uint32_t limit, + uint32_t interval, uint32_t flows, uint32_t quantum) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + struct fqcodel *fqcodel; + + fqcodel = xmalloc(sizeof *fqcodel); + tc_init(&fqcodel->tc, &tc_ops_fqcodel); + fqcodel->target = target; + fqcodel->limit = limit; + fqcodel->interval = interval; + fqcodel->flows = flows; + fqcodel->quantum = quantum; + + netdev->tc = &fqcodel->tc; +} + +static int +fqcodel_setup_qdisc__(struct netdev *netdev, uint32_t target, uint32_t limit, + uint32_t interval, uint32_t flows, uint32_t quantum) +{ + size_t opt_offset; + struct ofpbuf request; + struct tcmsg *tcmsg; + uint32_t otarget, olimit, ointerval, oflows, oquantum; + int error; + + tc_del_qdisc(netdev); + + tcmsg = tc_make_request(netdev, RTM_NEWQDISC, + NLM_F_EXCL | NLM_F_CREATE, &request); + if (!tcmsg) { + return ENODEV; + } + tcmsg->tcm_handle = tc_make_handle(1, 0); + tcmsg->tcm_parent = TC_H_ROOT; + + otarget = target ? target : 5000; + olimit = limit ? limit : 10240; + ointerval = interval ? interval : 100000; + oflows = flows ? flows : 1024; + oquantum = quantum ? quantum : 1514; /* fq_codel default quantum is 1514 + not mtu */ + + nl_msg_put_string(&request, TCA_KIND, "fq_codel"); + opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS); + nl_msg_put_u32(&request, TCA_FQ_CODEL_TARGET, otarget); + nl_msg_put_u32(&request, TCA_FQ_CODEL_LIMIT, olimit); + nl_msg_put_u32(&request, TCA_FQ_CODEL_INTERVAL, ointerval); + nl_msg_put_u32(&request, TCA_FQ_CODEL_FLOWS, oflows); + nl_msg_put_u32(&request, TCA_FQ_CODEL_QUANTUM, oquantum); + nl_msg_end_nested(&request, opt_offset); + + error = tc_transact(&request, NULL); + if (error) { + VLOG_WARN_RL(&rl, "failed to replace %s qdisc, " + "target %u, limit %u, interval %u, flows %u, quantum %u error %d(%s)", + netdev_get_name(netdev), + otarget, olimit, ointerval, oflows, oquantum, + error, ovs_strerror(error)); + } + return error; +} + +static void +fqcodel_parse_qdisc_details__(struct netdev *netdev OVS_UNUSED, + const struct smap *details, struct fqcodel *fqcodel) +{ + const char *target_s; + const char *limit_s; + const char *interval_s; + const char *flows_s; + const char *quantum_s; + + target_s = smap_get(details, "target"); + limit_s = smap_get(details, "limit"); + interval_s = smap_get(details, "interval"); + flows_s = smap_get(details, "flows"); + quantum_s = smap_get(details, "quantum"); + fqcodel->target = target_s ? strtoull(target_s, NULL, 10) : 0; + fqcodel->limit = limit_s ? strtoull(limit_s, NULL, 10) : 0; + fqcodel->interval = interval_s ? strtoull(interval_s, NULL, 10) : 0; + fqcodel->flows = flows_s ? strtoull(flows_s, NULL, 10) : 0; + fqcodel->quantum = quantum_s ? strtoull(quantum_s, NULL, 10) : 0; + if (!fqcodel->target) { + fqcodel->target = 5000; + } + if (!fqcodel->limit) { + fqcodel->limit = 10240; + } + if (!fqcodel->interval) { + fqcodel->interval = 1000000; + } + if (!fqcodel->flows) { + fqcodel->flows = 1024; + } + if (!fqcodel->quantum) { + fqcodel->quantum = 1514; + } +} + +static int +fqcodel_tc_install(struct netdev *netdev, const struct smap *details) +{ + int error; + struct fqcodel fqcodel; + + fqcodel_parse_qdisc_details__(netdev, details, &fqcodel); + error = fqcodel_setup_qdisc__(netdev, fqcodel.target, fqcodel.limit, + fqcodel.interval, fqcodel.flows, + fqcodel.quantum); + if (!error) { + fqcodel_install__(netdev, fqcodel.target, fqcodel.limit, + fqcodel.interval, fqcodel.flows, fqcodel.quantum); + } + return error; +} + +static int +fqcodel_parse_tca_options__(struct nlattr *nl_options, struct fqcodel *fqcodel) +{ + static const struct nl_policy tca_fqcodel_policy[] = { + [TCA_FQ_CODEL_TARGET] = { .type = NL_A_U32 }, + [TCA_FQ_CODEL_LIMIT] = { .type = NL_A_U32 }, + [TCA_FQ_CODEL_INTERVAL] = { .type = NL_A_U32 }, + [TCA_FQ_CODEL_FLOWS] = { .type = NL_A_U32 }, + [TCA_FQ_CODEL_QUANTUM] = { .type = NL_A_U32 } + }; + + struct nlattr *attrs[ARRAY_SIZE(tca_fqcodel_policy)]; + + if (!nl_parse_nested(nl_options, tca_fqcodel_policy, + attrs, ARRAY_SIZE(tca_fqcodel_policy))) { + VLOG_WARN_RL(&rl, "failed to parse FQ_CoDel class options"); + return EPROTO; + } + + fqcodel->target = nl_attr_get_u32(attrs[TCA_FQ_CODEL_TARGET]); + fqcodel->limit = nl_attr_get_u32(attrs[TCA_FQ_CODEL_LIMIT]); + fqcodel->interval =nl_attr_get_u32(attrs[TCA_FQ_CODEL_INTERVAL]); + fqcodel->flows = nl_attr_get_u32(attrs[TCA_FQ_CODEL_FLOWS]); + fqcodel->quantum = nl_attr_get_u32(attrs[TCA_FQ_CODEL_QUANTUM]); + return 0; +} + +static int +fqcodel_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg) +{ + struct nlattr *nlattr; + const char * kind; + int error; + struct fqcodel fqcodel; + + error = tc_parse_qdisc(nlmsg, &kind, &nlattr); + if (error != 0) { + return error; + } + + error = fqcodel_parse_tca_options__(nlattr, &fqcodel); + if (error != 0) { + return error; + } + + fqcodel_install__(netdev, fqcodel.target, fqcodel.limit, fqcodel.interval, + fqcodel.flows, fqcodel.quantum); + return 0; +} + +static void +fqcodel_tc_destroy(struct tc *tc) +{ + struct fqcodel *fqcodel = CONTAINER_OF(tc, struct fqcodel, tc); + tc_destroy(tc); + free(fqcodel); +} + +static int +fqcodel_qdisc_get(const struct netdev *netdev, struct smap *details) +{ + const struct fqcodel *fqcodel = fqcodel_get__(netdev); + smap_add_format(details, "target", "%u", fqcodel->target); + smap_add_format(details, "limit", "%u", fqcodel->limit); + smap_add_format(details, "interval", "%u", fqcodel->interval); + smap_add_format(details, "flows", "%u", fqcodel->flows); + smap_add_format(details, "quantum", "%u", fqcodel->quantum); + return 0; +} + +static int +fqcodel_qdisc_set(struct netdev *netdev, const struct smap *details) +{ + struct fqcodel fqcodel; + + fqcodel_parse_qdisc_details__(netdev, details, &fqcodel); + fqcodel_install__(netdev, fqcodel.target, fqcodel.limit, fqcodel.interval, + fqcodel.flows, fqcodel.quantum); + fqcodel_get__(netdev)->target = fqcodel.target; + fqcodel_get__(netdev)->limit = fqcodel.limit; + fqcodel_get__(netdev)->interval = fqcodel.interval; + fqcodel_get__(netdev)->flows = fqcodel.flows; + fqcodel_get__(netdev)->quantum = fqcodel.quantum; + return 0; +} + +static const struct tc_ops tc_ops_fqcodel = { + "fq_codel", /* linux_name */ + "linux-fq_codel", /* ovs_name */ + FQCODEL_N_QUEUES, /* n_queues */ + fqcodel_tc_install, + fqcodel_tc_load, + fqcodel_tc_destroy, + fqcodel_qdisc_get, + fqcodel_qdisc_set, + NULL, + NULL, + NULL, + NULL, + NULL +}; + +/* SFQ traffic control class. */ + +#define SFQ_N_QUEUES 0x0000 + +struct sfq { + struct tc tc; + uint32_t quantum; + uint32_t perturb; +}; + +static struct sfq * +sfq_get__(const struct netdev *netdev_) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + return CONTAINER_OF(netdev->tc, struct sfq, tc); +} + +static void +sfq_install__(struct netdev *netdev_, uint32_t quantum, uint32_t perturb) +{ + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + struct sfq *sfq; + + sfq = xmalloc(sizeof *sfq); + tc_init(&sfq->tc, &tc_ops_sfq); + sfq->perturb = perturb; + sfq->quantum = quantum; + + netdev->tc = &sfq->tc; +} + +static int +sfq_setup_qdisc__(struct netdev *netdev, uint32_t quantum, uint32_t perturb) +{ + struct tc_sfq_qopt opt; + struct ofpbuf request; + struct tcmsg *tcmsg; + int mtu; + int mtu_error, error; + mtu_error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu); + + tc_del_qdisc(netdev); + + tcmsg = tc_make_request(netdev, RTM_NEWQDISC, + NLM_F_EXCL | NLM_F_CREATE, &request); + if (!tcmsg) { + return ENODEV; + } + tcmsg->tcm_handle = tc_make_handle(1, 0); + tcmsg->tcm_parent = TC_H_ROOT; + + memset(&opt, 0, sizeof opt); + if (!quantum) { + if (!mtu_error) { + opt.quantum = mtu; /* if we cannot find mtu, use default */ + } + } else { + opt.quantum = quantum; + } + + if (!perturb) { + opt.perturb_period = 10; + } else { + opt.perturb_period = perturb; + } + + nl_msg_put_string(&request, TCA_KIND, "sfq"); + nl_msg_put_unspec(&request, TCA_OPTIONS, &opt, sizeof opt); + + error = tc_transact(&request, NULL); + if (error) { + VLOG_WARN_RL(&rl, "failed to replace %s qdisc, " + "quantum %u, perturb %u error %d(%s)", + netdev_get_name(netdev), + opt.quantum, opt.perturb_period, + error, ovs_strerror(error)); } - return retval; + return error; } -static int -nd_to_iff_flags(enum netdev_flags nd) +static void +sfq_parse_qdisc_details__(struct netdev *netdev, + const struct smap *details, struct sfq *sfq) { - int iff = 0; - if (nd & NETDEV_UP) { - iff |= IFF_UP; + const char *perturb_s; + const char *quantum_s; + int mtu; + int mtu_error; + + perturb_s = smap_get(details, "perturb"); + quantum_s = smap_get(details, "quantum"); + sfq->perturb = perturb_s ? strtoull(perturb_s, NULL, 10) : 0; + sfq->quantum = quantum_s ? strtoull(quantum_s, NULL, 10) : 0; + if (!sfq->perturb) { + sfq->perturb = 10; } - if (nd & NETDEV_PROMISC) { - iff |= IFF_PROMISC; + + if (!sfq->quantum) { + mtu_error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu); + if (!mtu_error) { + sfq->quantum = mtu; + } else { + VLOG_WARN_RL(&rl, "when using SFQ, you must specify quantum on a " + "device without mtu"); + return; + } } - return iff; } static int -iff_to_nd_flags(int iff) +sfq_tc_install(struct netdev *netdev, const struct smap *details) { - enum netdev_flags nd = 0; - if (iff & IFF_UP) { - nd |= NETDEV_UP; - } - if (iff & IFF_PROMISC) { - nd |= NETDEV_PROMISC; + int error; + struct sfq sfq; + + sfq_parse_qdisc_details__(netdev, details, &sfq); + error = sfq_setup_qdisc__(netdev, sfq.quantum, sfq.perturb); + if (!error) { + sfq_install__(netdev, sfq.quantum, sfq.perturb); } - return nd; + return error; } static int -netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off, - enum netdev_flags on, enum netdev_flags *old_flagsp) +sfq_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg) { - struct netdev_dev_linux *netdev_dev; - int old_flags, new_flags; - int error = 0; + const struct tc_sfq_qopt *sfq; + struct nlattr *nlattr; + const char * kind; + int error; - netdev_dev = netdev_dev_linux_cast(netdev_get_dev(netdev)); - old_flags = netdev_dev->ifi_flags; - *old_flagsp = iff_to_nd_flags(old_flags); - 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); - get_flags(&netdev_dev->netdev_dev, &netdev_dev->ifi_flags); + error = tc_parse_qdisc(nlmsg, &kind, &nlattr); + if (error == 0) { + sfq = nl_attr_get(nlattr); + sfq_install__(netdev, sfq->perturb_period, sfq->quantum); + return 0; } + return error; } -static unsigned int -netdev_linux_change_seq(const struct netdev *netdev) +static void +sfq_tc_destroy(struct tc *tc) { - return netdev_dev_linux_cast(netdev_get_dev(netdev))->change_seq; + struct sfq *sfq = CONTAINER_OF(tc, struct sfq, tc); + tc_destroy(tc); + free(sfq); } -#define NETDEV_LINUX_CLASS(NAME, CREATE, GET_STATS, SET_STATS, \ - GET_FEATURES, GET_STATUS) \ -{ \ - NAME, \ - \ - netdev_linux_init, \ - netdev_linux_run, \ - netdev_linux_wait, \ - \ - CREATE, \ - netdev_linux_destroy, \ - NULL, /* get_config */ \ - NULL, /* set_config */ \ - NULL, /* get_tunnel_config */ \ - \ - netdev_linux_open, \ - netdev_linux_close, \ - \ - netdev_linux_listen, \ - netdev_linux_recv, \ - netdev_linux_recv_wait, \ - netdev_linux_drain, \ - \ - netdev_linux_send, \ - netdev_linux_send_wait, \ - \ - netdev_linux_set_etheraddr, \ - netdev_linux_get_etheraddr, \ - netdev_linux_get_mtu, \ - netdev_linux_set_mtu, \ - netdev_linux_get_ifindex, \ - netdev_linux_get_carrier, \ - netdev_linux_get_carrier_resets, \ - netdev_linux_set_miimon_interval, \ - GET_STATS, \ - SET_STATS, \ - \ - GET_FEATURES, \ - netdev_linux_set_advertisements, \ - \ - netdev_linux_set_policing, \ - netdev_linux_get_qos_types, \ - netdev_linux_get_qos_capabilities, \ - netdev_linux_get_qos, \ - netdev_linux_set_qos, \ - netdev_linux_get_queue, \ - netdev_linux_set_queue, \ - netdev_linux_delete_queue, \ - netdev_linux_get_queue_stats, \ - netdev_linux_dump_queues, \ - netdev_linux_dump_queue_stats, \ - \ - netdev_linux_get_in4, \ - netdev_linux_set_in4, \ - netdev_linux_get_in6, \ - netdev_linux_add_router, \ - netdev_linux_get_next_hop, \ - GET_STATUS, \ - netdev_linux_arp_lookup, \ - \ - netdev_linux_update_flags, \ - \ - netdev_linux_change_seq \ +static int +sfq_qdisc_get(const struct netdev *netdev, struct smap *details) +{ + const struct sfq *sfq = sfq_get__(netdev); + smap_add_format(details, "quantum", "%u", sfq->quantum); + smap_add_format(details, "perturb", "%u", sfq->perturb); + return 0; } -const struct netdev_class netdev_linux_class = - NETDEV_LINUX_CLASS( - "system", - netdev_linux_create, - netdev_linux_get_stats, - NULL, /* set_stats */ - netdev_linux_get_features, - netdev_linux_get_status); +static int +sfq_qdisc_set(struct netdev *netdev, const struct smap *details) +{ + struct sfq sfq; -const struct netdev_class netdev_tap_class = - NETDEV_LINUX_CLASS( - "tap", - netdev_linux_create_tap, - netdev_tap_get_stats, - NULL, /* set_stats */ - netdev_linux_get_features, - netdev_linux_get_status); + sfq_parse_qdisc_details__(netdev, details, &sfq); + sfq_install__(netdev, sfq.quantum, sfq.perturb); + sfq_get__(netdev)->quantum = sfq.quantum; + sfq_get__(netdev)->perturb = sfq.perturb; + return 0; +} -const struct netdev_class netdev_internal_class = - NETDEV_LINUX_CLASS( - "internal", - netdev_linux_create, - netdev_internal_get_stats, - netdev_internal_set_stats, - NULL, /* get_features */ - netdev_internal_get_status); +static const struct tc_ops tc_ops_sfq = { + "sfq", /* linux_name */ + "linux-sfq", /* ovs_name */ + SFQ_N_QUEUES, /* n_queues */ + sfq_tc_install, + sfq_tc_load, + sfq_tc_destroy, + sfq_qdisc_get, + sfq_qdisc_set, + NULL, + NULL, + NULL, + NULL, + NULL +}; /* HTB traffic control class. */ #define HTB_N_QUEUES 0xf000 +#define HTB_RATE2QUANTUM 10 struct htb { struct tc tc; @@ -2582,25 +3597,23 @@ struct htb_class { }; static struct htb * -htb_get__(const struct netdev *netdev) +htb_get__(const struct netdev *netdev_) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); - return CONTAINER_OF(netdev_dev->tc, struct htb, tc); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + return CONTAINER_OF(netdev->tc, struct htb, tc); } static void -htb_install__(struct netdev *netdev, uint64_t max_rate) +htb_install__(struct netdev *netdev_, uint64_t max_rate) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct htb *htb; htb = xmalloc(sizeof *htb); tc_init(&htb->tc, &tc_ops_htb); htb->max_rate = max_rate; - netdev_dev->tc = &htb->tc; + netdev->tc = &htb->tc; } /* Create an HTB qdisc. @@ -2627,7 +3640,7 @@ htb_setup_qdisc__(struct netdev *netdev) nl_msg_put_string(&request, TCA_KIND, "htb"); memset(&opt, 0, sizeof opt); - opt.rate2quantum = 10; + opt.rate2quantum = HTB_RATE2QUANTUM; opt.version = 3; opt.defcls = 1; @@ -2651,7 +3664,7 @@ htb_setup_class__(struct netdev *netdev, unsigned int handle, int error; int mtu; - error = netdev_get_mtu(netdev, &mtu); + error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu); if (error) { VLOG_WARN_RL(&rl, "cannot set up HTB on device %s that lacks MTU", netdev_get_name(netdev)); @@ -2661,6 +3674,11 @@ htb_setup_class__(struct netdev *netdev, unsigned int handle, memset(&opt, 0, sizeof opt); tc_fill_rate(&opt.rate, class->min_rate, mtu); tc_fill_rate(&opt.ceil, class->max_rate, mtu); + /* Makes sure the quantum is at least MTU. Setting quantum will + * make htb ignore the r2q for this class. */ + if ((class->min_rate / HTB_RATE2QUANTUM) < mtu) { + opt.quantum = mtu; + } opt.buffer = tc_calc_buffer(opt.rate.rate, mtu, class->burst); opt.cbuffer = tc_calc_buffer(opt.ceil.rate, mtu, class->burst); opt.prio = class->priority; @@ -2687,7 +3705,7 @@ htb_setup_class__(struct netdev *netdev, unsigned int handle, tc_get_major(handle), tc_get_minor(handle), tc_get_major(parent), tc_get_minor(parent), class->min_rate, class->max_rate, - class->burst, class->priority, strerror(error)); + class->burst, class->priority, ovs_strerror(error)); } return error; } @@ -2747,9 +3765,10 @@ htb_parse_tcmsg__(struct ofpbuf *tcmsg, unsigned int *queue_id, } static void -htb_parse_qdisc_details__(struct netdev *netdev, +htb_parse_qdisc_details__(struct netdev *netdev_, const struct smap *details, struct htb_class *hc) { + struct netdev_linux *netdev = netdev_linux_cast(netdev_); const char *max_rate_s; max_rate_s = smap_get(details, "max-rate"); @@ -2757,7 +3776,8 @@ htb_parse_qdisc_details__(struct netdev *netdev, if (!hc->max_rate) { enum netdev_features current; - netdev_get_features(netdev, ¤t, NULL, NULL, NULL); + netdev_linux_read_features(netdev); + current = !netdev->get_features_error ? netdev->current : 0; hc->max_rate = netdev_features_to_bps(current, 100 * 1000 * 1000) / 8; } hc->min_rate = hc->max_rate; @@ -2776,7 +3796,7 @@ htb_parse_class_details__(struct netdev *netdev, const char *priority_s = smap_get(details, "priority"); int mtu, error; - error = netdev_get_mtu(netdev, &mtu); + error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu); if (error) { VLOG_WARN_RL(&rl, "cannot parse HTB class on device %s that lacks MTU", netdev_get_name(netdev)); @@ -2871,6 +3891,7 @@ htb_update_queue__(struct netdev *netdev, unsigned int queue_id, hcp = xmalloc(sizeof *hcp); queue = &hcp->tc_queue; queue->queue_id = queue_id; + queue->created = time_msec(); hmap_insert(&htb->tc.queues, &queue->hmap_node, hash); } @@ -2884,7 +3905,7 @@ static int htb_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED) { struct ofpbuf msg; - struct nl_dump dump; + struct queue_dump_state state; struct htb_class hc; /* Get qdisc options. */ @@ -2893,17 +3914,17 @@ htb_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED) htb_install__(netdev, hc.max_rate); /* Get queues. */ - if (!start_queue_dump(netdev, &dump)) { + if (!start_queue_dump(netdev, &state)) { return ENODEV; } - while (nl_dump_next(&dump, &msg)) { + while (nl_dump_next(&state.dump, &msg, &state.buf)) { unsigned int queue_id; if (!htb_parse_tcmsg__(&msg, &queue_id, &hc, NULL)) { htb_update_queue__(netdev, queue_id, &hc); } } - nl_dump_done(&dump); + finish_queue_dump(&state); return 0; } @@ -3061,11 +4082,10 @@ struct hfsc_class { }; static struct hfsc * -hfsc_get__(const struct netdev *netdev) +hfsc_get__(const struct netdev *netdev_) { - struct netdev_dev_linux *netdev_dev; - netdev_dev = netdev_dev_linux_cast(netdev_get_dev(netdev)); - return CONTAINER_OF(netdev_dev->tc, struct hfsc, tc); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + return CONTAINER_OF(netdev->tc, struct hfsc, tc); } static struct hfsc_class * @@ -3075,16 +4095,15 @@ hfsc_class_cast__(const struct tc_queue *queue) } static void -hfsc_install__(struct netdev *netdev, uint32_t max_rate) +hfsc_install__(struct netdev *netdev_, uint32_t max_rate) { - struct netdev_dev_linux * netdev_dev; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct hfsc *hfsc; - netdev_dev = netdev_dev_linux_cast(netdev_get_dev(netdev)); hfsc = xmalloc(sizeof *hfsc); tc_init(&hfsc->tc, &tc_ops_hfsc); hfsc->max_rate = max_rate; - netdev_dev->tc = &hfsc->tc; + netdev->tc = &hfsc->tc; } static void @@ -3106,6 +4125,7 @@ hfsc_update_queue__(struct netdev *netdev, unsigned int queue_id, hcp = xmalloc(sizeof *hcp); queue = &hcp->tc_queue; queue->queue_id = queue_id; + queue->created = time_msec(); hmap_insert(&hfsc->tc.queues, &queue->hmap_node, hash); } @@ -3224,9 +4244,10 @@ hfsc_query_class__(const struct netdev *netdev, unsigned int handle, } static void -hfsc_parse_qdisc_details__(struct netdev *netdev, const struct smap *details, +hfsc_parse_qdisc_details__(struct netdev *netdev_, const struct smap *details, struct hfsc_class *class) { + struct netdev_linux *netdev = netdev_linux_cast(netdev_); uint32_t max_rate; const char *max_rate_s; @@ -3236,7 +4257,8 @@ hfsc_parse_qdisc_details__(struct netdev *netdev, const struct smap *details, if (!max_rate) { enum netdev_features current; - netdev_get_features(netdev, ¤t, NULL, NULL, NULL); + netdev_linux_read_features(netdev); + current = !netdev->get_features_error ? netdev->current : 0; max_rate = netdev_features_to_bps(current, 100 * 1000 * 1000) / 8; } @@ -3349,7 +4371,7 @@ hfsc_setup_class__(struct netdev *netdev, unsigned int handle, netdev_get_name(netdev), tc_get_major(handle), tc_get_minor(handle), tc_get_major(parent), tc_get_minor(parent), - class->min_rate, class->max_rate, strerror(error)); + class->min_rate, class->max_rate, ovs_strerror(error)); } return error; @@ -3383,18 +4405,18 @@ static int hfsc_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED) { struct ofpbuf msg; - struct nl_dump dump; + struct queue_dump_state state; struct hfsc_class hc; hc.max_rate = 0; hfsc_query_class__(netdev, tc_make_handle(1, 0xfffe), 0, &hc, NULL); hfsc_install__(netdev, hc.max_rate); - if (!start_queue_dump(netdev, &dump)) { + if (!start_queue_dump(netdev, &state)) { return ENODEV; } - while (nl_dump_next(&dump, &msg)) { + while (nl_dump_next(&state.dump, &msg, &state.buf)) { unsigned int queue_id; if (!hfsc_parse_tcmsg__(&msg, &queue_id, &hc, NULL)) { @@ -3402,7 +4424,7 @@ hfsc_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED) } } - nl_dump_done(&dump); + finish_queue_dump(&state); return 0; } @@ -3555,17 +4577,14 @@ static const struct tc_ops tc_ops_hfsc = { * the "" (empty string) QoS type in the OVS database. */ static void -default_install__(struct netdev *netdev) +default_install__(struct netdev *netdev_) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); - static struct tc *tc; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + static const struct tc tc = TC_INITIALIZER(&tc, &tc_ops_default); - if (!tc) { - tc = xmalloc(sizeof *tc); - tc_init(tc, &tc_ops_default); - } - netdev_dev->tc = tc; + /* Nothing but a tc class implementation is allowed to write to a tc. This + * class never does that, so we can legitimately use a const tc object. */ + netdev->tc = CONST_CAST(struct tc *, &tc); } static int @@ -3604,17 +4623,14 @@ static const struct tc_ops tc_ops_default = { * */ static int -other_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED) +other_tc_load(struct netdev *netdev_, struct ofpbuf *nlmsg OVS_UNUSED) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); - static struct tc *tc; + struct netdev_linux *netdev = netdev_linux_cast(netdev_); + static const struct tc tc = TC_INITIALIZER(&tc, &tc_ops_other); - if (!tc) { - tc = xmalloc(sizeof *tc); - tc_init(tc, &tc_ops_other); - } - netdev_dev->tc = tc; + /* Nothing but a tc class implementation is allowed to write to a tc. This + * class never does that, so we can legitimately use a const tc object. */ + netdev->tc = CONST_CAST(struct tc *, &tc); return 0; } @@ -3706,7 +4722,7 @@ tc_make_request(const struct netdev *netdev, int type, unsigned int flags, static int tc_transact(struct ofpbuf *request, struct ofpbuf **replyp) { - int error = nl_sock_transact(rtnl_sock, request, replyp); + int error = nl_transact(NETLINK_ROUTE, request, replyp); ofpbuf_uninit(request); return error; } @@ -3765,12 +4781,13 @@ tc_add_del_ingress_qdisc(struct netdev *netdev, bool add) * mtu 65535 drop * * The configuration and stats may be seen with the following command: - * /sbin/tc -s filter show eth0 parent ffff: + * /sbin/tc -s filter show dev parent ffff: * * Returns 0 if successful, otherwise a positive errno value. */ static int -tc_add_policer(struct netdev *netdev, int kbits_rate, int kbits_burst) +tc_add_policer(struct netdev *netdev, + uint32_t kbits_rate, uint32_t kbits_burst) { struct tc_police tc_police; struct ofpbuf request; @@ -3783,9 +4800,23 @@ tc_add_policer(struct netdev *netdev, int kbits_rate, int kbits_burst) memset(&tc_police, 0, sizeof tc_police); tc_police.action = TC_POLICE_SHOT; tc_police.mtu = mtu; - tc_fill_rate(&tc_police.rate, (kbits_rate * 1000)/8, mtu); - tc_police.burst = tc_bytes_to_ticks(tc_police.rate.rate, - kbits_burst * 1024); + tc_fill_rate(&tc_police.rate, ((uint64_t) kbits_rate * 1000)/8, mtu); + + /* The following appears wrong in two ways: + * + * - tc_bytes_to_ticks() should take "bytes" as quantity for both of its + * arguments (or at least consistently "bytes" as both or "bits" as + * both), but this supplies bytes for the first argument and bits for the + * second. + * + * - In networking a kilobit is usually 1000 bits but this uses 1024 bits. + * + * However if you "fix" those problems then "tc filter show ..." shows + * "125000b", meaning 125,000 bits, when OVS configures it for 1000 kbit == + * 1,000,000 bits, whereas this actually ends up doing the right thing from + * tc's point of view. Whatever. */ + tc_police.burst = tc_bytes_to_ticks( + tc_police.rate.rate, MIN(UINT32_MAX / 1024, kbits_burst) * 1024); tcmsg = tc_make_request(netdev, RTM_NEWTFILTER, NLM_F_EXCL | NLM_F_CREATE, &request); @@ -3851,30 +4882,35 @@ read_psched(void) * [5] 2.6.32.21.22 (approx.) from Ubuntu 10.04 on VMware Fusion * [6] 2.6.34 from kernel.org on KVM */ + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; static const char fn[] = "/proc/net/psched"; unsigned int a, b, c, d; FILE *stream; + if (!ovsthread_once_start(&once)) { + return; + } + ticks_per_s = 1.0; buffer_hz = 100; stream = fopen(fn, "r"); if (!stream) { - VLOG_WARN("%s: open failed: %s", fn, strerror(errno)); - return; + VLOG_WARN("%s: open failed: %s", fn, ovs_strerror(errno)); + goto exit; } if (fscanf(stream, "%x %x %x %x", &a, &b, &c, &d) != 4) { VLOG_WARN("%s: read failed", fn); fclose(stream); - return; + goto exit; } VLOG_DBG("%s: psched parameters are: %u %u %u %u", fn, a, b, c, d); fclose(stream); if (!a || !c) { VLOG_WARN("%s: invalid scheduler parameters", fn); - return; + goto exit; } ticks_per_s = (double) a * c / b; @@ -3885,6 +4921,9 @@ read_psched(void) fn, a, b, c, d); } VLOG_DBG("%s: ticks_per_s=%f buffer_hz=%u", fn, ticks_per_s, buffer_hz); + +exit: + ovsthread_once_done(&once); } /* Returns the number of bytes that can be transmitted in 'ticks' ticks at a @@ -3892,9 +4931,7 @@ read_psched(void) static unsigned int tc_ticks_to_bytes(unsigned int rate, unsigned int ticks) { - if (!buffer_hz) { - read_psched(); - } + read_psched(); return (rate * ticks) / ticks_per_s; } @@ -3903,9 +4940,7 @@ tc_ticks_to_bytes(unsigned int rate, unsigned int ticks) static unsigned int tc_bytes_to_ticks(unsigned int rate, unsigned int size) { - if (!buffer_hz) { - read_psched(); - } + read_psched(); return rate ? ((unsigned long long int) ticks_per_s * size) / rate : 0; } @@ -3914,9 +4949,7 @@ tc_bytes_to_ticks(unsigned int rate, unsigned int size) static unsigned int tc_buffer_per_jiffy(unsigned int rate) { - if (!buffer_hz) { - read_psched(); - } + read_psched(); return rate / buffer_hz; } @@ -4064,7 +5097,7 @@ tc_query_class(const struct netdev *netdev, netdev_get_name(netdev), tc_get_major(handle), tc_get_minor(handle), tc_get_major(parent), tc_get_minor(parent), - strerror(error)); + ovs_strerror(error)); } return error; } @@ -4089,22 +5122,21 @@ tc_delete_class(const struct netdev *netdev, unsigned int handle) VLOG_WARN_RL(&rl, "delete %s class %u:%u failed (%s)", netdev_get_name(netdev), tc_get_major(handle), tc_get_minor(handle), - strerror(error)); + ovs_strerror(error)); } return error; } /* Equivalent to "tc qdisc del dev root". */ static int -tc_del_qdisc(struct netdev *netdev) +tc_del_qdisc(struct netdev *netdev_) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct ofpbuf request; struct tcmsg *tcmsg; int error; - tcmsg = tc_make_request(netdev, RTM_DELQDISC, 0, &request); + tcmsg = tc_make_request(netdev_, RTM_DELQDISC, 0, &request); if (!tcmsg) { return ENODEV; } @@ -4117,30 +5149,54 @@ tc_del_qdisc(struct netdev *netdev) * case we've accomplished our purpose. */ error = 0; } - if (!error && netdev_dev->tc) { - if (netdev_dev->tc->ops->tc_destroy) { - netdev_dev->tc->ops->tc_destroy(netdev_dev->tc); + if (!error && netdev->tc) { + if (netdev->tc->ops->tc_destroy) { + netdev->tc->ops->tc_destroy(netdev->tc); } - netdev_dev->tc = NULL; + netdev->tc = NULL; } return error; } +static bool +getqdisc_is_safe(void) +{ + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; + static bool safe = false; + + if (ovsthread_once_start(&once)) { + struct utsname utsname; + int major, minor; + + if (uname(&utsname) == -1) { + VLOG_WARN("uname failed (%s)", ovs_strerror(errno)); + } else if (!ovs_scan(utsname.release, "%d.%d", &major, &minor)) { + VLOG_WARN("uname reported bad OS release (%s)", utsname.release); + } else if (major < 2 || (major == 2 && minor < 35)) { + VLOG_INFO("disabling unsafe RTM_GETQDISC in Linux kernel %s", + utsname.release); + } else { + safe = true; + } + ovsthread_once_done(&once); + } + return safe; +} + /* If 'netdev''s qdisc type and parameters are not yet known, queries the * kernel to determine what they are. Returns 0 if successful, otherwise a * positive errno value. */ static int -tc_query_qdisc(const struct netdev *netdev) +tc_query_qdisc(const struct netdev *netdev_) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct ofpbuf request, *qdisc; const struct tc_ops *ops; struct tcmsg *tcmsg; int load_error; int error; - if (netdev_dev->tc) { + if (netdev->tc) { return 0; } @@ -4156,18 +5212,21 @@ tc_query_qdisc(const struct netdev *netdev) * create will have a class with handle 1:0. The built-in qdiscs only have * a class with handle 0:0. * - * We could check for Linux 2.6.35+ and use a more straightforward method - * there. */ - tcmsg = tc_make_request(netdev, RTM_GETQDISC, NLM_F_ECHO, &request); + * On Linux 2.6.35+ we use the straightforward method because it allows us + * to handle non-builtin qdiscs without handle 1:0 (e.g. codel). However, + * in such a case we get no response at all from the kernel (!) if a + * builtin qdisc is in use (which is later caught by "!error && + * !qdisc->size"). */ + tcmsg = tc_make_request(netdev_, RTM_GETQDISC, NLM_F_ECHO, &request); if (!tcmsg) { return ENODEV; } - tcmsg->tcm_handle = tc_make_handle(1, 0); - tcmsg->tcm_parent = 0; + tcmsg->tcm_handle = tc_make_handle(getqdisc_is_safe() ? 0 : 1, 0); + tcmsg->tcm_parent = getqdisc_is_safe() ? TC_H_ROOT : 0; /* Figure out what tc class to instantiate. */ error = tc_transact(&request, &qdisc); - if (!error) { + if (!error && qdisc->size) { const char *kind; error = tc_parse_qdisc(qdisc, &kind, NULL); @@ -4177,27 +5236,27 @@ tc_query_qdisc(const struct netdev *netdev) ops = tc_lookup_linux_name(kind); if (!ops) { static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 1); - VLOG_INFO_RL(&rl2, "unknown qdisc \"%s\"", kind); + VLOG_DBG_RL(&rl2, "unknown qdisc \"%s\"", kind); ops = &tc_ops_other; } } - } else if (error == ENOENT) { - /* Either it's a built-in qdisc, or it's a qdisc set up by some - * other entity that doesn't have a handle 1:0. We will assume - * that it's the system default qdisc. */ + } else if ((!error && !qdisc->size) || error == ENOENT) { + /* Either it's a built-in qdisc, or (on Linux pre-2.6.35) it's a qdisc + * set up by some other entity that doesn't have a handle 1:0. We will + * assume that it's the system default qdisc. */ ops = &tc_ops_default; error = 0; } else { /* Who knows? Maybe the device got deleted. */ VLOG_WARN_RL(&rl, "query %s qdisc failed (%s)", - netdev_get_name(netdev), strerror(error)); + netdev_get_name(netdev_), ovs_strerror(error)); ops = &tc_ops_other; } /* Instantiate it. */ - load_error = ops->tc_load(CONST_CAST(struct netdev *, netdev), qdisc); - ovs_assert((load_error == 0) == (netdev_dev->tc != NULL)); + load_error = ops->tc_load(CONST_CAST(struct netdev *, netdev_), qdisc); + ovs_assert((load_error == 0) == (netdev->tc != NULL)); ofpbuf_delete(qdisc); return error ? error : load_error; @@ -4274,14 +5333,6 @@ tc_calc_buffer(unsigned int Bps, int mtu, uint64_t burst_bytes) /* Linux-only functions declared in netdev-linux.h */ -/* Returns a fd for an AF_INET socket or a negative errno value. */ -int -netdev_linux_get_af_inet_sock(void) -{ - int error = netdev_linux_init(); - return error ? -error : af_inet_sock; -} - /* Modifies the 'flag' bit in ethtool's flags field for 'netdev'. If * 'enable' is true, the bit is set. Otherwise, it is cleared. */ int @@ -4303,7 +5354,11 @@ netdev_linux_ethtool_set_flag(struct netdev *netdev, uint32_t flag, } COVERAGE_INC(netdev_set_ethtool); - evalue.data = new_flags = (evalue.data & ~flag) | (enable ? flag : 0); + new_flags = (evalue.data & ~flag) | (enable ? flag : 0); + if (new_flags == evalue.data) { + return 0; + } + evalue.data = new_flags; error = netdev_linux_do_ethtool(netdev_name, (struct ethtool_cmd *)&evalue, ETHTOOL_SFLAGS, "ETHTOOL_SFLAGS"); @@ -4360,122 +5415,86 @@ netdev_stats_from_rtnl_link_stats(struct netdev_stats *dst, dst->tx_window_errors = src->tx_window_errors; } -static int -get_stats_via_netlink(int ifindex, struct netdev_stats *stats) +/* Copies 'src' into 'dst', performing format conversion in the process. */ +static void +netdev_stats_from_rtnl_link_stats64(struct netdev_stats *dst, + const struct rtnl_link_stats64 *src) { - /* Policy for RTNLGRP_LINK messages. - * - * There are *many* more fields in these messages, but currently we only - * care about these fields. */ - static const struct nl_policy rtnlgrp_link_policy[] = { - [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false }, - [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true, - .min_len = sizeof(struct rtnl_link_stats) }, - }; + dst->rx_packets = src->rx_packets; + dst->tx_packets = src->tx_packets; + dst->rx_bytes = src->rx_bytes; + dst->tx_bytes = src->tx_bytes; + dst->rx_errors = src->rx_errors; + dst->tx_errors = src->tx_errors; + dst->rx_dropped = src->rx_dropped; + dst->tx_dropped = src->tx_dropped; + dst->multicast = src->multicast; + dst->collisions = src->collisions; + dst->rx_length_errors = src->rx_length_errors; + dst->rx_over_errors = src->rx_over_errors; + dst->rx_crc_errors = src->rx_crc_errors; + dst->rx_frame_errors = src->rx_frame_errors; + dst->rx_fifo_errors = src->rx_fifo_errors; + dst->rx_missed_errors = src->rx_missed_errors; + dst->tx_aborted_errors = src->tx_aborted_errors; + dst->tx_carrier_errors = src->tx_carrier_errors; + dst->tx_fifo_errors = src->tx_fifo_errors; + dst->tx_heartbeat_errors = src->tx_heartbeat_errors; + dst->tx_window_errors = src->tx_window_errors; +} +static int +get_stats_via_netlink(const struct netdev *netdev_, struct netdev_stats *stats) +{ struct ofpbuf request; struct ofpbuf *reply; - struct ifinfomsg *ifi; - struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)]; int error; ofpbuf_init(&request, 0); - nl_msg_put_nlmsghdr(&request, sizeof *ifi, RTM_GETLINK, NLM_F_REQUEST); - ifi = ofpbuf_put_zeros(&request, sizeof *ifi); - ifi->ifi_family = PF_UNSPEC; - ifi->ifi_index = ifindex; - error = nl_sock_transact(rtnl_sock, &request, &reply); + nl_msg_put_nlmsghdr(&request, + sizeof(struct ifinfomsg) + NL_ATTR_SIZE(IFNAMSIZ), + RTM_GETLINK, NLM_F_REQUEST); + ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg)); + nl_msg_put_string(&request, IFLA_IFNAME, netdev_get_name(netdev_)); + error = nl_transact(NETLINK_ROUTE, &request, &reply); ofpbuf_uninit(&request); if (error) { return error; } - if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg), - rtnlgrp_link_policy, - attrs, ARRAY_SIZE(rtnlgrp_link_policy))) { - ofpbuf_delete(reply); - return EPROTO; - } - - if (!attrs[IFLA_STATS]) { - VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats"); - ofpbuf_delete(reply); - return EPROTO; + if (ofpbuf_try_pull(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg))) { + const struct nlattr *a = nl_attr_find(reply, 0, IFLA_STATS64); + if (a && nl_attr_get_size(a) >= sizeof(struct rtnl_link_stats64)) { + netdev_stats_from_rtnl_link_stats64(stats, nl_attr_get(a)); + error = 0; + } else { + const struct nlattr *a = nl_attr_find(reply, 0, IFLA_STATS); + if (a && nl_attr_get_size(a) >= sizeof(struct rtnl_link_stats)) { + netdev_stats_from_rtnl_link_stats(stats, nl_attr_get(a)); + error = 0; + } else { + VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats"); + error = EPROTO; + } + } + } else { + VLOG_WARN_RL(&rl, "short RTM_GETLINK reply"); + error = EPROTO; } - netdev_stats_from_rtnl_link_stats(stats, nl_attr_get(attrs[IFLA_STATS])); ofpbuf_delete(reply); - - return 0; -} - -static int -get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats) -{ - static const char fn[] = "/proc/net/dev"; - char line[1024]; - FILE *stream; - int ln; - - stream = fopen(fn, "r"); - if (!stream) { - VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno)); - return errno; - } - - ln = 0; - while (fgets(line, sizeof line, stream)) { - if (++ln >= 3) { - char devname[16]; -#define X64 "%"SCNu64 - if (sscanf(line, - " %15[^:]:" - X64 X64 X64 X64 X64 X64 X64 "%*u" - X64 X64 X64 X64 X64 X64 X64 "%*u", - devname, - &stats->rx_bytes, - &stats->rx_packets, - &stats->rx_errors, - &stats->rx_dropped, - &stats->rx_fifo_errors, - &stats->rx_frame_errors, - &stats->multicast, - &stats->tx_bytes, - &stats->tx_packets, - &stats->tx_errors, - &stats->tx_dropped, - &stats->tx_fifo_errors, - &stats->collisions, - &stats->tx_carrier_errors) != 15) { - VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln); - } else if (!strcmp(devname, netdev_name)) { - stats->rx_length_errors = UINT64_MAX; - stats->rx_over_errors = UINT64_MAX; - stats->rx_crc_errors = UINT64_MAX; - stats->rx_missed_errors = UINT64_MAX; - stats->tx_aborted_errors = UINT64_MAX; - stats->tx_heartbeat_errors = UINT64_MAX; - stats->tx_window_errors = UINT64_MAX; - fclose(stream); - return 0; - } - } - } - VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name); - fclose(stream); - return ENODEV; + return error; } static int -get_flags(const struct netdev_dev *dev, unsigned int *flags) +get_flags(const struct netdev *dev, unsigned int *flags) { struct ifreq ifr; int error; *flags = 0; - error = netdev_linux_do_ioctl(dev->name, &ifr, SIOCGIFFLAGS, - "SIOCGIFFLAGS"); + error = af_inet_ifreq_ioctl(dev->name, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS"); if (!error) { *flags = ifr.ifr_flags; } @@ -4483,26 +5502,28 @@ get_flags(const struct netdev_dev *dev, unsigned int *flags) } static int -set_flags(struct netdev *netdev, unsigned int flags) +set_flags(const char *name, unsigned int flags) { struct ifreq ifr; ifr.ifr_flags = flags; - return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, SIOCSIFFLAGS, - "SIOCSIFFLAGS"); + return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS"); } static int do_get_ifindex(const char *netdev_name) { struct ifreq ifr; + int error; ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name); COVERAGE_INC(netdev_get_ifindex); - if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) { + + error = af_inet_ioctl(SIOCGIFINDEX, &ifr); + if (error) { VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s", - netdev_name, strerror(errno)); - return -errno; + netdev_name, ovs_strerror(error)); + return -error; } return ifr.ifr_ifindex; } @@ -4510,70 +5531,72 @@ do_get_ifindex(const char *netdev_name) static int get_ifindex(const struct netdev *netdev_, int *ifindexp) { - struct netdev_dev_linux *netdev_dev = - netdev_dev_linux_cast(netdev_get_dev(netdev_)); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); - if (!(netdev_dev->cache_valid & VALID_IFINDEX)) { + if (!(netdev->cache_valid & VALID_IFINDEX)) { int ifindex = do_get_ifindex(netdev_get_name(netdev_)); if (ifindex < 0) { - netdev_dev->get_ifindex_error = -ifindex; - netdev_dev->ifindex = 0; + netdev->get_ifindex_error = -ifindex; + netdev->ifindex = 0; } else { - netdev_dev->get_ifindex_error = 0; - netdev_dev->ifindex = ifindex; + netdev->get_ifindex_error = 0; + netdev->ifindex = ifindex; } - netdev_dev->cache_valid |= VALID_IFINDEX; + netdev->cache_valid |= VALID_IFINDEX; } - *ifindexp = netdev_dev->ifindex; - return netdev_dev->get_ifindex_error; + *ifindexp = netdev->ifindex; + return netdev->get_ifindex_error; } static int -get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]) +get_etheraddr(const char *netdev_name, struct eth_addr *ea) { struct ifreq ifr; int hwaddr_family; + int error; memset(&ifr, 0, sizeof ifr); ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name); COVERAGE_INC(netdev_get_hwaddr); - if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) { + error = af_inet_ioctl(SIOCGIFHWADDR, &ifr); + if (error) { /* ENODEV probably means that a vif disappeared asynchronously and * hasn't been removed from the database yet, so reduce the log level * to INFO for that case. */ - VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR, + VLOG(error == ENODEV ? VLL_INFO : VLL_ERR, "ioctl(SIOCGIFHWADDR) on %s device failed: %s", - netdev_name, strerror(errno)); - return errno; + netdev_name, ovs_strerror(error)); + return error; } hwaddr_family = ifr.ifr_hwaddr.sa_family; if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) { - VLOG_WARN("%s device has unknown hardware address family %d", + VLOG_INFO("%s device has unknown hardware address family %d", netdev_name, hwaddr_family); + return EINVAL; } memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN); return 0; } static int -set_etheraddr(const char *netdev_name, - const uint8_t mac[ETH_ADDR_LEN]) +set_etheraddr(const char *netdev_name, const struct eth_addr mac) { struct ifreq ifr; + int error; memset(&ifr, 0, sizeof ifr); ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name); ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; - memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN); + memcpy(ifr.ifr_hwaddr.sa_data, &mac, ETH_ADDR_LEN); COVERAGE_INC(netdev_set_hwaddr); - if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) { + error = af_inet_ioctl(SIOCSIFHWADDR, &ifr); + if (error) { VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s", - netdev_name, strerror(errno)); - return errno; + netdev_name, ovs_strerror(error)); } - return 0; + return error; } static int @@ -4581,37 +5604,24 @@ netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *ecmd, int cmd, const char *cmd_name) { struct ifreq ifr; + int error; memset(&ifr, 0, sizeof ifr); ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name); ifr.ifr_data = (caddr_t) ecmd; ecmd->cmd = cmd; - if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) { - return 0; - } else { - if (errno != EOPNOTSUPP) { + error = af_inet_ioctl(SIOCETHTOOL, &ifr); + if (error) { + if (error != EOPNOTSUPP) { VLOG_WARN_RL(&rl, "ethtool command %s on network device %s " - "failed: %s", cmd_name, name, strerror(errno)); + "failed: %s", cmd_name, name, ovs_strerror(error)); } else { /* The device doesn't support this operation. That's pretty * common, so there's no point in logging anything. */ } - return errno; } -} - -static int -netdev_linux_do_ioctl(const char *name, struct ifreq *ifr, int cmd, - const char *cmd_name) -{ - ovs_strzcpy(ifr->ifr_name, name, sizeof ifr->ifr_name); - if (ioctl(af_inet_sock, cmd, ifr) == -1) { - VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name, - strerror(errno)); - return errno; - } - return 0; + return error; } static int @@ -4622,9 +5632,10 @@ netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip, int error; ifr.ifr_addr.sa_family = AF_INET; - error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name); + error = af_inet_ifreq_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name); if (!error) { - const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr; + const struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *, + &ifr.ifr_addr); *ip = sin->sin_addr; } return error; @@ -4634,9 +5645,10 @@ netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip, static int af_packet_sock(void) { - static int sock = INT_MIN; + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; + static int sock; - if (sock == INT_MIN) { + if (ovsthread_once_start(&once)) { sock = socket(AF_PACKET, SOCK_RAW, 0); if (sock >= 0) { int error = set_nonblocking(sock); @@ -4646,8 +5658,10 @@ af_packet_sock(void) } } else { sock = -errno; - VLOG_ERR("failed to create packet socket: %s", strerror(errno)); + VLOG_ERR("failed to create packet socket: %s", + ovs_strerror(errno)); } + ovsthread_once_done(&once); } return sock;