netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / netdev-windows.c
1 /*
2  * Copyright (c) 2014, 2016 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 #include <iphlpapi.h>
21
22 #include <net/if.h>
23
24 #include "coverage.h"
25 #include "fatal-signal.h"
26 #include "netdev-provider.h"
27 #include "ofpbuf.h"
28 #include "packets.h"
29 #include "poll-loop.h"
30 #include "shash.h"
31 #include "svec.h"
32 #include "openvswitch/vlog.h"
33 #include "odp-netlink.h"
34 #include "netlink-socket.h"
35 #include "netlink.h"
36
37 VLOG_DEFINE_THIS_MODULE(netdev_windows);
38 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
39
40 enum {
41     VALID_ETHERADDR         = 1 << 0,
42     VALID_MTU               = 1 << 1,
43     VALID_IFFLAG            = 1 << 5,
44 };
45
46 /* Caches the information of a netdev. */
47 struct netdev_windows {
48     struct netdev up;
49     int32_t dev_type;
50     uint32_t port_no;
51
52     unsigned int change_seq;
53
54     unsigned int cache_valid;
55     int ifindex;
56     struct eth_addr mac;
57     uint32_t mtu;
58     unsigned int ifi_flags;
59 };
60
61 /* Utility structure for netdev commands. */
62 struct netdev_windows_netdev_info {
63     /* Generic Netlink header. */
64     uint8_t cmd;
65
66     /* Information that is relevant to ovs. */
67     uint32_t dp_ifindex;
68     uint32_t port_no;
69     uint32_t ovs_type;
70
71     /* General information of a network device. */
72     const char *name;
73     struct eth_addr mac_address;
74     uint32_t mtu;
75     uint32_t ifi_flags;
76 };
77
78 static int query_netdev(const char *devname,
79                         struct netdev_windows_netdev_info *reply,
80                         struct ofpbuf **bufp);
81 static struct netdev *netdev_windows_alloc(void);
82 static int netdev_windows_init_(void);
83
84 /* Generic Netlink family numbers for OVS.
85  *
86  * Initialized by netdev_windows_init_(). */
87 static int ovs_win_netdev_family;
88 struct nl_sock *ovs_win_netdev_sock;
89
90
91 static bool
92 is_netdev_windows_class(const struct netdev_class *netdev_class)
93 {
94     return netdev_class->alloc == netdev_windows_alloc;
95 }
96
97 static struct netdev_windows *
98 netdev_windows_cast(const struct netdev *netdev_)
99 {
100     ovs_assert(is_netdev_windows_class(netdev_get_class(netdev_)));
101     return CONTAINER_OF(netdev_, struct netdev_windows, up);
102 }
103
104 static int
105 netdev_windows_init_(void)
106 {
107     int error = 0;
108     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
109
110     if (ovsthread_once_start(&once)) {
111         error = nl_lookup_genl_family(OVS_WIN_NETDEV_FAMILY,
112                                       &ovs_win_netdev_family);
113         if (error) {
114             VLOG_ERR("Generic Netlink family '%s' does not exist. "
115                      "The Open vSwitch kernel module is probably not loaded.",
116                      OVS_WIN_NETDEV_FAMILY);
117         }
118         if (!error) {
119             /* XXX: Where to close this socket? */
120             error = nl_sock_create(NETLINK_GENERIC, &ovs_win_netdev_sock);
121         }
122
123         ovsthread_once_done(&once);
124     }
125
126     return error;
127 }
128
129 static struct netdev *
130 netdev_windows_alloc(void)
131 {
132     struct netdev_windows *netdev = xzalloc(sizeof *netdev);
133     return netdev ? &netdev->up : NULL;
134 }
135
136 static uint32_t
137 dp_to_netdev_ifi_flags(uint32_t dp_flags)
138 {
139     uint32_t nd_flags = 0;
140
141     if (dp_flags && OVS_WIN_NETDEV_IFF_UP) {
142         nd_flags |= NETDEV_UP;
143     }
144
145     if (dp_flags && OVS_WIN_NETDEV_IFF_PROMISC) {
146         nd_flags |= NETDEV_PROMISC;
147     }
148
149     return nd_flags;
150 }
151
152 static int
153 netdev_windows_system_construct(struct netdev *netdev_)
154 {
155     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
156     struct netdev_windows_netdev_info info;
157     struct ofpbuf *buf;
158     int ret;
159
160     /* Query the attributes and runtime status of the netdev. */
161     ret = query_netdev(netdev_get_name(&netdev->up), &info, &buf);
162     if (ret) {
163         return ret;
164     }
165     ofpbuf_delete(buf);
166
167     netdev->change_seq = 1;
168     netdev->dev_type = info.ovs_type;
169     netdev->port_no = info.port_no;
170
171     netdev->mac = info.mac_address;
172     netdev->cache_valid = VALID_ETHERADDR;
173     netdev->ifindex = -EOPNOTSUPP;
174
175     netdev->mtu = info.mtu;
176     netdev->cache_valid |= VALID_MTU;
177
178     netdev->ifi_flags = dp_to_netdev_ifi_flags(info.ifi_flags);
179     netdev->cache_valid |= VALID_IFFLAG;
180
181     VLOG_DBG("construct device %s, ovs_type: %u.",
182              netdev_get_name(&netdev->up), info.ovs_type);
183     return 0;
184 }
185
186 static int
187 netdev_windows_netdev_to_ofpbuf(struct netdev_windows_netdev_info *info,
188                                 struct ofpbuf *buf)
189 {
190     struct ovs_header *ovs_header;
191     int error = EINVAL;
192
193     nl_msg_put_genlmsghdr(buf, 0, ovs_win_netdev_family,
194                           NLM_F_REQUEST | NLM_F_ECHO,
195                           info->cmd, OVS_WIN_NETDEV_VERSION);
196
197     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
198     ovs_header->dp_ifindex = info->dp_ifindex;
199
200     if (info->name) {
201         nl_msg_put_string(buf, OVS_WIN_NETDEV_ATTR_NAME, info->name);
202         error = 0;
203     }
204
205     return error;
206 }
207
208 static void
209 netdev_windows_info_init(struct netdev_windows_netdev_info *info)
210 {
211     memset(info, 0, sizeof *info);
212 }
213
214 static int
215 netdev_windows_netdev_from_ofpbuf(struct netdev_windows_netdev_info *info,
216                                   struct ofpbuf *buf)
217 {
218     static const struct nl_policy ovs_netdev_policy[] = {
219         [OVS_WIN_NETDEV_ATTR_PORT_NO] = { .type = NL_A_U32 },
220         [OVS_WIN_NETDEV_ATTR_TYPE] = { .type = NL_A_U32 },
221         [OVS_WIN_NETDEV_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
222         [OVS_WIN_NETDEV_ATTR_MAC_ADDR] = { NL_POLICY_FOR(info->mac_address) },
223         [OVS_WIN_NETDEV_ATTR_MTU] = { .type = NL_A_U32 },
224         [OVS_WIN_NETDEV_ATTR_IF_FLAGS] = { .type = NL_A_U32 },
225     };
226
227     netdev_windows_info_init(info);
228
229     struct ofpbuf b = ofpbuf_const_initializer(&b, buf->data, buf->size);
230     struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
231     struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
232     struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
233
234     struct nlattr *a[ARRAY_SIZE(ovs_netdev_policy)];
235     if (!nlmsg || !genl || !ovs_header
236         || nlmsg->nlmsg_type != ovs_win_netdev_family
237         || !nl_policy_parse(&b, 0, ovs_netdev_policy, a,
238                             ARRAY_SIZE(ovs_netdev_policy))) {
239         return EINVAL;
240     }
241
242     info->cmd = genl->cmd;
243     info->dp_ifindex = ovs_header->dp_ifindex;
244     info->port_no = nl_attr_get_odp_port(a[OVS_WIN_NETDEV_ATTR_PORT_NO]);
245     info->ovs_type = nl_attr_get_u32(a[OVS_WIN_NETDEV_ATTR_TYPE]);
246     info->name = nl_attr_get_string(a[OVS_WIN_NETDEV_ATTR_NAME]);
247     memcpy(&info->mac_address, nl_attr_get_unspec(a[OVS_WIN_NETDEV_ATTR_MAC_ADDR],
248                sizeof(info->mac_address)), sizeof(info->mac_address));
249     info->mtu = nl_attr_get_u32(a[OVS_WIN_NETDEV_ATTR_MTU]);
250     info->ifi_flags = nl_attr_get_u32(a[OVS_WIN_NETDEV_ATTR_IF_FLAGS]);
251
252     return 0;
253 }
254
255 static int
256 query_netdev(const char *devname,
257              struct netdev_windows_netdev_info *info,
258              struct ofpbuf **bufp)
259 {
260     int error = 0;
261     struct ofpbuf *request_buf;
262
263     ovs_assert(info != NULL);
264     netdev_windows_info_init(info);
265
266     error = netdev_windows_init_();
267     if (error) {
268         if (info) {
269             *bufp = NULL;
270             netdev_windows_info_init(info);
271         }
272         return error;
273     }
274
275     request_buf = ofpbuf_new(1024);
276     info->cmd = OVS_WIN_NETDEV_CMD_GET;
277     info->name = devname;
278     error = netdev_windows_netdev_to_ofpbuf(info, request_buf);
279     if (error) {
280         ofpbuf_delete(request_buf);
281         return error;
282     }
283
284     error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
285     ofpbuf_delete(request_buf);
286
287     if (info) {
288         if (!error) {
289             error = netdev_windows_netdev_from_ofpbuf(info, *bufp);
290         }
291         if (error) {
292             netdev_windows_info_init(info);
293             ofpbuf_delete(*bufp);
294             *bufp = NULL;
295         }
296     }
297
298     return 0;
299 }
300
301 static void
302 netdev_windows_destruct(struct netdev *netdev_)
303 {
304
305 }
306
307 static void
308 netdev_windows_dealloc(struct netdev *netdev_)
309 {
310     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
311     free(netdev);
312 }
313
314 static int
315 netdev_windows_get_etheraddr(const struct netdev *netdev_,
316                              struct eth_addr *mac)
317 {
318     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
319
320     ovs_assert((netdev->cache_valid & VALID_ETHERADDR) != 0);
321     if (netdev->cache_valid & VALID_ETHERADDR) {
322         *mac = netdev->mac;
323     } else {
324         return EINVAL;
325     }
326     return 0;
327 }
328
329 static int
330 netdev_windows_get_mtu(const struct netdev *netdev_, int *mtup)
331 {
332     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
333
334     ovs_assert((netdev->cache_valid & VALID_MTU) != 0);
335     if (netdev->cache_valid & VALID_MTU) {
336         *mtup = netdev->mtu;
337     } else {
338         return EINVAL;
339     }
340     return 0;
341 }
342
343 /* This functionality is not really required by the datapath.
344  * But vswitchd bringup expects this to be implemented. */
345 static int
346 netdev_windows_set_etheraddr(const struct netdev *netdev_,
347                              const struct eth_addr mac)
348 {
349     return 0;
350 }
351
352 /* This functionality is not really required by the datapath.
353  * But vswitchd bringup expects this to be implemented. */
354 static int
355 netdev_windows_update_flags(struct netdev *netdev_,
356                             enum netdev_flags off,
357                             enum netdev_flags on,
358                             enum netdev_flags *old_flagsp)
359 {
360     struct netdev_windows *netdev = netdev_windows_cast(netdev_);
361
362     ovs_assert((netdev->cache_valid & VALID_IFFLAG) != 0);
363     if (netdev->cache_valid & VALID_IFFLAG) {
364         *old_flagsp = netdev->ifi_flags;
365         /* Setting the interface flags is not supported. */
366     } else {
367         return EINVAL;
368     }
369     return 0;
370 }
371
372 /* Looks up in the ARP table entry for a given 'ip'. If it is found, the
373  * corresponding MAC address will be copied in 'mac' and return 0. If no
374  * matching entry is found or an error occurs it will log it and return ENXIO.
375  */
376 static int
377 netdev_windows_arp_lookup(const struct netdev *netdev,
378                           ovs_be32 ip, struct eth_addr *mac)
379 {
380     PMIB_IPNETTABLE arp_table = NULL;
381     /* The buffer length of all ARP entries */
382     uint32_t buffer_length = 0;
383     uint32_t ret_val = 0;
384     uint32_t counter = 0;
385
386     ret_val = GetIpNetTable(arp_table, &buffer_length, false);
387
388     if (ret_val != ERROR_INSUFFICIENT_BUFFER ) {
389         VLOG_ERR("Call to GetIpNetTable failed with error: %s",
390                  ovs_format_message(ret_val));
391         return ENXIO;
392     }
393
394     arp_table = (MIB_IPNETTABLE *) malloc(buffer_length);
395
396     if (arp_table == NULL) {
397         VLOG_ERR("Could not allocate memory for all the interfaces");
398         return ENXIO;
399     }
400
401     ret_val = GetIpNetTable(arp_table, &buffer_length, false);
402
403     if (ret_val == NO_ERROR) {
404         for (counter = 0; counter < arp_table->dwNumEntries; counter++) {
405             if (arp_table->table[counter].dwAddr == ip) {
406                 memcpy(mac, arp_table->table[counter].bPhysAddr, ETH_ADDR_LEN);
407
408                 free(arp_table);
409                 return 0;
410             }
411         }
412     } else {
413         VLOG_ERR("Call to GetIpNetTable failed with error: %s",
414                  ovs_format_message(ret_val));
415     }
416
417     free(arp_table);
418     return ENXIO;
419 }
420
421 static int
422 netdev_windows_get_next_hop(const struct in_addr *host,
423                             struct in_addr *next_hop,
424                             char **netdev_name)
425 {
426     uint32_t ret_val = 0;
427     /* The buffer length of all addresses */
428     uint32_t buffer_length = 1000;
429     PIP_ADAPTER_ADDRESSES all_addr = NULL;
430     PIP_ADAPTER_ADDRESSES cur_addr = NULL;
431
432     ret_val = GetAdaptersAddresses(AF_INET,
433                                    GAA_FLAG_INCLUDE_PREFIX |
434                                    GAA_FLAG_INCLUDE_GATEWAYS,
435                                    NULL, all_addr, &buffer_length);
436
437     if (ret_val != ERROR_INSUFFICIENT_BUFFER ) {
438         VLOG_ERR("Call to GetAdaptersAddresses failed with error: %s",
439                  ovs_format_message(ret_val));
440         return ENXIO;
441     }
442
443     all_addr = (IP_ADAPTER_ADDRESSES *) malloc(buffer_length);
444
445     if (all_addr == NULL) {
446         VLOG_ERR("Could not allocate memory for all the interfaces");
447         return ENXIO;
448     }
449
450     ret_val = GetAdaptersAddresses(AF_INET,
451                                    GAA_FLAG_INCLUDE_PREFIX |
452                                    GAA_FLAG_INCLUDE_GATEWAYS,
453                                    NULL, all_addr, &buffer_length);
454
455     if (ret_val == NO_ERROR) {
456         cur_addr = all_addr;
457         while (cur_addr) {
458             if(cur_addr->FirstGatewayAddress &&
459                cur_addr->FirstGatewayAddress->Address.lpSockaddr) {
460                 struct sockaddr_in *ipv4 = (struct sockaddr_in *)
461                                            cur_addr->FirstGatewayAddress->Address.lpSockaddr;
462                 next_hop->s_addr = ipv4->sin_addr.S_un.S_addr;
463                 *netdev_name = xstrdup((char *)cur_addr->FriendlyName);
464
465                 free(all_addr);
466
467                 return 0;
468             }
469
470             cur_addr = cur_addr->Next;
471         }
472     } else {
473         VLOG_ERR("Call to GetAdaptersAddresses failed with error: %s",
474                  ovs_format_message(ret_val));
475     }
476
477     if (all_addr) {
478         free(all_addr);
479     }
480     return ENXIO;
481 }
482
483 static int
484 netdev_windows_internal_construct(struct netdev *netdev_)
485 {
486     return netdev_windows_system_construct(netdev_);
487 }
488
489
490 #define NETDEV_WINDOWS_CLASS(NAME, CONSTRUCT)                           \
491 {                                                                       \
492     .type               = NAME,                                         \
493     .alloc              = netdev_windows_alloc,                         \
494     .construct          = CONSTRUCT,                                    \
495     .destruct           = netdev_windows_destruct,                      \
496     .dealloc            = netdev_windows_dealloc,                       \
497     .get_etheraddr      = netdev_windows_get_etheraddr,                 \
498     .set_etheraddr      = netdev_windows_set_etheraddr,                 \
499     .update_flags       = netdev_windows_update_flags,                  \
500     .get_next_hop       = netdev_windows_get_next_hop,                  \
501     .arp_lookup         = netdev_windows_arp_lookup,                    \
502 }
503
504 const struct netdev_class netdev_windows_class =
505     NETDEV_WINDOWS_CLASS(
506         "system",
507         netdev_windows_system_construct);
508
509 const struct netdev_class netdev_internal_class =
510     NETDEV_WINDOWS_CLASS(
511         "internal",
512         netdev_windows_internal_construct);