2
0
mirror of https://github.com/openvswitch/ovs synced 2025-09-01 14:55:18 +00:00

netdev: Extract netdev vport functions.

All devices implemented as vports have a common interface, so
pull out ioctl code from the GRE netdev so it can be used in
other places as well.
This commit is contained in:
Jesse Gross
2010-05-17 15:04:10 -07:00
parent 4fe6488610
commit 777ece0952
5 changed files with 306 additions and 220 deletions

View File

@@ -66,6 +66,8 @@ lib_libopenvswitch_a_SOURCES = \
lib/netdev-gre.c \
lib/netdev-linux.c \
lib/netdev-provider.h \
lib/netdev-vport.c \
lib/netdev-vport.h \
lib/netdev.c \
lib/netdev.h \
lib/odp-util.c \

View File

@@ -20,13 +20,12 @@
#include <net/if.h>
#include <sys/ioctl.h>
#include "list.h"
#include "netdev-provider.h"
#include "netdev-vport.h"
#include "openflow/openflow.h"
#include "openvswitch/datapath-protocol.h"
#include "openvswitch/gre.h"
#include "packets.h"
#include "shash.h"
#include "socket-util.h"
#define THIS_MODULE VLM_netdev_gre
@@ -40,17 +39,6 @@ struct netdev_gre {
struct netdev netdev;
};
struct netdev_gre_notifier {
struct netdev_notifier notifier;
struct list node;
};
static int ioctl_fd = -1;
static struct shash netdev_gre_notifiers =
SHASH_INITIALIZER(&netdev_gre_notifiers);
static void poll_notify(const struct netdev_gre *netdev);
static struct netdev_dev_gre *
netdev_dev_gre_cast(const struct netdev_dev *netdev_dev)
{
@@ -65,26 +53,6 @@ netdev_gre_cast(const struct netdev *netdev)
return CONTAINER_OF(netdev, struct netdev_gre, netdev);
}
static int
netdev_gre_init(void)
{
static int status = -1;
if (status < 0) {
ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
status = ioctl_fd >= 0 ? 0 : errno;
if (status) {
VLOG_ERR("failed to open ioctl fd: %s", strerror(status));
}
}
return status;
}
static int
do_ioctl(int cmd, void *arg)
{
return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
}
static int
parse_config(const char *name, const struct shash *args,
struct gre_port_config *config)
@@ -183,16 +151,16 @@ netdev_gre_create(const char *name, const char *type OVS_UNUSED,
return err;
}
err = do_ioctl(ODP_VPORT_ADD, &ova);
err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
if (err == EEXIST) {
VLOG_WARN("%s: destroying existing device", name);
err = do_ioctl(ODP_VPORT_DEL, ova.devname);
err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
if (err) {
return err;
}
err = do_ioctl(ODP_VPORT_ADD, &ova);
err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
}
if (err) {
@@ -222,7 +190,7 @@ netdev_gre_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
return err;
}
return do_ioctl(ODP_VPORT_MOD, &ovm);
return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
}
static void
@@ -230,7 +198,7 @@ netdev_gre_destroy(struct netdev_dev *netdev_dev_)
{
struct netdev_dev_gre *netdev_dev = netdev_dev_gre_cast(netdev_dev_);
do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
free(netdev_dev);
}
@@ -254,183 +222,10 @@ netdev_gre_close(struct netdev *netdev_)
free(netdev);
}
static int
netdev_gre_set_etheraddr(struct netdev *netdev_,
const uint8_t mac[ETH_ADDR_LEN])
{
struct netdev_gre *netdev = netdev_gre_cast(netdev_);
struct odp_vport_ether vport_ether;
int err;
ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev_),
sizeof vport_ether.devname);
memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
err = ioctl(ioctl_fd, ODP_VPORT_ETHER_SET, &vport_ether);
if (err) {
return err;
}
poll_notify(netdev);
return 0;
}
static int
netdev_gre_get_etheraddr(const struct netdev *netdev_,
uint8_t mac[ETH_ADDR_LEN])
{
struct odp_vport_ether vport_ether;
int err;
ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev_),
sizeof vport_ether.devname);
err = ioctl(ioctl_fd, ODP_VPORT_ETHER_GET, &vport_ether);
if (err) {
return err;
}
memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
return 0;
}
static int
netdev_gre_get_mtu(const struct netdev *netdev_, int *mtup)
{
struct odp_vport_mtu vport_mtu;
int err;
ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev_),
sizeof vport_mtu.devname);
err = ioctl(ioctl_fd, ODP_VPORT_MTU_GET, &vport_mtu);
if (err) {
return err;
}
*mtup = vport_mtu.mtu;
return 0;
}
static int
netdev_gre_get_carrier(const struct netdev *netdev OVS_UNUSED, bool *carrier)
{
*carrier = true;
return 0;
}
static int
netdev_gre_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
{
const char *name = netdev_get_name(netdev_);
struct odp_vport_stats_req ovsr;
int err;
ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
err = do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
if (err) {
return err;
}
stats->rx_packets = ovsr.stats.rx_packets;
stats->tx_packets = ovsr.stats.tx_packets;
stats->rx_bytes = ovsr.stats.rx_bytes;
stats->tx_bytes = ovsr.stats.tx_bytes;
stats->rx_errors = ovsr.stats.rx_errors;
stats->tx_errors = ovsr.stats.tx_errors;
stats->rx_dropped = ovsr.stats.rx_dropped;
stats->tx_dropped = ovsr.stats.tx_dropped;
stats->multicast = UINT64_MAX;
stats->collisions = ovsr.stats.collisions;
stats->rx_length_errors = UINT64_MAX;
stats->rx_over_errors = ovsr.stats.rx_over_err;
stats->rx_crc_errors = ovsr.stats.rx_crc_err;
stats->rx_frame_errors = ovsr.stats.rx_frame_err;
stats->rx_fifo_errors = UINT64_MAX;
stats->rx_missed_errors = UINT64_MAX;
stats->tx_aborted_errors = UINT64_MAX;
stats->tx_carrier_errors = UINT64_MAX;
stats->tx_fifo_errors = UINT64_MAX;
stats->tx_heartbeat_errors = UINT64_MAX;
stats->tx_window_errors = UINT64_MAX;
return 0;
}
static int
netdev_gre_update_flags(struct netdev *netdev OVS_UNUSED,
enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
enum netdev_flags *old_flagsp)
{
if (off & (NETDEV_UP | NETDEV_PROMISC)) {
return EOPNOTSUPP;
}
*old_flagsp = NETDEV_UP | NETDEV_PROMISC;
return 0;
}
static int
netdev_gre_poll_add(struct netdev *netdev, void (*cb)(struct netdev_notifier *),
void *aux, struct netdev_notifier **notifierp)
{
const char *netdev_name = netdev_get_name(netdev);
struct netdev_gre_notifier *notifier;
struct list *list;
list = shash_find_data(&netdev_gre_notifiers, netdev_name);
if (!list) {
list = xmalloc(sizeof *list);
list_init(list);
shash_add(&netdev_gre_notifiers, netdev_name, list);
}
notifier = xmalloc(sizeof *notifier);
netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
list_push_back(list, &notifier->node);
*notifierp = &notifier->notifier;
return 0;
}
static void
netdev_gre_poll_remove(struct netdev_notifier *notifier_)
{
struct netdev_gre_notifier *notifier =
CONTAINER_OF(notifier_, struct netdev_gre_notifier, notifier);
struct list *list;
list = list_remove(&notifier->node);
if (list_is_empty(list)) {
const char *netdev_name = netdev_get_name(notifier_->netdev);
shash_delete(&netdev_gre_notifiers,
shash_find(&netdev_gre_notifiers, netdev_name));
free(list);
}
free(notifier);
}
static void
poll_notify(const struct netdev_gre *netdev)
{
struct list *list = shash_find_data(&netdev_gre_notifiers,
netdev_get_name(&netdev->netdev));
if (list) {
struct netdev_gre_notifier *notifier;
LIST_FOR_EACH (notifier, struct netdev_gre_notifier, node, list) {
struct netdev_notifier *n = &notifier->notifier;
n->cb(n);
}
}
}
const struct netdev_class netdev_gre_class = {
"gre",
netdev_gre_init,
NULL, /* init */
NULL, /* run */
NULL, /* wait */
@@ -450,12 +245,12 @@ const struct netdev_class netdev_gre_class = {
NULL, /* send */
NULL, /* send_wait */
netdev_gre_set_etheraddr,
netdev_gre_get_etheraddr,
netdev_gre_get_mtu,
netdev_vport_set_etheraddr,
netdev_vport_get_etheraddr,
netdev_vport_get_mtu,
NULL, /* get_ifindex */
netdev_gre_get_carrier,
netdev_gre_get_stats,
netdev_vport_get_carrier,
netdev_vport_get_stats,
NULL, /* set_stats */
NULL, /* get_features */
@@ -470,8 +265,8 @@ const struct netdev_class netdev_gre_class = {
NULL, /* get_next_hop */
NULL, /* arp_lookup */
netdev_gre_update_flags,
netdev_vport_update_flags,
netdev_gre_poll_add,
netdev_gre_poll_remove,
netdev_vport_poll_add,
netdev_vport_poll_remove,
};

246
lib/netdev-vport.c Normal file
View File

@@ -0,0 +1,246 @@
/*
* Copyright (c) 2010 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or apatched to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "list.h"
#include "netdev-vport.h"
#include "openvswitch/datapath-protocol.h"
#include "shash.h"
#include "socket-util.h"
#define THIS_MODULE VLM_netdev_vport
#include "vlog.h"
struct netdev_vport_notifier {
struct netdev_notifier notifier;
struct list list_node;
struct shash_node *shash_node;
};
static struct shash netdev_vport_notifiers =
SHASH_INITIALIZER(&netdev_vport_notifiers);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
int
netdev_vport_do_ioctl(int cmd, void *arg)
{
static int ioctl_fd = -1;
if (ioctl_fd < 0) {
ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
if (ioctl_fd < 0) {
VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
return errno;
}
}
return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
}
int
netdev_vport_set_etheraddr(struct netdev *netdev,
const uint8_t mac[ETH_ADDR_LEN])
{
struct odp_vport_ether vport_ether;
int err;
ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
sizeof vport_ether.devname);
memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
if (err) {
return err;
}
netdev_vport_poll_notify(netdev);
return 0;
}
int
netdev_vport_get_etheraddr(const struct netdev *netdev,
uint8_t mac[ETH_ADDR_LEN])
{
struct odp_vport_ether vport_ether;
int err;
ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
sizeof vport_ether.devname);
err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
if (err) {
return err;
}
memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
return 0;
}
int
netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
{
struct odp_vport_mtu vport_mtu;
int err;
ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
sizeof vport_mtu.devname);
err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
if (err) {
return err;
}
*mtup = vport_mtu.mtu;
return 0;
}
int
netdev_vport_get_carrier(const struct netdev *netdev OVS_UNUSED, bool *carrier)
{
*carrier = true;
return 0;
}
int
netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
{
const char *name = netdev_get_name(netdev);
struct odp_vport_stats_req ovsr;
int err;
ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
if (err) {
return err;
}
stats->rx_packets = ovsr.stats.rx_packets;
stats->tx_packets = ovsr.stats.tx_packets;
stats->rx_bytes = ovsr.stats.rx_bytes;
stats->tx_bytes = ovsr.stats.tx_bytes;
stats->rx_errors = ovsr.stats.rx_errors;
stats->tx_errors = ovsr.stats.tx_errors;
stats->rx_dropped = ovsr.stats.rx_dropped;
stats->tx_dropped = ovsr.stats.tx_dropped;
stats->multicast = UINT64_MAX;
stats->collisions = ovsr.stats.collisions;
stats->rx_length_errors = UINT64_MAX;
stats->rx_over_errors = ovsr.stats.rx_over_err;
stats->rx_crc_errors = ovsr.stats.rx_crc_err;
stats->rx_frame_errors = ovsr.stats.rx_frame_err;
stats->rx_fifo_errors = UINT64_MAX;
stats->rx_missed_errors = UINT64_MAX;
stats->tx_aborted_errors = UINT64_MAX;
stats->tx_carrier_errors = UINT64_MAX;
stats->tx_fifo_errors = UINT64_MAX;
stats->tx_heartbeat_errors = UINT64_MAX;
stats->tx_window_errors = UINT64_MAX;
return 0;
}
int
netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
enum netdev_flags *old_flagsp)
{
if (off & (NETDEV_UP | NETDEV_PROMISC)) {
return EOPNOTSUPP;
}
*old_flagsp = NETDEV_UP | NETDEV_PROMISC;
return 0;
}
static char *
make_poll_name(const struct netdev *netdev)
{
return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
}
int
netdev_vport_poll_add(struct netdev *netdev,
void (*cb)(struct netdev_notifier *), void *aux,
struct netdev_notifier **notifierp)
{
char *poll_name = make_poll_name(netdev);
struct netdev_vport_notifier *notifier;
struct list *list;
struct shash_node *shash_node;
shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
if (!shash_node) {
list = xmalloc(sizeof *list);
list_init(list);
shash_node = shash_add(&netdev_vport_notifiers,
netdev_get_name(netdev), list);
} else {
list = shash_node->data;
}
notifier = xmalloc(sizeof *notifier);
netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
list_push_back(list, &notifier->list_node);
notifier->shash_node = shash_node;
*notifierp = &notifier->notifier;
free(poll_name);
return 0;
}
void
netdev_vport_poll_remove(struct netdev_notifier *notifier_)
{
struct netdev_vport_notifier *notifier =
CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
struct list *list;
list = list_remove(&notifier->list_node);
if (list_is_empty(list)) {
shash_delete(&netdev_vport_notifiers, notifier->shash_node);
free(list);
}
free(notifier);
}
void
netdev_vport_poll_notify(const struct netdev *netdev)
{
char *poll_name = make_poll_name(netdev);
struct list *list = shash_find_data(&netdev_vport_notifiers,
poll_name);
if (list) {
struct netdev_vport_notifier *notifier;
LIST_FOR_EACH (notifier, struct netdev_vport_notifier,
list_node, list) {
struct netdev_notifier *n = &notifier->notifier;
n->cb(n);
}
}
free(poll_name);
}

42
lib/netdev-vport.h Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2010 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NETDEV_VPORT_H
#define NETDEV_VPORT_H 1
#include "netdev-provider.h"
#include "packets.h"
int netdev_vport_do_ioctl(int cmd, void *arg);
int netdev_vport_set_etheraddr(struct netdev *,
const uint8_t mac[ETH_ADDR_LEN]);
int netdev_vport_get_etheraddr(const struct netdev *,
uint8_t mac[ETH_ADDR_LEN]);
int netdev_vport_get_mtu(const struct netdev *, int *mtup);
int netdev_vport_get_carrier(const struct netdev *, bool *carrier);
int netdev_vport_get_stats(const struct netdev *, struct netdev_stats *);
int netdev_vport_update_flags(struct netdev *, enum netdev_flags off,
enum netdev_flags on,
enum netdev_flags *old_flagsp);
int netdev_vport_poll_add(struct netdev *,
void (*cb)(struct netdev_notifier *), void *aux,
struct netdev_notifier **);
void netdev_vport_poll_remove(struct netdev_notifier *);
void netdev_vport_poll_notify(const struct netdev *);
#endif /* netdev-vport.h */

View File

@@ -45,6 +45,7 @@ VLOG_MODULE(mac_learning)
VLOG_MODULE(netdev)
VLOG_MODULE(netdev_gre)
VLOG_MODULE(netdev_linux)
VLOG_MODULE(netdev_vport)
VLOG_MODULE(netflow)
VLOG_MODULE(netlink)
VLOG_MODULE(ofctl)