/*
- * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "netdev-linux.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include "hmap.h"
#include "netdev-provider.h"
#include "netdev-vport.h"
-#include "netlink.h"
#include "netlink-notifier.h"
#include "netlink-socket.h"
+#include "netlink.h"
#include "ofpbuf.h"
#include "openflow/openflow.h"
#include "packets.h"
#include "poll-loop.h"
#include "rtnetlink-link.h"
-#include "socket-util.h"
#include "shash.h"
+#include "socket-util.h"
#include "sset.h"
#include "timer.h"
+#include "unaligned.h"
#include "vlog.h"
VLOG_DEFINE_THIS_MODULE(netdev_linux);
* 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
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_default, /* Default qdisc (see tc-pfifo_fast(8)). */
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 set_flags(const char *, unsigned int flags);
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,
netdev_dev_linux_cast(const struct netdev_dev *netdev_dev)
{
const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev);
- assert(is_netdev_linux_class(netdev_class));
+ ovs_assert(is_netdev_linux_class(netdev_class));
return CONTAINER_OF(netdev_dev, struct netdev_dev_linux, netdev_dev);
}
{
struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev);
- assert(is_netdev_linux_class(netdev_class));
+ ovs_assert(is_netdev_linux_class(netdev_class));
return CONTAINER_OF(netdev, struct netdev_linux, netdev);
}
netdev_linux_miimon_wait();
}
-static int
-netdev_linux_get_drvinfo(struct netdev_dev_linux *netdev_dev)
-{
-
- int error;
-
- if (netdev_dev->cache_valid & VALID_DRVINFO) {
- return 0;
- }
-
- COVERAGE_INC(netdev_get_ethtool);
- memset(&netdev_dev->drvinfo, 0, sizeof netdev_dev->drvinfo);
- error = netdev_linux_do_ethtool(netdev_dev->netdev_dev.name,
- (struct ethtool_cmd *)&netdev_dev->drvinfo,
- ETHTOOL_GDRVINFO,
- "ETHTOOL_GDRVINFO");
- if (!error) {
- netdev_dev->cache_valid |= VALID_DRVINFO;
- }
- return error;
-}
-
static void
netdev_dev_linux_changed(struct netdev_dev_linux *dev,
unsigned int ifi_flags,
cache_notifier_ref(void)
{
if (!cache_notifier_refcount) {
- assert(!netdev_linux_cache_notifier);
+ ovs_assert(!netdev_linux_cache_notifier);
netdev_linux_cache_notifier =
rtnetlink_link_notifier_create(netdev_linux_cache_cb, NULL);
static void
cache_notifier_unref(void)
{
- assert(cache_notifier_refcount > 0);
+ ovs_assert(cache_notifier_refcount > 0);
if (!--cache_notifier_refcount) {
- assert(netdev_linux_cache_notifier);
+ ovs_assert(netdev_linux_cache_notifier);
rtnetlink_link_notifier_destroy(netdev_linux_cache_notifier);
netdev_linux_cache_notifier = NULL;
}
sock = af_packet_sock();
if (sock < 0) {
- return sock;
+ return -sock;
}
error = get_ifindex(netdev_, &ifindex);
{
struct netdev_dev_linux *netdev_dev =
netdev_dev_linux_cast(netdev_get_dev(netdev_));
+ struct netdev_saved_flags *sf = NULL;
int error;
- bool up_again = false;
if (netdev_dev->cache_valid & VALID_ETHERADDR) {
if (netdev_dev->ether_addr_error) {
enum netdev_flags flags;
if (!netdev_get_flags(netdev_, &flags) && (flags & NETDEV_UP)) {
- netdev_turn_flags_off(netdev_, NETDEV_UP, false);
- up_again = true;
+ netdev_turn_flags_off(netdev_, NETDEV_UP, &sf);
}
}
error = set_etheraddr(netdev_get_name(netdev_), mac);
}
}
- if (up_again) {
- netdev_turn_flags_on(netdev_, NETDEV_UP, false);
- }
+ netdev_restore_flags(sf);
return error;
}
*b = tmp;
}
+/* Copies 'src' into 'dst', performing format conversion in the process.
+ *
+ * 'src' is allowed to be misaligned. */
+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->multicast = 0;
+ dst->collisions = 0;
+ dst->rx_length_errors = 0;
+ dst->rx_over_errors = 0;
+ dst->rx_crc_errors = 0;
+ dst->rx_frame_errors = 0;
+ dst->rx_fifo_errors = 0;
+ dst->rx_missed_errors = 0;
+ dst->tx_aborted_errors = 0;
+ dst->tx_carrier_errors = 0;
+ dst->tx_fifo_errors = 0;
+ dst->tx_heartbeat_errors = 0;
+ dst->tx_window_errors = 0;
+}
+
+static int
+get_stats_via_vport__(const struct netdev *netdev, struct netdev_stats *stats)
+{
+ struct dpif_linux_vport reply;
+ struct ofpbuf *buf;
+ int error;
+
+ error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
+ if (error) {
+ return error;
+ } else if (!reply.stats) {
+ ofpbuf_delete(buf);
+ return EOPNOTSUPP;
+ }
+
+ netdev_stats_from_ovs_vport_stats(stats, reply.stats);
+
+ ofpbuf_delete(buf);
+
+ return 0;
+}
+
static void
get_stats_via_vport(const struct netdev *netdev_,
struct netdev_stats *stats)
!(netdev_dev->cache_valid & VALID_VPORT_STAT_ERROR)) {
int error;
- error = netdev_vport_get_stats(netdev_, stats);
+ error = get_stats_via_vport__(netdev_, stats);
if (error && error != ENOENT) {
VLOG_WARN_RL(&rl, "%s: obtaining netdev stats via vport failed "
"(%s)", netdev_get_name(netdev_), strerror(error));
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);
+
+ /* 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;
+}
+
static void
netdev_linux_read_features(struct netdev_dev_linux *netdev_dev)
{
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;
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;
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;
if (error) {
return error;
}
- assert(netdev_dev->tc == NULL);
+ ovs_assert(netdev_dev->tc == NULL);
/* Install new qdisc. */
error = new_ops->tc_install(netdev, details);
- assert((error == 0) == (netdev_dev->tc != NULL));
+ ovs_assert((error == 0) == (netdev_dev->tc != NULL));
return error;
}
}
static int
-netdev_linux_get_drv_info(const struct netdev *netdev, struct smap *smap)
+netdev_linux_get_status(const struct netdev *netdev, struct smap *smap)
{
- int error;
- struct netdev_dev_linux *netdev_dev =
- netdev_dev_linux_cast(netdev_get_dev(netdev));
+ struct netdev_dev_linux *netdev_dev;
+ 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;
+
+ COVERAGE_INC(netdev_get_ethtool);
+ memset(&netdev_dev->drvinfo, 0, sizeof netdev_dev->drvinfo);
+ error = netdev_linux_do_ethtool(netdev_dev->netdev_dev.name,
+ cmd,
+ ETHTOOL_GDRVINFO,
+ "ETHTOOL_GDRVINFO");
+ if (!error) {
+ netdev_dev->cache_valid |= VALID_DRVINFO;
+ }
+ }
- error = netdev_linux_get_drvinfo(netdev_dev);
if (!error) {
smap_add(smap, "driver_name", netdev_dev->drvinfo.driver);
smap_add(smap, "driver_version", netdev_dev->drvinfo.version);
}
static int
-netdev_internal_get_drv_info(const struct netdev *netdev OVS_UNUSED,
- struct smap *smap)
+netdev_internal_get_status(const struct netdev *netdev OVS_UNUSED,
+ struct smap *smap)
{
smap_add(smap, "driver_name", "openvswitch");
return 0;
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), strerror(retval));
}
return retval;
}
}
static int
-netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off,
+netdev_linux_update_flags(struct netdev_dev *dev_, enum netdev_flags off,
enum netdev_flags on, enum netdev_flags *old_flagsp)
{
struct netdev_dev_linux *netdev_dev;
int old_flags, new_flags;
int error = 0;
- netdev_dev = netdev_dev_linux_cast(netdev_get_dev(netdev));
+ netdev_dev = netdev_dev_linux_cast(dev_);
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);
+ error = set_flags(netdev_dev_get_name(dev_), new_flags);
get_flags(&netdev_dev->netdev_dev, &netdev_dev->ifi_flags);
}
return error;
netdev_linux_destroy, \
NULL, /* get_config */ \
NULL, /* set_config */ \
+ NULL, /* get_tunnel_config */ \
\
netdev_linux_open, \
netdev_linux_close, \
netdev_linux_get_stats,
NULL, /* set_stats */
netdev_linux_get_features,
- netdev_linux_get_drv_info);
+ netdev_linux_get_status);
const struct netdev_class netdev_tap_class =
NETDEV_LINUX_CLASS(
netdev_tap_get_stats,
NULL, /* set_stats */
netdev_linux_get_features,
- netdev_linux_get_drv_info);
+ netdev_linux_get_status);
const struct netdev_class netdev_internal_class =
NETDEV_LINUX_CLASS(
"internal",
netdev_linux_create,
netdev_internal_get_stats,
- netdev_vport_set_stats,
+ netdev_internal_set_stats,
NULL, /* get_features */
- netdev_internal_get_drv_info);
+ netdev_internal_get_status);
\f
/* HTB traffic control class. */
{
struct netdev_dev_linux *netdev_dev =
netdev_dev_linux_cast(netdev_get_dev(netdev));
- static struct tc *tc;
+ 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_dev->tc = CONST_CAST(struct tc *, &tc);
}
static int
{
struct netdev_dev_linux *netdev_dev =
netdev_dev_linux_cast(netdev_get_dev(netdev));
- static struct tc *tc;
+ 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_dev->tc = CONST_CAST(struct tc *, &tc);
return 0;
}
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/8 * 1000, 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);
/* Instantiate it. */
load_error = ops->tc_load(CONST_CAST(struct netdev *, netdev), qdisc);
- assert((load_error == 0) == (netdev_dev->tc != NULL));
+ ovs_assert((load_error == 0) == (netdev_dev->tc != NULL));
ofpbuf_delete(qdisc);
return error ? error : load_error;
}
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 netdev_linux_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
}
static int
if (sock == INT_MIN) {
sock = socket(AF_PACKET, SOCK_RAW, 0);
if (sock >= 0) {
- set_nonblocking(sock);
+ int error = set_nonblocking(sock);
+ if (error) {
+ close(sock);
+ sock = -error;
+ }
} else {
sock = -errno;
VLOG_ERR("failed to create packet socket: %s", strerror(errno));