96834115e39f1f6941e2965a814c1bdec3cc5b6d
[sliver-openvswitch.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21
22 #include "list.h"
23 #include "netdev-vport.h"
24 #include "openvswitch/datapath-protocol.h"
25 #include "shash.h"
26 #include "socket-util.h"
27 #include "vlog.h"
28
29 VLOG_DEFINE_THIS_MODULE(netdev_vport)
30
31 struct netdev_vport_notifier {
32     struct netdev_notifier notifier;
33     struct list list_node;
34     struct shash_node *shash_node; 
35 };
36
37 static struct shash netdev_vport_notifiers =
38                                     SHASH_INITIALIZER(&netdev_vport_notifiers);
39
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
41
42 int
43 netdev_vport_do_ioctl(int cmd, void *arg)
44 {
45     static int ioctl_fd = -1;
46
47     if (ioctl_fd < 0) {
48         ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
49         if (ioctl_fd < 0) {
50             VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
51             return errno;
52         }
53     }
54
55     return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
56 }
57
58 int
59 netdev_vport_set_etheraddr(struct netdev *netdev,
60                            const uint8_t mac[ETH_ADDR_LEN])
61 {
62     struct odp_vport_ether vport_ether;
63     int err;
64
65     ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
66                 sizeof vport_ether.devname);
67
68     memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
69
70     err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
71     if (err) {
72         return err;
73     }
74
75     netdev_vport_poll_notify(netdev);
76     return 0;
77 }
78
79 int
80 netdev_vport_get_etheraddr(const struct netdev *netdev,
81                            uint8_t mac[ETH_ADDR_LEN])
82 {
83     struct odp_vport_ether vport_ether;
84     int err;
85
86     ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
87                 sizeof vport_ether.devname);
88
89     err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
90     if (err) {
91         return err;
92     }
93
94     memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
95     return 0;
96 }
97
98 int
99 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
100 {
101     struct odp_vport_mtu vport_mtu;
102     int err;
103
104     ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
105                 sizeof vport_mtu.devname);
106
107     err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
108     if (err) {
109         return err;
110     }
111
112     *mtup = vport_mtu.mtu;
113     return 0;
114 }
115
116 int
117 netdev_vport_get_carrier(const struct netdev *netdev OVS_UNUSED, bool *carrier)
118 {
119     *carrier = true;
120     return 0;
121 }
122
123 int
124 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
125 {
126     const char *name = netdev_get_name(netdev);
127     struct odp_vport_stats_req ovsr;
128     int err;
129
130     ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
131     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
132     if (err) {
133         return err;
134     }
135
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;
157
158     return 0;
159 }
160
161 int
162 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
163 {
164     struct odp_vport_stats_req ovsr;
165     int err;
166
167     ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
168
169     ovsr.stats.rx_packets = stats->rx_packets;
170     ovsr.stats.tx_packets = stats->tx_packets;
171     ovsr.stats.rx_bytes = stats->rx_bytes;
172     ovsr.stats.tx_bytes = stats->tx_bytes;
173     ovsr.stats.rx_errors = stats->rx_errors;
174     ovsr.stats.tx_errors = stats->tx_errors;
175     ovsr.stats.rx_dropped = stats->rx_dropped;
176     ovsr.stats.tx_dropped = stats->tx_dropped;
177     ovsr.stats.collisions = stats->collisions;
178     ovsr.stats.rx_over_err = stats->rx_over_errors;
179     ovsr.stats.rx_crc_err = stats->rx_crc_errors;
180     ovsr.stats.rx_frame_err = stats->rx_frame_errors;
181
182     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
183
184     /* If the vport layer doesn't know about the device, that doesn't mean it
185      * doesn't exist (after all were able to open it when netdev_open() was
186      * called), it just means that it isn't attached and we'll be getting
187      * stats a different way. */
188     if (err == ENODEV) {
189         err = EOPNOTSUPP;
190     }
191
192     return err;
193 }
194
195 int
196 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
197                         enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
198                         enum netdev_flags *old_flagsp)
199 {
200     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
201         return EOPNOTSUPP;
202     }
203
204     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
205     return 0;
206 }
207
208 static char *
209 make_poll_name(const struct netdev *netdev)
210 {
211     return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
212 }
213
214 int
215 netdev_vport_poll_add(struct netdev *netdev,
216                       void (*cb)(struct netdev_notifier *), void *aux,
217                       struct netdev_notifier **notifierp)
218 {
219     char *poll_name = make_poll_name(netdev);
220     struct netdev_vport_notifier *notifier;
221     struct list *list;
222     struct shash_node *shash_node;
223
224     shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
225     if (!shash_node) {
226         list = xmalloc(sizeof *list);
227         list_init(list);
228         shash_node = shash_add(&netdev_vport_notifiers,
229                                netdev_get_name(netdev), list);
230     } else {
231         list = shash_node->data;
232     }
233
234     notifier = xmalloc(sizeof *notifier);
235     netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
236     list_push_back(list, &notifier->list_node);
237     notifier->shash_node = shash_node;
238
239     *notifierp = &notifier->notifier;
240     free(poll_name);
241
242     return 0;
243 }
244
245 void
246 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
247 {
248     struct netdev_vport_notifier *notifier =
249                 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
250
251     struct list *list;
252
253     list = list_remove(&notifier->list_node);
254     if (list_is_empty(list)) {
255         shash_delete(&netdev_vport_notifiers, notifier->shash_node);
256         free(list);
257     }
258
259     free(notifier);
260 }
261
262 void
263 netdev_vport_poll_notify(const struct netdev *netdev)
264 {
265     char *poll_name = make_poll_name(netdev);
266     struct list *list = shash_find_data(&netdev_vport_notifiers,
267                                         poll_name);
268
269     if (list) {
270         struct netdev_vport_notifier *notifier;
271
272         LIST_FOR_EACH (notifier, struct netdev_vport_notifier,
273                        list_node, list) {
274             struct netdev_notifier *n = &notifier->notifier;
275             n->cb(n);
276         }
277     }
278
279     free(poll_name);
280 }