/* Open and validate network device. */
memset(&netdev_options, 0, sizeof netdev_options);
netdev_options.name = devname;
- netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
if (dp->class == &dpif_dummy_class) {
netdev_options.type = "dummy";
} else if (internal) {
/* XXX reject loopback devices */
/* XXX reject non-Ethernet devices */
+ error = netdev_listen(netdev);
+ if (error) {
+ VLOG_ERR("%s: cannot receive packets on this network device (%s)",
+ devname, strerror(errno));
+ netdev_close(netdev);
+ return error;
+ }
+
error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
if (error) {
netdev_close(netdev);
/*
- * Copyright (c) 2010 Nicira Networks.
+ * Copyright (c) 2010, 2011 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
static int
-netdev_dummy_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
- struct netdev **netdevp)
+netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
{
struct netdev_dummy *netdev;
free(netdev);
}
+static int
+netdev_dummy_listen(struct netdev *netdev_ OVS_UNUSED)
+{
+ /* It's OK to listen on a dummy device. It just never receives any
+ * packets. */
+ return 0;
+}
+
+static int
+netdev_dummy_recv(struct netdev *netdev_ OVS_UNUSED,
+ void *buffer OVS_UNUSED, size_t size OVS_UNUSED)
+{
+ /* A dummy device never receives any packets. */
+ return -EAGAIN;
+}
+
static int
netdev_dummy_set_etheraddr(struct netdev *netdev,
const uint8_t mac[ETH_ADDR_LEN])
NULL, /* enumerate */
- NULL, /* recv */
+ netdev_dummy_listen, /* listen */
+ netdev_dummy_recv, /* recv */
NULL, /* recv_wait */
NULL, /* drain */
}
static int
-netdev_linux_open(struct netdev_dev *netdev_dev_, int ethertype,
- struct netdev **netdevp)
+netdev_linux_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
{
struct netdev_dev_linux *netdev_dev = netdev_dev_linux_cast(netdev_dev_);
struct netdev_linux *netdev;
* directions appearing to be reversed. */
netdev->fd = netdev_dev->state.tap.fd;
netdev_dev->state.tap.opened = true;
- } else if (ethertype != NETDEV_ETH_TYPE_NONE) {
- struct sockaddr_ll sll;
- int protocol;
- int ifindex;
-
- /* Create file descriptor. */
- protocol = (ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
- : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
- : ethertype);
- netdev->fd = socket(PF_PACKET, SOCK_RAW,
- (OVS_FORCE int) htons(protocol));
- if (netdev->fd < 0) {
- error = errno;
- goto error;
- }
-
- /* Set non-blocking mode. */
- error = set_nonblocking(netdev->fd);
- if (error) {
- goto error;
- }
-
- /* Get ethernet device index. */
- error = get_ifindex(&netdev->netdev, &ifindex);
- if (error) {
- goto error;
- }
-
- /* Bind to specific ethernet device. */
- memset(&sll, 0, sizeof sll);
- sll.sll_family = AF_PACKET;
- sll.sll_ifindex = ifindex;
- if (bind(netdev->fd,
- (struct sockaddr *) &sll, sizeof sll) < 0) {
- error = errno;
- VLOG_ERR("bind to %s failed: %s", netdev_dev_get_name(netdev_dev_),
- strerror(error));
- goto error;
- }
-
- /* Between the socket() and bind() calls above, the socket receives all
- * packets of the requested type on all system interfaces. We do not
- * want to receive that data, but there is no way to avoid it. So we
- * must now drain out the receive queue. */
- error = drain_rcvbuf(netdev->fd);
- if (error) {
- goto error;
- }
}
*netdevp = &netdev->netdev;
}
}
+static int
+netdev_linux_listen(struct netdev *netdev_)
+{
+ struct netdev_linux *netdev = netdev_linux_cast(netdev_);
+ struct sockaddr_ll sll;
+ int ifindex;
+ int error;
+ int fd;
+
+ if (netdev->fd >= 0) {
+ return 0;
+ }
+
+ /* 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;
+ }
+
+ /* Set non-blocking mode. */
+ error = set_nonblocking(fd);
+ if (error) {
+ goto error;
+ }
+
+ /* Get ethernet device index. */
+ error = get_ifindex(&netdev->netdev, &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 = (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;
+ }
+
+ netdev->fd = fd;
+ return 0;
+
+error:
+ if (fd >= 0) {
+ close(fd);
+ }
+ return error;
+}
+
static int
netdev_linux_recv(struct netdev *netdev_, void *data, size_t size)
{
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
if (netdev->fd < 0) {
- /* Device was opened with NETDEV_ETH_TYPE_NONE. */
+ /* Device is not listening. */
return -EAGAIN;
}
\
ENUMERATE, \
\
+ netdev_linux_listen, \
netdev_linux_recv, \
netdev_linux_recv_wait, \
netdev_linux_drain, \
const struct shash *args);
/* Attempts to open a network device. On success, sets 'netdevp'
- * to the new network device.
- *
- * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order
- * to capture frames of that type received on the device. It may also be
- * one of the 'enum netdev_pseudo_ethertype' values to receive frames in
- * one of those categories. */
- int (*open)(struct netdev_dev *netdev_dev, int ethertype,
- struct netdev **netdevp);
+ * to the new network device. */
+ int (*open)(struct netdev_dev *netdev_dev, struct netdev **netdevp);
/* Closes 'netdev'. */
void (*close)(struct netdev *netdev);
* If this netdev class does not support enumeration, this may be a null
* pointer. */
int (*enumerate)(struct sset *all_names);
+\f
+/* ## ----------------- ## */
+/* ## Receiving Packets ## */
+/* ## ----------------- ## */
+
+/* The network provider interface is mostly used for inspecting and configuring
+ * device "metadata", not for sending and receiving packets directly. It may
+ * be impractical to implement these functions on some operating systems and
+ * hardware. These functions may all be NULL in such cases.
+ *
+ * (However, the "dpif-netdev" implementation, which is the easiest way to
+ * integrate Open vSwitch with a new operating system or hardware, does require
+ * the ability to receive packets.) */
+
+ /* Attempts to set up 'netdev' for receiving packets with ->recv().
+ * Returns 0 if successful, otherwise a positive errno value. Return
+ * EOPNOTSUPP to indicate that the network device does not implement packet
+ * reception through this interface. This function may be set to null if
+ * it would always return EOPNOTSUPP anyhow. (This will prevent the
+ * network device from being usefully used by the netdev-based "userspace
+ * datapath".)*/
+ int (*listen)(struct netdev *netdev);
/* Attempts to receive a packet from 'netdev' into the 'size' bytes in
* 'buffer'. If successful, returns the number of bytes in the received
* packet, otherwise a negative errno value. Returns -EAGAIN immediately
* if no packet is ready to be received.
*
- * May return -EOPNOTSUPP if a network device does not implement packet
- * reception through this interface. This function may be set to null if
- * it would always return -EOPNOTSUPP anyhow. (This will prevent the
- * network device from being usefully used by the netdev-based "userspace
- * datapath".) */
+ * This function can only be expected to return a packet if ->listen() has
+ * been called successfully.
+ *
+ * May be null if not needed, such as for a network device that does not
+ * implement packet reception through the 'recv' member function. */
int (*recv)(struct netdev *netdev, void *buffer, size_t size);
/* Registers with the poll loop to wake up from the next call to
* May be null if not needed, such as for a network device that does not
* implement packet reception through the 'recv' member function. */
int (*drain)(struct netdev *netdev);
-
+\f
/* Sends the 'size'-byte packet in 'buffer' on 'netdev'. Returns 0 if
* successful, otherwise a positive errno value. Returns EAGAIN without
* blocking if the packet cannot be queued immediately. Returns EMSGSIZE
}
static int
-netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
- struct netdev **netdevp)
+netdev_vport_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
{
struct netdev_vport *netdev;
\
NULL, /* enumerate */ \
\
+ NULL, /* listen */ \
NULL, /* recv */ \
NULL, /* recv_wait */ \
NULL, /* drain */ \
* to the new network device, otherwise to null.
*
* If this is the first time the device has been opened, then create is called
- * before opening. The device is created using the given type and arguments.
- *
- * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
- * capture frames of that type received on the device. It may also be one of
- * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
- * categories. */
+ * before opening. The device is created using the given type and
+ * arguments. */
int
netdev_open(struct netdev_options *options, struct netdev **netdevp)
{
return EINVAL;
}
- error = netdev_dev->netdev_class->open(netdev_dev, options->ethertype,
- netdevp);
+ error = netdev_dev->netdev_class->open(netdev_dev, netdevp);
if (!error) {
netdev_dev->ref_cnt++;
memset(&options, 0, sizeof options);
options.name = name;
- options.ethertype = NETDEV_ETH_TYPE_NONE;
return netdev_open(&options, netdevp);
}
return error;
}
+/* Attempts to set up 'netdev' for receiving packets with netdev_recv().
+ * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
+ * indicates that the network device does not implement packet reception
+ * through this interface. */
+int
+netdev_listen(struct netdev *netdev)
+{
+ int (*listen)(struct netdev *);
+
+ listen = netdev_get_dev(netdev)->netdev_class->listen;
+ return listen ? (listen)(netdev) : EOPNOTSUPP;
+}
+
/* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
* must have initialized with sufficient room for the packet. The space
* required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
* (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
* need not be included.)
*
+ * This function can only be expected to return a packet if ->listen() has
+ * been called successfully.
+ *
* If a packet is successfully retrieved, returns 0. In this case 'buffer' is
* guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
* positive errno value. Returns EAGAIN immediately if no packet is ready to
NETDEV_LOOPBACK = 0x0004 /* This is a loopback device. */
};
-enum netdev_pseudo_ethertype {
- NETDEV_ETH_TYPE_NONE = -128, /* Receive no frames. */
- NETDEV_ETH_TYPE_ANY, /* Receive all frames. */
- NETDEV_ETH_TYPE_802_2 /* Receive all IEEE 802.2 frames. */
-};
-
/* Network device statistics.
*
* Values of unsupported statistics are set to all-1-bits (UINT64_MAX). */
const char *name;
const char *type;
const struct shash *args;
- int ethertype;
};
struct netdev;
int netdev_get_ifindex(const struct netdev *);
/* Packet send and receive. */
+int netdev_listen(struct netdev *);
int netdev_recv(struct netdev *, struct ofpbuf *);
void netdev_recv_wait(struct netdev *);
int netdev_drain(struct netdev *);
memset(&netdev_options, 0, sizeof netdev_options);
netdev_options.name = ofproto_port->name;
netdev_options.type = ofproto_port->type;
- netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
error = netdev_open(&netdev_options, &netdev);
if (error) {
options.name = strtok_r(argv[i], ",", &save_ptr);
options.type = "system";
options.args = &args;
- options.ethertype = NETDEV_ETH_TYPE_NONE;
if (!options.name) {
ovs_error(0, "%s is not a valid network device name", argv[i]);
netdev_options.name = dpif_port.name;
netdev_options.type = dpif_port.type;
netdev_options.args = NULL;
- netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
error = netdev_open(&netdev_options, &netdev);
if (!error) {
const struct shash_node **nodes;
options.name = iface->name;
options.type = iface->type;
options.args = &args;
- options.ethertype = NETDEV_ETH_TYPE_NONE;
error = netdev_open(&options, &iface->netdev);
} else {
error = netdev_set_config(iface->netdev, &args);
options.name = port->name;
options.type = "internal";
options.args = NULL;
- options.ethertype = NETDEV_ETH_TYPE_NONE;
error = netdev_open(&options, &netdev);
if (!error) {
ofproto_port_add(br->ofproto, netdev, NULL);