mirror of
https://github.com/openvswitch/ovs
synced 2025-09-01 14:55:18 +00:00
netdev: Get rid of netdev_open_tap().
netdev_open() can always be used in place of netdev_open_tap(). The former is going to be generalized to support pluggable network device types, so it makes sense to use it everywhere.
This commit is contained in:
@@ -377,7 +377,9 @@ do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
|
|||||||
if (!internal) {
|
if (!internal) {
|
||||||
error = netdev_open(devname, NETDEV_ETH_TYPE_ANY, &netdev);
|
error = netdev_open(devname, NETDEV_ETH_TYPE_ANY, &netdev);
|
||||||
} else {
|
} else {
|
||||||
error = netdev_open_tap(devname, &netdev);
|
char *tapname = xasprintf("tap:%s", devname);
|
||||||
|
error = netdev_open(tapname, NETDEV_ETH_TYPE_ANY, &netdev);
|
||||||
|
free(tapname);
|
||||||
}
|
}
|
||||||
if (error) {
|
if (error) {
|
||||||
return error;
|
return error;
|
||||||
|
86
lib/netdev.c
86
lib/netdev.c
@@ -335,58 +335,48 @@ do_get_features(struct netdev *netdev,
|
|||||||
int
|
int
|
||||||
netdev_open(const char *name, int ethertype, struct netdev **netdevp)
|
netdev_open(const char *name, int ethertype, struct netdev **netdevp)
|
||||||
{
|
{
|
||||||
if (!strncmp(name, "tap:", 4)) {
|
if (strncmp(name, "tap:", 4)) {
|
||||||
return netdev_open_tap(name + 4, netdevp);
|
|
||||||
} else {
|
|
||||||
return do_open_netdev(name, ethertype, -1, netdevp);
|
return do_open_netdev(name, ethertype, -1, netdevp);
|
||||||
|
} else {
|
||||||
|
static const char tap_dev[] = "/dev/net/tun";
|
||||||
|
struct ifreq ifr;
|
||||||
|
int error;
|
||||||
|
int tap_fd;
|
||||||
|
|
||||||
|
tap_fd = open(tap_dev, O_RDWR);
|
||||||
|
if (tap_fd < 0) {
|
||||||
|
ovs_error(errno, "opening \"%s\" failed", tap_dev);
|
||||||
|
return errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&ifr, 0, sizeof ifr);
|
||||||
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
|
||||||
|
if (name) {
|
||||||
|
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
|
||||||
|
}
|
||||||
|
if (ioctl(tap_fd, TUNSETIFF, &ifr) < 0) {
|
||||||
|
int error = errno;
|
||||||
|
ovs_error(error, "ioctl(TUNSETIFF) on \"%s\" failed", tap_dev);
|
||||||
|
close(tap_fd);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
error = set_nonblocking(tap_fd);
|
||||||
|
if (error) {
|
||||||
|
ovs_error(error, "set_nonblocking on \"%s\" failed", tap_dev);
|
||||||
|
close(tap_fd);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
error = do_open_netdev(ifr.ifr_name, NETDEV_ETH_TYPE_NONE, tap_fd,
|
||||||
|
netdevp);
|
||||||
|
if (error) {
|
||||||
|
close(tap_fd);
|
||||||
|
}
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Opens a TAP virtual network device. If 'name' is a nonnull, non-empty
|
|
||||||
* string, attempts to assign that name to the TAP device (failing if the name
|
|
||||||
* is already in use); otherwise, a name is automatically assigned. Returns
|
|
||||||
* zero if successful, otherwise a positive errno value. On success, sets
|
|
||||||
* '*netdevp' to the new network device, otherwise to null. */
|
|
||||||
int
|
|
||||||
netdev_open_tap(const char *name, struct netdev **netdevp)
|
|
||||||
{
|
|
||||||
static const char tap_dev[] = "/dev/net/tun";
|
|
||||||
struct ifreq ifr;
|
|
||||||
int error;
|
|
||||||
int tap_fd;
|
|
||||||
|
|
||||||
tap_fd = open(tap_dev, O_RDWR);
|
|
||||||
if (tap_fd < 0) {
|
|
||||||
ovs_error(errno, "opening \"%s\" failed", tap_dev);
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&ifr, 0, sizeof ifr);
|
|
||||||
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
|
|
||||||
if (name) {
|
|
||||||
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
|
|
||||||
}
|
|
||||||
if (ioctl(tap_fd, TUNSETIFF, &ifr) < 0) {
|
|
||||||
int error = errno;
|
|
||||||
ovs_error(error, "ioctl(TUNSETIFF) on \"%s\" failed", tap_dev);
|
|
||||||
close(tap_fd);
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
error = set_nonblocking(tap_fd);
|
|
||||||
if (error) {
|
|
||||||
ovs_error(error, "set_nonblocking on \"%s\" failed", tap_dev);
|
|
||||||
close(tap_fd);
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
error = do_open_netdev(ifr.ifr_name, NETDEV_ETH_TYPE_NONE, tap_fd,
|
|
||||||
netdevp);
|
|
||||||
if (error) {
|
|
||||||
close(tap_fd);
|
|
||||||
}
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
do_open_netdev(const char *name, int ethertype, int tap_fd,
|
do_open_netdev(const char *name, int ethertype, int tap_fd,
|
||||||
|
@@ -75,7 +75,6 @@ struct netdev_stats {
|
|||||||
struct netdev;
|
struct netdev;
|
||||||
|
|
||||||
int netdev_open(const char *name, int ethertype, struct netdev **);
|
int netdev_open(const char *name, int ethertype, struct netdev **);
|
||||||
int netdev_open_tap(const char *name, struct netdev **);
|
|
||||||
void netdev_close(struct netdev *);
|
void netdev_close(struct netdev *);
|
||||||
|
|
||||||
int netdev_recv(struct netdev *, struct ofpbuf *);
|
int netdev_recv(struct netdev *, struct ofpbuf *);
|
||||||
|
Reference in New Issue
Block a user