datapath-windows: Remove the old IOCTL vport functions.
[cascardo/ovs.git] / lib / netdev-windows.c
1 /*
2  * Copyright (c) 2014 VMware, Inc.
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 agreed 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 <stdlib.h>
18 #include <config.h>
19 #include <errno.h>
20
21 #include <net/if.h>
22
23 #include "coverage.h"
24 #include "fatal-signal.h"
25 #include "netdev-provider.h"
26 #include "ofpbuf.h"
27 #include "poll-loop.h"
28 #include "shash.h"
29 #include "svec.h"
30 #include "vlog.h"
31 #include "odp-netlink.h"
32 #include "netlink-socket.h"
33 #include "netlink.h"
34
35 VLOG_DEFINE_THIS_MODULE(netdev_windows);
36 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
37
38 enum {
39     VALID_ETHERADDR         = 1 << 0,
40     VALID_MTU               = 1 << 1,
41     VALID_IFFLAG            = 1 << 5,
42 };
43
44 /* Caches the information of a netdev. */
45 struct netdev_windows {
46     struct netdev up;
47     int32_t dev_type;
48     uint32_t port_no;
49
50     unsigned int change_seq;
51
52     unsigned int cache_valid;
53     int ifindex;
54     uint8_t mac[ETH_ADDR_LEN];
55     uint32_t mtu;
56     unsigned int ifi_flags;
57 };
58
59 /* Utility structure for netdev commands. */
60 struct netdev_windows_netdev_info {
61     /* Generic Netlink header. */
62     uint8_t cmd;
63
64     /* Information that is relevant to ovs. */
65     uint32_t dp_ifindex;
66     uint32_t port_no;
67     uint32_t ovs_type;
68
69     /* General information of a network device. */
70     const char *name;
71     uint8_t mac_address[ETH_ADDR_LEN];
72     uint32_t mtu;
73     uint32_t ifi_flags;
74 };
75
76 static int refresh_port_status(struct netdev_windows *netdev);
77 static int query_netdev(const char *devname,
78                         struct netdev_windows_netdev_info *reply,
79                         struct ofpbuf **bufp);
80 static struct netdev *netdev_windows_alloc(void);
81 static int netdev_windows_init_(void);
82
83 /* Generic Netlink family numbers for OVS.
84  *
85  * Initialized by netdev_windows_init_(). */
86 static int ovs_win_netdev_family;
87 struct nl_sock *ovs_win_netdev_sock;
88
89
90 static bool
91 is_netdev_windows_class(const struct netdev_class *netdev_class)
92 {
93     return netdev_class->alloc == netdev_windows_alloc;
94 }
95
96 static struct netdev_windows *
97 netdev_windows_cast(const struct netdev *netdev_)
98 {
99     ovs_assert(is_netdev_windows_class(netdev_get_class(netdev_)));
100     return CONTAINER_OF(netdev_, struct netdev_windows, up);
101 }
102
103 static int
104 netdev_windows_init_(void)
105 {
106     int error = 0;
107     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
108
109     if (ovsthread_once_start(&once)) {
110         error = nl_lookup_genl_family(OVS_WIN_NETDEV_FAMILY,
111                                       &ovs_win_netdev_family);
112         if (error) {
113             VLOG_ERR("Generic Netlink family '%s' does not exist. "
114                      "The Open vSwitch kernel module is probably not loaded.",
115                      OVS_WIN_NETDEV_FAMILY);
116         }
117         if (!error) {
118             /* XXX: Where to close this socket? */
119             error = nl_sock_create(NETLINK_GENERIC, &ovs_win_netdev_sock);
120         }
121
122         ovsthread_once_done(&once);
123     }
124
125     return error;
126 }
127
128 static struct netdev *
129 netdev_windows_alloc(void)
130 {
131     struct netdev_windows *netdev = xzalloc(sizeof *netdev);
132     return netdev ? &netdev->up : NULL;
133 }
134
135 static int
136 netdev_windows_system_construct(struct netdev *netdev_)
137 {
138     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
139     uint8_t mac[ETH_ADDR_LEN];
140     struct netdev_windows_netdev_info info;
141     struct ofpbuf *buf;
142     int ret;
143
144     /* Query the attributes and runtime status of the netdev. */
145     ret = query_netdev(netdev_get_name(&netdev->up), &info, &buf);
146     if (ret) {
147         return ret;
148     }
149     ofpbuf_delete(buf);
150
151     netdev->change_seq = 1;
152     netdev->dev_type = info.ovs_type;
153     netdev->port_no = info.port_no;
154
155     memcpy(netdev->mac, info.mac_address, ETH_ADDR_LEN);
156     netdev->cache_valid = VALID_ETHERADDR;
157     netdev->ifindex = -EOPNOTSUPP;
158
159     netdev->mtu = info.mtu;
160     netdev->cache_valid |= VALID_MTU;
161
162     netdev->ifi_flags = info.ifi_flags;
163     netdev->cache_valid |= VALID_IFFLAG;
164
165     VLOG_DBG("construct device %s, ovs_type: %u.",
166              netdev_get_name(&netdev->up), info.ovs_type);
167     return 0;
168 }
169
170 static int
171 netdev_windows_netdev_to_ofpbuf(struct netdev_windows_netdev_info *info,
172                                 struct ofpbuf *buf)
173 {
174     struct ovs_header *ovs_header;
175     int error = EINVAL;
176
177     nl_msg_put_genlmsghdr(buf, 0, ovs_win_netdev_family,
178                           NLM_F_REQUEST | NLM_F_ECHO,
179                           info->cmd, OVS_WIN_NETDEV_VERSION);
180
181     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
182     ovs_header->dp_ifindex = info->dp_ifindex;
183
184     if (info->name) {
185         nl_msg_put_string(buf, OVS_WIN_NETDEV_ATTR_NAME, info->name);
186         error = 0;
187     }
188
189     return error;
190 }
191
192 static void
193 netdev_windows_info_init(struct netdev_windows_netdev_info *info)
194 {
195     memset(info, 0, sizeof *info);
196 }
197
198 static int
199 netdev_windows_netdev_from_ofpbuf(struct netdev_windows_netdev_info *info,
200                                   struct ofpbuf *buf)
201 {
202     static const struct nl_policy ovs_netdev_policy[] = {
203         [OVS_WIN_NETDEV_ATTR_PORT_NO] = { .type = NL_A_U32 },
204         [OVS_WIN_NETDEV_ATTR_TYPE] = { .type = NL_A_U32 },
205         [OVS_WIN_NETDEV_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
206         [OVS_WIN_NETDEV_ATTR_MAC_ADDR] = { NL_POLICY_FOR(info->mac_address) },
207         [OVS_WIN_NETDEV_ATTR_MTU] = { .type = NL_A_U32 },
208         [OVS_WIN_NETDEV_ATTR_IF_FLAGS] = { .type = NL_A_U32 },
209     };
210
211     struct nlattr *a[ARRAY_SIZE(ovs_netdev_policy)];
212     struct ovs_header *ovs_header;
213     struct nlmsghdr *nlmsg;
214     struct genlmsghdr *genl;
215     struct ofpbuf b;
216
217     netdev_windows_info_init(info);
218
219     ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
220     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
221     genl = ofpbuf_try_pull(&b, sizeof *genl);
222     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
223     if (!nlmsg || !genl || !ovs_header
224         || nlmsg->nlmsg_type != ovs_win_netdev_family
225         || !nl_policy_parse(&b, 0, ovs_netdev_policy, a,
226                             ARRAY_SIZE(ovs_netdev_policy))) {
227         return EINVAL;
228     }
229
230     info->cmd = genl->cmd;
231     info->dp_ifindex = ovs_header->dp_ifindex;
232     info->port_no = nl_attr_get_odp_port(a[OVS_WIN_NETDEV_ATTR_PORT_NO]);
233     info->ovs_type = nl_attr_get_u32(a[OVS_WIN_NETDEV_ATTR_TYPE]);
234     info->name = nl_attr_get_string(a[OVS_WIN_NETDEV_ATTR_NAME]);
235     memcpy(info->mac_address, nl_attr_get_string(a[OVS_WIN_NETDEV_ATTR_NAME]),
236            sizeof(info->mac_address));
237     info->mtu = nl_attr_get_u32(a[OVS_WIN_NETDEV_ATTR_MTU]);
238     info->ifi_flags = nl_attr_get_u32(a[OVS_WIN_NETDEV_ATTR_IF_FLAGS]);
239
240     return 0;
241 }
242
243 static int
244 query_netdev(const char *devname,
245              struct netdev_windows_netdev_info *info,
246              struct ofpbuf **bufp)
247 {
248     int error = 0;
249     struct ofpbuf *request_buf;
250
251     ovs_assert(info != NULL);
252     netdev_windows_info_init(info);
253
254     error = netdev_windows_init_();
255     if (error) {
256         if (info) {
257             *bufp = NULL;
258             netdev_windows_info_init(info);
259         }
260         return error;
261     }
262
263     request_buf = ofpbuf_new(1024);
264     info->cmd = OVS_WIN_NETDEV_CMD_GET;
265     info->name = devname;
266     error = netdev_windows_netdev_to_ofpbuf(info, request_buf);
267     if (error) {
268         ofpbuf_delete(request_buf);
269         return error;
270     }
271
272     error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
273     ofpbuf_delete(request_buf);
274
275     if (info) {
276         if (!error) {
277             error = netdev_windows_netdev_from_ofpbuf(info, *bufp);
278         }
279         if (error) {
280             netdev_windows_info_init(info);
281             ofpbuf_delete(*bufp);
282             *bufp = NULL;
283         }
284     }
285
286     return 0;
287 }
288
289 static void
290 netdev_windows_destruct(struct netdev *netdev_)
291 {
292
293 }
294
295 static void
296 netdev_windows_dealloc(struct netdev *netdev_)
297 {
298     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
299     free(netdev);
300 }
301
302 static int
303 netdev_windows_get_etheraddr(const struct netdev *netdev_, uint8_t mac[6])
304 {
305     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
306
307     ovs_assert((netdev->cache_valid & VALID_ETHERADDR) != 0);
308     if (netdev->cache_valid & VALID_ETHERADDR) {
309         memcpy(mac, netdev->mac, ETH_ADDR_LEN);
310     } else {
311         return EINVAL;
312     }
313     return 0;
314 }
315
316 static int
317 netdev_windows_get_mtu(const struct netdev *netdev_, int *mtup)
318 {
319     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
320
321     ovs_assert((netdev->cache_valid & VALID_MTU) != 0);
322     if (netdev->cache_valid & VALID_MTU) {
323         *mtup = netdev->mtu;
324     } else {
325         return EINVAL;
326     }
327     return 0;
328 }
329 \f
330
331 static int
332 netdev_windows_internal_construct(struct netdev *netdev_)
333 {
334     return netdev_windows_system_construct(netdev_);
335 }
336
337
338 #define NETDEV_WINDOWS_CLASS(NAME, CONSTRUCT)                           \
339 {                                                                       \
340     .type               = NAME,                                         \
341     .alloc              = netdev_windows_alloc,                         \
342     .construct          = CONSTRUCT,                                    \
343     .destruct           = netdev_windows_destruct,                      \
344     .dealloc            = netdev_windows_dealloc,                       \
345     .get_etheraddr      = netdev_windows_get_etheraddr,                 \
346 }
347
348 const struct netdev_class netdev_windows_class =
349     NETDEV_WINDOWS_CLASS(
350         "system",
351         netdev_windows_system_construct);
352
353 const struct netdev_class netdev_internal_class =
354     NETDEV_WINDOWS_CLASS(
355         "internal",
356         netdev_windows_internal_construct);