netdev: Fix carrier status for down interfaces.
[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_stats(const struct netdev *netdev, struct netdev_stats *stats)
118 {
119     const char *name = netdev_get_name(netdev);
120     struct odp_vport_stats_req ovsr;
121     int err;
122
123     ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
124     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
125     if (err) {
126         return err;
127     }
128
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;
150
151     return 0;
152 }
153
154 int
155 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
156 {
157     struct odp_vport_stats_req ovsr;
158     int err;
159
160     ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
161
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;
174
175     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
176
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. */
181     if (err == ENODEV) {
182         err = EOPNOTSUPP;
183     }
184
185     return err;
186 }
187
188 int
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)
192 {
193     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
194         return EOPNOTSUPP;
195     }
196
197     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
198     return 0;
199 }
200
201 static char *
202 make_poll_name(const struct netdev *netdev)
203 {
204     return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
205 }
206
207 int
208 netdev_vport_poll_add(struct netdev *netdev,
209                       void (*cb)(struct netdev_notifier *), void *aux,
210                       struct netdev_notifier **notifierp)
211 {
212     char *poll_name = make_poll_name(netdev);
213     struct netdev_vport_notifier *notifier;
214     struct list *list;
215     struct shash_node *shash_node;
216
217     shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
218     if (!shash_node) {
219         list = xmalloc(sizeof *list);
220         list_init(list);
221         shash_node = shash_add(&netdev_vport_notifiers,
222                                netdev_get_name(netdev), list);
223     } else {
224         list = shash_node->data;
225     }
226
227     notifier = xmalloc(sizeof *notifier);
228     netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
229     list_push_back(list, &notifier->list_node);
230     notifier->shash_node = shash_node;
231
232     *notifierp = &notifier->notifier;
233     free(poll_name);
234
235     return 0;
236 }
237
238 void
239 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
240 {
241     struct netdev_vport_notifier *notifier =
242                 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
243
244     struct list *list;
245
246     list = list_remove(&notifier->list_node);
247     if (list_is_empty(list)) {
248         shash_delete(&netdev_vport_notifiers, notifier->shash_node);
249         free(list);
250     }
251
252     free(notifier);
253 }
254
255 void
256 netdev_vport_poll_notify(const struct netdev *netdev)
257 {
258     char *poll_name = make_poll_name(netdev);
259     struct list *list = shash_find_data(&netdev_vport_notifiers,
260                                         poll_name);
261
262     if (list) {
263         struct netdev_vport_notifier *notifier;
264
265         LIST_FOR_EACH (notifier, struct netdev_vport_notifier,
266                        list_node, list) {
267             struct netdev_notifier *n = &notifier->notifier;
268             n->cb(n);
269         }
270     }
271
272     free(poll_name);
273 }