]> git.proxmox.com Git - mirror_ovs.git/blob - lib/dpif-netlink.c
dpif-netdev: Fix few comments.
[mirror_ovs.git] / lib / dpif-netlink.c
1 /*
2 * Copyright (c) 2008-2017 Nicira, 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 <config.h>
18
19 #include "dpif-netlink.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <net/if.h>
26 #include <linux/types.h>
27 #include <linux/pkt_sched.h>
28 #include <poll.h>
29 #include <stdlib.h>
30 #include <strings.h>
31 #include <sys/epoll.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "bitmap.h"
36 #include "dpif-provider.h"
37 #include "dpif-netlink-rtnl.h"
38 #include "openvswitch/dynamic-string.h"
39 #include "flow.h"
40 #include "fat-rwlock.h"
41 #include "netdev.h"
42 #include "netdev-provider.h"
43 #include "netdev-linux.h"
44 #include "netdev-vport.h"
45 #include "netlink-conntrack.h"
46 #include "netlink-notifier.h"
47 #include "netlink-socket.h"
48 #include "netlink.h"
49 #include "odp-util.h"
50 #include "openvswitch/ofpbuf.h"
51 #include "packets.h"
52 #include "poll-loop.h"
53 #include "random.h"
54 #include "openvswitch/shash.h"
55 #include "sset.h"
56 #include "timeval.h"
57 #include "unaligned.h"
58 #include "util.h"
59 #include "openvswitch/vlog.h"
60 #include "openvswitch/flow.h"
61
62 VLOG_DEFINE_THIS_MODULE(dpif_netlink);
63 #ifdef _WIN32
64 #include "wmi.h"
65 enum { WINDOWS = 1 };
66 #else
67 enum { WINDOWS = 0 };
68 #endif
69 enum { MAX_PORTS = USHRT_MAX };
70
71 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
72 * missing if we have old headers. */
73 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
74
75 #define FLOW_DUMP_MAX_BATCH 50
76 #define OPERATE_MAX_OPS 50
77
78 struct dpif_netlink_dp {
79 /* Generic Netlink header. */
80 uint8_t cmd;
81
82 /* struct ovs_header. */
83 int dp_ifindex;
84
85 /* Attributes. */
86 const char *name; /* OVS_DP_ATTR_NAME. */
87 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
88 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
89 const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
90 const struct ovs_dp_megaflow_stats *megaflow_stats;
91 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
92 };
93
94 static void dpif_netlink_dp_init(struct dpif_netlink_dp *);
95 static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *,
96 const struct ofpbuf *);
97 static void dpif_netlink_dp_dump_start(struct nl_dump *);
98 static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
99 struct dpif_netlink_dp *reply,
100 struct ofpbuf **bufp);
101 static int dpif_netlink_dp_get(const struct dpif *,
102 struct dpif_netlink_dp *reply,
103 struct ofpbuf **bufp);
104
105 struct dpif_netlink_flow {
106 /* Generic Netlink header. */
107 uint8_t cmd;
108
109 /* struct ovs_header. */
110 unsigned int nlmsg_flags;
111 int dp_ifindex;
112
113 /* Attributes.
114 *
115 * The 'stats' member points to 64-bit data that might only be aligned on
116 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
117 * values.
118 *
119 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
120 * the Netlink version of the command, even if actions_len is zero. */
121 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
122 size_t key_len;
123 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
124 size_t mask_len;
125 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
126 size_t actions_len;
127 ovs_u128 ufid; /* OVS_FLOW_ATTR_FLOW_ID. */
128 bool ufid_present; /* Is there a UFID? */
129 bool ufid_terse; /* Skip serializing key/mask/acts? */
130 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
131 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
132 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
133 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
134 bool probe; /* OVS_FLOW_ATTR_PROBE. */
135 };
136
137 static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
138 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
139 const struct ofpbuf *);
140 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
141 struct ofpbuf *);
142 static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
143 struct dpif_netlink_flow *reply,
144 struct ofpbuf **bufp);
145 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
146 struct dpif_flow_stats *);
147 static void dpif_netlink_flow_to_dpif_flow(struct dpif *, struct dpif_flow *,
148 const struct dpif_netlink_flow *);
149
150 /* One of the dpif channels between the kernel and userspace. */
151 struct dpif_channel {
152 struct nl_sock *sock; /* Netlink socket. */
153 long long int last_poll; /* Last time this channel was polled. */
154 };
155
156 #ifdef _WIN32
157 #define VPORT_SOCK_POOL_SIZE 1
158 /* On Windows, there is no native support for epoll. There are equivalent
159 * interfaces though, that are not used currently. For simpicity, a pool of
160 * netlink sockets is used. Each socket is represented by 'struct
161 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
162 * sharing the same socket. In the future, we can add a reference count and
163 * such fields. */
164 struct dpif_windows_vport_sock {
165 struct nl_sock *nl_sock; /* netlink socket. */
166 };
167 #endif
168
169 struct dpif_handler {
170 struct dpif_channel *channels;/* Array of channels for each handler. */
171 struct epoll_event *epoll_events;
172 int epoll_fd; /* epoll fd that includes channel socks. */
173 int n_events; /* Num events returned by epoll_wait(). */
174 int event_offset; /* Offset into 'epoll_events'. */
175
176 #ifdef _WIN32
177 /* Pool of sockets. */
178 struct dpif_windows_vport_sock *vport_sock_pool;
179 size_t last_used_pool_idx; /* Index to aid in allocating a
180 socket in the pool to a port. */
181 #endif
182 };
183
184 /* Datapath interface for the openvswitch Linux kernel module. */
185 struct dpif_netlink {
186 struct dpif dpif;
187 int dp_ifindex;
188
189 /* Upcall messages. */
190 struct fat_rwlock upcall_lock;
191 struct dpif_handler *handlers;
192 uint32_t n_handlers; /* Num of upcall handlers. */
193 int uc_array_size; /* Size of 'handler->channels' and */
194 /* 'handler->epoll_events'. */
195
196 /* Change notification. */
197 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
198 bool refresh_channels;
199 };
200
201 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
202 uint32_t ch_idx, uint32_t handler_id);
203
204 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
205
206 /* Generic Netlink family numbers for OVS.
207 *
208 * Initialized by dpif_netlink_init(). */
209 static int ovs_datapath_family;
210 static int ovs_vport_family;
211 static int ovs_flow_family;
212 static int ovs_packet_family;
213
214 /* Generic Netlink multicast groups for OVS.
215 *
216 * Initialized by dpif_netlink_init(). */
217 static unsigned int ovs_vport_mcgroup;
218
219 /* If true, tunnel devices are created using OVS compat/genetlink.
220 * If false, tunnel devices are created with rtnetlink and using light weight
221 * tunnels. If we fail to create the tunnel the rtnetlink+LWT, then we fallback
222 * to using the compat interface. */
223 static bool ovs_tunnels_out_of_tree = true;
224
225 static int dpif_netlink_init(void);
226 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
227 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
228 odp_port_t port_no, uint32_t hash);
229 static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
230 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
231 uint32_t n_handlers);
232 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
233 struct ofpbuf *);
234 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
235 const struct ofpbuf *);
236 static int dpif_netlink_port_query__(const struct dpif_netlink *dpif,
237 odp_port_t port_no, const char *port_name,
238 struct dpif_port *dpif_port);
239
240 static struct dpif_netlink *
241 dpif_netlink_cast(const struct dpif *dpif)
242 {
243 dpif_assert_class(dpif, &dpif_netlink_class);
244 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
245 }
246
247 static int
248 dpif_netlink_enumerate(struct sset *all_dps,
249 const struct dpif_class *dpif_class OVS_UNUSED)
250 {
251 struct nl_dump dump;
252 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
253 struct ofpbuf msg, buf;
254 int error;
255
256 error = dpif_netlink_init();
257 if (error) {
258 return error;
259 }
260
261 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
262 dpif_netlink_dp_dump_start(&dump);
263 while (nl_dump_next(&dump, &msg, &buf)) {
264 struct dpif_netlink_dp dp;
265
266 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
267 sset_add(all_dps, dp.name);
268 }
269 }
270 ofpbuf_uninit(&buf);
271 return nl_dump_done(&dump);
272 }
273
274 static int
275 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
276 bool create, struct dpif **dpifp)
277 {
278 struct dpif_netlink_dp dp_request, dp;
279 struct ofpbuf *buf;
280 uint32_t upcall_pid;
281 int error;
282
283 error = dpif_netlink_init();
284 if (error) {
285 return error;
286 }
287
288 /* Create or look up datapath. */
289 dpif_netlink_dp_init(&dp_request);
290 if (create) {
291 dp_request.cmd = OVS_DP_CMD_NEW;
292 upcall_pid = 0;
293 dp_request.upcall_pid = &upcall_pid;
294 } else {
295 /* Use OVS_DP_CMD_SET to report user features */
296 dp_request.cmd = OVS_DP_CMD_SET;
297 }
298 dp_request.name = name;
299 dp_request.user_features |= OVS_DP_F_UNALIGNED;
300 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
301 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
302 if (error) {
303 return error;
304 }
305
306 error = open_dpif(&dp, dpifp);
307 ofpbuf_delete(buf);
308 return error;
309 }
310
311 static int
312 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
313 {
314 struct dpif_netlink *dpif;
315
316 dpif = xzalloc(sizeof *dpif);
317 dpif->port_notifier = NULL;
318 fat_rwlock_init(&dpif->upcall_lock);
319
320 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
321 dp->dp_ifindex, dp->dp_ifindex);
322
323 dpif->dp_ifindex = dp->dp_ifindex;
324 *dpifp = &dpif->dpif;
325
326 return 0;
327 }
328
329 /* Destroys the netlink sockets pointed by the elements in 'socksp'
330 * and frees the 'socksp'. */
331 static void
332 vport_del_socksp__(struct nl_sock **socksp, uint32_t n_socks)
333 {
334 size_t i;
335
336 for (i = 0; i < n_socks; i++) {
337 nl_sock_destroy(socksp[i]);
338 }
339
340 free(socksp);
341 }
342
343 /* Creates an array of netlink sockets. Returns an array of the
344 * corresponding pointers. Records the error in 'error'. */
345 static struct nl_sock **
346 vport_create_socksp__(uint32_t n_socks, int *error)
347 {
348 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
349 size_t i;
350
351 for (i = 0; i < n_socks; i++) {
352 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
353 if (*error) {
354 goto error;
355 }
356 }
357
358 return socksp;
359
360 error:
361 vport_del_socksp__(socksp, n_socks);
362
363 return NULL;
364 }
365
366 #ifdef _WIN32
367 static void
368 vport_delete_sock_pool(struct dpif_handler *handler)
369 OVS_REQ_WRLOCK(dpif->upcall_lock)
370 {
371 if (handler->vport_sock_pool) {
372 uint32_t i;
373 struct dpif_windows_vport_sock *sock_pool =
374 handler->vport_sock_pool;
375
376 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
377 if (sock_pool[i].nl_sock) {
378 nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
379 nl_sock_destroy(sock_pool[i].nl_sock);
380 sock_pool[i].nl_sock = NULL;
381 }
382 }
383
384 free(handler->vport_sock_pool);
385 handler->vport_sock_pool = NULL;
386 }
387 }
388
389 static int
390 vport_create_sock_pool(struct dpif_handler *handler)
391 OVS_REQ_WRLOCK(dpif->upcall_lock)
392 {
393 struct dpif_windows_vport_sock *sock_pool;
394 size_t i;
395 int error = 0;
396
397 sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
398 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
399 error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
400 if (error) {
401 goto error;
402 }
403
404 /* Enable the netlink socket to receive packets. This is equivalent to
405 * calling nl_sock_join_mcgroup() to receive events. */
406 error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
407 if (error) {
408 goto error;
409 }
410 }
411
412 handler->vport_sock_pool = sock_pool;
413 handler->last_used_pool_idx = 0;
414 return 0;
415
416 error:
417 vport_delete_sock_pool(handler);
418 return error;
419 }
420
421 /* Returns an array pointers to netlink sockets. The sockets are picked from a
422 * pool. Records the error in 'error'. */
423 static struct nl_sock **
424 vport_create_socksp_windows(struct dpif_netlink *dpif, int *error)
425 OVS_REQ_WRLOCK(dpif->upcall_lock)
426 {
427 uint32_t n_socks = dpif->n_handlers;
428 struct nl_sock **socksp;
429 size_t i;
430
431 ovs_assert(n_socks <= 1);
432 socksp = xzalloc(n_socks * sizeof *socksp);
433
434 /* Pick netlink sockets to use in a round-robin fashion from each
435 * handler's pool of sockets. */
436 for (i = 0; i < n_socks; i++) {
437 struct dpif_handler *handler = &dpif->handlers[i];
438 struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
439 size_t index = handler->last_used_pool_idx;
440
441 /* A pool of sockets is allocated when the handler is initialized. */
442 if (sock_pool == NULL) {
443 free(socksp);
444 *error = EINVAL;
445 return NULL;
446 }
447
448 ovs_assert(index < VPORT_SOCK_POOL_SIZE);
449 socksp[i] = sock_pool[index].nl_sock;
450 socksp[i] = sock_pool[index].nl_sock;
451 ovs_assert(socksp[i]);
452 index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
453 handler->last_used_pool_idx = index;
454 }
455
456 return socksp;
457 }
458
459 static void
460 vport_del_socksp_windows(struct dpif_netlink *dpif, struct nl_sock **socksp)
461 {
462 free(socksp);
463 }
464 #endif /* _WIN32 */
465
466 static struct nl_sock **
467 vport_create_socksp(struct dpif_netlink *dpif, int *error)
468 {
469 #ifdef _WIN32
470 return vport_create_socksp_windows(dpif, error);
471 #else
472 return vport_create_socksp__(dpif->n_handlers, error);
473 #endif
474 }
475
476 static void
477 vport_del_socksp(struct dpif_netlink *dpif, struct nl_sock **socksp)
478 {
479 #ifdef _WIN32
480 vport_del_socksp_windows(dpif, socksp);
481 #else
482 vport_del_socksp__(socksp, dpif->n_handlers);
483 #endif
484 }
485
486 /* Given the array of pointers to netlink sockets 'socksp', returns
487 * the array of corresponding pids. If the 'socksp' is NULL, returns
488 * a single-element array of value 0. */
489 static uint32_t *
490 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
491 {
492 uint32_t *pids;
493
494 if (!socksp) {
495 pids = xzalloc(sizeof *pids);
496 } else {
497 size_t i;
498
499 pids = xzalloc(n_socks * sizeof *pids);
500 for (i = 0; i < n_socks; i++) {
501 pids[i] = nl_sock_pid(socksp[i]);
502 }
503 }
504
505 return pids;
506 }
507
508 /* Given the port number 'port_idx', extracts the pids of netlink sockets
509 * associated to the port and assigns it to 'upcall_pids'. */
510 static bool
511 vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
512 uint32_t **upcall_pids)
513 {
514 uint32_t *pids;
515 size_t i;
516
517 /* Since the nl_sock can only be assigned in either all
518 * or none "dpif->handlers" channels, the following check
519 * would suffice. */
520 if (!dpif->handlers[0].channels[port_idx].sock) {
521 return false;
522 }
523 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
524
525 pids = xzalloc(dpif->n_handlers * sizeof *pids);
526
527 for (i = 0; i < dpif->n_handlers; i++) {
528 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
529 }
530
531 *upcall_pids = pids;
532
533 return true;
534 }
535
536 static int
537 vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
538 struct nl_sock **socksp)
539 {
540 struct epoll_event event;
541 uint32_t port_idx = odp_to_u32(port_no);
542 size_t i, j;
543 int error;
544
545 if (dpif->handlers == NULL) {
546 return 0;
547 }
548
549 /* We assume that the datapath densely chooses port numbers, which can
550 * therefore be used as an index into 'channels' and 'epoll_events' of
551 * 'dpif->handler'. */
552 if (port_idx >= dpif->uc_array_size) {
553 uint32_t new_size = port_idx + 1;
554
555 if (new_size > MAX_PORTS) {
556 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
557 dpif_name(&dpif->dpif), port_no);
558 return EFBIG;
559 }
560
561 for (i = 0; i < dpif->n_handlers; i++) {
562 struct dpif_handler *handler = &dpif->handlers[i];
563
564 handler->channels = xrealloc(handler->channels,
565 new_size * sizeof *handler->channels);
566
567 for (j = dpif->uc_array_size; j < new_size; j++) {
568 handler->channels[j].sock = NULL;
569 }
570
571 handler->epoll_events = xrealloc(handler->epoll_events,
572 new_size * sizeof *handler->epoll_events);
573
574 }
575 dpif->uc_array_size = new_size;
576 }
577
578 memset(&event, 0, sizeof event);
579 event.events = EPOLLIN;
580 event.data.u32 = port_idx;
581
582 for (i = 0; i < dpif->n_handlers; i++) {
583 struct dpif_handler *handler = &dpif->handlers[i];
584
585 #ifndef _WIN32
586 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
587 &event) < 0) {
588 error = errno;
589 goto error;
590 }
591 #endif
592 dpif->handlers[i].channels[port_idx].sock = socksp[i];
593 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
594 }
595
596 return 0;
597
598 error:
599 for (j = 0; j < i; j++) {
600 #ifndef _WIN32
601 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
602 nl_sock_fd(socksp[j]), NULL);
603 #endif
604 dpif->handlers[j].channels[port_idx].sock = NULL;
605 }
606
607 return error;
608 }
609
610 static void
611 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
612 {
613 uint32_t port_idx = odp_to_u32(port_no);
614 size_t i;
615
616 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
617 return;
618 }
619
620 /* Since the sock can only be assigned in either all or none
621 * of "dpif->handlers" channels, the following check would
622 * suffice. */
623 if (!dpif->handlers[0].channels[port_idx].sock) {
624 return;
625 }
626
627 for (i = 0; i < dpif->n_handlers; i++) {
628 struct dpif_handler *handler = &dpif->handlers[i];
629 #ifndef _WIN32
630 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
631 nl_sock_fd(handler->channels[port_idx].sock), NULL);
632 nl_sock_destroy(handler->channels[port_idx].sock);
633 #endif
634 handler->channels[port_idx].sock = NULL;
635 handler->event_offset = handler->n_events = 0;
636 }
637 }
638
639 static void
640 destroy_all_channels(struct dpif_netlink *dpif)
641 OVS_REQ_WRLOCK(dpif->upcall_lock)
642 {
643 unsigned int i;
644
645 if (!dpif->handlers) {
646 return;
647 }
648
649 for (i = 0; i < dpif->uc_array_size; i++ ) {
650 struct dpif_netlink_vport vport_request;
651 uint32_t upcall_pids = 0;
652
653 /* Since the sock can only be assigned in either all or none
654 * of "dpif->handlers" channels, the following check would
655 * suffice. */
656 if (!dpif->handlers[0].channels[i].sock) {
657 continue;
658 }
659
660 /* Turn off upcalls. */
661 dpif_netlink_vport_init(&vport_request);
662 vport_request.cmd = OVS_VPORT_CMD_SET;
663 vport_request.dp_ifindex = dpif->dp_ifindex;
664 vport_request.port_no = u32_to_odp(i);
665 vport_request.n_upcall_pids = 1;
666 vport_request.upcall_pids = &upcall_pids;
667 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
668
669 vport_del_channels(dpif, u32_to_odp(i));
670 }
671
672 for (i = 0; i < dpif->n_handlers; i++) {
673 struct dpif_handler *handler = &dpif->handlers[i];
674
675 dpif_netlink_handler_uninit(handler);
676 free(handler->epoll_events);
677 free(handler->channels);
678 }
679
680 free(dpif->handlers);
681 dpif->handlers = NULL;
682 dpif->n_handlers = 0;
683 dpif->uc_array_size = 0;
684 }
685
686 static void
687 dpif_netlink_close(struct dpif *dpif_)
688 {
689 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
690
691 nl_sock_destroy(dpif->port_notifier);
692
693 fat_rwlock_wrlock(&dpif->upcall_lock);
694 destroy_all_channels(dpif);
695 fat_rwlock_unlock(&dpif->upcall_lock);
696
697 fat_rwlock_destroy(&dpif->upcall_lock);
698 free(dpif);
699 }
700
701 static int
702 dpif_netlink_destroy(struct dpif *dpif_)
703 {
704 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
705 struct dpif_netlink_dp dp;
706
707 dpif_netlink_dp_init(&dp);
708 dp.cmd = OVS_DP_CMD_DEL;
709 dp.dp_ifindex = dpif->dp_ifindex;
710 return dpif_netlink_dp_transact(&dp, NULL, NULL);
711 }
712
713 static bool
714 dpif_netlink_run(struct dpif *dpif_)
715 {
716 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
717
718 if (dpif->refresh_channels) {
719 dpif->refresh_channels = false;
720 fat_rwlock_wrlock(&dpif->upcall_lock);
721 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
722 fat_rwlock_unlock(&dpif->upcall_lock);
723 }
724 return false;
725 }
726
727 static int
728 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
729 {
730 struct dpif_netlink_dp dp;
731 struct ofpbuf *buf;
732 int error;
733
734 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
735 if (!error) {
736 memset(stats, 0, sizeof *stats);
737
738 if (dp.stats) {
739 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
740 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
741 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
742 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
743 }
744
745 if (dp.megaflow_stats) {
746 stats->n_masks = dp.megaflow_stats->n_masks;
747 stats->n_mask_hit = get_32aligned_u64(
748 &dp.megaflow_stats->n_mask_hit);
749 } else {
750 stats->n_masks = UINT32_MAX;
751 stats->n_mask_hit = UINT64_MAX;
752 }
753 ofpbuf_delete(buf);
754 }
755 return error;
756 }
757
758 static const char *
759 get_vport_type(const struct dpif_netlink_vport *vport)
760 {
761 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
762
763 switch (vport->type) {
764 case OVS_VPORT_TYPE_NETDEV: {
765 const char *type = netdev_get_type_from_name(vport->name);
766
767 return type ? type : "system";
768 }
769
770 case OVS_VPORT_TYPE_INTERNAL:
771 return "internal";
772
773 case OVS_VPORT_TYPE_GENEVE:
774 return "geneve";
775
776 case OVS_VPORT_TYPE_GRE:
777 return "gre";
778
779 case OVS_VPORT_TYPE_VXLAN:
780 return "vxlan";
781
782 case OVS_VPORT_TYPE_LISP:
783 return "lisp";
784
785 case OVS_VPORT_TYPE_STT:
786 return "stt";
787
788 case OVS_VPORT_TYPE_UNSPEC:
789 case __OVS_VPORT_TYPE_MAX:
790 break;
791 }
792
793 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
794 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
795 return "unknown";
796 }
797
798 enum ovs_vport_type
799 netdev_to_ovs_vport_type(const char *type)
800 {
801 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
802 return OVS_VPORT_TYPE_NETDEV;
803 } else if (!strcmp(type, "internal")) {
804 return OVS_VPORT_TYPE_INTERNAL;
805 } else if (strstr(type, "stt")) {
806 return OVS_VPORT_TYPE_STT;
807 } else if (!strcmp(type, "geneve")) {
808 return OVS_VPORT_TYPE_GENEVE;
809 } else if (strstr(type, "gre")) {
810 return OVS_VPORT_TYPE_GRE;
811 } else if (!strcmp(type, "vxlan")) {
812 return OVS_VPORT_TYPE_VXLAN;
813 } else if (!strcmp(type, "lisp")) {
814 return OVS_VPORT_TYPE_LISP;
815 } else {
816 return OVS_VPORT_TYPE_UNSPEC;
817 }
818 }
819
820 static int
821 dpif_netlink_port_add__(struct dpif_netlink *dpif, const char *name,
822 enum ovs_vport_type type,
823 struct ofpbuf *options,
824 odp_port_t *port_nop)
825 OVS_REQ_WRLOCK(dpif->upcall_lock)
826 {
827 struct dpif_netlink_vport request, reply;
828 struct ofpbuf *buf;
829 struct nl_sock **socksp = NULL;
830 uint32_t *upcall_pids;
831 int error = 0;
832
833 if (dpif->handlers) {
834 socksp = vport_create_socksp(dpif, &error);
835 if (!socksp) {
836 return error;
837 }
838 }
839
840 dpif_netlink_vport_init(&request);
841 request.cmd = OVS_VPORT_CMD_NEW;
842 request.dp_ifindex = dpif->dp_ifindex;
843 request.type = type;
844 request.name = name;
845
846 request.port_no = *port_nop;
847 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
848 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
849 request.upcall_pids = upcall_pids;
850
851 if (options) {
852 request.options = options->data;
853 request.options_len = options->size;
854 }
855
856 error = dpif_netlink_vport_transact(&request, &reply, &buf);
857 if (!error) {
858 *port_nop = reply.port_no;
859 } else {
860 if (error == EBUSY && *port_nop != ODPP_NONE) {
861 VLOG_INFO("%s: requested port %"PRIu32" is in use",
862 dpif_name(&dpif->dpif), *port_nop);
863 }
864
865 vport_del_socksp(dpif, socksp);
866 goto exit;
867 }
868
869 if (socksp) {
870 error = vport_add_channels(dpif, *port_nop, socksp);
871 if (error) {
872 VLOG_INFO("%s: could not add channel for port %s",
873 dpif_name(&dpif->dpif), name);
874
875 /* Delete the port. */
876 dpif_netlink_vport_init(&request);
877 request.cmd = OVS_VPORT_CMD_DEL;
878 request.dp_ifindex = dpif->dp_ifindex;
879 request.port_no = *port_nop;
880 dpif_netlink_vport_transact(&request, NULL, NULL);
881 vport_del_socksp(dpif, socksp);
882 goto exit;
883 }
884 }
885 free(socksp);
886
887 exit:
888 ofpbuf_delete(buf);
889 free(upcall_pids);
890
891 return error;
892 }
893
894 static int
895 dpif_netlink_port_add_compat(struct dpif_netlink *dpif, struct netdev *netdev,
896 odp_port_t *port_nop)
897 OVS_REQ_WRLOCK(dpif->upcall_lock)
898 {
899 const struct netdev_tunnel_config *tnl_cfg;
900 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
901 const char *type = netdev_get_type(netdev);
902 uint64_t options_stub[64 / 8];
903 enum ovs_vport_type ovs_type;
904 struct ofpbuf options;
905 const char *name;
906
907 name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
908
909 ovs_type = netdev_to_ovs_vport_type(netdev_get_type(netdev));
910 if (ovs_type == OVS_VPORT_TYPE_UNSPEC) {
911 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
912 "unsupported type `%s'",
913 dpif_name(&dpif->dpif), name, type);
914 return EINVAL;
915 }
916
917 if (ovs_type == OVS_VPORT_TYPE_NETDEV) {
918 #ifdef _WIN32
919 /* XXX : Map appropiate Windows handle */
920 #else
921 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
922 #endif
923 }
924
925 #ifdef _WIN32
926 if (ovs_type == OVS_VPORT_TYPE_INTERNAL) {
927 if (!create_wmi_port(name)){
928 VLOG_ERR("Could not create wmi internal port with name:%s", name);
929 return EINVAL;
930 };
931 }
932 #endif
933
934 tnl_cfg = netdev_get_tunnel_config(netdev);
935 if (tnl_cfg && (tnl_cfg->dst_port != 0 || tnl_cfg->exts)) {
936 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
937 if (tnl_cfg->dst_port) {
938 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
939 ntohs(tnl_cfg->dst_port));
940 }
941 if (tnl_cfg->exts) {
942 size_t ext_ofs;
943 int i;
944
945 ext_ofs = nl_msg_start_nested(&options, OVS_TUNNEL_ATTR_EXTENSION);
946 for (i = 0; i < 32; i++) {
947 if (tnl_cfg->exts & (1 << i)) {
948 nl_msg_put_flag(&options, i);
949 }
950 }
951 nl_msg_end_nested(&options, ext_ofs);
952 }
953 return dpif_netlink_port_add__(dpif, name, ovs_type, &options,
954 port_nop);
955 } else {
956 return dpif_netlink_port_add__(dpif, name, ovs_type, NULL, port_nop);
957 }
958
959 }
960
961 static int
962 dpif_netlink_rtnl_port_create_and_add(struct dpif_netlink *dpif,
963 struct netdev *netdev,
964 odp_port_t *port_nop)
965 OVS_REQ_WRLOCK(dpif->upcall_lock)
966 {
967 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
968 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
969 const char *name;
970 int error;
971
972 error = dpif_netlink_rtnl_port_create(netdev);
973 if (error) {
974 if (error != EOPNOTSUPP) {
975 VLOG_INFO_RL(&rl, "Failed to create %s with rtnetlink: %s",
976 netdev_get_name(netdev), ovs_strerror(error));
977 }
978 return error;
979 }
980
981 name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
982 error = dpif_netlink_port_add__(dpif, name, OVS_VPORT_TYPE_NETDEV, NULL,
983 port_nop);
984 if (error) {
985 dpif_netlink_rtnl_port_destroy(name, netdev_get_type(netdev));
986 }
987 return error;
988 }
989
990 static int
991 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
992 odp_port_t *port_nop)
993 {
994 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
995 int error = EOPNOTSUPP;
996
997 fat_rwlock_wrlock(&dpif->upcall_lock);
998 if (!ovs_tunnels_out_of_tree) {
999 error = dpif_netlink_rtnl_port_create_and_add(dpif, netdev, port_nop);
1000 }
1001 if (error) {
1002 error = dpif_netlink_port_add_compat(dpif, netdev, port_nop);
1003 }
1004 fat_rwlock_unlock(&dpif->upcall_lock);
1005
1006 return error;
1007 }
1008
1009 static int
1010 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
1011 OVS_REQ_WRLOCK(dpif->upcall_lock)
1012 {
1013 struct dpif_netlink_vport vport;
1014 struct dpif_port dpif_port;
1015 int error;
1016
1017 error = dpif_netlink_port_query__(dpif, port_no, NULL, &dpif_port);
1018 if (error) {
1019 return error;
1020 }
1021
1022 dpif_netlink_vport_init(&vport);
1023 vport.cmd = OVS_VPORT_CMD_DEL;
1024 vport.dp_ifindex = dpif->dp_ifindex;
1025 vport.port_no = port_no;
1026 #ifdef _WIN32
1027 if (!strcmp(dpif_port.type, "internal")) {
1028 if (!delete_wmi_port(dpif_port.name)) {
1029 VLOG_ERR("Could not delete wmi port with name: %s",
1030 dpif_port.name);
1031 };
1032 }
1033 #endif
1034 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
1035
1036 vport_del_channels(dpif, port_no);
1037
1038 if (!error && !ovs_tunnels_out_of_tree) {
1039 error = dpif_netlink_rtnl_port_destroy(dpif_port.name, dpif_port.type);
1040 if (error == EOPNOTSUPP) {
1041 error = 0;
1042 }
1043 }
1044
1045 dpif_port_destroy(&dpif_port);
1046
1047 return error;
1048 }
1049
1050 static int
1051 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
1052 {
1053 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1054 int error;
1055
1056 fat_rwlock_wrlock(&dpif->upcall_lock);
1057 error = dpif_netlink_port_del__(dpif, port_no);
1058 fat_rwlock_unlock(&dpif->upcall_lock);
1059
1060 return error;
1061 }
1062
1063 static int
1064 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
1065 const char *port_name, struct dpif_port *dpif_port)
1066 {
1067 struct dpif_netlink_vport request;
1068 struct dpif_netlink_vport reply;
1069 struct ofpbuf *buf;
1070 int error;
1071
1072 dpif_netlink_vport_init(&request);
1073 request.cmd = OVS_VPORT_CMD_GET;
1074 request.dp_ifindex = dpif->dp_ifindex;
1075 request.port_no = port_no;
1076 request.name = port_name;
1077
1078 error = dpif_netlink_vport_transact(&request, &reply, &buf);
1079 if (!error) {
1080 if (reply.dp_ifindex != request.dp_ifindex) {
1081 /* A query by name reported that 'port_name' is in some datapath
1082 * other than 'dpif', but the caller wants to know about 'dpif'. */
1083 error = ENODEV;
1084 } else if (dpif_port) {
1085 dpif_port->name = xstrdup(reply.name);
1086 dpif_port->type = xstrdup(get_vport_type(&reply));
1087 dpif_port->port_no = reply.port_no;
1088 }
1089 ofpbuf_delete(buf);
1090 }
1091 return error;
1092 }
1093
1094 static int
1095 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
1096 struct dpif_port *dpif_port)
1097 {
1098 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1099
1100 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
1101 }
1102
1103 static int
1104 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
1105 struct dpif_port *dpif_port)
1106 {
1107 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1108
1109 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
1110 }
1111
1112 static uint32_t
1113 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
1114 odp_port_t port_no, uint32_t hash)
1115 OVS_REQ_RDLOCK(dpif->upcall_lock)
1116 {
1117 uint32_t port_idx = odp_to_u32(port_no);
1118 uint32_t pid = 0;
1119
1120 if (dpif->handlers && dpif->uc_array_size > 0) {
1121 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1122 * channel, since it is not heavily loaded. */
1123 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
1124 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
1125
1126 /* Needs to check in case the socket pointer is changed in between
1127 * the holding of upcall_lock. A known case happens when the main
1128 * thread deletes the vport while the handler thread is handling
1129 * the upcall from that port. */
1130 if (h->channels[idx].sock) {
1131 pid = nl_sock_pid(h->channels[idx].sock);
1132 }
1133 }
1134
1135 return pid;
1136 }
1137
1138 static uint32_t
1139 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
1140 uint32_t hash)
1141 {
1142 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1143 uint32_t ret;
1144
1145 fat_rwlock_rdlock(&dpif->upcall_lock);
1146 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
1147 fat_rwlock_unlock(&dpif->upcall_lock);
1148
1149 return ret;
1150 }
1151
1152 static int
1153 dpif_netlink_flow_flush(struct dpif *dpif_)
1154 {
1155 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1156 struct dpif_netlink_flow flow;
1157
1158 dpif_netlink_flow_init(&flow);
1159 flow.cmd = OVS_FLOW_CMD_DEL;
1160 flow.dp_ifindex = dpif->dp_ifindex;
1161
1162 if (netdev_is_flow_api_enabled()) {
1163 netdev_ports_flow_flush(DPIF_HMAP_KEY(dpif_));
1164 }
1165
1166 return dpif_netlink_flow_transact(&flow, NULL, NULL);
1167 }
1168
1169 struct dpif_netlink_port_state {
1170 struct nl_dump dump;
1171 struct ofpbuf buf;
1172 };
1173
1174 static void
1175 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
1176 struct nl_dump *dump)
1177 {
1178 struct dpif_netlink_vport request;
1179 struct ofpbuf *buf;
1180
1181 dpif_netlink_vport_init(&request);
1182 request.cmd = OVS_VPORT_CMD_GET;
1183 request.dp_ifindex = dpif->dp_ifindex;
1184
1185 buf = ofpbuf_new(1024);
1186 dpif_netlink_vport_to_ofpbuf(&request, buf);
1187 nl_dump_start(dump, NETLINK_GENERIC, buf);
1188 ofpbuf_delete(buf);
1189 }
1190
1191 static int
1192 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
1193 {
1194 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1195 struct dpif_netlink_port_state *state;
1196
1197 *statep = state = xmalloc(sizeof *state);
1198 dpif_netlink_port_dump_start__(dpif, &state->dump);
1199
1200 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
1201 return 0;
1202 }
1203
1204 static int
1205 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
1206 struct nl_dump *dump,
1207 struct dpif_netlink_vport *vport,
1208 struct ofpbuf *buffer)
1209 {
1210 struct ofpbuf buf;
1211 int error;
1212
1213 if (!nl_dump_next(dump, &buf, buffer)) {
1214 return EOF;
1215 }
1216
1217 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
1218 if (error) {
1219 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
1220 dpif_name(&dpif->dpif), ovs_strerror(error));
1221 }
1222 return error;
1223 }
1224
1225 static int
1226 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
1227 struct dpif_port *dpif_port)
1228 {
1229 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1230 struct dpif_netlink_port_state *state = state_;
1231 struct dpif_netlink_vport vport;
1232 int error;
1233
1234 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
1235 &state->buf);
1236 if (error) {
1237 return error;
1238 }
1239 dpif_port->name = CONST_CAST(char *, vport.name);
1240 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
1241 dpif_port->port_no = vport.port_no;
1242 return 0;
1243 }
1244
1245 static int
1246 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
1247 {
1248 struct dpif_netlink_port_state *state = state_;
1249 int error = nl_dump_done(&state->dump);
1250
1251 ofpbuf_uninit(&state->buf);
1252 free(state);
1253 return error;
1254 }
1255
1256 static int
1257 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1258 {
1259 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1260
1261 /* Lazily create the Netlink socket to listen for notifications. */
1262 if (!dpif->port_notifier) {
1263 struct nl_sock *sock;
1264 int error;
1265
1266 error = nl_sock_create(NETLINK_GENERIC, &sock);
1267 if (error) {
1268 return error;
1269 }
1270
1271 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1272 if (error) {
1273 nl_sock_destroy(sock);
1274 return error;
1275 }
1276 dpif->port_notifier = sock;
1277
1278 /* We have no idea of the current state so report that everything
1279 * changed. */
1280 return ENOBUFS;
1281 }
1282
1283 for (;;) {
1284 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1285 uint64_t buf_stub[4096 / 8];
1286 struct ofpbuf buf;
1287 int error;
1288
1289 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1290 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1291 if (!error) {
1292 struct dpif_netlink_vport vport;
1293
1294 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1295 if (!error) {
1296 if (vport.dp_ifindex == dpif->dp_ifindex
1297 && (vport.cmd == OVS_VPORT_CMD_NEW
1298 || vport.cmd == OVS_VPORT_CMD_DEL
1299 || vport.cmd == OVS_VPORT_CMD_SET)) {
1300 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1301 dpif->dpif.full_name, vport.name, vport.cmd);
1302 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1303 dpif->refresh_channels = true;
1304 }
1305 *devnamep = xstrdup(vport.name);
1306 ofpbuf_uninit(&buf);
1307 return 0;
1308 }
1309 }
1310 } else if (error != EAGAIN) {
1311 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1312 ovs_strerror(error));
1313 nl_sock_drain(dpif->port_notifier);
1314 error = ENOBUFS;
1315 }
1316
1317 ofpbuf_uninit(&buf);
1318 if (error) {
1319 return error;
1320 }
1321 }
1322 }
1323
1324 static void
1325 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1326 {
1327 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1328
1329 if (dpif->port_notifier) {
1330 nl_sock_wait(dpif->port_notifier, POLLIN);
1331 } else {
1332 poll_immediate_wake();
1333 }
1334 }
1335
1336 static void
1337 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow *request,
1338 const ovs_u128 *ufid, bool terse)
1339 {
1340 if (ufid) {
1341 request->ufid = *ufid;
1342 request->ufid_present = true;
1343 } else {
1344 request->ufid_present = false;
1345 }
1346 request->ufid_terse = terse;
1347 }
1348
1349 static void
1350 dpif_netlink_init_flow_get__(const struct dpif_netlink *dpif,
1351 const struct nlattr *key, size_t key_len,
1352 const ovs_u128 *ufid, bool terse,
1353 struct dpif_netlink_flow *request)
1354 {
1355 dpif_netlink_flow_init(request);
1356 request->cmd = OVS_FLOW_CMD_GET;
1357 request->dp_ifindex = dpif->dp_ifindex;
1358 request->key = key;
1359 request->key_len = key_len;
1360 dpif_netlink_flow_init_ufid(request, ufid, terse);
1361 }
1362
1363 static void
1364 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1365 const struct dpif_flow_get *get,
1366 struct dpif_netlink_flow *request)
1367 {
1368 dpif_netlink_init_flow_get__(dpif, get->key, get->key_len, get->ufid,
1369 false, request);
1370 }
1371
1372 static int
1373 dpif_netlink_flow_get__(const struct dpif_netlink *dpif,
1374 const struct nlattr *key, size_t key_len,
1375 const ovs_u128 *ufid, bool terse,
1376 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1377 {
1378 struct dpif_netlink_flow request;
1379
1380 dpif_netlink_init_flow_get__(dpif, key, key_len, ufid, terse, &request);
1381 return dpif_netlink_flow_transact(&request, reply, bufp);
1382 }
1383
1384 static int
1385 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1386 const struct dpif_netlink_flow *flow,
1387 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1388 {
1389 return dpif_netlink_flow_get__(dpif, flow->key, flow->key_len,
1390 flow->ufid_present ? &flow->ufid : NULL,
1391 false, reply, bufp);
1392 }
1393
1394 static void
1395 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1396 const struct dpif_flow_put *put,
1397 struct dpif_netlink_flow *request)
1398 {
1399 static const struct nlattr dummy_action;
1400
1401 dpif_netlink_flow_init(request);
1402 request->cmd = (put->flags & DPIF_FP_CREATE
1403 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1404 request->dp_ifindex = dpif->dp_ifindex;
1405 request->key = put->key;
1406 request->key_len = put->key_len;
1407 request->mask = put->mask;
1408 request->mask_len = put->mask_len;
1409 dpif_netlink_flow_init_ufid(request, put->ufid, false);
1410
1411 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1412 request->actions = (put->actions
1413 ? put->actions
1414 : CONST_CAST(struct nlattr *, &dummy_action));
1415 request->actions_len = put->actions_len;
1416 if (put->flags & DPIF_FP_ZERO_STATS) {
1417 request->clear = true;
1418 }
1419 if (put->flags & DPIF_FP_PROBE) {
1420 request->probe = true;
1421 }
1422 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1423 }
1424
1425 static void
1426 dpif_netlink_init_flow_del__(struct dpif_netlink *dpif,
1427 const struct nlattr *key, size_t key_len,
1428 const ovs_u128 *ufid, bool terse,
1429 struct dpif_netlink_flow *request)
1430 {
1431 dpif_netlink_flow_init(request);
1432 request->cmd = OVS_FLOW_CMD_DEL;
1433 request->dp_ifindex = dpif->dp_ifindex;
1434 request->key = key;
1435 request->key_len = key_len;
1436 dpif_netlink_flow_init_ufid(request, ufid, terse);
1437 }
1438
1439 static void
1440 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1441 const struct dpif_flow_del *del,
1442 struct dpif_netlink_flow *request)
1443 {
1444 dpif_netlink_init_flow_del__(dpif, del->key, del->key_len,
1445 del->ufid, del->terse, request);
1446 }
1447
1448 enum {
1449 DUMP_OVS_FLOWS_BIT = 0,
1450 DUMP_OFFLOADED_FLOWS_BIT = 1,
1451 };
1452
1453 enum {
1454 DUMP_OVS_FLOWS = (1 << DUMP_OVS_FLOWS_BIT),
1455 DUMP_OFFLOADED_FLOWS = (1 << DUMP_OFFLOADED_FLOWS_BIT),
1456 };
1457
1458 struct dpif_netlink_flow_dump {
1459 struct dpif_flow_dump up;
1460 struct nl_dump nl_dump;
1461 atomic_int status;
1462 struct netdev_flow_dump **netdev_dumps;
1463 int netdev_dumps_num; /* Number of netdev_flow_dumps */
1464 struct ovs_mutex netdev_lock; /* Guards the following. */
1465 int netdev_current_dump OVS_GUARDED; /* Shared current dump */
1466 int type; /* Type of dump */
1467 };
1468
1469 static struct dpif_netlink_flow_dump *
1470 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1471 {
1472 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1473 }
1474
1475 static void
1476 start_netdev_dump(const struct dpif *dpif_,
1477 struct dpif_netlink_flow_dump *dump)
1478 {
1479 ovs_mutex_init(&dump->netdev_lock);
1480
1481 if (!(dump->type & DUMP_OFFLOADED_FLOWS)) {
1482 dump->netdev_dumps_num = 0;
1483 dump->netdev_dumps = NULL;
1484 return;
1485 }
1486
1487 ovs_mutex_lock(&dump->netdev_lock);
1488 dump->netdev_current_dump = 0;
1489 dump->netdev_dumps
1490 = netdev_ports_flow_dump_create(DPIF_HMAP_KEY(dpif_),
1491 &dump->netdev_dumps_num);
1492 ovs_mutex_unlock(&dump->netdev_lock);
1493 }
1494
1495 static int
1496 dpif_netlink_get_dump_type(char *str) {
1497 int type = 0;
1498
1499 if (!str || !strcmp(str, "ovs") || !strcmp(str, "dpctl")) {
1500 type |= DUMP_OVS_FLOWS;
1501 }
1502 if ((netdev_is_flow_api_enabled() && !str)
1503 || (str && (!strcmp(str, "offloaded") || !strcmp(str, "dpctl")))) {
1504 type |= DUMP_OFFLOADED_FLOWS;
1505 }
1506
1507 return type;
1508 }
1509
1510 static struct dpif_flow_dump *
1511 dpif_netlink_flow_dump_create(const struct dpif *dpif_, bool terse,
1512 char *type)
1513 {
1514 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1515 struct dpif_netlink_flow_dump *dump;
1516 struct dpif_netlink_flow request;
1517 struct ofpbuf *buf;
1518
1519 dump = xmalloc(sizeof *dump);
1520 dpif_flow_dump_init(&dump->up, dpif_);
1521
1522 dump->type = dpif_netlink_get_dump_type(type);
1523
1524 if (dump->type & DUMP_OVS_FLOWS) {
1525 dpif_netlink_flow_init(&request);
1526 request.cmd = OVS_FLOW_CMD_GET;
1527 request.dp_ifindex = dpif->dp_ifindex;
1528 request.ufid_present = false;
1529 request.ufid_terse = terse;
1530
1531 buf = ofpbuf_new(1024);
1532 dpif_netlink_flow_to_ofpbuf(&request, buf);
1533 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1534 ofpbuf_delete(buf);
1535 }
1536 atomic_init(&dump->status, 0);
1537 dump->up.terse = terse;
1538
1539 start_netdev_dump(dpif_, dump);
1540
1541 return &dump->up;
1542 }
1543
1544 static int
1545 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1546 {
1547 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1548 unsigned int nl_status = 0;
1549 int dump_status;
1550
1551 if (dump->type & DUMP_OVS_FLOWS) {
1552 nl_status = nl_dump_done(&dump->nl_dump);
1553 }
1554
1555 for (int i = 0; i < dump->netdev_dumps_num; i++) {
1556 int err = netdev_flow_dump_destroy(dump->netdev_dumps[i]);
1557
1558 if (err != 0 && err != EOPNOTSUPP) {
1559 VLOG_ERR("failed dumping netdev: %s", ovs_strerror(err));
1560 }
1561 }
1562
1563 free(dump->netdev_dumps);
1564 ovs_mutex_destroy(&dump->netdev_lock);
1565
1566 /* No other thread has access to 'dump' at this point. */
1567 atomic_read_relaxed(&dump->status, &dump_status);
1568 free(dump);
1569 return dump_status ? dump_status : nl_status;
1570 }
1571
1572 struct dpif_netlink_flow_dump_thread {
1573 struct dpif_flow_dump_thread up;
1574 struct dpif_netlink_flow_dump *dump;
1575 struct dpif_netlink_flow flow;
1576 struct dpif_flow_stats stats;
1577 struct ofpbuf nl_flows; /* Always used to store flows. */
1578 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1579 int netdev_dump_idx; /* This thread current netdev dump index */
1580 bool netdev_done; /* If we are finished dumping netdevs */
1581
1582 /* (Key/Mask/Actions) Buffers for netdev dumping */
1583 struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH];
1584 struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH];
1585 struct odputil_keybuf actbuf[FLOW_DUMP_MAX_BATCH];
1586 };
1587
1588 static struct dpif_netlink_flow_dump_thread *
1589 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1590 {
1591 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1592 }
1593
1594 static struct dpif_flow_dump_thread *
1595 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1596 {
1597 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1598 struct dpif_netlink_flow_dump_thread *thread;
1599
1600 thread = xmalloc(sizeof *thread);
1601 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1602 thread->dump = dump;
1603 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1604 thread->nl_actions = NULL;
1605 thread->netdev_dump_idx = 0;
1606 thread->netdev_done = !(thread->netdev_dump_idx < dump->netdev_dumps_num);
1607
1608 return &thread->up;
1609 }
1610
1611 static void
1612 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1613 {
1614 struct dpif_netlink_flow_dump_thread *thread
1615 = dpif_netlink_flow_dump_thread_cast(thread_);
1616
1617 ofpbuf_uninit(&thread->nl_flows);
1618 ofpbuf_delete(thread->nl_actions);
1619 free(thread);
1620 }
1621
1622 static void
1623 dpif_netlink_flow_to_dpif_flow(struct dpif *dpif, struct dpif_flow *dpif_flow,
1624 const struct dpif_netlink_flow *datapath_flow)
1625 {
1626 dpif_flow->key = datapath_flow->key;
1627 dpif_flow->key_len = datapath_flow->key_len;
1628 dpif_flow->mask = datapath_flow->mask;
1629 dpif_flow->mask_len = datapath_flow->mask_len;
1630 dpif_flow->actions = datapath_flow->actions;
1631 dpif_flow->actions_len = datapath_flow->actions_len;
1632 dpif_flow->ufid_present = datapath_flow->ufid_present;
1633 dpif_flow->pmd_id = PMD_ID_NULL;
1634 if (datapath_flow->ufid_present) {
1635 dpif_flow->ufid = datapath_flow->ufid;
1636 } else {
1637 ovs_assert(datapath_flow->key && datapath_flow->key_len);
1638 dpif_flow_hash(dpif, datapath_flow->key, datapath_flow->key_len,
1639 &dpif_flow->ufid);
1640 }
1641 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1642 dpif_flow->offloaded = false;
1643 }
1644
1645 /* The design is such that all threads are working together on the first dump
1646 * to the last, in order (at first they all on dump 0).
1647 * When the first thread finds that the given dump is finished,
1648 * they all move to the next. If two or more threads find the same dump
1649 * is finished at the same time, the first one will advance the shared
1650 * netdev_current_dump and the others will catch up. */
1651 static void
1652 dpif_netlink_advance_netdev_dump(struct dpif_netlink_flow_dump_thread *thread)
1653 {
1654 struct dpif_netlink_flow_dump *dump = thread->dump;
1655
1656 ovs_mutex_lock(&dump->netdev_lock);
1657 /* if we haven't finished (dumped everything) */
1658 if (dump->netdev_current_dump < dump->netdev_dumps_num) {
1659 /* if we are the first to find that current dump is finished
1660 * advance it. */
1661 if (thread->netdev_dump_idx == dump->netdev_current_dump) {
1662 thread->netdev_dump_idx = ++dump->netdev_current_dump;
1663 /* did we just finish the last dump? done. */
1664 if (dump->netdev_current_dump == dump->netdev_dumps_num) {
1665 thread->netdev_done = true;
1666 }
1667 } else {
1668 /* otherwise, we are behind, catch up */
1669 thread->netdev_dump_idx = dump->netdev_current_dump;
1670 }
1671 } else {
1672 /* some other thread finished */
1673 thread->netdev_done = true;
1674 }
1675 ovs_mutex_unlock(&dump->netdev_lock);
1676 }
1677
1678 static int
1679 dpif_netlink_netdev_match_to_dpif_flow(struct match *match,
1680 struct ofpbuf *key_buf,
1681 struct ofpbuf *mask_buf,
1682 struct nlattr *actions,
1683 struct dpif_flow_stats *stats,
1684 ovs_u128 *ufid,
1685 struct dpif_flow *flow,
1686 bool terse OVS_UNUSED)
1687 {
1688
1689 struct odp_flow_key_parms odp_parms = {
1690 .flow = &match->flow,
1691 .mask = &match->wc.masks,
1692 .support = {
1693 .max_vlan_headers = 1,
1694 },
1695 };
1696 size_t offset;
1697
1698 memset(flow, 0, sizeof *flow);
1699
1700 /* Key */
1701 offset = key_buf->size;
1702 flow->key = ofpbuf_tail(key_buf);
1703 odp_flow_key_from_flow(&odp_parms, key_buf);
1704 flow->key_len = key_buf->size - offset;
1705
1706 /* Mask */
1707 offset = mask_buf->size;
1708 flow->mask = ofpbuf_tail(mask_buf);
1709 odp_parms.key_buf = key_buf;
1710 odp_flow_key_from_mask(&odp_parms, mask_buf);
1711 flow->mask_len = mask_buf->size - offset;
1712
1713 /* Actions */
1714 flow->actions = nl_attr_get(actions);
1715 flow->actions_len = nl_attr_get_size(actions);
1716
1717 /* Stats */
1718 memcpy(&flow->stats, stats, sizeof *stats);
1719
1720 /* UFID */
1721 flow->ufid_present = true;
1722 flow->ufid = *ufid;
1723
1724 flow->pmd_id = PMD_ID_NULL;
1725
1726 flow->offloaded = true;
1727
1728 return 0;
1729 }
1730
1731 static int
1732 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1733 struct dpif_flow *flows, int max_flows)
1734 {
1735 struct dpif_netlink_flow_dump_thread *thread
1736 = dpif_netlink_flow_dump_thread_cast(thread_);
1737 struct dpif_netlink_flow_dump *dump = thread->dump;
1738 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1739 int n_flows;
1740
1741 ofpbuf_delete(thread->nl_actions);
1742 thread->nl_actions = NULL;
1743
1744 n_flows = 0;
1745 max_flows = MIN(max_flows, FLOW_DUMP_MAX_BATCH);
1746
1747 while (!thread->netdev_done && n_flows < max_flows) {
1748 struct odputil_keybuf *maskbuf = &thread->maskbuf[n_flows];
1749 struct odputil_keybuf *keybuf = &thread->keybuf[n_flows];
1750 struct odputil_keybuf *actbuf = &thread->actbuf[n_flows];
1751 struct ofpbuf key, mask, act;
1752 struct dpif_flow *f = &flows[n_flows];
1753 int cur = thread->netdev_dump_idx;
1754 struct netdev_flow_dump *netdev_dump = dump->netdev_dumps[cur];
1755 struct match match;
1756 struct nlattr *actions;
1757 struct dpif_flow_stats stats;
1758 ovs_u128 ufid;
1759 bool has_next;
1760
1761 ofpbuf_use_stack(&key, keybuf, sizeof *keybuf);
1762 ofpbuf_use_stack(&act, actbuf, sizeof *actbuf);
1763 ofpbuf_use_stack(&mask, maskbuf, sizeof *maskbuf);
1764 has_next = netdev_flow_dump_next(netdev_dump, &match,
1765 &actions, &stats,
1766 &ufid,
1767 &thread->nl_flows,
1768 &act);
1769 if (has_next) {
1770 dpif_netlink_netdev_match_to_dpif_flow(&match,
1771 &key, &mask,
1772 actions,
1773 &stats,
1774 &ufid,
1775 f,
1776 dump->up.terse);
1777 n_flows++;
1778 } else {
1779 dpif_netlink_advance_netdev_dump(thread);
1780 }
1781 }
1782
1783 if (!(dump->type & DUMP_OVS_FLOWS)) {
1784 return n_flows;
1785 }
1786
1787 while (!n_flows
1788 || (n_flows < max_flows && thread->nl_flows.size)) {
1789 struct dpif_netlink_flow datapath_flow;
1790 struct ofpbuf nl_flow;
1791 int error;
1792
1793 /* Try to grab another flow. */
1794 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1795 break;
1796 }
1797
1798 /* Convert the flow to our output format. */
1799 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1800 if (error) {
1801 atomic_store_relaxed(&dump->status, error);
1802 break;
1803 }
1804
1805 if (dump->up.terse || datapath_flow.actions) {
1806 /* Common case: we don't want actions, or the flow includes
1807 * actions. */
1808 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1809 &datapath_flow);
1810 } else {
1811 /* Rare case: the flow does not include actions. Retrieve this
1812 * individual flow again to get the actions. */
1813 error = dpif_netlink_flow_get(dpif, &datapath_flow,
1814 &datapath_flow, &thread->nl_actions);
1815 if (error == ENOENT) {
1816 VLOG_DBG("dumped flow disappeared on get");
1817 continue;
1818 } else if (error) {
1819 VLOG_WARN("error fetching dumped flow: %s",
1820 ovs_strerror(error));
1821 atomic_store_relaxed(&dump->status, error);
1822 break;
1823 }
1824
1825 /* Save this flow. Then exit, because we only have one buffer to
1826 * handle this case. */
1827 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1828 &datapath_flow);
1829 break;
1830 }
1831 }
1832 return n_flows;
1833 }
1834
1835 static void
1836 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1837 struct ofpbuf *buf)
1838 {
1839 struct ovs_header *k_exec;
1840 size_t key_ofs;
1841
1842 ofpbuf_prealloc_tailroom(buf, (64
1843 + dp_packet_size(d_exec->packet)
1844 + ODP_KEY_METADATA_SIZE
1845 + d_exec->actions_len));
1846
1847 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1848 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1849
1850 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1851 k_exec->dp_ifindex = dp_ifindex;
1852
1853 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1854 dp_packet_data(d_exec->packet),
1855 dp_packet_size(d_exec->packet));
1856
1857 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1858 odp_key_from_dp_packet(buf, d_exec->packet);
1859 nl_msg_end_nested(buf, key_ofs);
1860
1861 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1862 d_exec->actions, d_exec->actions_len);
1863 if (d_exec->probe) {
1864 nl_msg_put_flag(buf, OVS_PACKET_ATTR_PROBE);
1865 }
1866 if (d_exec->mtu) {
1867 nl_msg_put_u16(buf, OVS_PACKET_ATTR_MRU, d_exec->mtu);
1868 }
1869 }
1870
1871 /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1872 * Returns the number actually executed (at least 1, if 'n_ops' is
1873 * positive). */
1874 static size_t
1875 dpif_netlink_operate__(struct dpif_netlink *dpif,
1876 struct dpif_op **ops, size_t n_ops)
1877 {
1878 struct op_auxdata {
1879 struct nl_transaction txn;
1880
1881 struct ofpbuf request;
1882 uint64_t request_stub[1024 / 8];
1883
1884 struct ofpbuf reply;
1885 uint64_t reply_stub[1024 / 8];
1886 } auxes[OPERATE_MAX_OPS];
1887
1888 struct nl_transaction *txnsp[OPERATE_MAX_OPS];
1889 size_t i;
1890
1891 n_ops = MIN(n_ops, OPERATE_MAX_OPS);
1892 for (i = 0; i < n_ops; i++) {
1893 struct op_auxdata *aux = &auxes[i];
1894 struct dpif_op *op = ops[i];
1895 struct dpif_flow_put *put;
1896 struct dpif_flow_del *del;
1897 struct dpif_flow_get *get;
1898 struct dpif_netlink_flow flow;
1899
1900 ofpbuf_use_stub(&aux->request,
1901 aux->request_stub, sizeof aux->request_stub);
1902 aux->txn.request = &aux->request;
1903
1904 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1905 aux->txn.reply = NULL;
1906
1907 switch (op->type) {
1908 case DPIF_OP_FLOW_PUT:
1909 put = &op->u.flow_put;
1910 dpif_netlink_init_flow_put(dpif, put, &flow);
1911 if (put->stats) {
1912 flow.nlmsg_flags |= NLM_F_ECHO;
1913 aux->txn.reply = &aux->reply;
1914 }
1915 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1916 break;
1917
1918 case DPIF_OP_FLOW_DEL:
1919 del = &op->u.flow_del;
1920 dpif_netlink_init_flow_del(dpif, del, &flow);
1921 if (del->stats) {
1922 flow.nlmsg_flags |= NLM_F_ECHO;
1923 aux->txn.reply = &aux->reply;
1924 }
1925 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1926 break;
1927
1928 case DPIF_OP_EXECUTE:
1929 /* Can't execute a packet that won't fit in a Netlink attribute. */
1930 if (OVS_UNLIKELY(nl_attr_oversized(
1931 dp_packet_size(op->u.execute.packet)))) {
1932 /* Report an error immediately if this is the first operation.
1933 * Otherwise the easiest thing to do is to postpone to the next
1934 * call (when this will be the first operation). */
1935 if (i == 0) {
1936 VLOG_ERR_RL(&error_rl,
1937 "dropping oversized %"PRIu32"-byte packet",
1938 dp_packet_size(op->u.execute.packet));
1939 op->error = ENOBUFS;
1940 return 1;
1941 }
1942 n_ops = i;
1943 } else {
1944 dpif_netlink_encode_execute(dpif->dp_ifindex, &op->u.execute,
1945 &aux->request);
1946 }
1947 break;
1948
1949 case DPIF_OP_FLOW_GET:
1950 get = &op->u.flow_get;
1951 dpif_netlink_init_flow_get(dpif, get, &flow);
1952 aux->txn.reply = get->buffer;
1953 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1954 break;
1955
1956 default:
1957 OVS_NOT_REACHED();
1958 }
1959 }
1960
1961 for (i = 0; i < n_ops; i++) {
1962 txnsp[i] = &auxes[i].txn;
1963 }
1964 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1965
1966 for (i = 0; i < n_ops; i++) {
1967 struct op_auxdata *aux = &auxes[i];
1968 struct nl_transaction *txn = &auxes[i].txn;
1969 struct dpif_op *op = ops[i];
1970 struct dpif_flow_put *put;
1971 struct dpif_flow_del *del;
1972 struct dpif_flow_get *get;
1973
1974 op->error = txn->error;
1975
1976 switch (op->type) {
1977 case DPIF_OP_FLOW_PUT:
1978 put = &op->u.flow_put;
1979 if (put->stats) {
1980 if (!op->error) {
1981 struct dpif_netlink_flow reply;
1982
1983 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1984 txn->reply);
1985 if (!op->error) {
1986 dpif_netlink_flow_get_stats(&reply, put->stats);
1987 }
1988 }
1989 }
1990 break;
1991
1992 case DPIF_OP_FLOW_DEL:
1993 del = &op->u.flow_del;
1994 if (del->stats) {
1995 if (!op->error) {
1996 struct dpif_netlink_flow reply;
1997
1998 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1999 txn->reply);
2000 if (!op->error) {
2001 dpif_netlink_flow_get_stats(&reply, del->stats);
2002 }
2003 }
2004 }
2005 break;
2006
2007 case DPIF_OP_EXECUTE:
2008 break;
2009
2010 case DPIF_OP_FLOW_GET:
2011 get = &op->u.flow_get;
2012 if (!op->error) {
2013 struct dpif_netlink_flow reply;
2014
2015 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
2016 if (!op->error) {
2017 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, get->flow,
2018 &reply);
2019 }
2020 }
2021 break;
2022
2023 default:
2024 OVS_NOT_REACHED();
2025 }
2026
2027 ofpbuf_uninit(&aux->request);
2028 ofpbuf_uninit(&aux->reply);
2029 }
2030
2031 return n_ops;
2032 }
2033
2034 static int
2035 parse_flow_get(struct dpif_netlink *dpif, struct dpif_flow_get *get)
2036 {
2037 struct dpif_flow *dpif_flow = get->flow;
2038 struct match match;
2039 struct nlattr *actions;
2040 struct dpif_flow_stats stats;
2041 struct ofpbuf buf;
2042 uint64_t act_buf[1024 / 8];
2043 struct odputil_keybuf maskbuf;
2044 struct odputil_keybuf keybuf;
2045 struct odputil_keybuf actbuf;
2046 struct ofpbuf key, mask, act;
2047 int err;
2048
2049 ofpbuf_use_stack(&buf, &act_buf, sizeof act_buf);
2050 err = netdev_ports_flow_get(DPIF_HMAP_KEY(&dpif->dpif), &match,
2051 &actions, get->ufid, &stats, &buf);
2052 if (err) {
2053 return err;
2054 }
2055
2056 VLOG_DBG("found flow from netdev, translating to dpif flow");
2057
2058 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2059 ofpbuf_use_stack(&act, &actbuf, sizeof actbuf);
2060 ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf);
2061 dpif_netlink_netdev_match_to_dpif_flow(&match, &key, &mask, actions,
2062 &stats,
2063 (ovs_u128 *) get->ufid,
2064 dpif_flow,
2065 false);
2066 ofpbuf_put(get->buffer, nl_attr_get(actions), nl_attr_get_size(actions));
2067 dpif_flow->actions = ofpbuf_at(get->buffer, 0, 0);
2068 dpif_flow->actions_len = nl_attr_get_size(actions);
2069
2070 return 0;
2071 }
2072
2073 static int
2074 parse_flow_put(struct dpif_netlink *dpif, struct dpif_flow_put *put)
2075 {
2076 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
2077 struct match match;
2078 odp_port_t in_port;
2079 const struct nlattr *nla;
2080 size_t left;
2081 int outputs = 0;
2082 struct netdev *dev;
2083 struct offload_info info;
2084 ovs_be16 dst_port = 0;
2085 int err;
2086
2087 if (put->flags & DPIF_FP_PROBE) {
2088 return EOPNOTSUPP;
2089 }
2090
2091 err = parse_key_and_mask_to_match(put->key, put->key_len, put->mask,
2092 put->mask_len, &match);
2093 if (err) {
2094 return err;
2095 }
2096
2097 /* When we try to install a dummy flow from a probed feature. */
2098 if (match.flow.dl_type == htons(0x1234)) {
2099 return EOPNOTSUPP;
2100 }
2101
2102 in_port = match.flow.in_port.odp_port;
2103 dev = netdev_ports_get(in_port, DPIF_HMAP_KEY(&dpif->dpif));
2104 if (!dev) {
2105 return EOPNOTSUPP;
2106 }
2107
2108 /* Get tunnel dst port and count outputs */
2109 NL_ATTR_FOR_EACH(nla, left, put->actions, put->actions_len) {
2110 if (nl_attr_type(nla) == OVS_ACTION_ATTR_OUTPUT) {
2111 const struct netdev_tunnel_config *tnl_cfg;
2112 struct netdev *outdev;
2113 odp_port_t out_port;
2114
2115 outputs++;
2116 if (outputs > 1) {
2117 VLOG_DBG_RL(&rl, "offloading multiple ports isn't supported");
2118 err = EOPNOTSUPP;
2119 goto out;
2120 }
2121
2122 out_port = nl_attr_get_odp_port(nla);
2123 outdev = netdev_ports_get(out_port, DPIF_HMAP_KEY(&dpif->dpif));
2124 if (!outdev) {
2125 err = EOPNOTSUPP;
2126 goto out;
2127 }
2128 tnl_cfg = netdev_get_tunnel_config(outdev);
2129 if (tnl_cfg && tnl_cfg->dst_port != 0) {
2130 dst_port = tnl_cfg->dst_port;
2131 }
2132 netdev_close(outdev);
2133 }
2134 }
2135
2136 info.port_hmap_obj = DPIF_HMAP_KEY(&dpif->dpif);
2137 info.tp_dst_port = dst_port;
2138 err = netdev_flow_put(dev, &match,
2139 CONST_CAST(struct nlattr *, put->actions),
2140 put->actions_len,
2141 CONST_CAST(ovs_u128 *, put->ufid),
2142 &info, put->stats);
2143
2144 if (!err) {
2145 if (put->flags & DPIF_FP_MODIFY) {
2146 struct dpif_op *opp;
2147 struct dpif_op op;
2148
2149 op.type = DPIF_OP_FLOW_DEL;
2150 op.u.flow_del.key = put->key;
2151 op.u.flow_del.key_len = put->key_len;
2152 op.u.flow_del.ufid = put->ufid;
2153 op.u.flow_del.pmd_id = put->pmd_id;
2154 op.u.flow_del.stats = NULL;
2155 op.u.flow_del.terse = false;
2156
2157 opp = &op;
2158 dpif_netlink_operate__(dpif, &opp, 1);
2159 }
2160
2161 VLOG_DBG("added flow");
2162 } else if (err != EEXIST) {
2163 VLOG_ERR_RL(&rl, "failed to offload flow: %s", ovs_strerror(err));
2164 }
2165
2166 out:
2167 if (err && err != EEXIST && (put->flags & DPIF_FP_MODIFY)) {
2168 /* Modified rule can't be offloaded, try and delete from HW */
2169 int del_err = netdev_flow_del(dev, put->ufid, put->stats);
2170
2171 if (!del_err) {
2172 /* Delete from hw success, so old flow was offloaded.
2173 * Change flags to create the flow in kernel */
2174 put->flags &= ~DPIF_FP_MODIFY;
2175 put->flags |= DPIF_FP_CREATE;
2176 } else if (del_err != ENOENT) {
2177 VLOG_ERR_RL(&rl, "failed to delete offloaded flow: %s",
2178 ovs_strerror(del_err));
2179 /* stop proccesing the flow in kernel */
2180 err = 0;
2181 }
2182 }
2183
2184 netdev_close(dev);
2185
2186 return err;
2187 }
2188
2189 static int
2190 try_send_to_netdev(struct dpif_netlink *dpif, struct dpif_op *op)
2191 {
2192 int err = EOPNOTSUPP;
2193
2194 switch (op->type) {
2195 case DPIF_OP_FLOW_PUT: {
2196 struct dpif_flow_put *put = &op->u.flow_put;
2197
2198 if (!put->ufid) {
2199 break;
2200 }
2201
2202 log_flow_put_message(&dpif->dpif, &this_module, put, 0);
2203 err = parse_flow_put(dpif, put);
2204 break;
2205 }
2206 case DPIF_OP_FLOW_DEL: {
2207 struct dpif_flow_del *del = &op->u.flow_del;
2208
2209 if (!del->ufid) {
2210 break;
2211 }
2212
2213 log_flow_del_message(&dpif->dpif, &this_module, del, 0);
2214 err = netdev_ports_flow_del(DPIF_HMAP_KEY(&dpif->dpif), del->ufid,
2215 del->stats);
2216 break;
2217 }
2218 case DPIF_OP_FLOW_GET: {
2219 struct dpif_flow_get *get = &op->u.flow_get;
2220
2221 if (!op->u.flow_get.ufid) {
2222 break;
2223 }
2224
2225 log_flow_get_message(&dpif->dpif, &this_module, get, 0);
2226 err = parse_flow_get(dpif, get);
2227 break;
2228 }
2229 case DPIF_OP_EXECUTE:
2230 default:
2231 break;
2232 }
2233
2234 return err;
2235 }
2236
2237 static void
2238 dpif_netlink_operate_chunks(struct dpif_netlink *dpif, struct dpif_op **ops,
2239 size_t n_ops)
2240 {
2241 while (n_ops > 0) {
2242 size_t chunk = dpif_netlink_operate__(dpif, ops, n_ops);
2243
2244 ops += chunk;
2245 n_ops -= chunk;
2246 }
2247 }
2248
2249 static void
2250 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
2251 {
2252 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2253 struct dpif_op *new_ops[OPERATE_MAX_OPS];
2254 int count = 0;
2255 int i = 0;
2256 int err = 0;
2257
2258 if (netdev_is_flow_api_enabled()) {
2259 while (n_ops > 0) {
2260 count = 0;
2261
2262 while (n_ops > 0 && count < OPERATE_MAX_OPS) {
2263 struct dpif_op *op = ops[i++];
2264
2265 err = try_send_to_netdev(dpif, op);
2266 if (err && err != EEXIST) {
2267 new_ops[count++] = op;
2268 } else {
2269 op->error = err;
2270 }
2271
2272 n_ops--;
2273 }
2274
2275 dpif_netlink_operate_chunks(dpif, new_ops, count);
2276 }
2277 } else {
2278 dpif_netlink_operate_chunks(dpif, ops, n_ops);
2279 }
2280 }
2281
2282 #if _WIN32
2283 static void
2284 dpif_netlink_handler_uninit(struct dpif_handler *handler)
2285 {
2286 vport_delete_sock_pool(handler);
2287 }
2288
2289 static int
2290 dpif_netlink_handler_init(struct dpif_handler *handler)
2291 {
2292 return vport_create_sock_pool(handler);
2293 }
2294 #else
2295
2296 static int
2297 dpif_netlink_handler_init(struct dpif_handler *handler)
2298 {
2299 handler->epoll_fd = epoll_create(10);
2300 return handler->epoll_fd < 0 ? errno : 0;
2301 }
2302
2303 static void
2304 dpif_netlink_handler_uninit(struct dpif_handler *handler)
2305 {
2306 close(handler->epoll_fd);
2307 }
2308 #endif
2309
2310 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
2311 * currently in 'dpif' in the kernel, by adding a new set of channels for
2312 * any kernel vport that lacks one and deleting any channels that have no
2313 * backing kernel vports. */
2314 static int
2315 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
2316 OVS_REQ_WRLOCK(dpif->upcall_lock)
2317 {
2318 unsigned long int *keep_channels;
2319 struct dpif_netlink_vport vport;
2320 size_t keep_channels_nbits;
2321 struct nl_dump dump;
2322 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
2323 struct ofpbuf buf;
2324 int retval = 0;
2325 size_t i;
2326
2327 ovs_assert(!WINDOWS || n_handlers <= 1);
2328 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
2329
2330 if (dpif->n_handlers != n_handlers) {
2331 destroy_all_channels(dpif);
2332 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
2333 for (i = 0; i < n_handlers; i++) {
2334 int error;
2335 struct dpif_handler *handler = &dpif->handlers[i];
2336
2337 error = dpif_netlink_handler_init(handler);
2338 if (error) {
2339 size_t j;
2340
2341 for (j = 0; j < i; j++) {
2342 struct dpif_handler *tmp = &dpif->handlers[j];
2343 dpif_netlink_handler_uninit(tmp);
2344 }
2345 free(dpif->handlers);
2346 dpif->handlers = NULL;
2347
2348 return error;
2349 }
2350 }
2351 dpif->n_handlers = n_handlers;
2352 }
2353
2354 for (i = 0; i < n_handlers; i++) {
2355 struct dpif_handler *handler = &dpif->handlers[i];
2356
2357 handler->event_offset = handler->n_events = 0;
2358 }
2359
2360 keep_channels_nbits = dpif->uc_array_size;
2361 keep_channels = bitmap_allocate(keep_channels_nbits);
2362
2363 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
2364 dpif_netlink_port_dump_start__(dpif, &dump);
2365 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
2366 uint32_t port_no = odp_to_u32(vport.port_no);
2367 uint32_t *upcall_pids = NULL;
2368 int error;
2369
2370 if (port_no >= dpif->uc_array_size
2371 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
2372 struct nl_sock **socksp = vport_create_socksp(dpif, &error);
2373
2374 if (!socksp) {
2375 goto error;
2376 }
2377
2378 error = vport_add_channels(dpif, vport.port_no, socksp);
2379 if (error) {
2380 VLOG_INFO("%s: could not add channels for port %s",
2381 dpif_name(&dpif->dpif), vport.name);
2382 vport_del_socksp(dpif, socksp);
2383 retval = error;
2384 goto error;
2385 }
2386 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
2387 free(socksp);
2388 }
2389
2390 /* Configure the vport to deliver misses to 'sock'. */
2391 if (vport.upcall_pids[0] == 0
2392 || vport.n_upcall_pids != dpif->n_handlers
2393 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
2394 *upcall_pids)) {
2395 struct dpif_netlink_vport vport_request;
2396
2397 dpif_netlink_vport_init(&vport_request);
2398 vport_request.cmd = OVS_VPORT_CMD_SET;
2399 vport_request.dp_ifindex = dpif->dp_ifindex;
2400 vport_request.port_no = vport.port_no;
2401 vport_request.n_upcall_pids = dpif->n_handlers;
2402 vport_request.upcall_pids = upcall_pids;
2403 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
2404 if (error) {
2405 VLOG_WARN_RL(&error_rl,
2406 "%s: failed to set upcall pid on port: %s",
2407 dpif_name(&dpif->dpif), ovs_strerror(error));
2408
2409 if (error != ENODEV && error != ENOENT) {
2410 retval = error;
2411 } else {
2412 /* The vport isn't really there, even though the dump says
2413 * it is. Probably we just hit a race after a port
2414 * disappeared. */
2415 }
2416 goto error;
2417 }
2418 }
2419
2420 if (port_no < keep_channels_nbits) {
2421 bitmap_set1(keep_channels, port_no);
2422 }
2423 free(upcall_pids);
2424 continue;
2425
2426 error:
2427 free(upcall_pids);
2428 vport_del_channels(dpif, vport.port_no);
2429 }
2430 nl_dump_done(&dump);
2431 ofpbuf_uninit(&buf);
2432
2433 /* Discard any saved channels that we didn't reuse. */
2434 for (i = 0; i < keep_channels_nbits; i++) {
2435 if (!bitmap_is_set(keep_channels, i)) {
2436 vport_del_channels(dpif, u32_to_odp(i));
2437 }
2438 }
2439 free(keep_channels);
2440
2441 return retval;
2442 }
2443
2444 static int
2445 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
2446 OVS_REQ_WRLOCK(dpif->upcall_lock)
2447 {
2448 if ((dpif->handlers != NULL) == enable) {
2449 return 0;
2450 } else if (!enable) {
2451 destroy_all_channels(dpif);
2452 return 0;
2453 } else {
2454 return dpif_netlink_refresh_channels(dpif, 1);
2455 }
2456 }
2457
2458 static int
2459 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
2460 {
2461 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2462 int error;
2463
2464 fat_rwlock_wrlock(&dpif->upcall_lock);
2465 error = dpif_netlink_recv_set__(dpif, enable);
2466 fat_rwlock_unlock(&dpif->upcall_lock);
2467
2468 return error;
2469 }
2470
2471 static int
2472 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
2473 {
2474 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2475 int error = 0;
2476
2477 #ifdef _WIN32
2478 /* Multiple upcall handlers will be supported once kernel datapath supports
2479 * it. */
2480 if (n_handlers > 1) {
2481 return error;
2482 }
2483 #endif
2484
2485 fat_rwlock_wrlock(&dpif->upcall_lock);
2486 if (dpif->handlers) {
2487 error = dpif_netlink_refresh_channels(dpif, n_handlers);
2488 }
2489 fat_rwlock_unlock(&dpif->upcall_lock);
2490
2491 return error;
2492 }
2493
2494 static int
2495 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
2496 uint32_t queue_id, uint32_t *priority)
2497 {
2498 if (queue_id < 0xf000) {
2499 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
2500 return 0;
2501 } else {
2502 return EINVAL;
2503 }
2504 }
2505
2506 static int
2507 parse_odp_packet(const struct dpif_netlink *dpif, struct ofpbuf *buf,
2508 struct dpif_upcall *upcall, int *dp_ifindex)
2509 {
2510 static const struct nl_policy ovs_packet_policy[] = {
2511 /* Always present. */
2512 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
2513 .min_len = ETH_HEADER_LEN },
2514 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
2515
2516 /* OVS_PACKET_CMD_ACTION only. */
2517 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
2518 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
2519 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2520 [OVS_PACKET_ATTR_MRU] = { .type = NL_A_U16, .optional = true }
2521 };
2522
2523 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2524 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2525 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2526 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2527
2528 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
2529 if (!nlmsg || !genl || !ovs_header
2530 || nlmsg->nlmsg_type != ovs_packet_family
2531 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
2532 ARRAY_SIZE(ovs_packet_policy))) {
2533 return EINVAL;
2534 }
2535
2536 int type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
2537 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
2538 : -1);
2539 if (type < 0) {
2540 return EINVAL;
2541 }
2542
2543 /* (Re)set ALL fields of '*upcall' on successful return. */
2544 upcall->type = type;
2545 upcall->key = CONST_CAST(struct nlattr *,
2546 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
2547 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
2548 dpif_flow_hash(&dpif->dpif, upcall->key, upcall->key_len, &upcall->ufid);
2549 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
2550 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
2551 upcall->actions = a[OVS_PACKET_ATTR_ACTIONS];
2552 upcall->mru = a[OVS_PACKET_ATTR_MRU];
2553
2554 /* Allow overwriting the netlink attribute header without reallocating. */
2555 dp_packet_use_stub(&upcall->packet,
2556 CONST_CAST(struct nlattr *,
2557 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
2558 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
2559 sizeof(struct nlattr));
2560 dp_packet_set_data(&upcall->packet,
2561 (char *)dp_packet_data(&upcall->packet) + sizeof(struct nlattr));
2562 dp_packet_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
2563
2564 if (nl_attr_find__(upcall->key, upcall->key_len, OVS_KEY_ATTR_ETHERNET)) {
2565 /* Ethernet frame */
2566 upcall->packet.packet_type = htonl(PT_ETH);
2567 } else {
2568 /* Non-Ethernet packet. Get the Ethertype from the NL attributes */
2569 ovs_be16 ethertype = 0;
2570 const struct nlattr *et_nla = nl_attr_find__(upcall->key,
2571 upcall->key_len,
2572 OVS_KEY_ATTR_ETHERTYPE);
2573 if (et_nla) {
2574 ethertype = nl_attr_get_be16(et_nla);
2575 }
2576 upcall->packet.packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
2577 ntohs(ethertype));
2578 dp_packet_set_l3(&upcall->packet, dp_packet_data(&upcall->packet));
2579 }
2580
2581 *dp_ifindex = ovs_header->dp_ifindex;
2582
2583 return 0;
2584 }
2585
2586 #ifdef _WIN32
2587 #define PACKET_RECV_BATCH_SIZE 50
2588 static int
2589 dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id,
2590 struct dpif_upcall *upcall, struct ofpbuf *buf)
2591 OVS_REQ_RDLOCK(dpif->upcall_lock)
2592 {
2593 struct dpif_handler *handler;
2594 int read_tries = 0;
2595 struct dpif_windows_vport_sock *sock_pool;
2596 uint32_t i;
2597
2598 if (!dpif->handlers) {
2599 return EAGAIN;
2600 }
2601
2602 /* Only one handler is supported currently. */
2603 if (handler_id >= 1) {
2604 return EAGAIN;
2605 }
2606
2607 if (handler_id >= dpif->n_handlers) {
2608 return EAGAIN;
2609 }
2610
2611 handler = &dpif->handlers[handler_id];
2612 sock_pool = handler->vport_sock_pool;
2613
2614 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2615 for (;;) {
2616 int dp_ifindex;
2617 int error;
2618
2619 if (++read_tries > PACKET_RECV_BATCH_SIZE) {
2620 return EAGAIN;
2621 }
2622
2623 error = nl_sock_recv(sock_pool[i].nl_sock, buf, false);
2624 if (error == ENOBUFS) {
2625 /* ENOBUFS typically means that we've received so many
2626 * packets that the buffer overflowed. Try again
2627 * immediately because there's almost certainly a packet
2628 * waiting for us. */
2629 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2630 continue;
2631 }
2632
2633 /* XXX: ch->last_poll = time_msec(); */
2634 if (error) {
2635 if (error == EAGAIN) {
2636 break;
2637 }
2638 return error;
2639 }
2640
2641 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2642 if (!error && dp_ifindex == dpif->dp_ifindex) {
2643 return 0;
2644 } else if (error) {
2645 return error;
2646 }
2647 }
2648 }
2649
2650 return EAGAIN;
2651 }
2652 #else
2653 static int
2654 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
2655 struct dpif_upcall *upcall, struct ofpbuf *buf)
2656 OVS_REQ_RDLOCK(dpif->upcall_lock)
2657 {
2658 struct dpif_handler *handler;
2659 int read_tries = 0;
2660
2661 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
2662 return EAGAIN;
2663 }
2664
2665 handler = &dpif->handlers[handler_id];
2666 if (handler->event_offset >= handler->n_events) {
2667 int retval;
2668
2669 handler->event_offset = handler->n_events = 0;
2670
2671 do {
2672 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
2673 dpif->uc_array_size, 0);
2674 } while (retval < 0 && errno == EINTR);
2675
2676 if (retval < 0) {
2677 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2678 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
2679 } else if (retval > 0) {
2680 handler->n_events = retval;
2681 }
2682 }
2683
2684 while (handler->event_offset < handler->n_events) {
2685 int idx = handler->epoll_events[handler->event_offset].data.u32;
2686 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
2687
2688 handler->event_offset++;
2689
2690 for (;;) {
2691 int dp_ifindex;
2692 int error;
2693
2694 if (++read_tries > 50) {
2695 return EAGAIN;
2696 }
2697
2698 error = nl_sock_recv(ch->sock, buf, false);
2699 if (error == ENOBUFS) {
2700 /* ENOBUFS typically means that we've received so many
2701 * packets that the buffer overflowed. Try again
2702 * immediately because there's almost certainly a packet
2703 * waiting for us. */
2704 report_loss(dpif, ch, idx, handler_id);
2705 continue;
2706 }
2707
2708 ch->last_poll = time_msec();
2709 if (error) {
2710 if (error == EAGAIN) {
2711 break;
2712 }
2713 return error;
2714 }
2715
2716 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2717 if (!error && dp_ifindex == dpif->dp_ifindex) {
2718 return 0;
2719 } else if (error) {
2720 return error;
2721 }
2722 }
2723 }
2724
2725 return EAGAIN;
2726 }
2727 #endif
2728
2729 static int
2730 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
2731 struct dpif_upcall *upcall, struct ofpbuf *buf)
2732 {
2733 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2734 int error;
2735
2736 fat_rwlock_rdlock(&dpif->upcall_lock);
2737 #ifdef _WIN32
2738 error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf);
2739 #else
2740 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
2741 #endif
2742 fat_rwlock_unlock(&dpif->upcall_lock);
2743
2744 return error;
2745 }
2746
2747 static void
2748 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
2749 OVS_REQ_RDLOCK(dpif->upcall_lock)
2750 {
2751 #ifdef _WIN32
2752 uint32_t i;
2753 struct dpif_windows_vport_sock *sock_pool =
2754 dpif->handlers[handler_id].vport_sock_pool;
2755
2756 /* Only one handler is supported currently. */
2757 if (handler_id >= 1) {
2758 return;
2759 }
2760
2761 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2762 nl_sock_wait(sock_pool[i].nl_sock, POLLIN);
2763 }
2764 #else
2765 if (dpif->handlers && handler_id < dpif->n_handlers) {
2766 struct dpif_handler *handler = &dpif->handlers[handler_id];
2767
2768 poll_fd_wait(handler->epoll_fd, POLLIN);
2769 }
2770 #endif
2771 }
2772
2773 static void
2774 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
2775 {
2776 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2777
2778 fat_rwlock_rdlock(&dpif->upcall_lock);
2779 dpif_netlink_recv_wait__(dpif, handler_id);
2780 fat_rwlock_unlock(&dpif->upcall_lock);
2781 }
2782
2783 static void
2784 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
2785 OVS_REQ_WRLOCK(dpif->upcall_lock)
2786 {
2787 if (dpif->handlers) {
2788 size_t i, j;
2789
2790 for (i = 0; i < dpif->uc_array_size; i++ ) {
2791 if (!dpif->handlers[0].channels[i].sock) {
2792 continue;
2793 }
2794
2795 for (j = 0; j < dpif->n_handlers; j++) {
2796 nl_sock_drain(dpif->handlers[j].channels[i].sock);
2797 }
2798 }
2799 }
2800 }
2801
2802 static void
2803 dpif_netlink_recv_purge(struct dpif *dpif_)
2804 {
2805 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2806
2807 fat_rwlock_wrlock(&dpif->upcall_lock);
2808 dpif_netlink_recv_purge__(dpif);
2809 fat_rwlock_unlock(&dpif->upcall_lock);
2810 }
2811
2812 static char *
2813 dpif_netlink_get_datapath_version(void)
2814 {
2815 char *version_str = NULL;
2816
2817 #ifdef __linux__
2818
2819 #define MAX_VERSION_STR_SIZE 80
2820 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2821 FILE *f;
2822
2823 f = fopen(LINUX_DATAPATH_VERSION_FILE, "r");
2824 if (f) {
2825 char *newline;
2826 char version[MAX_VERSION_STR_SIZE];
2827
2828 if (fgets(version, MAX_VERSION_STR_SIZE, f)) {
2829 newline = strchr(version, '\n');
2830 if (newline) {
2831 *newline = '\0';
2832 }
2833 version_str = xstrdup(version);
2834 }
2835 fclose(f);
2836 }
2837 #endif
2838
2839 return version_str;
2840 }
2841
2842 struct dpif_netlink_ct_dump_state {
2843 struct ct_dpif_dump_state up;
2844 struct nl_ct_dump_state *nl_ct_dump;
2845 };
2846
2847 static int
2848 dpif_netlink_ct_dump_start(struct dpif *dpif OVS_UNUSED,
2849 struct ct_dpif_dump_state **dump_,
2850 const uint16_t *zone)
2851 {
2852 struct dpif_netlink_ct_dump_state *dump;
2853 int err;
2854
2855 dump = xzalloc(sizeof *dump);
2856 err = nl_ct_dump_start(&dump->nl_ct_dump, zone);
2857 if (err) {
2858 free(dump);
2859 return err;
2860 }
2861
2862 *dump_ = &dump->up;
2863
2864 return 0;
2865 }
2866
2867 static int
2868 dpif_netlink_ct_dump_next(struct dpif *dpif OVS_UNUSED,
2869 struct ct_dpif_dump_state *dump_,
2870 struct ct_dpif_entry *entry)
2871 {
2872 struct dpif_netlink_ct_dump_state *dump;
2873
2874 INIT_CONTAINER(dump, dump_, up);
2875
2876 return nl_ct_dump_next(dump->nl_ct_dump, entry);
2877 }
2878
2879 static int
2880 dpif_netlink_ct_dump_done(struct dpif *dpif OVS_UNUSED,
2881 struct ct_dpif_dump_state *dump_)
2882 {
2883 struct dpif_netlink_ct_dump_state *dump;
2884 int err;
2885
2886 INIT_CONTAINER(dump, dump_, up);
2887
2888 err = nl_ct_dump_done(dump->nl_ct_dump);
2889 free(dump);
2890 return err;
2891 }
2892
2893 static int
2894 dpif_netlink_ct_flush(struct dpif *dpif OVS_UNUSED, const uint16_t *zone)
2895 {
2896 if (zone) {
2897 return nl_ct_flush_zone(*zone);
2898 } else {
2899 return nl_ct_flush();
2900 }
2901 }
2902
2903 \f
2904 /* Meters */
2905 static void
2906 dpif_netlink_meter_get_features(const struct dpif * dpif OVS_UNUSED,
2907 struct ofputil_meter_features *features)
2908 {
2909 features->max_meters = 0;
2910 features->band_types = 0;
2911 features->capabilities = 0;
2912 features->max_bands = 0;
2913 features->max_color = 0;
2914 }
2915
2916 static int
2917 dpif_netlink_meter_set(struct dpif *dpif OVS_UNUSED,
2918 ofproto_meter_id *meter_id OVS_UNUSED,
2919 struct ofputil_meter_config *config OVS_UNUSED)
2920 {
2921 return EFBIG; /* meter_id out of range */
2922 }
2923
2924 static int
2925 dpif_netlink_meter_get(const struct dpif *dpif OVS_UNUSED,
2926 ofproto_meter_id meter_id OVS_UNUSED,
2927 struct ofputil_meter_stats *stats OVS_UNUSED,
2928 uint16_t n_bands OVS_UNUSED)
2929 {
2930 return EFBIG; /* meter_id out of range */
2931 }
2932
2933 static int
2934 dpif_netlink_meter_del(struct dpif *dpif OVS_UNUSED,
2935 ofproto_meter_id meter_id OVS_UNUSED,
2936 struct ofputil_meter_stats *stats OVS_UNUSED,
2937 uint16_t n_bands OVS_UNUSED)
2938 {
2939 return EFBIG; /* meter_id out of range */
2940 }
2941
2942 \f
2943 const struct dpif_class dpif_netlink_class = {
2944 "system",
2945 NULL, /* init */
2946 dpif_netlink_enumerate,
2947 NULL,
2948 dpif_netlink_open,
2949 dpif_netlink_close,
2950 dpif_netlink_destroy,
2951 dpif_netlink_run,
2952 NULL, /* wait */
2953 dpif_netlink_get_stats,
2954 dpif_netlink_port_add,
2955 dpif_netlink_port_del,
2956 NULL, /* port_set_config */
2957 dpif_netlink_port_query_by_number,
2958 dpif_netlink_port_query_by_name,
2959 dpif_netlink_port_get_pid,
2960 dpif_netlink_port_dump_start,
2961 dpif_netlink_port_dump_next,
2962 dpif_netlink_port_dump_done,
2963 dpif_netlink_port_poll,
2964 dpif_netlink_port_poll_wait,
2965 dpif_netlink_flow_flush,
2966 dpif_netlink_flow_dump_create,
2967 dpif_netlink_flow_dump_destroy,
2968 dpif_netlink_flow_dump_thread_create,
2969 dpif_netlink_flow_dump_thread_destroy,
2970 dpif_netlink_flow_dump_next,
2971 dpif_netlink_operate,
2972 dpif_netlink_recv_set,
2973 dpif_netlink_handlers_set,
2974 NULL, /* set_config */
2975 dpif_netlink_queue_to_priority,
2976 dpif_netlink_recv,
2977 dpif_netlink_recv_wait,
2978 dpif_netlink_recv_purge,
2979 NULL, /* register_dp_purge_cb */
2980 NULL, /* register_upcall_cb */
2981 NULL, /* enable_upcall */
2982 NULL, /* disable_upcall */
2983 dpif_netlink_get_datapath_version, /* get_datapath_version */
2984 dpif_netlink_ct_dump_start,
2985 dpif_netlink_ct_dump_next,
2986 dpif_netlink_ct_dump_done,
2987 dpif_netlink_ct_flush,
2988 dpif_netlink_meter_get_features,
2989 dpif_netlink_meter_set,
2990 dpif_netlink_meter_get,
2991 dpif_netlink_meter_del,
2992 };
2993
2994 static int
2995 dpif_netlink_init(void)
2996 {
2997 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2998 static int error;
2999
3000 if (ovsthread_once_start(&once)) {
3001 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
3002 &ovs_datapath_family);
3003 if (error) {
3004 VLOG_WARN("Generic Netlink family '%s' does not exist. "
3005 "The Open vSwitch kernel module is probably not loaded.",
3006 OVS_DATAPATH_FAMILY);
3007 }
3008 if (!error) {
3009 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
3010 }
3011 if (!error) {
3012 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
3013 }
3014 if (!error) {
3015 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
3016 &ovs_packet_family);
3017 }
3018 if (!error) {
3019 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
3020 &ovs_vport_mcgroup);
3021 }
3022
3023 ovs_tunnels_out_of_tree = dpif_netlink_rtnl_probe_oot_tunnels();
3024
3025 ovsthread_once_done(&once);
3026 }
3027
3028 return error;
3029 }
3030
3031 bool
3032 dpif_netlink_is_internal_device(const char *name)
3033 {
3034 struct dpif_netlink_vport reply;
3035 struct ofpbuf *buf;
3036 int error;
3037
3038 error = dpif_netlink_vport_get(name, &reply, &buf);
3039 if (!error) {
3040 ofpbuf_delete(buf);
3041 } else if (error != ENODEV && error != ENOENT) {
3042 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
3043 name, ovs_strerror(error));
3044 }
3045
3046 return reply.type == OVS_VPORT_TYPE_INTERNAL;
3047 }
3048
3049 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
3050 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
3051 * positive errno value.
3052 *
3053 * 'vport' will contain pointers into 'buf', so the caller should not free
3054 * 'buf' while 'vport' is still in use. */
3055 static int
3056 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
3057 const struct ofpbuf *buf)
3058 {
3059 static const struct nl_policy ovs_vport_policy[] = {
3060 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
3061 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
3062 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
3063 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
3064 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
3065 .optional = true },
3066 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
3067 };
3068
3069 dpif_netlink_vport_init(vport);
3070
3071 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
3072 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
3073 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
3074 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
3075
3076 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
3077 if (!nlmsg || !genl || !ovs_header
3078 || nlmsg->nlmsg_type != ovs_vport_family
3079 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
3080 ARRAY_SIZE(ovs_vport_policy))) {
3081 return EINVAL;
3082 }
3083
3084 vport->cmd = genl->cmd;
3085 vport->dp_ifindex = ovs_header->dp_ifindex;
3086 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
3087 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
3088 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
3089 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
3090 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
3091 / (sizeof *vport->upcall_pids);
3092 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
3093
3094 }
3095 if (a[OVS_VPORT_ATTR_STATS]) {
3096 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
3097 }
3098 if (a[OVS_VPORT_ATTR_OPTIONS]) {
3099 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
3100 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
3101 }
3102 return 0;
3103 }
3104
3105 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
3106 * followed by Netlink attributes corresponding to 'vport'. */
3107 static void
3108 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
3109 struct ofpbuf *buf)
3110 {
3111 struct ovs_header *ovs_header;
3112
3113 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
3114 vport->cmd, OVS_VPORT_VERSION);
3115
3116 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
3117 ovs_header->dp_ifindex = vport->dp_ifindex;
3118
3119 if (vport->port_no != ODPP_NONE) {
3120 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
3121 }
3122
3123 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
3124 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
3125 }
3126
3127 if (vport->name) {
3128 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
3129 }
3130
3131 if (vport->upcall_pids) {
3132 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
3133 vport->upcall_pids,
3134 vport->n_upcall_pids * sizeof *vport->upcall_pids);
3135 }
3136
3137 if (vport->stats) {
3138 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
3139 vport->stats, sizeof *vport->stats);
3140 }
3141
3142 if (vport->options) {
3143 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
3144 vport->options, vport->options_len);
3145 }
3146 }
3147
3148 /* Clears 'vport' to "empty" values. */
3149 void
3150 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
3151 {
3152 memset(vport, 0, sizeof *vport);
3153 vport->port_no = ODPP_NONE;
3154 }
3155
3156 /* Executes 'request' in the kernel datapath. If the command fails, returns a
3157 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
3158 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
3159 * result of the command is expected to be an ovs_vport also, which is decoded
3160 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
3161 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
3162 int
3163 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
3164 struct dpif_netlink_vport *reply,
3165 struct ofpbuf **bufp)
3166 {
3167 struct ofpbuf *request_buf;
3168 int error;
3169
3170 ovs_assert((reply != NULL) == (bufp != NULL));
3171
3172 error = dpif_netlink_init();
3173 if (error) {
3174 if (reply) {
3175 *bufp = NULL;
3176 dpif_netlink_vport_init(reply);
3177 }
3178 return error;
3179 }
3180
3181 request_buf = ofpbuf_new(1024);
3182 dpif_netlink_vport_to_ofpbuf(request, request_buf);
3183 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
3184 ofpbuf_delete(request_buf);
3185
3186 if (reply) {
3187 if (!error) {
3188 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
3189 }
3190 if (error) {
3191 dpif_netlink_vport_init(reply);
3192 ofpbuf_delete(*bufp);
3193 *bufp = NULL;
3194 }
3195 }
3196 return error;
3197 }
3198
3199 /* Obtains information about the kernel vport named 'name' and stores it into
3200 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
3201 * longer needed ('reply' will contain pointers into '*bufp'). */
3202 int
3203 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
3204 struct ofpbuf **bufp)
3205 {
3206 struct dpif_netlink_vport request;
3207
3208 dpif_netlink_vport_init(&request);
3209 request.cmd = OVS_VPORT_CMD_GET;
3210 request.name = name;
3211
3212 return dpif_netlink_vport_transact(&request, reply, bufp);
3213 }
3214
3215 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
3216 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
3217 * positive errno value.
3218 *
3219 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
3220 * while 'dp' is still in use. */
3221 static int
3222 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
3223 {
3224 static const struct nl_policy ovs_datapath_policy[] = {
3225 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
3226 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
3227 .optional = true },
3228 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
3229 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
3230 .optional = true },
3231 };
3232
3233 dpif_netlink_dp_init(dp);
3234
3235 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
3236 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
3237 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
3238 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
3239
3240 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
3241 if (!nlmsg || !genl || !ovs_header
3242 || nlmsg->nlmsg_type != ovs_datapath_family
3243 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
3244 ARRAY_SIZE(ovs_datapath_policy))) {
3245 return EINVAL;
3246 }
3247
3248 dp->cmd = genl->cmd;
3249 dp->dp_ifindex = ovs_header->dp_ifindex;
3250 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
3251 if (a[OVS_DP_ATTR_STATS]) {
3252 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
3253 }
3254
3255 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
3256 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
3257 }
3258
3259 return 0;
3260 }
3261
3262 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
3263 static void
3264 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
3265 {
3266 struct ovs_header *ovs_header;
3267
3268 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
3269 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
3270 OVS_DATAPATH_VERSION);
3271
3272 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
3273 ovs_header->dp_ifindex = dp->dp_ifindex;
3274
3275 if (dp->name) {
3276 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
3277 }
3278
3279 if (dp->upcall_pid) {
3280 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
3281 }
3282
3283 if (dp->user_features) {
3284 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
3285 }
3286
3287 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
3288 }
3289
3290 /* Clears 'dp' to "empty" values. */
3291 static void
3292 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
3293 {
3294 memset(dp, 0, sizeof *dp);
3295 }
3296
3297 static void
3298 dpif_netlink_dp_dump_start(struct nl_dump *dump)
3299 {
3300 struct dpif_netlink_dp request;
3301 struct ofpbuf *buf;
3302
3303 dpif_netlink_dp_init(&request);
3304 request.cmd = OVS_DP_CMD_GET;
3305
3306 buf = ofpbuf_new(1024);
3307 dpif_netlink_dp_to_ofpbuf(&request, buf);
3308 nl_dump_start(dump, NETLINK_GENERIC, buf);
3309 ofpbuf_delete(buf);
3310 }
3311
3312 /* Executes 'request' in the kernel datapath. If the command fails, returns a
3313 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
3314 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
3315 * result of the command is expected to be of the same form, which is decoded
3316 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
3317 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
3318 static int
3319 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
3320 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
3321 {
3322 struct ofpbuf *request_buf;
3323 int error;
3324
3325 ovs_assert((reply != NULL) == (bufp != NULL));
3326
3327 request_buf = ofpbuf_new(1024);
3328 dpif_netlink_dp_to_ofpbuf(request, request_buf);
3329 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
3330 ofpbuf_delete(request_buf);
3331
3332 if (reply) {
3333 dpif_netlink_dp_init(reply);
3334 if (!error) {
3335 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
3336 }
3337 if (error) {
3338 ofpbuf_delete(*bufp);
3339 *bufp = NULL;
3340 }
3341 }
3342 return error;
3343 }
3344
3345 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
3346 * The caller must free '*bufp' when the reply is no longer needed ('reply'
3347 * will contain pointers into '*bufp'). */
3348 static int
3349 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
3350 struct ofpbuf **bufp)
3351 {
3352 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
3353 struct dpif_netlink_dp request;
3354
3355 dpif_netlink_dp_init(&request);
3356 request.cmd = OVS_DP_CMD_GET;
3357 request.dp_ifindex = dpif->dp_ifindex;
3358
3359 return dpif_netlink_dp_transact(&request, reply, bufp);
3360 }
3361
3362 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
3363 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
3364 * positive errno value.
3365 *
3366 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
3367 * while 'flow' is still in use. */
3368 static int
3369 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
3370 const struct ofpbuf *buf)
3371 {
3372 static const struct nl_policy ovs_flow_policy[__OVS_FLOW_ATTR_MAX] = {
3373 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED, .optional = true },
3374 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
3375 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
3376 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
3377 .optional = true },
3378 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
3379 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
3380 [OVS_FLOW_ATTR_UFID] = { .type = NL_A_U128, .optional = true },
3381 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
3382 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
3383 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
3384 };
3385
3386 dpif_netlink_flow_init(flow);
3387
3388 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
3389 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
3390 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
3391 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
3392
3393 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
3394 if (!nlmsg || !genl || !ovs_header
3395 || nlmsg->nlmsg_type != ovs_flow_family
3396 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
3397 ARRAY_SIZE(ovs_flow_policy))) {
3398 return EINVAL;
3399 }
3400 if (!a[OVS_FLOW_ATTR_KEY] && !a[OVS_FLOW_ATTR_UFID]) {
3401 return EINVAL;
3402 }
3403
3404 flow->nlmsg_flags = nlmsg->nlmsg_flags;
3405 flow->dp_ifindex = ovs_header->dp_ifindex;
3406 if (a[OVS_FLOW_ATTR_KEY]) {
3407 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
3408 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
3409 }
3410
3411 if (a[OVS_FLOW_ATTR_UFID]) {
3412 flow->ufid = nl_attr_get_u128(a[OVS_FLOW_ATTR_UFID]);
3413 flow->ufid_present = true;
3414 }
3415 if (a[OVS_FLOW_ATTR_MASK]) {
3416 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
3417 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
3418 }
3419 if (a[OVS_FLOW_ATTR_ACTIONS]) {
3420 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
3421 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
3422 }
3423 if (a[OVS_FLOW_ATTR_STATS]) {
3424 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
3425 }
3426 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
3427 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
3428 }
3429 if (a[OVS_FLOW_ATTR_USED]) {
3430 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
3431 }
3432 return 0;
3433 }
3434
3435
3436 /*
3437 * If PACKET_TYPE attribute is present in 'data', it filters PACKET_TYPE out,
3438 * then puts 'data' to 'buf'.
3439 */
3440 static void
3441 put_exclude_packet_type(struct ofpbuf *buf, uint16_t type,
3442 const struct nlattr *data, uint16_t data_len)
3443 {
3444 const struct nlattr *packet_type;
3445
3446 packet_type = nl_attr_find__(data, data_len, OVS_KEY_ATTR_PACKET_TYPE);
3447
3448 if (packet_type) {
3449 /* exclude PACKET_TYPE Netlink attribute. */
3450 ovs_assert(NLA_ALIGN(packet_type->nla_len) == NL_A_U32_SIZE);
3451 size_t packet_type_len = NL_A_U32_SIZE;
3452 size_t first_chunk_size = (uint8_t *)packet_type - (uint8_t *)data;
3453 size_t second_chunk_size = data_len - first_chunk_size
3454 - packet_type_len;
3455 uint8_t *first_attr = NULL;
3456 struct nlattr *next_attr = nl_attr_next(packet_type);
3457
3458 first_attr = nl_msg_put_unspec_uninit(buf, type,
3459 data_len - packet_type_len);
3460 memcpy(first_attr, data, first_chunk_size);
3461 memcpy(first_attr + first_chunk_size, next_attr, second_chunk_size);
3462 } else {
3463 nl_msg_put_unspec(buf, type, data, data_len);
3464 }
3465 }
3466
3467 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
3468 * followed by Netlink attributes corresponding to 'flow'. */
3469 static void
3470 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
3471 struct ofpbuf *buf)
3472 {
3473 struct ovs_header *ovs_header;
3474
3475 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
3476 NLM_F_REQUEST | flow->nlmsg_flags,
3477 flow->cmd, OVS_FLOW_VERSION);
3478
3479 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
3480 ovs_header->dp_ifindex = flow->dp_ifindex;
3481
3482 if (flow->ufid_present) {
3483 nl_msg_put_u128(buf, OVS_FLOW_ATTR_UFID, flow->ufid);
3484 }
3485 if (flow->ufid_terse) {
3486 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UFID_FLAGS,
3487 OVS_UFID_F_OMIT_KEY | OVS_UFID_F_OMIT_MASK
3488 | OVS_UFID_F_OMIT_ACTIONS);
3489 }
3490 if (!flow->ufid_terse || !flow->ufid_present) {
3491 if (flow->key_len) {
3492 put_exclude_packet_type(buf, OVS_FLOW_ATTR_KEY, flow->key,
3493 flow->key_len);
3494 }
3495 if (flow->mask_len) {
3496 put_exclude_packet_type(buf, OVS_FLOW_ATTR_MASK, flow->mask,
3497 flow->mask_len);
3498 }
3499 if (flow->actions || flow->actions_len) {
3500 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
3501 flow->actions, flow->actions_len);
3502 }
3503 }
3504
3505 /* We never need to send these to the kernel. */
3506 ovs_assert(!flow->stats);
3507 ovs_assert(!flow->tcp_flags);
3508 ovs_assert(!flow->used);
3509
3510 if (flow->clear) {
3511 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
3512 }
3513 if (flow->probe) {
3514 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
3515 }
3516 }
3517
3518 /* Clears 'flow' to "empty" values. */
3519 static void
3520 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
3521 {
3522 memset(flow, 0, sizeof *flow);
3523 }
3524
3525 /* Executes 'request' in the kernel datapath. If the command fails, returns a
3526 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
3527 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
3528 * result of the command is expected to be a flow also, which is decoded and
3529 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
3530 * is no longer needed ('reply' will contain pointers into '*bufp'). */
3531 static int
3532 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
3533 struct dpif_netlink_flow *reply,
3534 struct ofpbuf **bufp)
3535 {
3536 struct ofpbuf *request_buf;
3537 int error;
3538
3539 ovs_assert((reply != NULL) == (bufp != NULL));
3540
3541 if (reply) {
3542 request->nlmsg_flags |= NLM_F_ECHO;
3543 }
3544
3545 request_buf = ofpbuf_new(1024);
3546 dpif_netlink_flow_to_ofpbuf(request, request_buf);
3547 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
3548 ofpbuf_delete(request_buf);
3549
3550 if (reply) {
3551 if (!error) {
3552 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
3553 }
3554 if (error) {
3555 dpif_netlink_flow_init(reply);
3556 ofpbuf_delete(*bufp);
3557 *bufp = NULL;
3558 }
3559 }
3560 return error;
3561 }
3562
3563 static void
3564 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
3565 struct dpif_flow_stats *stats)
3566 {
3567 if (flow->stats) {
3568 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
3569 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
3570 } else {
3571 stats->n_packets = 0;
3572 stats->n_bytes = 0;
3573 }
3574 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
3575 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
3576 }
3577
3578 /* Logs information about a packet that was recently lost in 'ch' (in
3579 * 'dpif_'). */
3580 static void
3581 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
3582 uint32_t handler_id)
3583 {
3584 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
3585 struct ds s;
3586
3587 if (VLOG_DROP_WARN(&rl)) {
3588 return;
3589 }
3590
3591 ds_init(&s);
3592 if (ch->last_poll != LLONG_MIN) {
3593 ds_put_format(&s, " (last polled %lld ms ago)",
3594 time_msec() - ch->last_poll);
3595 }
3596
3597 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
3598 dpif_name(&dpif->dpif), ch_idx, handler_id);
3599 ds_destroy(&s);
3600 }