2 * Copyright (c) 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or apatched to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <sys/ioctl.h>
23 #include "netdev-vport.h"
24 #include "openvswitch/datapath-protocol.h"
26 #include "socket-util.h"
28 #define THIS_MODULE VLM_netdev_vport
31 struct netdev_vport_notifier {
32 struct netdev_notifier notifier;
33 struct list list_node;
34 struct shash_node *shash_node;
37 static struct shash netdev_vport_notifiers =
38 SHASH_INITIALIZER(&netdev_vport_notifiers);
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
43 netdev_vport_do_ioctl(int cmd, void *arg)
45 static int ioctl_fd = -1;
48 ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
50 VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
55 return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
59 netdev_vport_set_etheraddr(struct netdev *netdev,
60 const uint8_t mac[ETH_ADDR_LEN])
62 struct odp_vport_ether vport_ether;
65 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
66 sizeof vport_ether.devname);
68 memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
70 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
75 netdev_vport_poll_notify(netdev);
80 netdev_vport_get_etheraddr(const struct netdev *netdev,
81 uint8_t mac[ETH_ADDR_LEN])
83 struct odp_vport_ether vport_ether;
86 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
87 sizeof vport_ether.devname);
89 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
94 memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
99 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
101 struct odp_vport_mtu vport_mtu;
104 ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
105 sizeof vport_mtu.devname);
107 err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
112 *mtup = vport_mtu.mtu;
117 netdev_vport_get_carrier(const struct netdev *netdev OVS_UNUSED, bool *carrier)
124 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
126 const char *name = netdev_get_name(netdev);
127 struct odp_vport_stats_req ovsr;
130 ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
131 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
136 stats->rx_packets = ovsr.stats.rx_packets;
137 stats->tx_packets = ovsr.stats.tx_packets;
138 stats->rx_bytes = ovsr.stats.rx_bytes;
139 stats->tx_bytes = ovsr.stats.tx_bytes;
140 stats->rx_errors = ovsr.stats.rx_errors;
141 stats->tx_errors = ovsr.stats.tx_errors;
142 stats->rx_dropped = ovsr.stats.rx_dropped;
143 stats->tx_dropped = ovsr.stats.tx_dropped;
144 stats->multicast = UINT64_MAX;
145 stats->collisions = ovsr.stats.collisions;
146 stats->rx_length_errors = UINT64_MAX;
147 stats->rx_over_errors = ovsr.stats.rx_over_err;
148 stats->rx_crc_errors = ovsr.stats.rx_crc_err;
149 stats->rx_frame_errors = ovsr.stats.rx_frame_err;
150 stats->rx_fifo_errors = UINT64_MAX;
151 stats->rx_missed_errors = UINT64_MAX;
152 stats->tx_aborted_errors = UINT64_MAX;
153 stats->tx_carrier_errors = UINT64_MAX;
154 stats->tx_fifo_errors = UINT64_MAX;
155 stats->tx_heartbeat_errors = UINT64_MAX;
156 stats->tx_window_errors = UINT64_MAX;
162 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
163 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
164 enum netdev_flags *old_flagsp)
166 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
170 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
175 make_poll_name(const struct netdev *netdev)
177 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
181 netdev_vport_poll_add(struct netdev *netdev,
182 void (*cb)(struct netdev_notifier *), void *aux,
183 struct netdev_notifier **notifierp)
185 char *poll_name = make_poll_name(netdev);
186 struct netdev_vport_notifier *notifier;
188 struct shash_node *shash_node;
190 shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
192 list = xmalloc(sizeof *list);
194 shash_node = shash_add(&netdev_vport_notifiers,
195 netdev_get_name(netdev), list);
197 list = shash_node->data;
200 notifier = xmalloc(sizeof *notifier);
201 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
202 list_push_back(list, ¬ifier->list_node);
203 notifier->shash_node = shash_node;
205 *notifierp = ¬ifier->notifier;
212 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
214 struct netdev_vport_notifier *notifier =
215 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
219 list = list_remove(¬ifier->list_node);
220 if (list_is_empty(list)) {
221 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
229 netdev_vport_poll_notify(const struct netdev *netdev)
231 char *poll_name = make_poll_name(netdev);
232 struct list *list = shash_find_data(&netdev_vport_notifiers,
236 struct netdev_vport_notifier *notifier;
238 LIST_FOR_EACH (notifier, struct netdev_vport_notifier,
240 struct netdev_notifier *n = ¬ifier->notifier;