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"
29 VLOG_DEFINE_THIS_MODULE(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_stats(const struct netdev *netdev, struct netdev_stats *stats)
119 const char *name = netdev_get_name(netdev);
120 struct odp_vport_stats_req ovsr;
123 ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
124 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
129 stats->rx_packets = ovsr.stats.rx_packets;
130 stats->tx_packets = ovsr.stats.tx_packets;
131 stats->rx_bytes = ovsr.stats.rx_bytes;
132 stats->tx_bytes = ovsr.stats.tx_bytes;
133 stats->rx_errors = ovsr.stats.rx_errors;
134 stats->tx_errors = ovsr.stats.tx_errors;
135 stats->rx_dropped = ovsr.stats.rx_dropped;
136 stats->tx_dropped = ovsr.stats.tx_dropped;
137 stats->multicast = UINT64_MAX;
138 stats->collisions = ovsr.stats.collisions;
139 stats->rx_length_errors = UINT64_MAX;
140 stats->rx_over_errors = ovsr.stats.rx_over_err;
141 stats->rx_crc_errors = ovsr.stats.rx_crc_err;
142 stats->rx_frame_errors = ovsr.stats.rx_frame_err;
143 stats->rx_fifo_errors = UINT64_MAX;
144 stats->rx_missed_errors = UINT64_MAX;
145 stats->tx_aborted_errors = UINT64_MAX;
146 stats->tx_carrier_errors = UINT64_MAX;
147 stats->tx_fifo_errors = UINT64_MAX;
148 stats->tx_heartbeat_errors = UINT64_MAX;
149 stats->tx_window_errors = UINT64_MAX;
155 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
157 struct odp_vport_stats_req ovsr;
160 ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
162 ovsr.stats.rx_packets = stats->rx_packets;
163 ovsr.stats.tx_packets = stats->tx_packets;
164 ovsr.stats.rx_bytes = stats->rx_bytes;
165 ovsr.stats.tx_bytes = stats->tx_bytes;
166 ovsr.stats.rx_errors = stats->rx_errors;
167 ovsr.stats.tx_errors = stats->tx_errors;
168 ovsr.stats.rx_dropped = stats->rx_dropped;
169 ovsr.stats.tx_dropped = stats->tx_dropped;
170 ovsr.stats.collisions = stats->collisions;
171 ovsr.stats.rx_over_err = stats->rx_over_errors;
172 ovsr.stats.rx_crc_err = stats->rx_crc_errors;
173 ovsr.stats.rx_frame_err = stats->rx_frame_errors;
175 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
177 /* If the vport layer doesn't know about the device, that doesn't mean it
178 * doesn't exist (after all were able to open it when netdev_open() was
179 * called), it just means that it isn't attached and we'll be getting
180 * stats a different way. */
189 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
190 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
191 enum netdev_flags *old_flagsp)
193 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
197 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
202 make_poll_name(const struct netdev *netdev)
204 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
208 netdev_vport_poll_add(struct netdev *netdev,
209 void (*cb)(struct netdev_notifier *), void *aux,
210 struct netdev_notifier **notifierp)
212 char *poll_name = make_poll_name(netdev);
213 struct netdev_vport_notifier *notifier;
215 struct shash_node *shash_node;
217 shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
219 list = xmalloc(sizeof *list);
221 shash_node = shash_add(&netdev_vport_notifiers,
222 netdev_get_name(netdev), list);
224 list = shash_node->data;
227 notifier = xmalloc(sizeof *notifier);
228 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
229 list_push_back(list, ¬ifier->list_node);
230 notifier->shash_node = shash_node;
232 *notifierp = ¬ifier->notifier;
239 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
241 struct netdev_vport_notifier *notifier =
242 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
246 list = list_remove(¬ifier->list_node);
247 if (list_is_empty(list)) {
248 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
256 netdev_vport_poll_notify(const struct netdev *netdev)
258 char *poll_name = make_poll_name(netdev);
259 struct list *list = shash_find_data(&netdev_vport_notifiers,
263 struct netdev_vport_notifier *notifier;
265 LIST_FOR_EACH (notifier, struct netdev_vport_notifier,
267 struct netdev_notifier *n = ¬ifier->notifier;