]> git.proxmox.com Git - mirror_ovs.git/blob - lib/dpif-netlink.c
dpif-netlink: Fix some variable naming.
[mirror_ovs.git] / lib / dpif-netlink.c
1 /*
2 * Copyright (c) 2008-2018 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-netlink-rtnl.h"
37 #include "dpif-provider.h"
38 #include "fat-rwlock.h"
39 #include "flow.h"
40 #include "netdev-linux.h"
41 #include "netdev-offload.h"
42 #include "netdev-provider.h"
43 #include "netdev-vport.h"
44 #include "netdev.h"
45 #include "netlink-conntrack.h"
46 #include "netlink-notifier.h"
47 #include "netlink-socket.h"
48 #include "netlink.h"
49 #include "netnsid.h"
50 #include "odp-util.h"
51 #include "openvswitch/dynamic-string.h"
52 #include "openvswitch/flow.h"
53 #include "openvswitch/hmap.h"
54 #include "openvswitch/match.h"
55 #include "openvswitch/ofpbuf.h"
56 #include "openvswitch/poll-loop.h"
57 #include "openvswitch/shash.h"
58 #include "openvswitch/thread.h"
59 #include "openvswitch/vlog.h"
60 #include "packets.h"
61 #include "random.h"
62 #include "sset.h"
63 #include "timeval.h"
64 #include "unaligned.h"
65 #include "util.h"
66
67 VLOG_DEFINE_THIS_MODULE(dpif_netlink);
68 #ifdef _WIN32
69 #include "wmi.h"
70 enum { WINDOWS = 1 };
71 #else
72 enum { WINDOWS = 0 };
73 #endif
74 enum { MAX_PORTS = USHRT_MAX };
75
76 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
77 * missing if we have old headers. */
78 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
79
80 #define FLOW_DUMP_MAX_BATCH 50
81 #define OPERATE_MAX_OPS 50
82
83 #ifndef EPOLLEXCLUSIVE
84 #define EPOLLEXCLUSIVE (1u << 28)
85 #endif
86
87 struct dpif_netlink_dp {
88 /* Generic Netlink header. */
89 uint8_t cmd;
90
91 /* struct ovs_header. */
92 int dp_ifindex;
93
94 /* Attributes. */
95 const char *name; /* OVS_DP_ATTR_NAME. */
96 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
97 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
98 const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
99 const struct ovs_dp_megaflow_stats *megaflow_stats;
100 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
101 };
102
103 static void dpif_netlink_dp_init(struct dpif_netlink_dp *);
104 static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *,
105 const struct ofpbuf *);
106 static void dpif_netlink_dp_dump_start(struct nl_dump *);
107 static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
108 struct dpif_netlink_dp *reply,
109 struct ofpbuf **bufp);
110 static int dpif_netlink_dp_get(const struct dpif *,
111 struct dpif_netlink_dp *reply,
112 struct ofpbuf **bufp);
113
114 struct dpif_netlink_flow {
115 /* Generic Netlink header. */
116 uint8_t cmd;
117
118 /* struct ovs_header. */
119 unsigned int nlmsg_flags;
120 int dp_ifindex;
121
122 /* Attributes.
123 *
124 * The 'stats' member points to 64-bit data that might only be aligned on
125 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
126 * values.
127 *
128 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
129 * the Netlink version of the command, even if actions_len is zero. */
130 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
131 size_t key_len;
132 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
133 size_t mask_len;
134 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
135 size_t actions_len;
136 ovs_u128 ufid; /* OVS_FLOW_ATTR_FLOW_ID. */
137 bool ufid_present; /* Is there a UFID? */
138 bool ufid_terse; /* Skip serializing key/mask/acts? */
139 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
140 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
141 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
142 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
143 bool probe; /* OVS_FLOW_ATTR_PROBE. */
144 };
145
146 static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
147 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
148 const struct ofpbuf *);
149 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
150 struct ofpbuf *);
151 static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
152 struct dpif_netlink_flow *reply,
153 struct ofpbuf **bufp);
154 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
155 struct dpif_flow_stats *);
156 static void dpif_netlink_flow_to_dpif_flow(struct dpif *, struct dpif_flow *,
157 const struct dpif_netlink_flow *);
158
159 /* One of the dpif channels between the kernel and userspace. */
160 struct dpif_channel {
161 struct nl_sock *sock; /* Netlink socket. */
162 long long int last_poll; /* Last time this channel was polled. */
163 };
164
165 #ifdef _WIN32
166 #define VPORT_SOCK_POOL_SIZE 1
167 /* On Windows, there is no native support for epoll. There are equivalent
168 * interfaces though, that are not used currently. For simpicity, a pool of
169 * netlink sockets is used. Each socket is represented by 'struct
170 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
171 * sharing the same socket. In the future, we can add a reference count and
172 * such fields. */
173 struct dpif_windows_vport_sock {
174 struct nl_sock *nl_sock; /* netlink socket. */
175 };
176 #endif
177
178 struct dpif_handler {
179 struct epoll_event *epoll_events;
180 int epoll_fd; /* epoll fd that includes channel socks. */
181 int n_events; /* Num events returned by epoll_wait(). */
182 int event_offset; /* Offset into 'epoll_events'. */
183
184 #ifdef _WIN32
185 /* Pool of sockets. */
186 struct dpif_windows_vport_sock *vport_sock_pool;
187 size_t last_used_pool_idx; /* Index to aid in allocating a
188 socket in the pool to a port. */
189 #endif
190 };
191
192 /* Datapath interface for the openvswitch Linux kernel module. */
193 struct dpif_netlink {
194 struct dpif dpif;
195 int dp_ifindex;
196
197 /* Upcall messages. */
198 struct fat_rwlock upcall_lock;
199 struct dpif_handler *handlers;
200 uint32_t n_handlers; /* Num of upcall handlers. */
201 struct dpif_channel *channels; /* Array of channels for each port. */
202 int uc_array_size; /* Size of 'handler->channels' and */
203 /* 'handler->epoll_events'. */
204
205 /* Change notification. */
206 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
207 bool refresh_channels;
208 };
209
210 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
211 uint32_t ch_idx, uint32_t handler_id);
212
213 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
214
215 /* Generic Netlink family numbers for OVS.
216 *
217 * Initialized by dpif_netlink_init(). */
218 static int ovs_datapath_family;
219 static int ovs_vport_family;
220 static int ovs_flow_family;
221 static int ovs_packet_family;
222 static int ovs_meter_family;
223 static int ovs_ct_limit_family;
224
225 /* Generic Netlink multicast groups for OVS.
226 *
227 * Initialized by dpif_netlink_init(). */
228 static unsigned int ovs_vport_mcgroup;
229
230 /* If true, tunnel devices are created using OVS compat/genetlink.
231 * If false, tunnel devices are created with rtnetlink and using light weight
232 * tunnels. If we fail to create the tunnel the rtnetlink+LWT, then we fallback
233 * to using the compat interface. */
234 static bool ovs_tunnels_out_of_tree = true;
235
236 static int dpif_netlink_init(void);
237 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
238 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
239 odp_port_t port_no);
240 static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
241 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
242 uint32_t n_handlers);
243 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
244 struct ofpbuf *);
245 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
246 const struct ofpbuf *);
247 static int dpif_netlink_port_query__(const struct dpif_netlink *dpif,
248 odp_port_t port_no, const char *port_name,
249 struct dpif_port *dpif_port);
250
251 static int
252 create_nl_sock(struct dpif_netlink *dpif OVS_UNUSED, struct nl_sock **sockp)
253 OVS_REQ_WRLOCK(dpif->upcall_lock)
254 {
255 #ifndef _WIN32
256 return nl_sock_create(NETLINK_GENERIC, sockp);
257 #else
258 /* Pick netlink sockets to use in a round-robin fashion from each
259 * handler's pool of sockets. */
260 struct dpif_handler *handler = &dpif->handlers[0];
261 struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
262 size_t index = handler->last_used_pool_idx;
263
264 /* A pool of sockets is allocated when the handler is initialized. */
265 if (sock_pool == NULL) {
266 *sockp = NULL;
267 return EINVAL;
268 }
269
270 ovs_assert(index < VPORT_SOCK_POOL_SIZE);
271 *sockp = sock_pool[index].nl_sock;
272 ovs_assert(*sockp);
273 index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
274 handler->last_used_pool_idx = index;
275 return 0;
276 #endif
277 }
278
279 static void
280 close_nl_sock(struct nl_sock *sock)
281 {
282 #ifndef _WIN32
283 nl_sock_destroy(sock);
284 #endif
285 }
286
287 static struct dpif_netlink *
288 dpif_netlink_cast(const struct dpif *dpif)
289 {
290 dpif_assert_class(dpif, &dpif_netlink_class);
291 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
292 }
293
294 static int
295 dpif_netlink_enumerate(struct sset *all_dps,
296 const struct dpif_class *dpif_class OVS_UNUSED)
297 {
298 struct nl_dump dump;
299 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
300 struct ofpbuf msg, buf;
301 int error;
302
303 error = dpif_netlink_init();
304 if (error) {
305 return error;
306 }
307
308 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
309 dpif_netlink_dp_dump_start(&dump);
310 while (nl_dump_next(&dump, &msg, &buf)) {
311 struct dpif_netlink_dp dp;
312
313 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
314 sset_add(all_dps, dp.name);
315 }
316 }
317 ofpbuf_uninit(&buf);
318 return nl_dump_done(&dump);
319 }
320
321 static int
322 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
323 bool create, struct dpif **dpifp)
324 {
325 struct dpif_netlink_dp dp_request, dp;
326 struct ofpbuf *buf;
327 uint32_t upcall_pid;
328 int error;
329
330 error = dpif_netlink_init();
331 if (error) {
332 return error;
333 }
334
335 /* Create or look up datapath. */
336 dpif_netlink_dp_init(&dp_request);
337 if (create) {
338 dp_request.cmd = OVS_DP_CMD_NEW;
339 upcall_pid = 0;
340 dp_request.upcall_pid = &upcall_pid;
341 } else {
342 /* Use OVS_DP_CMD_SET to report user features */
343 dp_request.cmd = OVS_DP_CMD_SET;
344 }
345 dp_request.name = name;
346 dp_request.user_features |= OVS_DP_F_UNALIGNED;
347 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
348 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
349 if (error) {
350 return error;
351 }
352
353 error = open_dpif(&dp, dpifp);
354 ofpbuf_delete(buf);
355 return error;
356 }
357
358 static int
359 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
360 {
361 struct dpif_netlink *dpif;
362
363 dpif = xzalloc(sizeof *dpif);
364 dpif->port_notifier = NULL;
365 fat_rwlock_init(&dpif->upcall_lock);
366
367 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
368 dp->dp_ifindex, dp->dp_ifindex);
369
370 dpif->dp_ifindex = dp->dp_ifindex;
371 *dpifp = &dpif->dpif;
372
373 return 0;
374 }
375
376 #ifdef _WIN32
377 static void
378 vport_delete_sock_pool(struct dpif_handler *handler)
379 OVS_REQ_WRLOCK(dpif->upcall_lock)
380 {
381 if (handler->vport_sock_pool) {
382 uint32_t i;
383 struct dpif_windows_vport_sock *sock_pool =
384 handler->vport_sock_pool;
385
386 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
387 if (sock_pool[i].nl_sock) {
388 nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
389 nl_sock_destroy(sock_pool[i].nl_sock);
390 sock_pool[i].nl_sock = NULL;
391 }
392 }
393
394 free(handler->vport_sock_pool);
395 handler->vport_sock_pool = NULL;
396 }
397 }
398
399 static int
400 vport_create_sock_pool(struct dpif_handler *handler)
401 OVS_REQ_WRLOCK(dpif->upcall_lock)
402 {
403 struct dpif_windows_vport_sock *sock_pool;
404 size_t i;
405 int error = 0;
406
407 sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
408 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
409 error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
410 if (error) {
411 goto error;
412 }
413
414 /* Enable the netlink socket to receive packets. This is equivalent to
415 * calling nl_sock_join_mcgroup() to receive events. */
416 error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
417 if (error) {
418 goto error;
419 }
420 }
421
422 handler->vport_sock_pool = sock_pool;
423 handler->last_used_pool_idx = 0;
424 return 0;
425
426 error:
427 vport_delete_sock_pool(handler);
428 return error;
429 }
430 #endif /* _WIN32 */
431
432 /* Given the port number 'port_idx', extracts the pid of netlink socket
433 * associated to the port and assigns it to 'upcall_pid'. */
434 static bool
435 vport_get_pid(struct dpif_netlink *dpif, uint32_t port_idx,
436 uint32_t *upcall_pid)
437 {
438 /* Since the nl_sock can only be assigned in either all
439 * or none "dpif" channels, the following check
440 * would suffice. */
441 if (!dpif->channels[port_idx].sock) {
442 return false;
443 }
444 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
445
446 *upcall_pid = nl_sock_pid(dpif->channels[port_idx].sock);
447
448 return true;
449 }
450
451 static int
452 vport_add_channel(struct dpif_netlink *dpif, odp_port_t port_no,
453 struct nl_sock *sock)
454 {
455 struct epoll_event event;
456 uint32_t port_idx = odp_to_u32(port_no);
457 size_t i;
458 int error;
459
460 if (dpif->handlers == NULL) {
461 close_nl_sock(sock);
462 return 0;
463 }
464
465 /* We assume that the datapath densely chooses port numbers, which can
466 * therefore be used as an index into 'channels' and 'epoll_events' of
467 * 'dpif'. */
468 if (port_idx >= dpif->uc_array_size) {
469 uint32_t new_size = port_idx + 1;
470
471 if (new_size > MAX_PORTS) {
472 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
473 dpif_name(&dpif->dpif), port_no);
474 return EFBIG;
475 }
476
477 dpif->channels = xrealloc(dpif->channels,
478 new_size * sizeof *dpif->channels);
479
480 for (i = dpif->uc_array_size; i < new_size; i++) {
481 dpif->channels[i].sock = NULL;
482 }
483
484 for (i = 0; i < dpif->n_handlers; i++) {
485 struct dpif_handler *handler = &dpif->handlers[i];
486
487 handler->epoll_events = xrealloc(handler->epoll_events,
488 new_size * sizeof *handler->epoll_events);
489
490 }
491 dpif->uc_array_size = new_size;
492 }
493
494 memset(&event, 0, sizeof event);
495 event.events = EPOLLIN | EPOLLEXCLUSIVE;
496 event.data.u32 = port_idx;
497
498 for (i = 0; i < dpif->n_handlers; i++) {
499 struct dpif_handler *handler = &dpif->handlers[i];
500
501 #ifndef _WIN32
502 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
503 &event) < 0) {
504 error = errno;
505 goto error;
506 }
507 #endif
508 }
509 dpif->channels[port_idx].sock = sock;
510 dpif->channels[port_idx].last_poll = LLONG_MIN;
511
512 return 0;
513
514 error:
515 #ifndef _WIN32
516 while (i--) {
517 epoll_ctl(dpif->handlers[i].epoll_fd, EPOLL_CTL_DEL,
518 nl_sock_fd(sock), NULL);
519 }
520 #endif
521 dpif->channels[port_idx].sock = NULL;
522
523 return error;
524 }
525
526 static void
527 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
528 {
529 uint32_t port_idx = odp_to_u32(port_no);
530 size_t i;
531
532 if (!dpif->handlers || port_idx >= dpif->uc_array_size
533 || !dpif->channels[port_idx].sock) {
534 return;
535 }
536
537 for (i = 0; i < dpif->n_handlers; i++) {
538 struct dpif_handler *handler = &dpif->handlers[i];
539 #ifndef _WIN32
540 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
541 nl_sock_fd(dpif->channels[port_idx].sock), NULL);
542 #endif
543 handler->event_offset = handler->n_events = 0;
544 }
545 #ifndef _WIN32
546 nl_sock_destroy(dpif->channels[port_idx].sock);
547 #endif
548 dpif->channels[port_idx].sock = NULL;
549 }
550
551 static void
552 destroy_all_channels(struct dpif_netlink *dpif)
553 OVS_REQ_WRLOCK(dpif->upcall_lock)
554 {
555 unsigned int i;
556
557 if (!dpif->handlers) {
558 return;
559 }
560
561 for (i = 0; i < dpif->uc_array_size; i++ ) {
562 struct dpif_netlink_vport vport_request;
563 uint32_t upcall_pids = 0;
564
565 if (!dpif->channels[i].sock) {
566 continue;
567 }
568
569 /* Turn off upcalls. */
570 dpif_netlink_vport_init(&vport_request);
571 vport_request.cmd = OVS_VPORT_CMD_SET;
572 vport_request.dp_ifindex = dpif->dp_ifindex;
573 vport_request.port_no = u32_to_odp(i);
574 vport_request.n_upcall_pids = 1;
575 vport_request.upcall_pids = &upcall_pids;
576 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
577
578 vport_del_channels(dpif, u32_to_odp(i));
579 }
580
581 for (i = 0; i < dpif->n_handlers; i++) {
582 struct dpif_handler *handler = &dpif->handlers[i];
583
584 dpif_netlink_handler_uninit(handler);
585 free(handler->epoll_events);
586 }
587 free(dpif->channels);
588 free(dpif->handlers);
589 dpif->handlers = NULL;
590 dpif->channels = NULL;
591 dpif->n_handlers = 0;
592 dpif->uc_array_size = 0;
593 }
594
595 static void
596 dpif_netlink_close(struct dpif *dpif_)
597 {
598 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
599
600 nl_sock_destroy(dpif->port_notifier);
601
602 fat_rwlock_wrlock(&dpif->upcall_lock);
603 destroy_all_channels(dpif);
604 fat_rwlock_unlock(&dpif->upcall_lock);
605
606 fat_rwlock_destroy(&dpif->upcall_lock);
607 free(dpif);
608 }
609
610 static int
611 dpif_netlink_destroy(struct dpif *dpif_)
612 {
613 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
614 struct dpif_netlink_dp dp;
615
616 dpif_netlink_dp_init(&dp);
617 dp.cmd = OVS_DP_CMD_DEL;
618 dp.dp_ifindex = dpif->dp_ifindex;
619 return dpif_netlink_dp_transact(&dp, NULL, NULL);
620 }
621
622 static bool
623 dpif_netlink_run(struct dpif *dpif_)
624 {
625 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
626
627 if (dpif->refresh_channels) {
628 dpif->refresh_channels = false;
629 fat_rwlock_wrlock(&dpif->upcall_lock);
630 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
631 fat_rwlock_unlock(&dpif->upcall_lock);
632 }
633 return false;
634 }
635
636 static int
637 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
638 {
639 struct dpif_netlink_dp dp;
640 struct ofpbuf *buf;
641 int error;
642
643 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
644 if (!error) {
645 memset(stats, 0, sizeof *stats);
646
647 if (dp.stats) {
648 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
649 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
650 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
651 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
652 }
653
654 if (dp.megaflow_stats) {
655 stats->n_masks = dp.megaflow_stats->n_masks;
656 stats->n_mask_hit = get_32aligned_u64(
657 &dp.megaflow_stats->n_mask_hit);
658 } else {
659 stats->n_masks = UINT32_MAX;
660 stats->n_mask_hit = UINT64_MAX;
661 }
662 ofpbuf_delete(buf);
663 }
664 return error;
665 }
666
667 static const char *
668 get_vport_type(const struct dpif_netlink_vport *vport)
669 {
670 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
671
672 switch (vport->type) {
673 case OVS_VPORT_TYPE_NETDEV: {
674 const char *type = netdev_get_type_from_name(vport->name);
675
676 return type ? type : "system";
677 }
678
679 case OVS_VPORT_TYPE_INTERNAL:
680 return "internal";
681
682 case OVS_VPORT_TYPE_GENEVE:
683 return "geneve";
684
685 case OVS_VPORT_TYPE_GRE:
686 return "gre";
687
688 case OVS_VPORT_TYPE_VXLAN:
689 return "vxlan";
690
691 case OVS_VPORT_TYPE_LISP:
692 return "lisp";
693
694 case OVS_VPORT_TYPE_STT:
695 return "stt";
696
697 case OVS_VPORT_TYPE_ERSPAN:
698 return "erspan";
699
700 case OVS_VPORT_TYPE_IP6ERSPAN:
701 return "ip6erspan";
702
703 case OVS_VPORT_TYPE_IP6GRE:
704 return "ip6gre";
705
706 case OVS_VPORT_TYPE_UNSPEC:
707 case __OVS_VPORT_TYPE_MAX:
708 break;
709 }
710
711 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
712 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
713 return "unknown";
714 }
715
716 enum ovs_vport_type
717 netdev_to_ovs_vport_type(const char *type)
718 {
719 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
720 return OVS_VPORT_TYPE_NETDEV;
721 } else if (!strcmp(type, "internal")) {
722 return OVS_VPORT_TYPE_INTERNAL;
723 } else if (strstr(type, "stt")) {
724 return OVS_VPORT_TYPE_STT;
725 } else if (!strcmp(type, "geneve")) {
726 return OVS_VPORT_TYPE_GENEVE;
727 } else if (!strcmp(type, "vxlan")) {
728 return OVS_VPORT_TYPE_VXLAN;
729 } else if (!strcmp(type, "lisp")) {
730 return OVS_VPORT_TYPE_LISP;
731 } else if (!strcmp(type, "erspan")) {
732 return OVS_VPORT_TYPE_ERSPAN;
733 } else if (!strcmp(type, "ip6erspan")) {
734 return OVS_VPORT_TYPE_IP6ERSPAN;
735 } else if (!strcmp(type, "ip6gre")) {
736 return OVS_VPORT_TYPE_IP6GRE;
737 } else if (!strcmp(type, "gre")) {
738 return OVS_VPORT_TYPE_GRE;
739 } else {
740 return OVS_VPORT_TYPE_UNSPEC;
741 }
742 }
743
744 static int
745 dpif_netlink_port_add__(struct dpif_netlink *dpif, const char *name,
746 enum ovs_vport_type type,
747 struct ofpbuf *options,
748 odp_port_t *port_nop)
749 OVS_REQ_WRLOCK(dpif->upcall_lock)
750 {
751 struct dpif_netlink_vport request, reply;
752 struct ofpbuf *buf;
753 struct nl_sock *sock = NULL;
754 uint32_t upcall_pids = 0;
755 int error = 0;
756
757 if (dpif->handlers) {
758 error = create_nl_sock(dpif, &sock);
759 if (error) {
760 return error;
761 }
762 }
763
764 dpif_netlink_vport_init(&request);
765 request.cmd = OVS_VPORT_CMD_NEW;
766 request.dp_ifindex = dpif->dp_ifindex;
767 request.type = type;
768 request.name = name;
769
770 request.port_no = *port_nop;
771 if (sock) {
772 upcall_pids = nl_sock_pid(sock);
773 }
774 request.n_upcall_pids = 1;
775 request.upcall_pids = &upcall_pids;
776
777 if (options) {
778 request.options = options->data;
779 request.options_len = options->size;
780 }
781
782 error = dpif_netlink_vport_transact(&request, &reply, &buf);
783 if (!error) {
784 *port_nop = reply.port_no;
785 } else {
786 if (error == EBUSY && *port_nop != ODPP_NONE) {
787 VLOG_INFO("%s: requested port %"PRIu32" is in use",
788 dpif_name(&dpif->dpif), *port_nop);
789 }
790
791 close_nl_sock(sock);
792 goto exit;
793 }
794
795 error = vport_add_channel(dpif, *port_nop, sock);
796 if (error) {
797 VLOG_INFO("%s: could not add channel for port %s",
798 dpif_name(&dpif->dpif), name);
799
800 /* Delete the port. */
801 dpif_netlink_vport_init(&request);
802 request.cmd = OVS_VPORT_CMD_DEL;
803 request.dp_ifindex = dpif->dp_ifindex;
804 request.port_no = *port_nop;
805 dpif_netlink_vport_transact(&request, NULL, NULL);
806 close_nl_sock(sock);
807 goto exit;
808 }
809
810 exit:
811 ofpbuf_delete(buf);
812
813 return error;
814 }
815
816 static int
817 dpif_netlink_port_add_compat(struct dpif_netlink *dpif, struct netdev *netdev,
818 odp_port_t *port_nop)
819 OVS_REQ_WRLOCK(dpif->upcall_lock)
820 {
821 const struct netdev_tunnel_config *tnl_cfg;
822 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
823 const char *type = netdev_get_type(netdev);
824 uint64_t options_stub[64 / 8];
825 enum ovs_vport_type ovs_type;
826 struct ofpbuf options;
827 const char *name;
828
829 name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
830
831 ovs_type = netdev_to_ovs_vport_type(netdev_get_type(netdev));
832 if (ovs_type == OVS_VPORT_TYPE_UNSPEC) {
833 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
834 "unsupported type `%s'",
835 dpif_name(&dpif->dpif), name, type);
836 return EINVAL;
837 }
838
839 if (ovs_type == OVS_VPORT_TYPE_NETDEV) {
840 #ifdef _WIN32
841 /* XXX : Map appropiate Windows handle */
842 #else
843 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
844 #endif
845 }
846
847 #ifdef _WIN32
848 if (ovs_type == OVS_VPORT_TYPE_INTERNAL) {
849 if (!create_wmi_port(name)){
850 VLOG_ERR("Could not create wmi internal port with name:%s", name);
851 return EINVAL;
852 };
853 }
854 #endif
855
856 tnl_cfg = netdev_get_tunnel_config(netdev);
857 if (tnl_cfg && (tnl_cfg->dst_port != 0 || tnl_cfg->exts)) {
858 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
859 if (tnl_cfg->dst_port) {
860 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
861 ntohs(tnl_cfg->dst_port));
862 }
863 if (tnl_cfg->exts) {
864 size_t ext_ofs;
865 int i;
866
867 ext_ofs = nl_msg_start_nested(&options, OVS_TUNNEL_ATTR_EXTENSION);
868 for (i = 0; i < 32; i++) {
869 if (tnl_cfg->exts & (1 << i)) {
870 nl_msg_put_flag(&options, i);
871 }
872 }
873 nl_msg_end_nested(&options, ext_ofs);
874 }
875 return dpif_netlink_port_add__(dpif, name, ovs_type, &options,
876 port_nop);
877 } else {
878 return dpif_netlink_port_add__(dpif, name, ovs_type, NULL, port_nop);
879 }
880
881 }
882
883 static int
884 dpif_netlink_rtnl_port_create_and_add(struct dpif_netlink *dpif,
885 struct netdev *netdev,
886 odp_port_t *port_nop)
887 OVS_REQ_WRLOCK(dpif->upcall_lock)
888 {
889 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
890 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
891 const char *name;
892 int error;
893
894 error = dpif_netlink_rtnl_port_create(netdev);
895 if (error) {
896 if (error != EOPNOTSUPP) {
897 VLOG_WARN_RL(&rl, "Failed to create %s with rtnetlink: %s",
898 netdev_get_name(netdev), ovs_strerror(error));
899 }
900 return error;
901 }
902
903 name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
904 error = dpif_netlink_port_add__(dpif, name, OVS_VPORT_TYPE_NETDEV, NULL,
905 port_nop);
906 if (error) {
907 dpif_netlink_rtnl_port_destroy(name, netdev_get_type(netdev));
908 }
909 return error;
910 }
911
912 static int
913 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
914 odp_port_t *port_nop)
915 {
916 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
917 int error = EOPNOTSUPP;
918
919 fat_rwlock_wrlock(&dpif->upcall_lock);
920 if (!ovs_tunnels_out_of_tree) {
921 error = dpif_netlink_rtnl_port_create_and_add(dpif, netdev, port_nop);
922 }
923 if (error) {
924 error = dpif_netlink_port_add_compat(dpif, netdev, port_nop);
925 }
926 fat_rwlock_unlock(&dpif->upcall_lock);
927
928 return error;
929 }
930
931 static int
932 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
933 OVS_REQ_WRLOCK(dpif->upcall_lock)
934 {
935 struct dpif_netlink_vport vport;
936 struct dpif_port dpif_port;
937 int error;
938
939 error = dpif_netlink_port_query__(dpif, port_no, NULL, &dpif_port);
940 if (error) {
941 return error;
942 }
943
944 dpif_netlink_vport_init(&vport);
945 vport.cmd = OVS_VPORT_CMD_DEL;
946 vport.dp_ifindex = dpif->dp_ifindex;
947 vport.port_no = port_no;
948 #ifdef _WIN32
949 if (!strcmp(dpif_port.type, "internal")) {
950 if (!delete_wmi_port(dpif_port.name)) {
951 VLOG_ERR("Could not delete wmi port with name: %s",
952 dpif_port.name);
953 };
954 }
955 #endif
956 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
957
958 vport_del_channels(dpif, port_no);
959
960 if (!error && !ovs_tunnels_out_of_tree) {
961 error = dpif_netlink_rtnl_port_destroy(dpif_port.name, dpif_port.type);
962 if (error == EOPNOTSUPP) {
963 error = 0;
964 }
965 }
966
967 dpif_port_destroy(&dpif_port);
968
969 return error;
970 }
971
972 static int
973 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
974 {
975 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
976 int error;
977
978 fat_rwlock_wrlock(&dpif->upcall_lock);
979 error = dpif_netlink_port_del__(dpif, port_no);
980 fat_rwlock_unlock(&dpif->upcall_lock);
981
982 return error;
983 }
984
985 static int
986 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
987 const char *port_name, struct dpif_port *dpif_port)
988 {
989 struct dpif_netlink_vport request;
990 struct dpif_netlink_vport reply;
991 struct ofpbuf *buf;
992 int error;
993
994 dpif_netlink_vport_init(&request);
995 request.cmd = OVS_VPORT_CMD_GET;
996 request.dp_ifindex = dpif->dp_ifindex;
997 request.port_no = port_no;
998 request.name = port_name;
999
1000 error = dpif_netlink_vport_transact(&request, &reply, &buf);
1001 if (!error) {
1002 if (reply.dp_ifindex != request.dp_ifindex) {
1003 /* A query by name reported that 'port_name' is in some datapath
1004 * other than 'dpif', but the caller wants to know about 'dpif'. */
1005 error = ENODEV;
1006 } else if (dpif_port) {
1007 dpif_port->name = xstrdup(reply.name);
1008 dpif_port->type = xstrdup(get_vport_type(&reply));
1009 dpif_port->port_no = reply.port_no;
1010 }
1011 ofpbuf_delete(buf);
1012 }
1013 return error;
1014 }
1015
1016 static int
1017 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
1018 struct dpif_port *dpif_port)
1019 {
1020 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1021
1022 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
1023 }
1024
1025 static int
1026 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
1027 struct dpif_port *dpif_port)
1028 {
1029 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1030
1031 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
1032 }
1033
1034 static uint32_t
1035 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
1036 odp_port_t port_no)
1037 OVS_REQ_RDLOCK(dpif->upcall_lock)
1038 {
1039 uint32_t port_idx = odp_to_u32(port_no);
1040 uint32_t pid = 0;
1041
1042 if (dpif->handlers && dpif->uc_array_size > 0) {
1043 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1044 * channel, since it is not heavily loaded. */
1045 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
1046
1047 /* Needs to check in case the socket pointer is changed in between
1048 * the holding of upcall_lock. A known case happens when the main
1049 * thread deletes the vport while the handler thread is handling
1050 * the upcall from that port. */
1051 if (dpif->channels[idx].sock) {
1052 pid = nl_sock_pid(dpif->channels[idx].sock);
1053 }
1054 }
1055
1056 return pid;
1057 }
1058
1059 static uint32_t
1060 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
1061 {
1062 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1063 uint32_t ret;
1064
1065 fat_rwlock_rdlock(&dpif->upcall_lock);
1066 ret = dpif_netlink_port_get_pid__(dpif, port_no);
1067 fat_rwlock_unlock(&dpif->upcall_lock);
1068
1069 return ret;
1070 }
1071
1072 static int
1073 dpif_netlink_flow_flush(struct dpif *dpif_)
1074 {
1075 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1076 struct dpif_netlink_flow flow;
1077
1078 dpif_netlink_flow_init(&flow);
1079 flow.cmd = OVS_FLOW_CMD_DEL;
1080 flow.dp_ifindex = dpif->dp_ifindex;
1081
1082 if (netdev_is_flow_api_enabled()) {
1083 netdev_ports_flow_flush(dpif_->dpif_class);
1084 }
1085
1086 return dpif_netlink_flow_transact(&flow, NULL, NULL);
1087 }
1088
1089 struct dpif_netlink_port_state {
1090 struct nl_dump dump;
1091 struct ofpbuf buf;
1092 };
1093
1094 static void
1095 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
1096 struct nl_dump *dump)
1097 {
1098 struct dpif_netlink_vport request;
1099 struct ofpbuf *buf;
1100
1101 dpif_netlink_vport_init(&request);
1102 request.cmd = OVS_VPORT_CMD_GET;
1103 request.dp_ifindex = dpif->dp_ifindex;
1104
1105 buf = ofpbuf_new(1024);
1106 dpif_netlink_vport_to_ofpbuf(&request, buf);
1107 nl_dump_start(dump, NETLINK_GENERIC, buf);
1108 ofpbuf_delete(buf);
1109 }
1110
1111 static int
1112 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
1113 {
1114 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1115 struct dpif_netlink_port_state *state;
1116
1117 *statep = state = xmalloc(sizeof *state);
1118 dpif_netlink_port_dump_start__(dpif, &state->dump);
1119
1120 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
1121 return 0;
1122 }
1123
1124 static int
1125 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
1126 struct nl_dump *dump,
1127 struct dpif_netlink_vport *vport,
1128 struct ofpbuf *buffer)
1129 {
1130 struct ofpbuf buf;
1131 int error;
1132
1133 if (!nl_dump_next(dump, &buf, buffer)) {
1134 return EOF;
1135 }
1136
1137 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
1138 if (error) {
1139 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
1140 dpif_name(&dpif->dpif), ovs_strerror(error));
1141 }
1142 return error;
1143 }
1144
1145 static int
1146 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
1147 struct dpif_port *dpif_port)
1148 {
1149 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1150 struct dpif_netlink_port_state *state = state_;
1151 struct dpif_netlink_vport vport;
1152 int error;
1153
1154 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
1155 &state->buf);
1156 if (error) {
1157 return error;
1158 }
1159 dpif_port->name = CONST_CAST(char *, vport.name);
1160 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
1161 dpif_port->port_no = vport.port_no;
1162 return 0;
1163 }
1164
1165 static int
1166 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
1167 {
1168 struct dpif_netlink_port_state *state = state_;
1169 int error = nl_dump_done(&state->dump);
1170
1171 ofpbuf_uninit(&state->buf);
1172 free(state);
1173 return error;
1174 }
1175
1176 static int
1177 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1178 {
1179 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1180
1181 /* Lazily create the Netlink socket to listen for notifications. */
1182 if (!dpif->port_notifier) {
1183 struct nl_sock *sock;
1184 int error;
1185
1186 error = nl_sock_create(NETLINK_GENERIC, &sock);
1187 if (error) {
1188 return error;
1189 }
1190
1191 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1192 if (error) {
1193 nl_sock_destroy(sock);
1194 return error;
1195 }
1196 dpif->port_notifier = sock;
1197
1198 /* We have no idea of the current state so report that everything
1199 * changed. */
1200 return ENOBUFS;
1201 }
1202
1203 for (;;) {
1204 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1205 uint64_t buf_stub[4096 / 8];
1206 struct ofpbuf buf;
1207 int error;
1208
1209 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1210 error = nl_sock_recv(dpif->port_notifier, &buf, NULL, false);
1211 if (!error) {
1212 struct dpif_netlink_vport vport;
1213
1214 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1215 if (!error) {
1216 if (vport.dp_ifindex == dpif->dp_ifindex
1217 && (vport.cmd == OVS_VPORT_CMD_NEW
1218 || vport.cmd == OVS_VPORT_CMD_DEL
1219 || vport.cmd == OVS_VPORT_CMD_SET)) {
1220 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1221 dpif->dpif.full_name, vport.name, vport.cmd);
1222 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1223 dpif->refresh_channels = true;
1224 }
1225 *devnamep = xstrdup(vport.name);
1226 ofpbuf_uninit(&buf);
1227 return 0;
1228 }
1229 }
1230 } else if (error != EAGAIN) {
1231 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1232 ovs_strerror(error));
1233 nl_sock_drain(dpif->port_notifier);
1234 error = ENOBUFS;
1235 }
1236
1237 ofpbuf_uninit(&buf);
1238 if (error) {
1239 return error;
1240 }
1241 }
1242 }
1243
1244 static void
1245 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1246 {
1247 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1248
1249 if (dpif->port_notifier) {
1250 nl_sock_wait(dpif->port_notifier, POLLIN);
1251 } else {
1252 poll_immediate_wake();
1253 }
1254 }
1255
1256 static void
1257 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow *request,
1258 const ovs_u128 *ufid, bool terse)
1259 {
1260 if (ufid) {
1261 request->ufid = *ufid;
1262 request->ufid_present = true;
1263 } else {
1264 request->ufid_present = false;
1265 }
1266 request->ufid_terse = terse;
1267 }
1268
1269 static void
1270 dpif_netlink_init_flow_get__(const struct dpif_netlink *dpif,
1271 const struct nlattr *key, size_t key_len,
1272 const ovs_u128 *ufid, bool terse,
1273 struct dpif_netlink_flow *request)
1274 {
1275 dpif_netlink_flow_init(request);
1276 request->cmd = OVS_FLOW_CMD_GET;
1277 request->dp_ifindex = dpif->dp_ifindex;
1278 request->key = key;
1279 request->key_len = key_len;
1280 dpif_netlink_flow_init_ufid(request, ufid, terse);
1281 }
1282
1283 static void
1284 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1285 const struct dpif_flow_get *get,
1286 struct dpif_netlink_flow *request)
1287 {
1288 dpif_netlink_init_flow_get__(dpif, get->key, get->key_len, get->ufid,
1289 false, request);
1290 }
1291
1292 static int
1293 dpif_netlink_flow_get__(const struct dpif_netlink *dpif,
1294 const struct nlattr *key, size_t key_len,
1295 const ovs_u128 *ufid, bool terse,
1296 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1297 {
1298 struct dpif_netlink_flow request;
1299
1300 dpif_netlink_init_flow_get__(dpif, key, key_len, ufid, terse, &request);
1301 return dpif_netlink_flow_transact(&request, reply, bufp);
1302 }
1303
1304 static int
1305 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1306 const struct dpif_netlink_flow *flow,
1307 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1308 {
1309 return dpif_netlink_flow_get__(dpif, flow->key, flow->key_len,
1310 flow->ufid_present ? &flow->ufid : NULL,
1311 false, reply, bufp);
1312 }
1313
1314 static void
1315 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1316 const struct dpif_flow_put *put,
1317 struct dpif_netlink_flow *request)
1318 {
1319 static const struct nlattr dummy_action;
1320
1321 dpif_netlink_flow_init(request);
1322 request->cmd = (put->flags & DPIF_FP_CREATE
1323 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1324 request->dp_ifindex = dpif->dp_ifindex;
1325 request->key = put->key;
1326 request->key_len = put->key_len;
1327 request->mask = put->mask;
1328 request->mask_len = put->mask_len;
1329 dpif_netlink_flow_init_ufid(request, put->ufid, false);
1330
1331 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1332 request->actions = (put->actions
1333 ? put->actions
1334 : CONST_CAST(struct nlattr *, &dummy_action));
1335 request->actions_len = put->actions_len;
1336 if (put->flags & DPIF_FP_ZERO_STATS) {
1337 request->clear = true;
1338 }
1339 if (put->flags & DPIF_FP_PROBE) {
1340 request->probe = true;
1341 }
1342 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1343 }
1344
1345 static void
1346 dpif_netlink_init_flow_del__(struct dpif_netlink *dpif,
1347 const struct nlattr *key, size_t key_len,
1348 const ovs_u128 *ufid, bool terse,
1349 struct dpif_netlink_flow *request)
1350 {
1351 dpif_netlink_flow_init(request);
1352 request->cmd = OVS_FLOW_CMD_DEL;
1353 request->dp_ifindex = dpif->dp_ifindex;
1354 request->key = key;
1355 request->key_len = key_len;
1356 dpif_netlink_flow_init_ufid(request, ufid, terse);
1357 }
1358
1359 static void
1360 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1361 const struct dpif_flow_del *del,
1362 struct dpif_netlink_flow *request)
1363 {
1364 dpif_netlink_init_flow_del__(dpif, del->key, del->key_len,
1365 del->ufid, del->terse, request);
1366 }
1367
1368 struct dpif_netlink_flow_dump {
1369 struct dpif_flow_dump up;
1370 struct nl_dump nl_dump;
1371 atomic_int status;
1372 struct netdev_flow_dump **netdev_dumps;
1373 int netdev_dumps_num; /* Number of netdev_flow_dumps */
1374 struct ovs_mutex netdev_lock; /* Guards the following. */
1375 int netdev_current_dump OVS_GUARDED; /* Shared current dump */
1376 struct dpif_flow_dump_types types; /* Type of dump */
1377 };
1378
1379 static struct dpif_netlink_flow_dump *
1380 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1381 {
1382 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1383 }
1384
1385 static void
1386 start_netdev_dump(const struct dpif *dpif_,
1387 struct dpif_netlink_flow_dump *dump)
1388 {
1389 ovs_mutex_init(&dump->netdev_lock);
1390
1391 if (!(dump->types.netdev_flows)) {
1392 dump->netdev_dumps_num = 0;
1393 dump->netdev_dumps = NULL;
1394 return;
1395 }
1396
1397 ovs_mutex_lock(&dump->netdev_lock);
1398 dump->netdev_current_dump = 0;
1399 dump->netdev_dumps
1400 = netdev_ports_flow_dump_create(dpif_->dpif_class,
1401 &dump->netdev_dumps_num);
1402 ovs_mutex_unlock(&dump->netdev_lock);
1403 }
1404
1405 static void
1406 dpif_netlink_populate_flow_dump_types(struct dpif_netlink_flow_dump *dump,
1407 struct dpif_flow_dump_types *types)
1408 {
1409 if (!types) {
1410 dump->types.ovs_flows = true;
1411 dump->types.netdev_flows = true;
1412 } else {
1413 memcpy(&dump->types, types, sizeof *types);
1414 }
1415 }
1416
1417 static struct dpif_flow_dump *
1418 dpif_netlink_flow_dump_create(const struct dpif *dpif_, bool terse,
1419 struct dpif_flow_dump_types *types)
1420 {
1421 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1422 struct dpif_netlink_flow_dump *dump;
1423 struct dpif_netlink_flow request;
1424 struct ofpbuf *buf;
1425
1426 dump = xmalloc(sizeof *dump);
1427 dpif_flow_dump_init(&dump->up, dpif_);
1428
1429 dpif_netlink_populate_flow_dump_types(dump, types);
1430
1431 if (dump->types.ovs_flows) {
1432 dpif_netlink_flow_init(&request);
1433 request.cmd = OVS_FLOW_CMD_GET;
1434 request.dp_ifindex = dpif->dp_ifindex;
1435 request.ufid_present = false;
1436 request.ufid_terse = terse;
1437
1438 buf = ofpbuf_new(1024);
1439 dpif_netlink_flow_to_ofpbuf(&request, buf);
1440 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1441 ofpbuf_delete(buf);
1442 }
1443 atomic_init(&dump->status, 0);
1444 dump->up.terse = terse;
1445
1446 start_netdev_dump(dpif_, dump);
1447
1448 return &dump->up;
1449 }
1450
1451 static int
1452 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1453 {
1454 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1455 unsigned int nl_status = 0;
1456 int dump_status;
1457
1458 if (dump->types.ovs_flows) {
1459 nl_status = nl_dump_done(&dump->nl_dump);
1460 }
1461
1462 for (int i = 0; i < dump->netdev_dumps_num; i++) {
1463 int err = netdev_flow_dump_destroy(dump->netdev_dumps[i]);
1464
1465 if (err != 0 && err != EOPNOTSUPP) {
1466 VLOG_ERR("failed dumping netdev: %s", ovs_strerror(err));
1467 }
1468 }
1469
1470 free(dump->netdev_dumps);
1471 ovs_mutex_destroy(&dump->netdev_lock);
1472
1473 /* No other thread has access to 'dump' at this point. */
1474 atomic_read_relaxed(&dump->status, &dump_status);
1475 free(dump);
1476 return dump_status ? dump_status : nl_status;
1477 }
1478
1479 struct dpif_netlink_flow_dump_thread {
1480 struct dpif_flow_dump_thread up;
1481 struct dpif_netlink_flow_dump *dump;
1482 struct dpif_netlink_flow flow;
1483 struct dpif_flow_stats stats;
1484 struct ofpbuf nl_flows; /* Always used to store flows. */
1485 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1486 int netdev_dump_idx; /* This thread current netdev dump index */
1487 bool netdev_done; /* If we are finished dumping netdevs */
1488
1489 /* (Key/Mask/Actions) Buffers for netdev dumping */
1490 struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH];
1491 struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH];
1492 struct odputil_keybuf actbuf[FLOW_DUMP_MAX_BATCH];
1493 };
1494
1495 static struct dpif_netlink_flow_dump_thread *
1496 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1497 {
1498 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1499 }
1500
1501 static struct dpif_flow_dump_thread *
1502 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1503 {
1504 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1505 struct dpif_netlink_flow_dump_thread *thread;
1506
1507 thread = xmalloc(sizeof *thread);
1508 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1509 thread->dump = dump;
1510 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1511 thread->nl_actions = NULL;
1512 thread->netdev_dump_idx = 0;
1513 thread->netdev_done = !(thread->netdev_dump_idx < dump->netdev_dumps_num);
1514
1515 return &thread->up;
1516 }
1517
1518 static void
1519 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1520 {
1521 struct dpif_netlink_flow_dump_thread *thread
1522 = dpif_netlink_flow_dump_thread_cast(thread_);
1523
1524 ofpbuf_uninit(&thread->nl_flows);
1525 ofpbuf_delete(thread->nl_actions);
1526 free(thread);
1527 }
1528
1529 static void
1530 dpif_netlink_flow_to_dpif_flow(struct dpif *dpif, struct dpif_flow *dpif_flow,
1531 const struct dpif_netlink_flow *datapath_flow)
1532 {
1533 dpif_flow->key = datapath_flow->key;
1534 dpif_flow->key_len = datapath_flow->key_len;
1535 dpif_flow->mask = datapath_flow->mask;
1536 dpif_flow->mask_len = datapath_flow->mask_len;
1537 dpif_flow->actions = datapath_flow->actions;
1538 dpif_flow->actions_len = datapath_flow->actions_len;
1539 dpif_flow->ufid_present = datapath_flow->ufid_present;
1540 dpif_flow->pmd_id = PMD_ID_NULL;
1541 if (datapath_flow->ufid_present) {
1542 dpif_flow->ufid = datapath_flow->ufid;
1543 } else {
1544 ovs_assert(datapath_flow->key && datapath_flow->key_len);
1545 dpif_flow_hash(dpif, datapath_flow->key, datapath_flow->key_len,
1546 &dpif_flow->ufid);
1547 }
1548 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1549 dpif_flow->attrs.offloaded = false;
1550 dpif_flow->attrs.dp_layer = "ovs";
1551 }
1552
1553 /* The design is such that all threads are working together on the first dump
1554 * to the last, in order (at first they all on dump 0).
1555 * When the first thread finds that the given dump is finished,
1556 * they all move to the next. If two or more threads find the same dump
1557 * is finished at the same time, the first one will advance the shared
1558 * netdev_current_dump and the others will catch up. */
1559 static void
1560 dpif_netlink_advance_netdev_dump(struct dpif_netlink_flow_dump_thread *thread)
1561 {
1562 struct dpif_netlink_flow_dump *dump = thread->dump;
1563
1564 ovs_mutex_lock(&dump->netdev_lock);
1565 /* if we haven't finished (dumped everything) */
1566 if (dump->netdev_current_dump < dump->netdev_dumps_num) {
1567 /* if we are the first to find that current dump is finished
1568 * advance it. */
1569 if (thread->netdev_dump_idx == dump->netdev_current_dump) {
1570 thread->netdev_dump_idx = ++dump->netdev_current_dump;
1571 /* did we just finish the last dump? done. */
1572 if (dump->netdev_current_dump == dump->netdev_dumps_num) {
1573 thread->netdev_done = true;
1574 }
1575 } else {
1576 /* otherwise, we are behind, catch up */
1577 thread->netdev_dump_idx = dump->netdev_current_dump;
1578 }
1579 } else {
1580 /* some other thread finished */
1581 thread->netdev_done = true;
1582 }
1583 ovs_mutex_unlock(&dump->netdev_lock);
1584 }
1585
1586 static int
1587 dpif_netlink_netdev_match_to_dpif_flow(struct match *match,
1588 struct ofpbuf *key_buf,
1589 struct ofpbuf *mask_buf,
1590 struct nlattr *actions,
1591 struct dpif_flow_stats *stats,
1592 struct dpif_flow_attrs *attrs,
1593 ovs_u128 *ufid,
1594 struct dpif_flow *flow,
1595 bool terse OVS_UNUSED)
1596 {
1597
1598 struct odp_flow_key_parms odp_parms = {
1599 .flow = &match->flow,
1600 .mask = &match->wc.masks,
1601 .support = {
1602 .max_vlan_headers = 2,
1603 },
1604 };
1605 size_t offset;
1606
1607 memset(flow, 0, sizeof *flow);
1608
1609 /* Key */
1610 offset = key_buf->size;
1611 flow->key = ofpbuf_tail(key_buf);
1612 odp_flow_key_from_flow(&odp_parms, key_buf);
1613 flow->key_len = key_buf->size - offset;
1614
1615 /* Mask */
1616 offset = mask_buf->size;
1617 flow->mask = ofpbuf_tail(mask_buf);
1618 odp_parms.key_buf = key_buf;
1619 odp_flow_key_from_mask(&odp_parms, mask_buf);
1620 flow->mask_len = mask_buf->size - offset;
1621
1622 /* Actions */
1623 flow->actions = nl_attr_get(actions);
1624 flow->actions_len = nl_attr_get_size(actions);
1625
1626 /* Stats */
1627 memcpy(&flow->stats, stats, sizeof *stats);
1628
1629 /* UFID */
1630 flow->ufid_present = true;
1631 flow->ufid = *ufid;
1632
1633 flow->pmd_id = PMD_ID_NULL;
1634
1635 memcpy(&flow->attrs, attrs, sizeof *attrs);
1636
1637 return 0;
1638 }
1639
1640 static int
1641 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1642 struct dpif_flow *flows, int max_flows)
1643 {
1644 struct dpif_netlink_flow_dump_thread *thread
1645 = dpif_netlink_flow_dump_thread_cast(thread_);
1646 struct dpif_netlink_flow_dump *dump = thread->dump;
1647 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1648 int n_flows;
1649
1650 ofpbuf_delete(thread->nl_actions);
1651 thread->nl_actions = NULL;
1652
1653 n_flows = 0;
1654 max_flows = MIN(max_flows, FLOW_DUMP_MAX_BATCH);
1655
1656 while (!thread->netdev_done && n_flows < max_flows) {
1657 struct odputil_keybuf *maskbuf = &thread->maskbuf[n_flows];
1658 struct odputil_keybuf *keybuf = &thread->keybuf[n_flows];
1659 struct odputil_keybuf *actbuf = &thread->actbuf[n_flows];
1660 struct ofpbuf key, mask, act;
1661 struct dpif_flow *f = &flows[n_flows];
1662 int cur = thread->netdev_dump_idx;
1663 struct netdev_flow_dump *netdev_dump = dump->netdev_dumps[cur];
1664 struct match match;
1665 struct nlattr *actions;
1666 struct dpif_flow_stats stats;
1667 struct dpif_flow_attrs attrs;
1668 ovs_u128 ufid;
1669 bool has_next;
1670
1671 ofpbuf_use_stack(&key, keybuf, sizeof *keybuf);
1672 ofpbuf_use_stack(&act, actbuf, sizeof *actbuf);
1673 ofpbuf_use_stack(&mask, maskbuf, sizeof *maskbuf);
1674 has_next = netdev_flow_dump_next(netdev_dump, &match,
1675 &actions, &stats, &attrs,
1676 &ufid,
1677 &thread->nl_flows,
1678 &act);
1679 if (has_next) {
1680 dpif_netlink_netdev_match_to_dpif_flow(&match,
1681 &key, &mask,
1682 actions,
1683 &stats,
1684 &attrs,
1685 &ufid,
1686 f,
1687 dump->up.terse);
1688 n_flows++;
1689 } else {
1690 dpif_netlink_advance_netdev_dump(thread);
1691 }
1692 }
1693
1694 if (!(dump->types.ovs_flows)) {
1695 return n_flows;
1696 }
1697
1698 while (!n_flows
1699 || (n_flows < max_flows && thread->nl_flows.size)) {
1700 struct dpif_netlink_flow datapath_flow;
1701 struct ofpbuf nl_flow;
1702 int error;
1703
1704 /* Try to grab another flow. */
1705 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1706 break;
1707 }
1708
1709 /* Convert the flow to our output format. */
1710 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1711 if (error) {
1712 atomic_store_relaxed(&dump->status, error);
1713 break;
1714 }
1715
1716 if (dump->up.terse || datapath_flow.actions) {
1717 /* Common case: we don't want actions, or the flow includes
1718 * actions. */
1719 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1720 &datapath_flow);
1721 } else {
1722 /* Rare case: the flow does not include actions. Retrieve this
1723 * individual flow again to get the actions. */
1724 error = dpif_netlink_flow_get(dpif, &datapath_flow,
1725 &datapath_flow, &thread->nl_actions);
1726 if (error == ENOENT) {
1727 VLOG_DBG("dumped flow disappeared on get");
1728 continue;
1729 } else if (error) {
1730 VLOG_WARN("error fetching dumped flow: %s",
1731 ovs_strerror(error));
1732 atomic_store_relaxed(&dump->status, error);
1733 break;
1734 }
1735
1736 /* Save this flow. Then exit, because we only have one buffer to
1737 * handle this case. */
1738 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1739 &datapath_flow);
1740 break;
1741 }
1742 }
1743 return n_flows;
1744 }
1745
1746 static void
1747 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1748 struct ofpbuf *buf)
1749 {
1750 struct ovs_header *k_exec;
1751 size_t key_ofs;
1752
1753 ofpbuf_prealloc_tailroom(buf, (64
1754 + dp_packet_size(d_exec->packet)
1755 + ODP_KEY_METADATA_SIZE
1756 + d_exec->actions_len));
1757
1758 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1759 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1760
1761 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1762 k_exec->dp_ifindex = dp_ifindex;
1763
1764 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1765 dp_packet_data(d_exec->packet),
1766 dp_packet_size(d_exec->packet));
1767
1768 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1769 odp_key_from_dp_packet(buf, d_exec->packet);
1770 nl_msg_end_nested(buf, key_ofs);
1771
1772 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1773 d_exec->actions, d_exec->actions_len);
1774 if (d_exec->probe) {
1775 nl_msg_put_flag(buf, OVS_PACKET_ATTR_PROBE);
1776 }
1777 if (d_exec->mtu) {
1778 nl_msg_put_u16(buf, OVS_PACKET_ATTR_MRU, d_exec->mtu);
1779 }
1780 }
1781
1782 /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1783 * Returns the number actually executed (at least 1, if 'n_ops' is
1784 * positive). */
1785 static size_t
1786 dpif_netlink_operate__(struct dpif_netlink *dpif,
1787 struct dpif_op **ops, size_t n_ops)
1788 {
1789 struct op_auxdata {
1790 struct nl_transaction txn;
1791
1792 struct ofpbuf request;
1793 uint64_t request_stub[1024 / 8];
1794
1795 struct ofpbuf reply;
1796 uint64_t reply_stub[1024 / 8];
1797 } auxes[OPERATE_MAX_OPS];
1798
1799 struct nl_transaction *txnsp[OPERATE_MAX_OPS];
1800 size_t i;
1801
1802 n_ops = MIN(n_ops, OPERATE_MAX_OPS);
1803 for (i = 0; i < n_ops; i++) {
1804 struct op_auxdata *aux = &auxes[i];
1805 struct dpif_op *op = ops[i];
1806 struct dpif_flow_put *put;
1807 struct dpif_flow_del *del;
1808 struct dpif_flow_get *get;
1809 struct dpif_netlink_flow flow;
1810
1811 ofpbuf_use_stub(&aux->request,
1812 aux->request_stub, sizeof aux->request_stub);
1813 aux->txn.request = &aux->request;
1814
1815 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1816 aux->txn.reply = NULL;
1817
1818 switch (op->type) {
1819 case DPIF_OP_FLOW_PUT:
1820 put = &op->flow_put;
1821 dpif_netlink_init_flow_put(dpif, put, &flow);
1822 if (put->stats) {
1823 flow.nlmsg_flags |= NLM_F_ECHO;
1824 aux->txn.reply = &aux->reply;
1825 }
1826 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1827 break;
1828
1829 case DPIF_OP_FLOW_DEL:
1830 del = &op->flow_del;
1831 dpif_netlink_init_flow_del(dpif, del, &flow);
1832 if (del->stats) {
1833 flow.nlmsg_flags |= NLM_F_ECHO;
1834 aux->txn.reply = &aux->reply;
1835 }
1836 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1837 break;
1838
1839 case DPIF_OP_EXECUTE:
1840 /* Can't execute a packet that won't fit in a Netlink attribute. */
1841 if (OVS_UNLIKELY(nl_attr_oversized(
1842 dp_packet_size(op->execute.packet)))) {
1843 /* Report an error immediately if this is the first operation.
1844 * Otherwise the easiest thing to do is to postpone to the next
1845 * call (when this will be the first operation). */
1846 if (i == 0) {
1847 VLOG_ERR_RL(&error_rl,
1848 "dropping oversized %"PRIu32"-byte packet",
1849 dp_packet_size(op->execute.packet));
1850 op->error = ENOBUFS;
1851 return 1;
1852 }
1853 n_ops = i;
1854 } else {
1855 dpif_netlink_encode_execute(dpif->dp_ifindex, &op->execute,
1856 &aux->request);
1857 }
1858 break;
1859
1860 case DPIF_OP_FLOW_GET:
1861 get = &op->flow_get;
1862 dpif_netlink_init_flow_get(dpif, get, &flow);
1863 aux->txn.reply = get->buffer;
1864 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1865 break;
1866
1867 default:
1868 OVS_NOT_REACHED();
1869 }
1870 }
1871
1872 for (i = 0; i < n_ops; i++) {
1873 txnsp[i] = &auxes[i].txn;
1874 }
1875 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1876
1877 for (i = 0; i < n_ops; i++) {
1878 struct op_auxdata *aux = &auxes[i];
1879 struct nl_transaction *txn = &auxes[i].txn;
1880 struct dpif_op *op = ops[i];
1881 struct dpif_flow_put *put;
1882 struct dpif_flow_del *del;
1883 struct dpif_flow_get *get;
1884
1885 op->error = txn->error;
1886
1887 switch (op->type) {
1888 case DPIF_OP_FLOW_PUT:
1889 put = &op->flow_put;
1890 if (put->stats) {
1891 if (!op->error) {
1892 struct dpif_netlink_flow reply;
1893
1894 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1895 txn->reply);
1896 if (!op->error) {
1897 dpif_netlink_flow_get_stats(&reply, put->stats);
1898 }
1899 }
1900 }
1901 break;
1902
1903 case DPIF_OP_FLOW_DEL:
1904 del = &op->flow_del;
1905 if (del->stats) {
1906 if (!op->error) {
1907 struct dpif_netlink_flow reply;
1908
1909 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1910 txn->reply);
1911 if (!op->error) {
1912 dpif_netlink_flow_get_stats(&reply, del->stats);
1913 }
1914 }
1915 }
1916 break;
1917
1918 case DPIF_OP_EXECUTE:
1919 break;
1920
1921 case DPIF_OP_FLOW_GET:
1922 get = &op->flow_get;
1923 if (!op->error) {
1924 struct dpif_netlink_flow reply;
1925
1926 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
1927 if (!op->error) {
1928 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, get->flow,
1929 &reply);
1930 }
1931 }
1932 break;
1933
1934 default:
1935 OVS_NOT_REACHED();
1936 }
1937
1938 ofpbuf_uninit(&aux->request);
1939 ofpbuf_uninit(&aux->reply);
1940 }
1941
1942 return n_ops;
1943 }
1944
1945 static int
1946 parse_flow_get(struct dpif_netlink *dpif, struct dpif_flow_get *get)
1947 {
1948 struct dpif_flow *dpif_flow = get->flow;
1949 struct match match;
1950 struct nlattr *actions;
1951 struct dpif_flow_stats stats;
1952 struct dpif_flow_attrs attrs;
1953 struct ofpbuf buf;
1954 uint64_t act_buf[1024 / 8];
1955 struct odputil_keybuf maskbuf;
1956 struct odputil_keybuf keybuf;
1957 struct odputil_keybuf actbuf;
1958 struct ofpbuf key, mask, act;
1959 int err;
1960
1961 ofpbuf_use_stack(&buf, &act_buf, sizeof act_buf);
1962 err = netdev_ports_flow_get(dpif->dpif.dpif_class, &match,
1963 &actions, get->ufid, &stats, &attrs, &buf);
1964 if (err) {
1965 return err;
1966 }
1967
1968 VLOG_DBG("found flow from netdev, translating to dpif flow");
1969
1970 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1971 ofpbuf_use_stack(&act, &actbuf, sizeof actbuf);
1972 ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf);
1973 dpif_netlink_netdev_match_to_dpif_flow(&match, &key, &mask, actions,
1974 &stats, &attrs,
1975 (ovs_u128 *) get->ufid,
1976 dpif_flow,
1977 false);
1978 ofpbuf_put(get->buffer, nl_attr_get(actions), nl_attr_get_size(actions));
1979 dpif_flow->actions = ofpbuf_at(get->buffer, 0, 0);
1980 dpif_flow->actions_len = nl_attr_get_size(actions);
1981
1982 return 0;
1983 }
1984
1985 static int
1986 parse_flow_put(struct dpif_netlink *dpif, struct dpif_flow_put *put)
1987 {
1988 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1989 const struct dpif_class *dpif_class = dpif->dpif.dpif_class;
1990 struct match match;
1991 odp_port_t in_port;
1992 const struct nlattr *nla;
1993 size_t left;
1994 struct netdev *dev;
1995 struct offload_info info;
1996 ovs_be16 dst_port = 0;
1997 uint8_t csum_on = false;
1998 int err;
1999
2000 if (put->flags & DPIF_FP_PROBE) {
2001 return EOPNOTSUPP;
2002 }
2003
2004 err = parse_key_and_mask_to_match(put->key, put->key_len, put->mask,
2005 put->mask_len, &match);
2006 if (err) {
2007 return err;
2008 }
2009
2010 in_port = match.flow.in_port.odp_port;
2011 dev = netdev_ports_get(in_port, dpif_class);
2012 if (!dev) {
2013 return EOPNOTSUPP;
2014 }
2015
2016 /* Get tunnel dst port */
2017 NL_ATTR_FOR_EACH(nla, left, put->actions, put->actions_len) {
2018 if (nl_attr_type(nla) == OVS_ACTION_ATTR_OUTPUT) {
2019 const struct netdev_tunnel_config *tnl_cfg;
2020 struct netdev *outdev;
2021 odp_port_t out_port;
2022
2023 out_port = nl_attr_get_odp_port(nla);
2024 outdev = netdev_ports_get(out_port, dpif_class);
2025 if (!outdev) {
2026 err = EOPNOTSUPP;
2027 goto out;
2028 }
2029 tnl_cfg = netdev_get_tunnel_config(outdev);
2030 if (tnl_cfg && tnl_cfg->dst_port != 0) {
2031 dst_port = tnl_cfg->dst_port;
2032 }
2033 if (tnl_cfg) {
2034 csum_on = tnl_cfg->csum;
2035 }
2036 netdev_close(outdev);
2037 }
2038 }
2039
2040 info.dpif_class = dpif_class;
2041 info.tp_dst_port = dst_port;
2042 info.tunnel_csum_on = csum_on;
2043 err = netdev_flow_put(dev, &match,
2044 CONST_CAST(struct nlattr *, put->actions),
2045 put->actions_len,
2046 CONST_CAST(ovs_u128 *, put->ufid),
2047 &info, put->stats);
2048
2049 if (!err) {
2050 if (put->flags & DPIF_FP_MODIFY) {
2051 struct dpif_op *opp;
2052 struct dpif_op op;
2053
2054 op.type = DPIF_OP_FLOW_DEL;
2055 op.flow_del.key = put->key;
2056 op.flow_del.key_len = put->key_len;
2057 op.flow_del.ufid = put->ufid;
2058 op.flow_del.pmd_id = put->pmd_id;
2059 op.flow_del.stats = NULL;
2060 op.flow_del.terse = false;
2061
2062 opp = &op;
2063 dpif_netlink_operate__(dpif, &opp, 1);
2064 }
2065
2066 VLOG_DBG("added flow");
2067 } else if (err != EEXIST) {
2068 struct netdev *oor_netdev = NULL;
2069 enum vlog_level level;
2070 if (err == ENOSPC && netdev_is_offload_rebalance_policy_enabled()) {
2071 /*
2072 * We need to set OOR on the input netdev (i.e, 'dev') for the
2073 * flow. But if the flow has a tunnel attribute (i.e, decap action,
2074 * with a virtual device like a VxLAN interface as its in-port),
2075 * then lookup and set OOR on the underlying tunnel (real) netdev.
2076 */
2077 oor_netdev = flow_get_tunnel_netdev(&match.flow.tunnel);
2078 if (!oor_netdev) {
2079 /* Not a 'tunnel' flow */
2080 oor_netdev = dev;
2081 }
2082 netdev_set_hw_info(oor_netdev, HW_INFO_TYPE_OOR, true);
2083 }
2084 level = (err == ENOSPC || err == EOPNOTSUPP) ? VLL_DBG : VLL_ERR;
2085 VLOG_RL(&rl, level, "failed to offload flow: %s: %s",
2086 ovs_strerror(err),
2087 (oor_netdev ? oor_netdev->name : dev->name));
2088 }
2089
2090 out:
2091 if (err && err != EEXIST && (put->flags & DPIF_FP_MODIFY)) {
2092 /* Modified rule can't be offloaded, try and delete from HW */
2093 int del_err = netdev_flow_del(dev, put->ufid, put->stats);
2094
2095 if (!del_err) {
2096 /* Delete from hw success, so old flow was offloaded.
2097 * Change flags to create the flow in kernel */
2098 put->flags &= ~DPIF_FP_MODIFY;
2099 put->flags |= DPIF_FP_CREATE;
2100 } else if (del_err != ENOENT) {
2101 VLOG_ERR_RL(&rl, "failed to delete offloaded flow: %s",
2102 ovs_strerror(del_err));
2103 /* stop proccesing the flow in kernel */
2104 err = 0;
2105 }
2106 }
2107
2108 netdev_close(dev);
2109
2110 return err;
2111 }
2112
2113 static int
2114 try_send_to_netdev(struct dpif_netlink *dpif, struct dpif_op *op)
2115 {
2116 int err = EOPNOTSUPP;
2117
2118 switch (op->type) {
2119 case DPIF_OP_FLOW_PUT: {
2120 struct dpif_flow_put *put = &op->flow_put;
2121
2122 if (!put->ufid) {
2123 break;
2124 }
2125
2126 log_flow_put_message(&dpif->dpif, &this_module, put, 0);
2127 err = parse_flow_put(dpif, put);
2128 break;
2129 }
2130 case DPIF_OP_FLOW_DEL: {
2131 struct dpif_flow_del *del = &op->flow_del;
2132
2133 if (!del->ufid) {
2134 break;
2135 }
2136
2137 log_flow_del_message(&dpif->dpif, &this_module, del, 0);
2138 err = netdev_ports_flow_del(dpif->dpif.dpif_class, del->ufid,
2139 del->stats);
2140 break;
2141 }
2142 case DPIF_OP_FLOW_GET: {
2143 struct dpif_flow_get *get = &op->flow_get;
2144
2145 if (!op->flow_get.ufid) {
2146 break;
2147 }
2148
2149 log_flow_get_message(&dpif->dpif, &this_module, get, 0);
2150 err = parse_flow_get(dpif, get);
2151 break;
2152 }
2153 case DPIF_OP_EXECUTE:
2154 default:
2155 break;
2156 }
2157
2158 return err;
2159 }
2160
2161 static void
2162 dpif_netlink_operate_chunks(struct dpif_netlink *dpif, struct dpif_op **ops,
2163 size_t n_ops)
2164 {
2165 while (n_ops > 0) {
2166 size_t chunk = dpif_netlink_operate__(dpif, ops, n_ops);
2167
2168 ops += chunk;
2169 n_ops -= chunk;
2170 }
2171 }
2172
2173 static void
2174 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops,
2175 enum dpif_offload_type offload_type)
2176 {
2177 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2178 struct dpif_op *new_ops[OPERATE_MAX_OPS];
2179 int count = 0;
2180 int i = 0;
2181 int err = 0;
2182
2183 if (offload_type == DPIF_OFFLOAD_ALWAYS && !netdev_is_flow_api_enabled()) {
2184 VLOG_DBG("Invalid offload_type: %d", offload_type);
2185 return;
2186 }
2187
2188 if (offload_type != DPIF_OFFLOAD_NEVER && netdev_is_flow_api_enabled()) {
2189 while (n_ops > 0) {
2190 count = 0;
2191
2192 while (n_ops > 0 && count < OPERATE_MAX_OPS) {
2193 struct dpif_op *op = ops[i++];
2194
2195 err = try_send_to_netdev(dpif, op);
2196 if (err && err != EEXIST) {
2197 if (offload_type == DPIF_OFFLOAD_ALWAYS) {
2198 /* We got an error while offloading an op. Since
2199 * OFFLOAD_ALWAYS is specified, we stop further
2200 * processing and return to the caller without
2201 * invoking kernel datapath as fallback. But the
2202 * interface requires us to process all n_ops; so
2203 * return the same error in the remaining ops too.
2204 */
2205 op->error = err;
2206 n_ops--;
2207 while (n_ops > 0) {
2208 op = ops[i++];
2209 op->error = err;
2210 n_ops--;
2211 }
2212 return;
2213 }
2214 new_ops[count++] = op;
2215 } else {
2216 op->error = err;
2217 }
2218
2219 n_ops--;
2220 }
2221
2222 dpif_netlink_operate_chunks(dpif, new_ops, count);
2223 }
2224 } else if (offload_type != DPIF_OFFLOAD_ALWAYS) {
2225 dpif_netlink_operate_chunks(dpif, ops, n_ops);
2226 }
2227 }
2228
2229 #if _WIN32
2230 static void
2231 dpif_netlink_handler_uninit(struct dpif_handler *handler)
2232 {
2233 vport_delete_sock_pool(handler);
2234 }
2235
2236 static int
2237 dpif_netlink_handler_init(struct dpif_handler *handler)
2238 {
2239 return vport_create_sock_pool(handler);
2240 }
2241 #else
2242
2243 static int
2244 dpif_netlink_handler_init(struct dpif_handler *handler)
2245 {
2246 handler->epoll_fd = epoll_create(10);
2247 return handler->epoll_fd < 0 ? errno : 0;
2248 }
2249
2250 static void
2251 dpif_netlink_handler_uninit(struct dpif_handler *handler)
2252 {
2253 close(handler->epoll_fd);
2254 }
2255 #endif
2256
2257 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
2258 * currently in 'dpif' in the kernel, by adding a new set of channels for
2259 * any kernel vport that lacks one and deleting any channels that have no
2260 * backing kernel vports. */
2261 static int
2262 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
2263 OVS_REQ_WRLOCK(dpif->upcall_lock)
2264 {
2265 unsigned long int *keep_channels;
2266 struct dpif_netlink_vport vport;
2267 size_t keep_channels_nbits;
2268 struct nl_dump dump;
2269 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
2270 struct ofpbuf buf;
2271 int retval = 0;
2272 size_t i;
2273
2274 ovs_assert(!WINDOWS || n_handlers <= 1);
2275 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
2276
2277 if (dpif->n_handlers != n_handlers) {
2278 destroy_all_channels(dpif);
2279 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
2280 for (i = 0; i < n_handlers; i++) {
2281 int error;
2282 struct dpif_handler *handler = &dpif->handlers[i];
2283
2284 error = dpif_netlink_handler_init(handler);
2285 if (error) {
2286 size_t j;
2287
2288 for (j = 0; j < i; j++) {
2289 struct dpif_handler *tmp = &dpif->handlers[j];
2290 dpif_netlink_handler_uninit(tmp);
2291 }
2292 free(dpif->handlers);
2293 dpif->handlers = NULL;
2294
2295 return error;
2296 }
2297 }
2298 dpif->n_handlers = n_handlers;
2299 }
2300
2301 for (i = 0; i < n_handlers; i++) {
2302 struct dpif_handler *handler = &dpif->handlers[i];
2303
2304 handler->event_offset = handler->n_events = 0;
2305 }
2306
2307 keep_channels_nbits = dpif->uc_array_size;
2308 keep_channels = bitmap_allocate(keep_channels_nbits);
2309
2310 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
2311 dpif_netlink_port_dump_start__(dpif, &dump);
2312 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
2313 uint32_t port_no = odp_to_u32(vport.port_no);
2314 uint32_t upcall_pid;
2315 int error;
2316
2317 if (port_no >= dpif->uc_array_size
2318 || !vport_get_pid(dpif, port_no, &upcall_pid)) {
2319 struct nl_sock *sock;
2320 error = create_nl_sock(dpif, &sock);
2321
2322 if (error) {
2323 goto error;
2324 }
2325
2326 error = vport_add_channel(dpif, vport.port_no, sock);
2327 if (error) {
2328 VLOG_INFO("%s: could not add channels for port %s",
2329 dpif_name(&dpif->dpif), vport.name);
2330 nl_sock_destroy(sock);
2331 retval = error;
2332 goto error;
2333 }
2334 upcall_pid = nl_sock_pid(sock);
2335 }
2336
2337 /* Configure the vport to deliver misses to 'sock'. */
2338 if (vport.upcall_pids[0] == 0
2339 || vport.n_upcall_pids != 1
2340 || upcall_pid != vport.upcall_pids[0]) {
2341 struct dpif_netlink_vport vport_request;
2342
2343 dpif_netlink_vport_init(&vport_request);
2344 vport_request.cmd = OVS_VPORT_CMD_SET;
2345 vport_request.dp_ifindex = dpif->dp_ifindex;
2346 vport_request.port_no = vport.port_no;
2347 vport_request.n_upcall_pids = 1;
2348 vport_request.upcall_pids = &upcall_pid;
2349 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
2350 if (error) {
2351 VLOG_WARN_RL(&error_rl,
2352 "%s: failed to set upcall pid on port: %s",
2353 dpif_name(&dpif->dpif), ovs_strerror(error));
2354
2355 if (error != ENODEV && error != ENOENT) {
2356 retval = error;
2357 } else {
2358 /* The vport isn't really there, even though the dump says
2359 * it is. Probably we just hit a race after a port
2360 * disappeared. */
2361 }
2362 goto error;
2363 }
2364 }
2365
2366 if (port_no < keep_channels_nbits) {
2367 bitmap_set1(keep_channels, port_no);
2368 }
2369 continue;
2370
2371 error:
2372 vport_del_channels(dpif, vport.port_no);
2373 }
2374 nl_dump_done(&dump);
2375 ofpbuf_uninit(&buf);
2376
2377 /* Discard any saved channels that we didn't reuse. */
2378 for (i = 0; i < keep_channels_nbits; i++) {
2379 if (!bitmap_is_set(keep_channels, i)) {
2380 vport_del_channels(dpif, u32_to_odp(i));
2381 }
2382 }
2383 free(keep_channels);
2384
2385 return retval;
2386 }
2387
2388 static int
2389 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
2390 OVS_REQ_WRLOCK(dpif->upcall_lock)
2391 {
2392 if ((dpif->handlers != NULL) == enable) {
2393 return 0;
2394 } else if (!enable) {
2395 destroy_all_channels(dpif);
2396 return 0;
2397 } else {
2398 return dpif_netlink_refresh_channels(dpif, 1);
2399 }
2400 }
2401
2402 static int
2403 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
2404 {
2405 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2406 int error;
2407
2408 fat_rwlock_wrlock(&dpif->upcall_lock);
2409 error = dpif_netlink_recv_set__(dpif, enable);
2410 fat_rwlock_unlock(&dpif->upcall_lock);
2411
2412 return error;
2413 }
2414
2415 static int
2416 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
2417 {
2418 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2419 int error = 0;
2420
2421 #ifdef _WIN32
2422 /* Multiple upcall handlers will be supported once kernel datapath supports
2423 * it. */
2424 if (n_handlers > 1) {
2425 return error;
2426 }
2427 #endif
2428
2429 fat_rwlock_wrlock(&dpif->upcall_lock);
2430 if (dpif->handlers) {
2431 error = dpif_netlink_refresh_channels(dpif, n_handlers);
2432 }
2433 fat_rwlock_unlock(&dpif->upcall_lock);
2434
2435 return error;
2436 }
2437
2438 static int
2439 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
2440 uint32_t queue_id, uint32_t *priority)
2441 {
2442 if (queue_id < 0xf000) {
2443 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
2444 return 0;
2445 } else {
2446 return EINVAL;
2447 }
2448 }
2449
2450 static int
2451 parse_odp_packet(const struct dpif_netlink *dpif, struct ofpbuf *buf,
2452 struct dpif_upcall *upcall, int *dp_ifindex)
2453 {
2454 static const struct nl_policy ovs_packet_policy[] = {
2455 /* Always present. */
2456 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
2457 .min_len = ETH_HEADER_LEN },
2458 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
2459
2460 /* OVS_PACKET_CMD_ACTION only. */
2461 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
2462 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
2463 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2464 [OVS_PACKET_ATTR_MRU] = { .type = NL_A_U16, .optional = true }
2465 };
2466
2467 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2468 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2469 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2470 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2471
2472 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
2473 if (!nlmsg || !genl || !ovs_header
2474 || nlmsg->nlmsg_type != ovs_packet_family
2475 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
2476 ARRAY_SIZE(ovs_packet_policy))) {
2477 return EINVAL;
2478 }
2479
2480 int type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
2481 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
2482 : -1);
2483 if (type < 0) {
2484 return EINVAL;
2485 }
2486
2487 /* (Re)set ALL fields of '*upcall' on successful return. */
2488 upcall->type = type;
2489 upcall->key = CONST_CAST(struct nlattr *,
2490 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
2491 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
2492 dpif_flow_hash(&dpif->dpif, upcall->key, upcall->key_len, &upcall->ufid);
2493 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
2494 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
2495 upcall->actions = a[OVS_PACKET_ATTR_ACTIONS];
2496 upcall->mru = a[OVS_PACKET_ATTR_MRU];
2497
2498 /* Allow overwriting the netlink attribute header without reallocating. */
2499 dp_packet_use_stub(&upcall->packet,
2500 CONST_CAST(struct nlattr *,
2501 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
2502 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
2503 sizeof(struct nlattr));
2504 dp_packet_set_data(&upcall->packet,
2505 (char *)dp_packet_data(&upcall->packet) + sizeof(struct nlattr));
2506 dp_packet_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
2507
2508 if (nl_attr_find__(upcall->key, upcall->key_len, OVS_KEY_ATTR_ETHERNET)) {
2509 /* Ethernet frame */
2510 upcall->packet.packet_type = htonl(PT_ETH);
2511 } else {
2512 /* Non-Ethernet packet. Get the Ethertype from the NL attributes */
2513 ovs_be16 ethertype = 0;
2514 const struct nlattr *et_nla = nl_attr_find__(upcall->key,
2515 upcall->key_len,
2516 OVS_KEY_ATTR_ETHERTYPE);
2517 if (et_nla) {
2518 ethertype = nl_attr_get_be16(et_nla);
2519 }
2520 upcall->packet.packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
2521 ntohs(ethertype));
2522 dp_packet_set_l3(&upcall->packet, dp_packet_data(&upcall->packet));
2523 }
2524
2525 *dp_ifindex = ovs_header->dp_ifindex;
2526
2527 return 0;
2528 }
2529
2530 #ifdef _WIN32
2531 #define PACKET_RECV_BATCH_SIZE 50
2532 static int
2533 dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id,
2534 struct dpif_upcall *upcall, struct ofpbuf *buf)
2535 OVS_REQ_RDLOCK(dpif->upcall_lock)
2536 {
2537 struct dpif_handler *handler;
2538 int read_tries = 0;
2539 struct dpif_windows_vport_sock *sock_pool;
2540 uint32_t i;
2541
2542 if (!dpif->handlers) {
2543 return EAGAIN;
2544 }
2545
2546 /* Only one handler is supported currently. */
2547 if (handler_id >= 1) {
2548 return EAGAIN;
2549 }
2550
2551 if (handler_id >= dpif->n_handlers) {
2552 return EAGAIN;
2553 }
2554
2555 handler = &dpif->handlers[handler_id];
2556 sock_pool = handler->vport_sock_pool;
2557
2558 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2559 for (;;) {
2560 int dp_ifindex;
2561 int error;
2562
2563 if (++read_tries > PACKET_RECV_BATCH_SIZE) {
2564 return EAGAIN;
2565 }
2566
2567 error = nl_sock_recv(sock_pool[i].nl_sock, buf, NULL, false);
2568 if (error == ENOBUFS) {
2569 /* ENOBUFS typically means that we've received so many
2570 * packets that the buffer overflowed. Try again
2571 * immediately because there's almost certainly a packet
2572 * waiting for us. */
2573 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2574 continue;
2575 }
2576
2577 /* XXX: ch->last_poll = time_msec(); */
2578 if (error) {
2579 if (error == EAGAIN) {
2580 break;
2581 }
2582 return error;
2583 }
2584
2585 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2586 if (!error && dp_ifindex == dpif->dp_ifindex) {
2587 return 0;
2588 } else if (error) {
2589 return error;
2590 }
2591 }
2592 }
2593
2594 return EAGAIN;
2595 }
2596 #else
2597 static int
2598 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
2599 struct dpif_upcall *upcall, struct ofpbuf *buf)
2600 OVS_REQ_RDLOCK(dpif->upcall_lock)
2601 {
2602 struct dpif_handler *handler;
2603 int read_tries = 0;
2604
2605 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
2606 return EAGAIN;
2607 }
2608
2609 handler = &dpif->handlers[handler_id];
2610 if (handler->event_offset >= handler->n_events) {
2611 int retval;
2612
2613 handler->event_offset = handler->n_events = 0;
2614
2615 do {
2616 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
2617 dpif->uc_array_size, 0);
2618 } while (retval < 0 && errno == EINTR);
2619
2620 if (retval < 0) {
2621 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2622 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
2623 } else if (retval > 0) {
2624 handler->n_events = retval;
2625 }
2626 }
2627
2628 while (handler->event_offset < handler->n_events) {
2629 int idx = handler->epoll_events[handler->event_offset].data.u32;
2630 struct dpif_channel *ch = &dpif->channels[idx];
2631
2632 handler->event_offset++;
2633
2634 for (;;) {
2635 int dp_ifindex;
2636 int error;
2637
2638 if (++read_tries > 50) {
2639 return EAGAIN;
2640 }
2641
2642 error = nl_sock_recv(ch->sock, buf, NULL, false);
2643 if (error == ENOBUFS) {
2644 /* ENOBUFS typically means that we've received so many
2645 * packets that the buffer overflowed. Try again
2646 * immediately because there's almost certainly a packet
2647 * waiting for us. */
2648 report_loss(dpif, ch, idx, handler_id);
2649 continue;
2650 }
2651
2652 ch->last_poll = time_msec();
2653 if (error) {
2654 if (error == EAGAIN) {
2655 break;
2656 }
2657 return error;
2658 }
2659
2660 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2661 if (!error && dp_ifindex == dpif->dp_ifindex) {
2662 return 0;
2663 } else if (error) {
2664 return error;
2665 }
2666 }
2667 }
2668
2669 return EAGAIN;
2670 }
2671 #endif
2672
2673 static int
2674 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
2675 struct dpif_upcall *upcall, struct ofpbuf *buf)
2676 {
2677 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2678 int error;
2679
2680 fat_rwlock_rdlock(&dpif->upcall_lock);
2681 #ifdef _WIN32
2682 error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf);
2683 #else
2684 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
2685 #endif
2686 fat_rwlock_unlock(&dpif->upcall_lock);
2687
2688 return error;
2689 }
2690
2691 static void
2692 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
2693 OVS_REQ_RDLOCK(dpif->upcall_lock)
2694 {
2695 #ifdef _WIN32
2696 uint32_t i;
2697 struct dpif_windows_vport_sock *sock_pool =
2698 dpif->handlers[handler_id].vport_sock_pool;
2699
2700 /* Only one handler is supported currently. */
2701 if (handler_id >= 1) {
2702 return;
2703 }
2704
2705 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2706 nl_sock_wait(sock_pool[i].nl_sock, POLLIN);
2707 }
2708 #else
2709 if (dpif->handlers && handler_id < dpif->n_handlers) {
2710 struct dpif_handler *handler = &dpif->handlers[handler_id];
2711
2712 poll_fd_wait(handler->epoll_fd, POLLIN);
2713 }
2714 #endif
2715 }
2716
2717 static void
2718 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
2719 {
2720 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2721
2722 fat_rwlock_rdlock(&dpif->upcall_lock);
2723 dpif_netlink_recv_wait__(dpif, handler_id);
2724 fat_rwlock_unlock(&dpif->upcall_lock);
2725 }
2726
2727 static void
2728 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
2729 OVS_REQ_WRLOCK(dpif->upcall_lock)
2730 {
2731 if (dpif->handlers) {
2732 size_t i;
2733
2734 if (!dpif->channels[0].sock) {
2735 return;
2736 }
2737 for (i = 0; i < dpif->uc_array_size; i++ ) {
2738
2739 nl_sock_drain(dpif->channels[i].sock);
2740 }
2741 }
2742 }
2743
2744 static void
2745 dpif_netlink_recv_purge(struct dpif *dpif_)
2746 {
2747 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2748
2749 fat_rwlock_wrlock(&dpif->upcall_lock);
2750 dpif_netlink_recv_purge__(dpif);
2751 fat_rwlock_unlock(&dpif->upcall_lock);
2752 }
2753
2754 static char *
2755 dpif_netlink_get_datapath_version(void)
2756 {
2757 char *version_str = NULL;
2758
2759 #ifdef __linux__
2760
2761 #define MAX_VERSION_STR_SIZE 80
2762 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2763 FILE *f;
2764
2765 f = fopen(LINUX_DATAPATH_VERSION_FILE, "r");
2766 if (f) {
2767 char *newline;
2768 char version[MAX_VERSION_STR_SIZE];
2769
2770 if (fgets(version, MAX_VERSION_STR_SIZE, f)) {
2771 newline = strchr(version, '\n');
2772 if (newline) {
2773 *newline = '\0';
2774 }
2775 version_str = xstrdup(version);
2776 }
2777 fclose(f);
2778 }
2779 #endif
2780
2781 return version_str;
2782 }
2783
2784 struct dpif_netlink_ct_dump_state {
2785 struct ct_dpif_dump_state up;
2786 struct nl_ct_dump_state *nl_ct_dump;
2787 };
2788
2789 static int
2790 dpif_netlink_ct_dump_start(struct dpif *dpif OVS_UNUSED,
2791 struct ct_dpif_dump_state **dump_,
2792 const uint16_t *zone, int *ptot_bkts)
2793 {
2794 struct dpif_netlink_ct_dump_state *dump;
2795 int err;
2796
2797 dump = xzalloc(sizeof *dump);
2798 err = nl_ct_dump_start(&dump->nl_ct_dump, zone, ptot_bkts);
2799 if (err) {
2800 free(dump);
2801 return err;
2802 }
2803
2804 *dump_ = &dump->up;
2805
2806 return 0;
2807 }
2808
2809 static int
2810 dpif_netlink_ct_dump_next(struct dpif *dpif OVS_UNUSED,
2811 struct ct_dpif_dump_state *dump_,
2812 struct ct_dpif_entry *entry)
2813 {
2814 struct dpif_netlink_ct_dump_state *dump;
2815
2816 INIT_CONTAINER(dump, dump_, up);
2817
2818 return nl_ct_dump_next(dump->nl_ct_dump, entry);
2819 }
2820
2821 static int
2822 dpif_netlink_ct_dump_done(struct dpif *dpif OVS_UNUSED,
2823 struct ct_dpif_dump_state *dump_)
2824 {
2825 struct dpif_netlink_ct_dump_state *dump;
2826
2827 INIT_CONTAINER(dump, dump_, up);
2828
2829 int err = nl_ct_dump_done(dump->nl_ct_dump);
2830 free(dump);
2831 return err;
2832 }
2833
2834 static int
2835 dpif_netlink_ct_flush(struct dpif *dpif OVS_UNUSED, const uint16_t *zone,
2836 const struct ct_dpif_tuple *tuple)
2837 {
2838 if (tuple) {
2839 return nl_ct_flush_tuple(tuple, zone ? *zone : 0);
2840 } else if (zone) {
2841 return nl_ct_flush_zone(*zone);
2842 } else {
2843 return nl_ct_flush();
2844 }
2845 }
2846
2847 static int
2848 dpif_netlink_ct_set_limits(struct dpif *dpif OVS_UNUSED,
2849 const uint32_t *default_limits,
2850 const struct ovs_list *zone_limits)
2851 {
2852 struct ovs_zone_limit req_zone_limit;
2853
2854 if (ovs_ct_limit_family < 0) {
2855 return EOPNOTSUPP;
2856 }
2857
2858 struct ofpbuf *request = ofpbuf_new(NL_DUMP_BUFSIZE);
2859 nl_msg_put_genlmsghdr(request, 0, ovs_ct_limit_family,
2860 NLM_F_REQUEST | NLM_F_ECHO, OVS_CT_LIMIT_CMD_SET,
2861 OVS_CT_LIMIT_VERSION);
2862
2863 struct ovs_header *ovs_header;
2864 ovs_header = ofpbuf_put_uninit(request, sizeof *ovs_header);
2865 ovs_header->dp_ifindex = 0;
2866
2867 size_t opt_offset;
2868 opt_offset = nl_msg_start_nested(request, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
2869 if (default_limits) {
2870 req_zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE;
2871 req_zone_limit.limit = *default_limits;
2872 nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit);
2873 }
2874
2875 if (!ovs_list_is_empty(zone_limits)) {
2876 struct ct_dpif_zone_limit *zone_limit;
2877
2878 LIST_FOR_EACH (zone_limit, node, zone_limits) {
2879 req_zone_limit.zone_id = zone_limit->zone;
2880 req_zone_limit.limit = zone_limit->limit;
2881 nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit);
2882 }
2883 }
2884 nl_msg_end_nested(request, opt_offset);
2885
2886 int err = nl_transact(NETLINK_GENERIC, request, NULL);
2887 ofpbuf_delete(request);
2888 return err;
2889 }
2890
2891 static int
2892 dpif_netlink_zone_limits_from_ofpbuf(const struct ofpbuf *buf,
2893 uint32_t *default_limit,
2894 struct ovs_list *zone_limits)
2895 {
2896 static const struct nl_policy ovs_ct_limit_policy[] = {
2897 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NL_A_NESTED,
2898 .optional = true },
2899 };
2900
2901 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2902 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2903 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2904 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2905
2906 struct nlattr *attr[ARRAY_SIZE(ovs_ct_limit_policy)];
2907
2908 if (!nlmsg || !genl || !ovs_header
2909 || nlmsg->nlmsg_type != ovs_ct_limit_family
2910 || !nl_policy_parse(&b, 0, ovs_ct_limit_policy, attr,
2911 ARRAY_SIZE(ovs_ct_limit_policy))) {
2912 return EINVAL;
2913 }
2914
2915
2916 if (!attr[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2917 return EINVAL;
2918 }
2919
2920 int rem = NLA_ALIGN(
2921 nl_attr_get_size(attr[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]));
2922 const struct ovs_zone_limit *zone_limit =
2923 nl_attr_get(attr[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]);
2924
2925 while (rem >= sizeof *zone_limit) {
2926 if (zone_limit->zone_id == OVS_ZONE_LIMIT_DEFAULT_ZONE) {
2927 *default_limit = zone_limit->limit;
2928 } else if (zone_limit->zone_id < OVS_ZONE_LIMIT_DEFAULT_ZONE ||
2929 zone_limit->zone_id > UINT16_MAX) {
2930 } else {
2931 ct_dpif_push_zone_limit(zone_limits, zone_limit->zone_id,
2932 zone_limit->limit, zone_limit->count);
2933 }
2934 rem -= NLA_ALIGN(sizeof *zone_limit);
2935 zone_limit = ALIGNED_CAST(struct ovs_zone_limit *,
2936 (unsigned char *) zone_limit + NLA_ALIGN(sizeof *zone_limit));
2937 }
2938 return 0;
2939 }
2940
2941 static int
2942 dpif_netlink_ct_get_limits(struct dpif *dpif OVS_UNUSED,
2943 uint32_t *default_limit,
2944 const struct ovs_list *zone_limits_request,
2945 struct ovs_list *zone_limits_reply)
2946 {
2947 if (ovs_ct_limit_family < 0) {
2948 return EOPNOTSUPP;
2949 }
2950
2951 struct ofpbuf *request = ofpbuf_new(NL_DUMP_BUFSIZE);
2952 nl_msg_put_genlmsghdr(request, 0, ovs_ct_limit_family,
2953 NLM_F_REQUEST | NLM_F_ECHO, OVS_CT_LIMIT_CMD_GET,
2954 OVS_CT_LIMIT_VERSION);
2955
2956 struct ovs_header *ovs_header;
2957 ovs_header = ofpbuf_put_uninit(request, sizeof *ovs_header);
2958 ovs_header->dp_ifindex = 0;
2959
2960 if (!ovs_list_is_empty(zone_limits_request)) {
2961 size_t opt_offset = nl_msg_start_nested(request,
2962 OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
2963
2964 struct ovs_zone_limit req_zone_limit;
2965 req_zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE;
2966 nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit);
2967
2968 struct ct_dpif_zone_limit *zone_limit;
2969 LIST_FOR_EACH (zone_limit, node, zone_limits_request) {
2970 req_zone_limit.zone_id = zone_limit->zone;
2971 nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit);
2972 }
2973
2974 nl_msg_end_nested(request, opt_offset);
2975 }
2976
2977 struct ofpbuf *reply;
2978 int err = nl_transact(NETLINK_GENERIC, request, &reply);
2979 if (err) {
2980 goto out;
2981 }
2982
2983 err = dpif_netlink_zone_limits_from_ofpbuf(reply, default_limit,
2984 zone_limits_reply);
2985
2986 out:
2987 ofpbuf_delete(request);
2988 ofpbuf_delete(reply);
2989 return err;
2990 }
2991
2992 static int
2993 dpif_netlink_ct_del_limits(struct dpif *dpif OVS_UNUSED,
2994 const struct ovs_list *zone_limits)
2995 {
2996 if (ovs_ct_limit_family < 0) {
2997 return EOPNOTSUPP;
2998 }
2999
3000 struct ofpbuf *request = ofpbuf_new(NL_DUMP_BUFSIZE);
3001 nl_msg_put_genlmsghdr(request, 0, ovs_ct_limit_family,
3002 NLM_F_REQUEST | NLM_F_ECHO, OVS_CT_LIMIT_CMD_DEL,
3003 OVS_CT_LIMIT_VERSION);
3004
3005 struct ovs_header *ovs_header;
3006 ovs_header = ofpbuf_put_uninit(request, sizeof *ovs_header);
3007 ovs_header->dp_ifindex = 0;
3008
3009 if (!ovs_list_is_empty(zone_limits)) {
3010 size_t opt_offset =
3011 nl_msg_start_nested(request, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
3012
3013 struct ct_dpif_zone_limit *zone_limit;
3014 LIST_FOR_EACH (zone_limit, node, zone_limits) {
3015 struct ovs_zone_limit req_zone_limit;
3016 req_zone_limit.zone_id = zone_limit->zone;
3017 nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit);
3018 }
3019 nl_msg_end_nested(request, opt_offset);
3020 }
3021
3022 int err = nl_transact(NETLINK_GENERIC, request, NULL);
3023
3024 ofpbuf_delete(request);
3025 return err;
3026 }
3027
3028 #define NL_TP_NAME_PREFIX "ovs_tp_"
3029
3030 struct dpif_netlink_timeout_policy_protocol {
3031 uint16_t l3num;
3032 uint8_t l4num;
3033 };
3034
3035 enum OVS_PACKED_ENUM dpif_netlink_support_timeout_policy_protocol {
3036 DPIF_NL_TP_AF_INET_TCP,
3037 DPIF_NL_TP_AF_INET_UDP,
3038 DPIF_NL_TP_AF_INET_ICMP,
3039 DPIF_NL_TP_AF_INET6_TCP,
3040 DPIF_NL_TP_AF_INET6_UDP,
3041 DPIF_NL_TP_AF_INET6_ICMPV6,
3042 DPIF_NL_TP_MAX
3043 };
3044
3045 #define DPIF_NL_ALL_TP ((1UL << DPIF_NL_TP_MAX) - 1)
3046
3047
3048 static struct dpif_netlink_timeout_policy_protocol tp_protos[] = {
3049 [DPIF_NL_TP_AF_INET_TCP] = { .l3num = AF_INET, .l4num = IPPROTO_TCP },
3050 [DPIF_NL_TP_AF_INET_UDP] = { .l3num = AF_INET, .l4num = IPPROTO_UDP },
3051 [DPIF_NL_TP_AF_INET_ICMP] = { .l3num = AF_INET, .l4num = IPPROTO_ICMP },
3052 [DPIF_NL_TP_AF_INET6_TCP] = { .l3num = AF_INET6, .l4num = IPPROTO_TCP },
3053 [DPIF_NL_TP_AF_INET6_UDP] = { .l3num = AF_INET6, .l4num = IPPROTO_UDP },
3054 [DPIF_NL_TP_AF_INET6_ICMPV6] = { .l3num = AF_INET6,
3055 .l4num = IPPROTO_ICMPV6 },
3056 };
3057
3058 static void
3059 dpif_netlink_format_tp_name(uint32_t id, uint16_t l3num, uint8_t l4num,
3060 char **tp_name)
3061 {
3062 struct ds ds = DS_EMPTY_INITIALIZER;
3063 ds_put_format(&ds, "%s%"PRIu32"_", NL_TP_NAME_PREFIX, id);
3064 ct_dpif_format_ipproto(&ds, l4num);
3065
3066 if (l3num == AF_INET) {
3067 ds_put_cstr(&ds, "4");
3068 } else if (l3num == AF_INET6 && l4num != IPPROTO_ICMPV6) {
3069 ds_put_cstr(&ds, "6");
3070 }
3071
3072 ovs_assert(ds.length < CTNL_TIMEOUT_NAME_MAX);
3073
3074 *tp_name = ds_steal_cstr(&ds);
3075 }
3076
3077 static int
3078 dpif_netlink_ct_get_timeout_policy_name(struct dpif *dpif OVS_UNUSED,
3079 uint32_t tp_id, uint16_t dl_type,
3080 uint8_t nw_proto, char **tp_name,
3081 bool *is_generic)
3082 {
3083 dpif_netlink_format_tp_name(tp_id,
3084 dl_type == ETH_TYPE_IP ? AF_INET : AF_INET6,
3085 nw_proto, tp_name);
3086 *is_generic = false;
3087 return 0;
3088 }
3089
3090 #define CT_DPIF_NL_TP_TCP_MAPPINGS \
3091 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT, SYN_SENT) \
3092 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_RECV, SYN_RECV) \
3093 CT_DPIF_NL_TP_MAPPING(TCP, TCP, ESTABLISHED, ESTABLISHED) \
3094 CT_DPIF_NL_TP_MAPPING(TCP, TCP, FIN_WAIT, FIN_WAIT) \
3095 CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE_WAIT, CLOSE_WAIT) \
3096 CT_DPIF_NL_TP_MAPPING(TCP, TCP, LAST_ACK, LAST_ACK) \
3097 CT_DPIF_NL_TP_MAPPING(TCP, TCP, TIME_WAIT, TIME_WAIT) \
3098 CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE, CLOSE) \
3099 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT2, SYN_SENT2) \
3100 CT_DPIF_NL_TP_MAPPING(TCP, TCP, RETRANSMIT, RETRANS) \
3101 CT_DPIF_NL_TP_MAPPING(TCP, TCP, UNACK, UNACK)
3102
3103 #define CT_DPIF_NL_TP_UDP_MAPPINGS \
3104 CT_DPIF_NL_TP_MAPPING(UDP, UDP, SINGLE, UNREPLIED) \
3105 CT_DPIF_NL_TP_MAPPING(UDP, UDP, MULTIPLE, REPLIED)
3106
3107 #define CT_DPIF_NL_TP_ICMP_MAPPINGS \
3108 CT_DPIF_NL_TP_MAPPING(ICMP, ICMP, FIRST, TIMEOUT)
3109
3110 #define CT_DPIF_NL_TP_ICMPV6_MAPPINGS \
3111 CT_DPIF_NL_TP_MAPPING(ICMP, ICMPV6, FIRST, TIMEOUT)
3112
3113
3114 #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \
3115 if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \
3116 nl_tp->present |= 1 << CTA_TIMEOUT_##PROTO2##_##ATTR2; \
3117 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2] = \
3118 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]; \
3119 }
3120
3121 static void
3122 dpif_netlink_get_nl_tp_tcp_attrs(const struct ct_dpif_timeout_policy *tp,
3123 struct nl_ct_timeout_policy *nl_tp)
3124 {
3125 CT_DPIF_NL_TP_TCP_MAPPINGS
3126 }
3127
3128 static void
3129 dpif_netlink_get_nl_tp_udp_attrs(const struct ct_dpif_timeout_policy *tp,
3130 struct nl_ct_timeout_policy *nl_tp)
3131 {
3132 CT_DPIF_NL_TP_UDP_MAPPINGS
3133 }
3134
3135 static void
3136 dpif_netlink_get_nl_tp_icmp_attrs(const struct ct_dpif_timeout_policy *tp,
3137 struct nl_ct_timeout_policy *nl_tp)
3138 {
3139 CT_DPIF_NL_TP_ICMP_MAPPINGS
3140 }
3141
3142 static void
3143 dpif_netlink_get_nl_tp_icmpv6_attrs(const struct ct_dpif_timeout_policy *tp,
3144 struct nl_ct_timeout_policy *nl_tp)
3145 {
3146 CT_DPIF_NL_TP_ICMPV6_MAPPINGS
3147 }
3148
3149 #undef CT_DPIF_NL_TP_MAPPING
3150
3151 static void
3152 dpif_netlink_get_nl_tp_attrs(const struct ct_dpif_timeout_policy *tp,
3153 uint8_t l4num, struct nl_ct_timeout_policy *nl_tp)
3154 {
3155 nl_tp->present = 0;
3156
3157 if (l4num == IPPROTO_TCP) {
3158 dpif_netlink_get_nl_tp_tcp_attrs(tp, nl_tp);
3159 } else if (l4num == IPPROTO_UDP) {
3160 dpif_netlink_get_nl_tp_udp_attrs(tp, nl_tp);
3161 } else if (l4num == IPPROTO_ICMP) {
3162 dpif_netlink_get_nl_tp_icmp_attrs(tp, nl_tp);
3163 } else if (l4num == IPPROTO_ICMPV6) {
3164 dpif_netlink_get_nl_tp_icmpv6_attrs(tp, nl_tp);
3165 }
3166 }
3167
3168 #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \
3169 if (nl_tp->present & (1 << CTA_TIMEOUT_##PROTO2##_##ATTR2)) { \
3170 if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \
3171 if (tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] != \
3172 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]) { \
3173 VLOG_WARN_RL(&error_rl, "Inconsistent timeout policy %s " \
3174 "attribute %s=%"PRIu32" while %s=%"PRIu32, \
3175 nl_tp->name, "CTA_TIMEOUT_"#PROTO2"_"#ATTR2, \
3176 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2], \
3177 "CT_DPIF_TP_ATTR_"#PROTO1"_"#ATTR1, \
3178 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]); \
3179 } \
3180 } else { \
3181 tp->present |= 1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1; \
3182 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] = \
3183 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]; \
3184 } \
3185 }
3186
3187 static void
3188 dpif_netlink_set_ct_dpif_tp_tcp_attrs(const struct nl_ct_timeout_policy *nl_tp,
3189 struct ct_dpif_timeout_policy *tp)
3190 {
3191 CT_DPIF_NL_TP_TCP_MAPPINGS
3192 }
3193
3194 static void
3195 dpif_netlink_set_ct_dpif_tp_udp_attrs(const struct nl_ct_timeout_policy *nl_tp,
3196 struct ct_dpif_timeout_policy *tp)
3197 {
3198 CT_DPIF_NL_TP_UDP_MAPPINGS
3199 }
3200
3201 static void
3202 dpif_netlink_set_ct_dpif_tp_icmp_attrs(
3203 const struct nl_ct_timeout_policy *nl_tp,
3204 struct ct_dpif_timeout_policy *tp)
3205 {
3206 CT_DPIF_NL_TP_ICMP_MAPPINGS
3207 }
3208
3209 static void
3210 dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(
3211 const struct nl_ct_timeout_policy *nl_tp,
3212 struct ct_dpif_timeout_policy *tp)
3213 {
3214 CT_DPIF_NL_TP_ICMPV6_MAPPINGS
3215 }
3216
3217 #undef CT_DPIF_NL_TP_MAPPING
3218
3219 static void
3220 dpif_netlink_set_ct_dpif_tp_attrs(const struct nl_ct_timeout_policy *nl_tp,
3221 struct ct_dpif_timeout_policy *tp)
3222 {
3223 if (nl_tp->l4num == IPPROTO_TCP) {
3224 dpif_netlink_set_ct_dpif_tp_tcp_attrs(nl_tp, tp);
3225 } else if (nl_tp->l4num == IPPROTO_UDP) {
3226 dpif_netlink_set_ct_dpif_tp_udp_attrs(nl_tp, tp);
3227 } else if (nl_tp->l4num == IPPROTO_ICMP) {
3228 dpif_netlink_set_ct_dpif_tp_icmp_attrs(nl_tp, tp);
3229 } else if (nl_tp->l4num == IPPROTO_ICMPV6) {
3230 dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(nl_tp, tp);
3231 }
3232 }
3233
3234 #ifdef _WIN32
3235 static int
3236 dpif_netlink_ct_set_timeout_policy(struct dpif *dpif OVS_UNUSED,
3237 const struct ct_dpif_timeout_policy *tp)
3238 {
3239 return EOPNOTSUPP;
3240 }
3241
3242 static int
3243 dpif_netlink_ct_get_timeout_policy(struct dpif *dpif OVS_UNUSED,
3244 uint32_t tp_id,
3245 struct ct_dpif_timeout_policy *tp)
3246 {
3247 return EOPNOTSUPP;
3248 }
3249
3250 static int
3251 dpif_netlink_ct_del_timeout_policy(struct dpif *dpif OVS_UNUSED,
3252 uint32_t tp_id)
3253 {
3254 return EOPNOTSUPP;
3255 }
3256
3257 static int
3258 dpif_netlink_ct_timeout_policy_dump_start(struct dpif *dpif OVS_UNUSED,
3259 void **statep)
3260 {
3261 return EOPNOTSUPP;
3262 }
3263
3264 static int
3265 dpif_netlink_ct_timeout_policy_dump_next(struct dpif *dpif OVS_UNUSED,
3266 void *state,
3267 struct ct_dpif_timeout_policy **tp)
3268 {
3269 return EOPNOTSUPP;
3270 }
3271
3272 static int
3273 dpif_netlink_ct_timeout_policy_dump_done(struct dpif *dpif OVS_UNUSED,
3274 void *state)
3275 {
3276 return EOPNOTSUPP;
3277 }
3278 #else
3279 static int
3280 dpif_netlink_ct_set_timeout_policy(struct dpif *dpif OVS_UNUSED,
3281 const struct ct_dpif_timeout_policy *tp)
3282 {
3283 int err = 0;
3284
3285 for (int i = 0; i < ARRAY_SIZE(tp_protos); ++i) {
3286 struct nl_ct_timeout_policy nl_tp;
3287 char *nl_tp_name;
3288
3289 dpif_netlink_format_tp_name(tp->id, tp_protos[i].l3num,
3290 tp_protos[i].l4num, &nl_tp_name);
3291 ovs_strlcpy(nl_tp.name, nl_tp_name, sizeof nl_tp.name);
3292 free(nl_tp_name);
3293
3294 nl_tp.l3num = tp_protos[i].l3num;
3295 nl_tp.l4num = tp_protos[i].l4num;
3296 dpif_netlink_get_nl_tp_attrs(tp, tp_protos[i].l4num, &nl_tp);
3297 err = nl_ct_set_timeout_policy(&nl_tp);
3298 if (err) {
3299 VLOG_WARN_RL(&error_rl, "failed to add timeout policy %s (%s)",
3300 nl_tp.name, ovs_strerror(err));
3301 goto out;
3302 }
3303 }
3304
3305 out:
3306 return err;
3307 }
3308
3309 static int
3310 dpif_netlink_ct_get_timeout_policy(struct dpif *dpif OVS_UNUSED,
3311 uint32_t tp_id,
3312 struct ct_dpif_timeout_policy *tp)
3313 {
3314 int err = 0;
3315
3316 tp->id = tp_id;
3317 tp->present = 0;
3318 for (int i = 0; i < ARRAY_SIZE(tp_protos); ++i) {
3319 struct nl_ct_timeout_policy nl_tp;
3320 char *nl_tp_name;
3321
3322 dpif_netlink_format_tp_name(tp_id, tp_protos[i].l3num,
3323 tp_protos[i].l4num, &nl_tp_name);
3324 err = nl_ct_get_timeout_policy(nl_tp_name, &nl_tp);
3325
3326 if (err) {
3327 VLOG_WARN_RL(&error_rl, "failed to get timeout policy %s (%s)",
3328 nl_tp_name, ovs_strerror(err));
3329 free(nl_tp_name);
3330 goto out;
3331 }
3332 free(nl_tp_name);
3333 dpif_netlink_set_ct_dpif_tp_attrs(&nl_tp, tp);
3334 }
3335
3336 out:
3337 return err;
3338 }
3339
3340 /* Returns 0 if all the sub timeout policies are deleted or not exist in the
3341 * kernel. Returns 1 if any sub timeout policy deletion failed. */
3342 static int
3343 dpif_netlink_ct_del_timeout_policy(struct dpif *dpif OVS_UNUSED,
3344 uint32_t tp_id)
3345 {
3346 int ret = 0;
3347
3348 for (int i = 0; i < ARRAY_SIZE(tp_protos); ++i) {
3349 char *nl_tp_name;
3350 dpif_netlink_format_tp_name(tp_id, tp_protos[i].l3num,
3351 tp_protos[i].l4num, &nl_tp_name);
3352 int err = nl_ct_del_timeout_policy(nl_tp_name);
3353 if (err == ENOENT) {
3354 err = 0;
3355 }
3356 if (err) {
3357 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(6, 6);
3358 VLOG_INFO_RL(&rl, "failed to delete timeout policy %s (%s)",
3359 nl_tp_name, ovs_strerror(err));
3360 ret = 1;
3361 }
3362 free(nl_tp_name);
3363 }
3364
3365 return ret;
3366 }
3367
3368 struct dpif_netlink_ct_timeout_policy_dump_state {
3369 struct nl_ct_timeout_policy_dump_state *nl_dump_state;
3370 struct hmap tp_dump_map;
3371 };
3372
3373 struct dpif_netlink_tp_dump_node {
3374 struct hmap_node hmap_node; /* node in tp_dump_map. */
3375 struct ct_dpif_timeout_policy *tp;
3376 uint32_t l3_l4_present;
3377 };
3378
3379 static struct dpif_netlink_tp_dump_node *
3380 get_dpif_netlink_tp_dump_node_by_tp_id(uint32_t tp_id,
3381 struct hmap *tp_dump_map)
3382 {
3383 struct dpif_netlink_tp_dump_node *tp_dump_node;
3384
3385 HMAP_FOR_EACH_WITH_HASH (tp_dump_node, hmap_node, hash_int(tp_id, 0),
3386 tp_dump_map) {
3387 if (tp_dump_node->tp->id == tp_id) {
3388 return tp_dump_node;
3389 }
3390 }
3391 return NULL;
3392 }
3393
3394 static void
3395 update_dpif_netlink_tp_dump_node(
3396 const struct nl_ct_timeout_policy *nl_tp,
3397 struct dpif_netlink_tp_dump_node *tp_dump_node)
3398 {
3399 dpif_netlink_set_ct_dpif_tp_attrs(nl_tp, tp_dump_node->tp);
3400 for (int i = 0; i < DPIF_NL_TP_MAX; ++i) {
3401 if (nl_tp->l3num == tp_protos[i].l3num &&
3402 nl_tp->l4num == tp_protos[i].l4num) {
3403 tp_dump_node->l3_l4_present |= 1 << i;
3404 break;
3405 }
3406 }
3407 }
3408
3409 static int
3410 dpif_netlink_ct_timeout_policy_dump_start(struct dpif *dpif OVS_UNUSED,
3411 void **statep)
3412 {
3413 struct dpif_netlink_ct_timeout_policy_dump_state *dump_state;
3414
3415 *statep = dump_state = xzalloc(sizeof *dump_state);
3416 int err = nl_ct_timeout_policy_dump_start(&dump_state->nl_dump_state);
3417 if (err) {
3418 free(dump_state);
3419 return err;
3420 }
3421 hmap_init(&dump_state->tp_dump_map);
3422 return 0;
3423 }
3424
3425 static void
3426 get_and_cleanup_tp_dump_node(struct hmap *hmap,
3427 struct dpif_netlink_tp_dump_node *tp_dump_node,
3428 struct ct_dpif_timeout_policy *tp)
3429 {
3430 hmap_remove(hmap, &tp_dump_node->hmap_node);
3431 *tp = *tp_dump_node->tp;
3432 free(tp_dump_node->tp);
3433 free(tp_dump_node);
3434 }
3435
3436 static int
3437 dpif_netlink_ct_timeout_policy_dump_next(struct dpif *dpif OVS_UNUSED,
3438 void *state,
3439 struct ct_dpif_timeout_policy *tp)
3440 {
3441 struct dpif_netlink_ct_timeout_policy_dump_state *dump_state = state;
3442 struct dpif_netlink_tp_dump_node *tp_dump_node;
3443 int err;
3444
3445 /* Dumps all the timeout policies in the kernel. */
3446 do {
3447 struct nl_ct_timeout_policy nl_tp;
3448 uint32_t tp_id;
3449
3450 err = nl_ct_timeout_policy_dump_next(dump_state->nl_dump_state,
3451 &nl_tp);
3452 if (err) {
3453 break;
3454 }
3455
3456 /* We only interest in OVS installed timeout policies. */
3457 if (!ovs_scan(nl_tp.name, NL_TP_NAME_PREFIX"%"PRIu32, &tp_id)) {
3458 continue;
3459 }
3460
3461 tp_dump_node = get_dpif_netlink_tp_dump_node_by_tp_id(
3462 tp_id, &dump_state->tp_dump_map);
3463 if (!tp_dump_node) {
3464 tp_dump_node = xzalloc(sizeof *tp_dump_node);
3465 tp_dump_node->tp = xzalloc(sizeof *tp_dump_node->tp);
3466 tp_dump_node->tp->id = tp_id;
3467 hmap_insert(&dump_state->tp_dump_map, &tp_dump_node->hmap_node,
3468 hash_int(tp_id, 0));
3469 }
3470
3471 update_dpif_netlink_tp_dump_node(&nl_tp, tp_dump_node);
3472
3473 /* Returns one ct_dpif_timeout_policy if we gather all the L3/L4
3474 * sub-pieces. */
3475 if (tp_dump_node->l3_l4_present == DPIF_NL_ALL_TP) {
3476 get_and_cleanup_tp_dump_node(&dump_state->tp_dump_map,
3477 tp_dump_node, tp);
3478 break;
3479 }
3480 } while (true);
3481
3482 /* Dump the incomplete timeout policies. */
3483 if (err == EOF) {
3484 if (!hmap_is_empty(&dump_state->tp_dump_map)) {
3485 struct hmap_node *hmap_node = hmap_first(&dump_state->tp_dump_map);
3486 tp_dump_node = CONTAINER_OF(hmap_node,
3487 struct dpif_netlink_tp_dump_node,
3488 hmap_node);
3489 get_and_cleanup_tp_dump_node(&dump_state->tp_dump_map,
3490 tp_dump_node, tp);
3491 return 0;
3492 }
3493 }
3494
3495 return err;
3496 }
3497
3498 static int
3499 dpif_netlink_ct_timeout_policy_dump_done(struct dpif *dpif OVS_UNUSED,
3500 void *state)
3501 {
3502 struct dpif_netlink_ct_timeout_policy_dump_state *dump_state = state;
3503 struct dpif_netlink_tp_dump_node *tp_dump_node;
3504
3505 int err = nl_ct_timeout_policy_dump_done(dump_state->nl_dump_state);
3506 HMAP_FOR_EACH_POP (tp_dump_node, hmap_node, &dump_state->tp_dump_map) {
3507 free(tp_dump_node->tp);
3508 free(tp_dump_node);
3509 }
3510 hmap_destroy(&dump_state->tp_dump_map);
3511 free(dump_state);
3512 return err;
3513 }
3514 #endif
3515
3516 \f
3517 /* Meters */
3518
3519 /* Set of supported meter flags */
3520 #define DP_SUPPORTED_METER_FLAGS_MASK \
3521 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
3522
3523 /* Meter support was introduced in Linux 4.15. In some versions of
3524 * Linux 4.15, 4.16, and 4.17, there was a bug that never set the id
3525 * when the meter was created, so all meters essentially had an id of
3526 * zero. Check for that condition and disable meters on those kernels. */
3527 static bool probe_broken_meters(struct dpif *);
3528
3529 static void
3530 dpif_netlink_meter_init(struct dpif_netlink *dpif, struct ofpbuf *buf,
3531 void *stub, size_t size, uint32_t command)
3532 {
3533 ofpbuf_use_stub(buf, stub, size);
3534
3535 nl_msg_put_genlmsghdr(buf, 0, ovs_meter_family, NLM_F_REQUEST | NLM_F_ECHO,
3536 command, OVS_METER_VERSION);
3537
3538 struct ovs_header *ovs_header;
3539 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
3540 ovs_header->dp_ifindex = dpif->dp_ifindex;
3541 }
3542
3543 /* Execute meter 'request' in the kernel datapath. If the command
3544 * fails, returns a positive errno value. Otherwise, stores the reply
3545 * in '*replyp', parses the policy according to 'reply_policy' into the
3546 * array of Netlink attribute in 'a', and returns 0. On success, the
3547 * caller is responsible for calling ofpbuf_delete() on '*replyp'
3548 * ('replyp' will contain pointers into 'a'). */
3549 static int
3550 dpif_netlink_meter_transact(struct ofpbuf *request, struct ofpbuf **replyp,
3551 const struct nl_policy *reply_policy,
3552 struct nlattr **a, size_t size_a)
3553 {
3554 int error = nl_transact(NETLINK_GENERIC, request, replyp);
3555 ofpbuf_uninit(request);
3556
3557 if (error) {
3558 return error;
3559 }
3560
3561 struct nlmsghdr *nlmsg = ofpbuf_try_pull(*replyp, sizeof *nlmsg);
3562 struct genlmsghdr *genl = ofpbuf_try_pull(*replyp, sizeof *genl);
3563 struct ovs_header *ovs_header = ofpbuf_try_pull(*replyp,
3564 sizeof *ovs_header);
3565 if (!nlmsg || !genl || !ovs_header
3566 || nlmsg->nlmsg_type != ovs_meter_family
3567 || !nl_policy_parse(*replyp, 0, reply_policy, a, size_a)) {
3568 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3569 VLOG_DBG_RL(&rl,
3570 "Kernel module response to meter tranaction is invalid");
3571 return EINVAL;
3572 }
3573 return 0;
3574 }
3575
3576 static void
3577 dpif_netlink_meter_get_features(const struct dpif *dpif_,
3578 struct ofputil_meter_features *features)
3579 {
3580 if (probe_broken_meters(CONST_CAST(struct dpif *, dpif_))) {
3581 features = NULL;
3582 return;
3583 }
3584
3585 struct ofpbuf buf, *msg;
3586 uint64_t stub[1024 / 8];
3587
3588 static const struct nl_policy ovs_meter_features_policy[] = {
3589 [OVS_METER_ATTR_MAX_METERS] = { .type = NL_A_U32 },
3590 [OVS_METER_ATTR_MAX_BANDS] = { .type = NL_A_U32 },
3591 [OVS_METER_ATTR_BANDS] = { .type = NL_A_NESTED, .optional = true },
3592 };
3593 struct nlattr *a[ARRAY_SIZE(ovs_meter_features_policy)];
3594
3595 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
3596 dpif_netlink_meter_init(dpif, &buf, stub, sizeof stub,
3597 OVS_METER_CMD_FEATURES);
3598 if (dpif_netlink_meter_transact(&buf, &msg, ovs_meter_features_policy, a,
3599 ARRAY_SIZE(ovs_meter_features_policy))) {
3600 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3601 VLOG_INFO_RL(&rl,
3602 "dpif_netlink_meter_transact OVS_METER_CMD_FEATURES failed");
3603 return;
3604 }
3605
3606 features->max_meters = nl_attr_get_u32(a[OVS_METER_ATTR_MAX_METERS]);
3607 features->max_bands = nl_attr_get_u32(a[OVS_METER_ATTR_MAX_BANDS]);
3608
3609 /* Bands is a nested attribute of zero or more nested
3610 * band attributes. */
3611 if (a[OVS_METER_ATTR_BANDS]) {
3612 const struct nlattr *nla;
3613 size_t left;
3614
3615 NL_NESTED_FOR_EACH (nla, left, a[OVS_METER_ATTR_BANDS]) {
3616 const struct nlattr *band_nla;
3617 size_t band_left;
3618
3619 NL_NESTED_FOR_EACH (band_nla, band_left, nla) {
3620 if (nl_attr_type(band_nla) == OVS_BAND_ATTR_TYPE) {
3621 if (nl_attr_get_size(band_nla) == sizeof(uint32_t)) {
3622 switch (nl_attr_get_u32(band_nla)) {
3623 case OVS_METER_BAND_TYPE_DROP:
3624 features->band_types |= 1 << OFPMBT13_DROP;
3625 break;
3626 }
3627 }
3628 }
3629 }
3630 }
3631 }
3632 features->capabilities = DP_SUPPORTED_METER_FLAGS_MASK;
3633
3634 ofpbuf_delete(msg);
3635 }
3636
3637 static int
3638 dpif_netlink_meter_set__(struct dpif *dpif_, ofproto_meter_id meter_id,
3639 struct ofputil_meter_config *config)
3640 {
3641 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
3642 struct ofpbuf buf, *msg;
3643 uint64_t stub[1024 / 8];
3644
3645 static const struct nl_policy ovs_meter_set_response_policy[] = {
3646 [OVS_METER_ATTR_ID] = { .type = NL_A_U32 },
3647 };
3648 struct nlattr *a[ARRAY_SIZE(ovs_meter_set_response_policy)];
3649
3650 if (config->flags & ~DP_SUPPORTED_METER_FLAGS_MASK) {
3651 return EBADF; /* Unsupported flags set */
3652 }
3653
3654 for (size_t i = 0; i < config->n_bands; i++) {
3655 switch (config->bands[i].type) {
3656 case OFPMBT13_DROP:
3657 break;
3658 default:
3659 return ENODEV; /* Unsupported band type */
3660 }
3661 }
3662
3663 dpif_netlink_meter_init(dpif, &buf, stub, sizeof stub, OVS_METER_CMD_SET);
3664
3665 nl_msg_put_u32(&buf, OVS_METER_ATTR_ID, meter_id.uint32);
3666
3667 if (config->flags & OFPMF13_KBPS) {
3668 nl_msg_put_flag(&buf, OVS_METER_ATTR_KBPS);
3669 }
3670
3671 size_t bands_offset = nl_msg_start_nested(&buf, OVS_METER_ATTR_BANDS);
3672 /* Bands */
3673 for (size_t i = 0; i < config->n_bands; ++i) {
3674 struct ofputil_meter_band * band = &config->bands[i];
3675 uint32_t band_type;
3676
3677 size_t band_offset = nl_msg_start_nested(&buf, OVS_BAND_ATTR_UNSPEC);
3678
3679 switch (band->type) {
3680 case OFPMBT13_DROP:
3681 band_type = OVS_METER_BAND_TYPE_DROP;
3682 break;
3683 default:
3684 band_type = OVS_METER_BAND_TYPE_UNSPEC;
3685 }
3686 nl_msg_put_u32(&buf, OVS_BAND_ATTR_TYPE, band_type);
3687 nl_msg_put_u32(&buf, OVS_BAND_ATTR_RATE, band->rate);
3688 nl_msg_put_u32(&buf, OVS_BAND_ATTR_BURST,
3689 config->flags & OFPMF13_BURST ?
3690 band->burst_size : band->rate);
3691 nl_msg_end_nested(&buf, band_offset);
3692 }
3693 nl_msg_end_nested(&buf, bands_offset);
3694
3695 int error = dpif_netlink_meter_transact(&buf, &msg,
3696 ovs_meter_set_response_policy, a,
3697 ARRAY_SIZE(ovs_meter_set_response_policy));
3698 if (error) {
3699 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3700 VLOG_INFO_RL(&rl,
3701 "dpif_netlink_meter_transact OVS_METER_CMD_SET failed");
3702 return error;
3703 }
3704
3705 if (nl_attr_get_u32(a[OVS_METER_ATTR_ID]) != meter_id.uint32) {
3706 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3707 VLOG_INFO_RL(&rl,
3708 "Kernel returned a different meter id than requested");
3709 }
3710 ofpbuf_delete(msg);
3711 return 0;
3712 }
3713
3714 static int
3715 dpif_netlink_meter_set(struct dpif *dpif_, ofproto_meter_id meter_id,
3716 struct ofputil_meter_config *config)
3717 {
3718 if (probe_broken_meters(dpif_)) {
3719 return ENOMEM;
3720 }
3721
3722 return dpif_netlink_meter_set__(dpif_, meter_id, config);
3723 }
3724
3725 /* Retrieve statistics and/or delete meter 'meter_id'. Statistics are
3726 * stored in 'stats', if it is not null. If 'command' is
3727 * OVS_METER_CMD_DEL, the meter is deleted and statistics are optionally
3728 * retrieved. If 'command' is OVS_METER_CMD_GET, then statistics are
3729 * simply retrieved. */
3730 static int
3731 dpif_netlink_meter_get_stats(const struct dpif *dpif_,
3732 ofproto_meter_id meter_id,
3733 struct ofputil_meter_stats *stats,
3734 uint16_t max_bands,
3735 enum ovs_meter_cmd command)
3736 {
3737 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
3738 struct ofpbuf buf, *msg;
3739 uint64_t stub[1024 / 8];
3740
3741 static const struct nl_policy ovs_meter_stats_policy[] = {
3742 [OVS_METER_ATTR_ID] = { .type = NL_A_U32, .optional = true},
3743 [OVS_METER_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
3744 .optional = true},
3745 [OVS_METER_ATTR_BANDS] = { .type = NL_A_NESTED, .optional = true },
3746 };
3747 struct nlattr *a[ARRAY_SIZE(ovs_meter_stats_policy)];
3748
3749 dpif_netlink_meter_init(dpif, &buf, stub, sizeof stub, command);
3750
3751 nl_msg_put_u32(&buf, OVS_METER_ATTR_ID, meter_id.uint32);
3752
3753 int error = dpif_netlink_meter_transact(&buf, &msg,
3754 ovs_meter_stats_policy, a,
3755 ARRAY_SIZE(ovs_meter_stats_policy));
3756 if (error) {
3757 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3758 VLOG_INFO_RL(&rl, "dpif_netlink_meter_transact %s failed",
3759 command == OVS_METER_CMD_GET ? "get" : "del");
3760 return error;
3761 }
3762
3763 if (stats
3764 && a[OVS_METER_ATTR_ID]
3765 && a[OVS_METER_ATTR_STATS]
3766 && nl_attr_get_u32(a[OVS_METER_ATTR_ID]) == meter_id.uint32) {
3767 /* return stats */
3768 const struct ovs_flow_stats *stat;
3769 const struct nlattr *nla;
3770 size_t left;
3771
3772 stat = nl_attr_get(a[OVS_METER_ATTR_STATS]);
3773 stats->packet_in_count = get_32aligned_u64(&stat->n_packets);
3774 stats->byte_in_count = get_32aligned_u64(&stat->n_bytes);
3775
3776 if (a[OVS_METER_ATTR_BANDS]) {
3777 size_t n_bands = 0;
3778 NL_NESTED_FOR_EACH (nla, left, a[OVS_METER_ATTR_BANDS]) {
3779 const struct nlattr *band_nla;
3780 band_nla = nl_attr_find_nested(nla, OVS_BAND_ATTR_STATS);
3781 if (band_nla && nl_attr_get_size(band_nla) \
3782 == sizeof(struct ovs_flow_stats)) {
3783 stat = nl_attr_get(band_nla);
3784
3785 if (n_bands < max_bands) {
3786 stats->bands[n_bands].packet_count
3787 = get_32aligned_u64(&stat->n_packets);
3788 stats->bands[n_bands].byte_count
3789 = get_32aligned_u64(&stat->n_bytes);
3790 ++n_bands;
3791 }
3792 } else {
3793 stats->bands[n_bands].packet_count = 0;
3794 stats->bands[n_bands].byte_count = 0;
3795 ++n_bands;
3796 }
3797 }
3798 stats->n_bands = n_bands;
3799 } else {
3800 /* For a non-existent meter, return 0 stats. */
3801 stats->n_bands = 0;
3802 }
3803 }
3804
3805 ofpbuf_delete(msg);
3806 return error;
3807 }
3808
3809 static int
3810 dpif_netlink_meter_get(const struct dpif *dpif, ofproto_meter_id meter_id,
3811 struct ofputil_meter_stats *stats, uint16_t max_bands)
3812 {
3813 return dpif_netlink_meter_get_stats(dpif, meter_id, stats, max_bands,
3814 OVS_METER_CMD_GET);
3815 }
3816
3817 static int
3818 dpif_netlink_meter_del(struct dpif *dpif, ofproto_meter_id meter_id,
3819 struct ofputil_meter_stats *stats, uint16_t max_bands)
3820 {
3821 return dpif_netlink_meter_get_stats(dpif, meter_id, stats, max_bands,
3822 OVS_METER_CMD_DEL);
3823 }
3824
3825 static bool
3826 probe_broken_meters__(struct dpif *dpif)
3827 {
3828 /* This test is destructive if a probe occurs while ovs-vswitchd is
3829 * running (e.g., an ovs-dpctl meter command is called), so choose a
3830 * random high meter id to make this less likely to occur. */
3831 ofproto_meter_id id1 = { 54545401 };
3832 ofproto_meter_id id2 = { 54545402 };
3833 struct ofputil_meter_band band = {OFPMBT13_DROP, 0, 1, 0};
3834 struct ofputil_meter_config config1 = { 1, OFPMF13_KBPS, 1, &band};
3835 struct ofputil_meter_config config2 = { 2, OFPMF13_KBPS, 1, &band};
3836
3837 /* Try adding two meters and make sure that they both come back with
3838 * the proper meter id. Use the "__" version so that we don't cause
3839 * a recurve deadlock. */
3840 dpif_netlink_meter_set__(dpif, id1, &config1);
3841 dpif_netlink_meter_set__(dpif, id2, &config2);
3842
3843 if (dpif_netlink_meter_get(dpif, id1, NULL, 0)
3844 || dpif_netlink_meter_get(dpif, id2, NULL, 0)) {
3845 VLOG_INFO("The kernel module has a broken meter implementation.");
3846 return true;
3847 }
3848
3849 dpif_netlink_meter_del(dpif, id1, NULL, 0);
3850 dpif_netlink_meter_del(dpif, id2, NULL, 0);
3851
3852 return false;
3853 }
3854
3855 static bool
3856 probe_broken_meters(struct dpif *dpif)
3857 {
3858 /* This is a once-only test because currently OVS only has at most a single
3859 * Netlink capable datapath on any given platform. */
3860 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
3861
3862 static bool broken_meters = false;
3863 if (ovsthread_once_start(&once)) {
3864 broken_meters = probe_broken_meters__(dpif);
3865 ovsthread_once_done(&once);
3866 }
3867 return broken_meters;
3868 }
3869 \f
3870 const struct dpif_class dpif_netlink_class = {
3871 "system",
3872 false, /* cleanup_required */
3873 NULL, /* init */
3874 dpif_netlink_enumerate,
3875 NULL,
3876 dpif_netlink_open,
3877 dpif_netlink_close,
3878 dpif_netlink_destroy,
3879 dpif_netlink_run,
3880 NULL, /* wait */
3881 dpif_netlink_get_stats,
3882 dpif_netlink_port_add,
3883 dpif_netlink_port_del,
3884 NULL, /* port_set_config */
3885 dpif_netlink_port_query_by_number,
3886 dpif_netlink_port_query_by_name,
3887 dpif_netlink_port_get_pid,
3888 dpif_netlink_port_dump_start,
3889 dpif_netlink_port_dump_next,
3890 dpif_netlink_port_dump_done,
3891 dpif_netlink_port_poll,
3892 dpif_netlink_port_poll_wait,
3893 dpif_netlink_flow_flush,
3894 dpif_netlink_flow_dump_create,
3895 dpif_netlink_flow_dump_destroy,
3896 dpif_netlink_flow_dump_thread_create,
3897 dpif_netlink_flow_dump_thread_destroy,
3898 dpif_netlink_flow_dump_next,
3899 dpif_netlink_operate,
3900 dpif_netlink_recv_set,
3901 dpif_netlink_handlers_set,
3902 NULL, /* set_config */
3903 dpif_netlink_queue_to_priority,
3904 dpif_netlink_recv,
3905 dpif_netlink_recv_wait,
3906 dpif_netlink_recv_purge,
3907 NULL, /* register_dp_purge_cb */
3908 NULL, /* register_upcall_cb */
3909 NULL, /* enable_upcall */
3910 NULL, /* disable_upcall */
3911 dpif_netlink_get_datapath_version, /* get_datapath_version */
3912 dpif_netlink_ct_dump_start,
3913 dpif_netlink_ct_dump_next,
3914 dpif_netlink_ct_dump_done,
3915 dpif_netlink_ct_flush,
3916 NULL, /* ct_set_maxconns */
3917 NULL, /* ct_get_maxconns */
3918 NULL, /* ct_get_nconns */
3919 NULL, /* ct_set_tcp_seq_chk */
3920 NULL, /* ct_get_tcp_seq_chk */
3921 dpif_netlink_ct_set_limits,
3922 dpif_netlink_ct_get_limits,
3923 dpif_netlink_ct_del_limits,
3924 dpif_netlink_ct_set_timeout_policy,
3925 dpif_netlink_ct_get_timeout_policy,
3926 dpif_netlink_ct_del_timeout_policy,
3927 dpif_netlink_ct_timeout_policy_dump_start,
3928 dpif_netlink_ct_timeout_policy_dump_next,
3929 dpif_netlink_ct_timeout_policy_dump_done,
3930 dpif_netlink_ct_get_timeout_policy_name,
3931 NULL, /* ipf_set_enabled */
3932 NULL, /* ipf_set_min_frag */
3933 NULL, /* ipf_set_max_nfrags */
3934 NULL, /* ipf_get_status */
3935 NULL, /* ipf_dump_start */
3936 NULL, /* ipf_dump_next */
3937 NULL, /* ipf_dump_done */
3938 dpif_netlink_meter_get_features,
3939 dpif_netlink_meter_set,
3940 dpif_netlink_meter_get,
3941 dpif_netlink_meter_del,
3942 };
3943
3944 static int
3945 dpif_netlink_init(void)
3946 {
3947 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
3948 static int error;
3949
3950 if (ovsthread_once_start(&once)) {
3951 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
3952 &ovs_datapath_family);
3953 if (error) {
3954 VLOG_INFO("Generic Netlink family '%s' does not exist. "
3955 "The Open vSwitch kernel module is probably not loaded.",
3956 OVS_DATAPATH_FAMILY);
3957 }
3958 if (!error) {
3959 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
3960 }
3961 if (!error) {
3962 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
3963 }
3964 if (!error) {
3965 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
3966 &ovs_packet_family);
3967 }
3968 if (!error) {
3969 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
3970 &ovs_vport_mcgroup);
3971 }
3972 if (!error) {
3973 if (nl_lookup_genl_family(OVS_METER_FAMILY, &ovs_meter_family)) {
3974 VLOG_INFO("The kernel module does not support meters.");
3975 }
3976 }
3977 if (nl_lookup_genl_family(OVS_CT_LIMIT_FAMILY,
3978 &ovs_ct_limit_family) < 0) {
3979 VLOG_INFO("Generic Netlink family '%s' does not exist. "
3980 "Please update the Open vSwitch kernel module to enable "
3981 "the conntrack limit feature.", OVS_CT_LIMIT_FAMILY);
3982 }
3983
3984 ovs_tunnels_out_of_tree = dpif_netlink_rtnl_probe_oot_tunnels();
3985
3986 ovsthread_once_done(&once);
3987 }
3988
3989 return error;
3990 }
3991
3992 bool
3993 dpif_netlink_is_internal_device(const char *name)
3994 {
3995 struct dpif_netlink_vport reply;
3996 struct ofpbuf *buf;
3997 int error;
3998
3999 error = dpif_netlink_vport_get(name, &reply, &buf);
4000 if (!error) {
4001 ofpbuf_delete(buf);
4002 } else if (error != ENODEV && error != ENOENT) {
4003 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
4004 name, ovs_strerror(error));
4005 }
4006
4007 return reply.type == OVS_VPORT_TYPE_INTERNAL;
4008 }
4009
4010 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4011 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
4012 * positive errno value.
4013 *
4014 * 'vport' will contain pointers into 'buf', so the caller should not free
4015 * 'buf' while 'vport' is still in use. */
4016 static int
4017 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
4018 const struct ofpbuf *buf)
4019 {
4020 static const struct nl_policy ovs_vport_policy[] = {
4021 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
4022 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
4023 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
4024 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
4025 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
4026 .optional = true },
4027 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
4028 [OVS_VPORT_ATTR_NETNSID] = { .type = NL_A_U32, .optional = true },
4029 };
4030
4031 dpif_netlink_vport_init(vport);
4032
4033 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
4034 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
4035 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
4036 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
4037
4038 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
4039 if (!nlmsg || !genl || !ovs_header
4040 || nlmsg->nlmsg_type != ovs_vport_family
4041 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
4042 ARRAY_SIZE(ovs_vport_policy))) {
4043 return EINVAL;
4044 }
4045
4046 vport->cmd = genl->cmd;
4047 vport->dp_ifindex = ovs_header->dp_ifindex;
4048 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
4049 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
4050 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
4051 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
4052 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
4053 / (sizeof *vport->upcall_pids);
4054 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
4055
4056 }
4057 if (a[OVS_VPORT_ATTR_STATS]) {
4058 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
4059 }
4060 if (a[OVS_VPORT_ATTR_OPTIONS]) {
4061 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
4062 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
4063 }
4064 if (a[OVS_VPORT_ATTR_NETNSID]) {
4065 netnsid_set(&vport->netnsid,
4066 nl_attr_get_u32(a[OVS_VPORT_ATTR_NETNSID]));
4067 } else {
4068 netnsid_set_local(&vport->netnsid);
4069 }
4070 return 0;
4071 }
4072
4073 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
4074 * followed by Netlink attributes corresponding to 'vport'. */
4075 static void
4076 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
4077 struct ofpbuf *buf)
4078 {
4079 struct ovs_header *ovs_header;
4080
4081 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
4082 vport->cmd, OVS_VPORT_VERSION);
4083
4084 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
4085 ovs_header->dp_ifindex = vport->dp_ifindex;
4086
4087 if (vport->port_no != ODPP_NONE) {
4088 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
4089 }
4090
4091 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
4092 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
4093 }
4094
4095 if (vport->name) {
4096 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
4097 }
4098
4099 if (vport->upcall_pids) {
4100 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
4101 vport->upcall_pids,
4102 vport->n_upcall_pids * sizeof *vport->upcall_pids);
4103 }
4104
4105 if (vport->stats) {
4106 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
4107 vport->stats, sizeof *vport->stats);
4108 }
4109
4110 if (vport->options) {
4111 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
4112 vport->options, vport->options_len);
4113 }
4114 }
4115
4116 /* Clears 'vport' to "empty" values. */
4117 void
4118 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
4119 {
4120 memset(vport, 0, sizeof *vport);
4121 vport->port_no = ODPP_NONE;
4122 }
4123
4124 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4125 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4126 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4127 * result of the command is expected to be an ovs_vport also, which is decoded
4128 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
4129 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
4130 int
4131 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
4132 struct dpif_netlink_vport *reply,
4133 struct ofpbuf **bufp)
4134 {
4135 struct ofpbuf *request_buf;
4136 int error;
4137
4138 ovs_assert((reply != NULL) == (bufp != NULL));
4139
4140 error = dpif_netlink_init();
4141 if (error) {
4142 if (reply) {
4143 *bufp = NULL;
4144 dpif_netlink_vport_init(reply);
4145 }
4146 return error;
4147 }
4148
4149 request_buf = ofpbuf_new(1024);
4150 dpif_netlink_vport_to_ofpbuf(request, request_buf);
4151 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
4152 ofpbuf_delete(request_buf);
4153
4154 if (reply) {
4155 if (!error) {
4156 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
4157 }
4158 if (error) {
4159 dpif_netlink_vport_init(reply);
4160 ofpbuf_delete(*bufp);
4161 *bufp = NULL;
4162 }
4163 }
4164 return error;
4165 }
4166
4167 /* Obtains information about the kernel vport named 'name' and stores it into
4168 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
4169 * longer needed ('reply' will contain pointers into '*bufp'). */
4170 int
4171 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
4172 struct ofpbuf **bufp)
4173 {
4174 struct dpif_netlink_vport request;
4175
4176 dpif_netlink_vport_init(&request);
4177 request.cmd = OVS_VPORT_CMD_GET;
4178 request.name = name;
4179
4180 return dpif_netlink_vport_transact(&request, reply, bufp);
4181 }
4182
4183 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4184 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
4185 * positive errno value.
4186 *
4187 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
4188 * while 'dp' is still in use. */
4189 static int
4190 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
4191 {
4192 static const struct nl_policy ovs_datapath_policy[] = {
4193 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
4194 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
4195 .optional = true },
4196 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
4197 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
4198 .optional = true },
4199 };
4200
4201 dpif_netlink_dp_init(dp);
4202
4203 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
4204 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
4205 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
4206 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
4207
4208 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
4209 if (!nlmsg || !genl || !ovs_header
4210 || nlmsg->nlmsg_type != ovs_datapath_family
4211 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
4212 ARRAY_SIZE(ovs_datapath_policy))) {
4213 return EINVAL;
4214 }
4215
4216 dp->cmd = genl->cmd;
4217 dp->dp_ifindex = ovs_header->dp_ifindex;
4218 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
4219 if (a[OVS_DP_ATTR_STATS]) {
4220 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
4221 }
4222
4223 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
4224 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
4225 }
4226
4227 return 0;
4228 }
4229
4230 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
4231 static void
4232 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
4233 {
4234 struct ovs_header *ovs_header;
4235
4236 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
4237 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
4238 OVS_DATAPATH_VERSION);
4239
4240 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
4241 ovs_header->dp_ifindex = dp->dp_ifindex;
4242
4243 if (dp->name) {
4244 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
4245 }
4246
4247 if (dp->upcall_pid) {
4248 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
4249 }
4250
4251 if (dp->user_features) {
4252 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
4253 }
4254
4255 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
4256 }
4257
4258 /* Clears 'dp' to "empty" values. */
4259 static void
4260 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
4261 {
4262 memset(dp, 0, sizeof *dp);
4263 }
4264
4265 static void
4266 dpif_netlink_dp_dump_start(struct nl_dump *dump)
4267 {
4268 struct dpif_netlink_dp request;
4269 struct ofpbuf *buf;
4270
4271 dpif_netlink_dp_init(&request);
4272 request.cmd = OVS_DP_CMD_GET;
4273
4274 buf = ofpbuf_new(1024);
4275 dpif_netlink_dp_to_ofpbuf(&request, buf);
4276 nl_dump_start(dump, NETLINK_GENERIC, buf);
4277 ofpbuf_delete(buf);
4278 }
4279
4280 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4281 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4282 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4283 * result of the command is expected to be of the same form, which is decoded
4284 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
4285 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
4286 static int
4287 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
4288 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
4289 {
4290 struct ofpbuf *request_buf;
4291 int error;
4292
4293 ovs_assert((reply != NULL) == (bufp != NULL));
4294
4295 request_buf = ofpbuf_new(1024);
4296 dpif_netlink_dp_to_ofpbuf(request, request_buf);
4297 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
4298 ofpbuf_delete(request_buf);
4299
4300 if (reply) {
4301 dpif_netlink_dp_init(reply);
4302 if (!error) {
4303 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
4304 }
4305 if (error) {
4306 ofpbuf_delete(*bufp);
4307 *bufp = NULL;
4308 }
4309 }
4310 return error;
4311 }
4312
4313 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
4314 * The caller must free '*bufp' when the reply is no longer needed ('reply'
4315 * will contain pointers into '*bufp'). */
4316 static int
4317 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
4318 struct ofpbuf **bufp)
4319 {
4320 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
4321 struct dpif_netlink_dp request;
4322
4323 dpif_netlink_dp_init(&request);
4324 request.cmd = OVS_DP_CMD_GET;
4325 request.dp_ifindex = dpif->dp_ifindex;
4326
4327 return dpif_netlink_dp_transact(&request, reply, bufp);
4328 }
4329
4330 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4331 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
4332 * positive errno value.
4333 *
4334 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
4335 * while 'flow' is still in use. */
4336 static int
4337 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
4338 const struct ofpbuf *buf)
4339 {
4340 static const struct nl_policy ovs_flow_policy[__OVS_FLOW_ATTR_MAX] = {
4341 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED, .optional = true },
4342 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
4343 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
4344 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
4345 .optional = true },
4346 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
4347 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
4348 [OVS_FLOW_ATTR_UFID] = { .type = NL_A_U128, .optional = true },
4349 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
4350 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
4351 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
4352 };
4353
4354 dpif_netlink_flow_init(flow);
4355
4356 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
4357 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
4358 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
4359 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
4360
4361 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
4362 if (!nlmsg || !genl || !ovs_header
4363 || nlmsg->nlmsg_type != ovs_flow_family
4364 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
4365 ARRAY_SIZE(ovs_flow_policy))) {
4366 return EINVAL;
4367 }
4368 if (!a[OVS_FLOW_ATTR_KEY] && !a[OVS_FLOW_ATTR_UFID]) {
4369 return EINVAL;
4370 }
4371
4372 flow->nlmsg_flags = nlmsg->nlmsg_flags;
4373 flow->dp_ifindex = ovs_header->dp_ifindex;
4374 if (a[OVS_FLOW_ATTR_KEY]) {
4375 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
4376 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
4377 }
4378
4379 if (a[OVS_FLOW_ATTR_UFID]) {
4380 flow->ufid = nl_attr_get_u128(a[OVS_FLOW_ATTR_UFID]);
4381 flow->ufid_present = true;
4382 }
4383 if (a[OVS_FLOW_ATTR_MASK]) {
4384 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
4385 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
4386 }
4387 if (a[OVS_FLOW_ATTR_ACTIONS]) {
4388 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
4389 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
4390 }
4391 if (a[OVS_FLOW_ATTR_STATS]) {
4392 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
4393 }
4394 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
4395 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
4396 }
4397 if (a[OVS_FLOW_ATTR_USED]) {
4398 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
4399 }
4400 return 0;
4401 }
4402
4403
4404 /*
4405 * If PACKET_TYPE attribute is present in 'data', it filters PACKET_TYPE out.
4406 * If the flow is not Ethernet, the OVS_KEY_ATTR_PACKET_TYPE is converted to
4407 * OVS_KEY_ATTR_ETHERTYPE. Puts 'data' to 'buf'.
4408 */
4409 static void
4410 put_exclude_packet_type(struct ofpbuf *buf, uint16_t type,
4411 const struct nlattr *data, uint16_t data_len)
4412 {
4413 const struct nlattr *packet_type;
4414
4415 packet_type = nl_attr_find__(data, data_len, OVS_KEY_ATTR_PACKET_TYPE);
4416
4417 if (packet_type) {
4418 /* exclude PACKET_TYPE Netlink attribute. */
4419 ovs_assert(NLA_ALIGN(packet_type->nla_len) == NL_A_U32_SIZE);
4420 size_t packet_type_len = NL_A_U32_SIZE;
4421 size_t first_chunk_size = (uint8_t *)packet_type - (uint8_t *)data;
4422 size_t second_chunk_size = data_len - first_chunk_size
4423 - packet_type_len;
4424 struct nlattr *next_attr = nl_attr_next(packet_type);
4425 size_t ofs;
4426
4427 ofs = nl_msg_start_nested(buf, type);
4428 nl_msg_put(buf, data, first_chunk_size);
4429 nl_msg_put(buf, next_attr, second_chunk_size);
4430 if (!nl_attr_find__(data, data_len, OVS_KEY_ATTR_ETHERNET)) {
4431 ovs_be16 pt = pt_ns_type_be(nl_attr_get_be32(packet_type));
4432 const struct nlattr *nla;
4433
4434 nla = nl_attr_find(buf, ofs + NLA_HDRLEN, OVS_KEY_ATTR_ETHERTYPE);
4435 if (nla) {
4436 ovs_be16 *ethertype;
4437
4438 ethertype = CONST_CAST(ovs_be16 *, nl_attr_get(nla));
4439 *ethertype = pt;
4440 } else {
4441 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, pt);
4442 }
4443 }
4444 nl_msg_end_nested(buf, ofs);
4445 } else {
4446 nl_msg_put_unspec(buf, type, data, data_len);
4447 }
4448 }
4449
4450 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
4451 * followed by Netlink attributes corresponding to 'flow'. */
4452 static void
4453 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
4454 struct ofpbuf *buf)
4455 {
4456 struct ovs_header *ovs_header;
4457
4458 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
4459 NLM_F_REQUEST | flow->nlmsg_flags,
4460 flow->cmd, OVS_FLOW_VERSION);
4461
4462 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
4463 ovs_header->dp_ifindex = flow->dp_ifindex;
4464
4465 if (flow->ufid_present) {
4466 nl_msg_put_u128(buf, OVS_FLOW_ATTR_UFID, flow->ufid);
4467 }
4468 if (flow->ufid_terse) {
4469 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UFID_FLAGS,
4470 OVS_UFID_F_OMIT_KEY | OVS_UFID_F_OMIT_MASK
4471 | OVS_UFID_F_OMIT_ACTIONS);
4472 }
4473 if (!flow->ufid_terse || !flow->ufid_present) {
4474 if (flow->key_len) {
4475 put_exclude_packet_type(buf, OVS_FLOW_ATTR_KEY, flow->key,
4476 flow->key_len);
4477 }
4478 if (flow->mask_len) {
4479 put_exclude_packet_type(buf, OVS_FLOW_ATTR_MASK, flow->mask,
4480 flow->mask_len);
4481 }
4482 if (flow->actions || flow->actions_len) {
4483 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
4484 flow->actions, flow->actions_len);
4485 }
4486 }
4487
4488 /* We never need to send these to the kernel. */
4489 ovs_assert(!flow->stats);
4490 ovs_assert(!flow->tcp_flags);
4491 ovs_assert(!flow->used);
4492
4493 if (flow->clear) {
4494 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
4495 }
4496 if (flow->probe) {
4497 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
4498 }
4499 }
4500
4501 /* Clears 'flow' to "empty" values. */
4502 static void
4503 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
4504 {
4505 memset(flow, 0, sizeof *flow);
4506 }
4507
4508 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4509 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4510 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4511 * result of the command is expected to be a flow also, which is decoded and
4512 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
4513 * is no longer needed ('reply' will contain pointers into '*bufp'). */
4514 static int
4515 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
4516 struct dpif_netlink_flow *reply,
4517 struct ofpbuf **bufp)
4518 {
4519 struct ofpbuf *request_buf;
4520 int error;
4521
4522 ovs_assert((reply != NULL) == (bufp != NULL));
4523
4524 if (reply) {
4525 request->nlmsg_flags |= NLM_F_ECHO;
4526 }
4527
4528 request_buf = ofpbuf_new(1024);
4529 dpif_netlink_flow_to_ofpbuf(request, request_buf);
4530 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
4531 ofpbuf_delete(request_buf);
4532
4533 if (reply) {
4534 if (!error) {
4535 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
4536 }
4537 if (error) {
4538 dpif_netlink_flow_init(reply);
4539 ofpbuf_delete(*bufp);
4540 *bufp = NULL;
4541 }
4542 }
4543 return error;
4544 }
4545
4546 static void
4547 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
4548 struct dpif_flow_stats *stats)
4549 {
4550 if (flow->stats) {
4551 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
4552 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
4553 } else {
4554 stats->n_packets = 0;
4555 stats->n_bytes = 0;
4556 }
4557 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
4558 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
4559 }
4560
4561 /* Logs information about a packet that was recently lost in 'ch' (in
4562 * 'dpif_'). */
4563 static void
4564 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
4565 uint32_t handler_id)
4566 {
4567 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
4568 struct ds s;
4569
4570 if (VLOG_DROP_WARN(&rl)) {
4571 return;
4572 }
4573
4574 ds_init(&s);
4575 if (ch->last_poll != LLONG_MIN) {
4576 ds_put_format(&s, " (last polled %lld ms ago)",
4577 time_msec() - ch->last_poll);
4578 }
4579
4580 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
4581 dpif_name(&dpif->dpif), ch_idx, handler_id);
4582 ds_destroy(&s);
4583 }