netdev: Extract netdev vport functions.
[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
28 #define THIS_MODULE VLM_netdev_vport
29 #include "vlog.h"
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_update_flags(struct netdev *netdev OVS_UNUSED,
163                         enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
164                         enum netdev_flags *old_flagsp)
165 {
166     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
167         return EOPNOTSUPP;
168     }
169
170     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
171     return 0;
172 }
173
174 static char *
175 make_poll_name(const struct netdev *netdev)
176 {
177     return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
178 }
179
180 int
181 netdev_vport_poll_add(struct netdev *netdev,
182                       void (*cb)(struct netdev_notifier *), void *aux,
183                       struct netdev_notifier **notifierp)
184 {
185     char *poll_name = make_poll_name(netdev);
186     struct netdev_vport_notifier *notifier;
187     struct list *list;
188     struct shash_node *shash_node;
189
190     shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
191     if (!shash_node) {
192         list = xmalloc(sizeof *list);
193         list_init(list);
194         shash_node = shash_add(&netdev_vport_notifiers,
195                                netdev_get_name(netdev), list);
196     } else {
197         list = shash_node->data;
198     }
199
200     notifier = xmalloc(sizeof *notifier);
201     netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
202     list_push_back(list, &notifier->list_node);
203     notifier->shash_node = shash_node;
204
205     *notifierp = &notifier->notifier;
206     free(poll_name);
207
208     return 0;
209 }
210
211 void
212 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
213 {
214     struct netdev_vport_notifier *notifier =
215                 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
216
217     struct list *list;
218
219     list = list_remove(&notifier->list_node);
220     if (list_is_empty(list)) {
221         shash_delete(&netdev_vport_notifiers, notifier->shash_node);
222         free(list);
223     }
224
225     free(notifier);
226 }
227
228 void
229 netdev_vport_poll_notify(const struct netdev *netdev)
230 {
231     char *poll_name = make_poll_name(netdev);
232     struct list *list = shash_find_data(&netdev_vport_notifiers,
233                                         poll_name);
234
235     if (list) {
236         struct netdev_vport_notifier *notifier;
237
238         LIST_FOR_EACH (notifier, struct netdev_vport_notifier,
239                        list_node, list) {
240             struct netdev_notifier *n = &notifier->notifier;
241             n->cb(n);
242         }
243     }
244
245     free(poll_name);
246 }