]> git.proxmox.com Git - ovs.git/blob - lib/dpif-linux.c
datapath: Always use generic stats for devices (vports)
[ovs.git] / lib / dpif-linux.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or 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-linux.h"
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <net/if.h>
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "bitmap.h"
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
38 #include "flow.h"
39 #include "netdev.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-notifier.h"
43 #include "netlink-socket.h"
44 #include "netlink.h"
45 #include "odp-util.h"
46 #include "ofpbuf.h"
47 #include "openvswitch/tunnel.h"
48 #include "packets.h"
49 #include "poll-loop.h"
50 #include "shash.h"
51 #include "sset.h"
52 #include "unaligned.h"
53 #include "util.h"
54 #include "vlog.h"
55
56 VLOG_DEFINE_THIS_MODULE(dpif_linux);
57
58 enum { LRU_MAX_PORTS = 1024 };
59 enum { LRU_MASK = LRU_MAX_PORTS - 1};
60 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
61
62 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
63 * missing if we have old headers. */
64 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
65
66 struct dpif_linux_dp {
67 /* Generic Netlink header. */
68 uint8_t cmd;
69
70 /* struct ovs_header. */
71 int dp_ifindex;
72
73 /* Attributes. */
74 const char *name; /* OVS_DP_ATTR_NAME. */
75 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
76 enum ovs_frag_handling ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
77 const uint32_t *sampling; /* OVS_DP_ATTR_SAMPLING. */
78 uint32_t mcgroups[DPIF_N_UC_TYPES]; /* OVS_DP_ATTR_MCGROUPS. */
79 };
80
81 static void dpif_linux_dp_init(struct dpif_linux_dp *);
82 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
83 const struct ofpbuf *);
84 static void dpif_linux_dp_dump_start(struct nl_dump *);
85 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
86 struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
88 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
89 struct ofpbuf **bufp);
90
91 struct dpif_linux_flow {
92 /* Generic Netlink header. */
93 uint8_t cmd;
94
95 /* struct ovs_header. */
96 unsigned int nlmsg_flags;
97 int dp_ifindex;
98
99 /* Attributes.
100 *
101 * The 'stats' and 'used' members point to 64-bit data that might only be
102 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
103 * access their values.
104 *
105 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
106 * the Netlink version of the command, even if actions_len is zero. */
107 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
108 size_t key_len;
109 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
110 size_t actions_len;
111 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
112 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
113 const uint64_t *used; /* OVS_FLOW_ATTR_USED. */
114 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
115 };
116
117 static void dpif_linux_flow_init(struct dpif_linux_flow *);
118 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
119 const struct ofpbuf *);
120 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
121 struct ofpbuf *);
122 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
123 struct dpif_linux_flow *reply,
124 struct ofpbuf **bufp);
125 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
126 struct dpif_flow_stats *);
127
128 /* Datapath interface for the openvswitch Linux kernel module. */
129 struct dpif_linux {
130 struct dpif dpif;
131 int dp_ifindex;
132
133 /* Multicast group messages. */
134 struct nl_sock *mc_sock;
135 uint32_t mcgroups[DPIF_N_UC_TYPES];
136 unsigned int listen_mask;
137
138 /* Change notification. */
139 struct sset changed_ports; /* Ports that have changed. */
140 struct nln_notifier port_notifier;
141 bool change_error;
142
143 /* Queue of unused ports. */
144 unsigned long *lru_bitmap;
145 uint16_t lru_ports[LRU_MAX_PORTS];
146 size_t lru_head;
147 size_t lru_tail;
148 };
149
150 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
151
152 /* Generic Netlink family numbers for OVS. */
153 static int ovs_datapath_family;
154 static int ovs_vport_family;
155 static int ovs_flow_family;
156 static int ovs_packet_family;
157
158 /* Generic Netlink socket. */
159 static struct nl_sock *genl_sock;
160 static struct nln *nln = NULL;
161
162 static int dpif_linux_init(void);
163 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
164 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
165 static void dpif_linux_port_changed(const void *vport, void *dpif);
166
167 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
168 struct ofpbuf *);
169 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
170 const struct ofpbuf *);
171
172 static struct dpif_linux *
173 dpif_linux_cast(const struct dpif *dpif)
174 {
175 dpif_assert_class(dpif, &dpif_linux_class);
176 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
177 }
178
179 static void
180 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
181 {
182 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
183 bitmap_set1(dp->lru_bitmap, port);
184 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
185 }
186 }
187
188 static uint32_t
189 dpif_linux_pop_port(struct dpif_linux *dp)
190 {
191 uint16_t port;
192
193 if (dp->lru_head == dp->lru_tail) {
194 return UINT32_MAX;
195 }
196
197 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
198 bitmap_set0(dp->lru_bitmap, port);
199 return port;
200 }
201
202 static int
203 dpif_linux_enumerate(struct sset *all_dps)
204 {
205 struct nl_dump dump;
206 struct ofpbuf msg;
207 int error;
208
209 error = dpif_linux_init();
210 if (error) {
211 return error;
212 }
213
214 dpif_linux_dp_dump_start(&dump);
215 while (nl_dump_next(&dump, &msg)) {
216 struct dpif_linux_dp dp;
217
218 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
219 sset_add(all_dps, dp.name);
220 }
221 }
222 return nl_dump_done(&dump);
223 }
224
225 static int
226 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
227 bool create, struct dpif **dpifp)
228 {
229 struct dpif_linux_dp dp_request, dp;
230 struct ofpbuf *buf;
231 int error;
232
233 error = dpif_linux_init();
234 if (error) {
235 return error;
236 }
237
238 /* Create or look up datapath. */
239 dpif_linux_dp_init(&dp_request);
240 dp_request.cmd = create ? OVS_DP_CMD_NEW : OVS_DP_CMD_GET;
241 dp_request.name = name;
242 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
243 if (error) {
244 return error;
245 }
246 error = open_dpif(&dp, dpifp);
247 ofpbuf_delete(buf);
248
249 return error;
250 }
251
252 static int
253 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
254 {
255 struct dpif_linux *dpif;
256 int error;
257 int i;
258
259 dpif = xmalloc(sizeof *dpif);
260 error = nln_notifier_register(nln, &dpif->port_notifier,
261 dpif_linux_port_changed, dpif);
262 if (error) {
263 goto error_free;
264 }
265
266 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
267 dp->dp_ifindex, dp->dp_ifindex);
268
269 dpif->mc_sock = NULL;
270 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
271 dpif->mcgroups[i] = dp->mcgroups[i];
272 }
273 dpif->listen_mask = 0;
274 dpif->dp_ifindex = dp->dp_ifindex;
275 sset_init(&dpif->changed_ports);
276 dpif->change_error = false;
277 *dpifp = &dpif->dpif;
278
279 dpif->lru_head = dpif->lru_tail = 0;
280 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
281 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
282 for (i = 1; i < LRU_MAX_PORTS; i++) {
283 dpif_linux_push_port(dpif, i);
284 }
285 return 0;
286
287 error_free:
288 free(dpif);
289 return error;
290 }
291
292 static void
293 dpif_linux_close(struct dpif *dpif_)
294 {
295 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
296
297 if (nln) {
298 nln_notifier_unregister(nln, &dpif->port_notifier);
299 }
300
301 nl_sock_destroy(dpif->mc_sock);
302 sset_destroy(&dpif->changed_ports);
303 free(dpif->lru_bitmap);
304 free(dpif);
305 }
306
307 static int
308 dpif_linux_destroy(struct dpif *dpif_)
309 {
310 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
311 struct dpif_linux_dp dp;
312
313 dpif_linux_dp_init(&dp);
314 dp.cmd = OVS_DP_CMD_DEL;
315 dp.dp_ifindex = dpif->dp_ifindex;
316 return dpif_linux_dp_transact(&dp, NULL, NULL);
317 }
318
319 static void
320 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
321 {
322 if (nln) {
323 nln_notifier_run(nln);
324 }
325 }
326
327 static void
328 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
329 {
330 if (nln) {
331 nln_notifier_wait(nln);
332 }
333 }
334
335 static int
336 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
337 {
338 struct dpif_linux_dp dp;
339 struct ofpbuf *buf;
340 int error;
341
342 error = dpif_linux_dp_get(dpif_, &dp, &buf);
343 if (!error) {
344 *stats = dp.stats;
345 ofpbuf_delete(buf);
346 }
347 return error;
348 }
349
350 static int
351 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
352 {
353 struct dpif_linux_dp dp;
354 struct ofpbuf *buf;
355 int error;
356
357 error = dpif_linux_dp_get(dpif_, &dp, &buf);
358 if (!error) {
359 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
360 ofpbuf_delete(buf);
361 }
362 return error;
363 }
364
365 static int
366 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
367 {
368 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
369 struct dpif_linux_dp dp;
370
371 dpif_linux_dp_init(&dp);
372 dp.cmd = OVS_DP_CMD_SET;
373 dp.dp_ifindex = dpif->dp_ifindex;
374 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
375 return dpif_linux_dp_transact(&dp, NULL, NULL);
376 }
377
378 static int
379 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
380 uint16_t *port_nop)
381 {
382 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
383 const char *name = netdev_get_name(netdev);
384 const char *type = netdev_get_type(netdev);
385 struct dpif_linux_vport request, reply;
386 const struct ofpbuf *options;
387 struct ofpbuf *buf;
388 int error;
389
390 dpif_linux_vport_init(&request);
391 request.cmd = OVS_VPORT_CMD_NEW;
392 request.dp_ifindex = dpif->dp_ifindex;
393 request.type = netdev_vport_get_vport_type(netdev);
394 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
395 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
396 "unsupported type `%s'",
397 dpif_name(dpif_), name, type);
398 return EINVAL;
399 }
400 request.name = name;
401
402 options = netdev_vport_get_options(netdev);
403 if (options && options->size) {
404 request.options = options->data;
405 request.options_len = options->size;
406 }
407
408 if (request.type == OVS_VPORT_TYPE_NETDEV) {
409 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
410 }
411
412 /* Loop until we find a port that isn't used. */
413 do {
414 request.port_no = dpif_linux_pop_port(dpif);
415 error = dpif_linux_vport_transact(&request, &reply, &buf);
416
417 if (!error) {
418 *port_nop = reply.port_no;
419 }
420 ofpbuf_delete(buf);
421 } while (request.port_no != UINT32_MAX
422 && (error == EBUSY || error == EFBIG));
423
424 return error;
425 }
426
427 static int
428 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
429 {
430 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
431 struct dpif_linux_vport vport;
432 int error;
433
434 dpif_linux_vport_init(&vport);
435 vport.cmd = OVS_VPORT_CMD_DEL;
436 vport.dp_ifindex = dpif->dp_ifindex;
437 vport.port_no = port_no;
438 error = dpif_linux_vport_transact(&vport, NULL, NULL);
439
440 if (!error) {
441 dpif_linux_push_port(dpif, port_no);
442 }
443 return error;
444 }
445
446 static int
447 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
448 const char *port_name, struct dpif_port *dpif_port)
449 {
450 struct dpif_linux_vport request;
451 struct dpif_linux_vport reply;
452 struct ofpbuf *buf;
453 int error;
454
455 dpif_linux_vport_init(&request);
456 request.cmd = OVS_VPORT_CMD_GET;
457 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
458 request.port_no = port_no;
459 request.name = port_name;
460
461 error = dpif_linux_vport_transact(&request, &reply, &buf);
462 if (!error) {
463 dpif_port->name = xstrdup(reply.name);
464 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
465 dpif_port->port_no = reply.port_no;
466 ofpbuf_delete(buf);
467 }
468 return error;
469 }
470
471 static int
472 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
473 struct dpif_port *dpif_port)
474 {
475 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
476 }
477
478 static int
479 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
480 struct dpif_port *dpif_port)
481 {
482 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
483 }
484
485 static int
486 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
487 {
488 /* If the datapath increases its range of supported ports, then it should
489 * start reporting that. */
490 return 1024;
491 }
492
493 static int
494 dpif_linux_flow_flush(struct dpif *dpif_)
495 {
496 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
497 struct dpif_linux_flow flow;
498
499 dpif_linux_flow_init(&flow);
500 flow.cmd = OVS_FLOW_CMD_DEL;
501 flow.dp_ifindex = dpif->dp_ifindex;
502 return dpif_linux_flow_transact(&flow, NULL, NULL);
503 }
504
505 struct dpif_linux_port_state {
506 struct nl_dump dump;
507 unsigned long *port_bitmap; /* Ports in the datapath. */
508 bool complete; /* Dump completed without error. */
509 };
510
511 static int
512 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
513 {
514 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
515 struct dpif_linux_port_state *state;
516 struct dpif_linux_vport request;
517 struct ofpbuf *buf;
518
519 *statep = state = xmalloc(sizeof *state);
520 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
521 state->complete = false;
522
523 dpif_linux_vport_init(&request);
524 request.cmd = OVS_DP_CMD_GET;
525 request.dp_ifindex = dpif->dp_ifindex;
526
527 buf = ofpbuf_new(1024);
528 dpif_linux_vport_to_ofpbuf(&request, buf);
529 nl_dump_start(&state->dump, genl_sock, buf);
530 ofpbuf_delete(buf);
531
532 return 0;
533 }
534
535 static int
536 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
537 struct dpif_port *dpif_port)
538 {
539 struct dpif_linux_port_state *state = state_;
540 struct dpif_linux_vport vport;
541 struct ofpbuf buf;
542 int error;
543
544 if (!nl_dump_next(&state->dump, &buf)) {
545 state->complete = true;
546 return EOF;
547 }
548
549 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
550 if (error) {
551 return error;
552 }
553
554 if (vport.port_no < LRU_MAX_PORTS) {
555 bitmap_set1(state->port_bitmap, vport.port_no);
556 }
557
558 dpif_port->name = (char *) vport.name;
559 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
560 dpif_port->port_no = vport.port_no;
561 return 0;
562 }
563
564 static int
565 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
566 {
567 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
568 struct dpif_linux_port_state *state = state_;
569 int error = nl_dump_done(&state->dump);
570
571 if (state->complete) {
572 uint16_t i;
573
574 for (i = 0; i < LRU_MAX_PORTS; i++) {
575 if (!bitmap_is_set(state->port_bitmap, i)) {
576 dpif_linux_push_port(dpif, i);
577 }
578 }
579 }
580
581 free(state->port_bitmap);
582 free(state);
583 return error;
584 }
585
586 static int
587 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
588 {
589 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
590
591 if (dpif->change_error) {
592 dpif->change_error = false;
593 sset_clear(&dpif->changed_ports);
594 return ENOBUFS;
595 } else if (!sset_is_empty(&dpif->changed_ports)) {
596 *devnamep = sset_pop(&dpif->changed_ports);
597 return 0;
598 } else {
599 return EAGAIN;
600 }
601 }
602
603 static void
604 dpif_linux_port_poll_wait(const struct dpif *dpif_)
605 {
606 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
607 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
608 poll_immediate_wake();
609 }
610 }
611
612 static int
613 dpif_linux_flow_get__(const struct dpif *dpif_,
614 const struct nlattr *key, size_t key_len,
615 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
616 {
617 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
618 struct dpif_linux_flow request;
619
620 dpif_linux_flow_init(&request);
621 request.cmd = OVS_FLOW_CMD_GET;
622 request.dp_ifindex = dpif->dp_ifindex;
623 request.key = key;
624 request.key_len = key_len;
625 return dpif_linux_flow_transact(&request, reply, bufp);
626 }
627
628 static int
629 dpif_linux_flow_get(const struct dpif *dpif_,
630 const struct nlattr *key, size_t key_len,
631 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
632 {
633 struct dpif_linux_flow reply;
634 struct ofpbuf *buf;
635 int error;
636
637 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
638 if (!error) {
639 if (stats) {
640 dpif_linux_flow_get_stats(&reply, stats);
641 }
642 if (actionsp) {
643 buf->data = (void *) reply.actions;
644 buf->size = reply.actions_len;
645 *actionsp = buf;
646 } else {
647 ofpbuf_delete(buf);
648 }
649 }
650 return error;
651 }
652
653 static int
654 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
655 const struct nlattr *key, size_t key_len,
656 const struct nlattr *actions, size_t actions_len,
657 struct dpif_flow_stats *stats)
658 {
659 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
660 struct dpif_linux_flow request, reply;
661 struct nlattr dummy_action;
662 struct ofpbuf *buf;
663 int error;
664
665 dpif_linux_flow_init(&request);
666 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
667 request.dp_ifindex = dpif->dp_ifindex;
668 request.key = key;
669 request.key_len = key_len;
670 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
671 request.actions = actions ? actions : &dummy_action;
672 request.actions_len = actions_len;
673 if (flags & DPIF_FP_ZERO_STATS) {
674 request.clear = true;
675 }
676 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
677 error = dpif_linux_flow_transact(&request,
678 stats ? &reply : NULL,
679 stats ? &buf : NULL);
680 if (!error && stats) {
681 dpif_linux_flow_get_stats(&reply, stats);
682 ofpbuf_delete(buf);
683 }
684 return error;
685 }
686
687 static int
688 dpif_linux_flow_del(struct dpif *dpif_,
689 const struct nlattr *key, size_t key_len,
690 struct dpif_flow_stats *stats)
691 {
692 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
693 struct dpif_linux_flow request, reply;
694 struct ofpbuf *buf;
695 int error;
696
697 dpif_linux_flow_init(&request);
698 request.cmd = OVS_FLOW_CMD_DEL;
699 request.dp_ifindex = dpif->dp_ifindex;
700 request.key = key;
701 request.key_len = key_len;
702 error = dpif_linux_flow_transact(&request,
703 stats ? &reply : NULL,
704 stats ? &buf : NULL);
705 if (!error && stats) {
706 dpif_linux_flow_get_stats(&reply, stats);
707 ofpbuf_delete(buf);
708 }
709 return error;
710 }
711
712 struct dpif_linux_flow_state {
713 struct nl_dump dump;
714 struct dpif_linux_flow flow;
715 struct dpif_flow_stats stats;
716 struct ofpbuf *buf;
717 };
718
719 static int
720 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
721 {
722 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
723 struct dpif_linux_flow_state *state;
724 struct dpif_linux_flow request;
725 struct ofpbuf *buf;
726
727 *statep = state = xmalloc(sizeof *state);
728
729 dpif_linux_flow_init(&request);
730 request.cmd = OVS_DP_CMD_GET;
731 request.dp_ifindex = dpif->dp_ifindex;
732
733 buf = ofpbuf_new(1024);
734 dpif_linux_flow_to_ofpbuf(&request, buf);
735 nl_dump_start(&state->dump, genl_sock, buf);
736 ofpbuf_delete(buf);
737
738 state->buf = NULL;
739
740 return 0;
741 }
742
743 static int
744 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
745 const struct nlattr **key, size_t *key_len,
746 const struct nlattr **actions, size_t *actions_len,
747 const struct dpif_flow_stats **stats)
748 {
749 struct dpif_linux_flow_state *state = state_;
750 struct ofpbuf buf;
751 int error;
752
753 do {
754 ofpbuf_delete(state->buf);
755 state->buf = NULL;
756
757 if (!nl_dump_next(&state->dump, &buf)) {
758 return EOF;
759 }
760
761 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
762 if (error) {
763 return error;
764 }
765
766 if (actions && !state->flow.actions) {
767 error = dpif_linux_flow_get__(dpif_, state->flow.key,
768 state->flow.key_len,
769 &state->flow, &state->buf);
770 if (error == ENOENT) {
771 VLOG_DBG("dumped flow disappeared on get");
772 } else if (error) {
773 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
774 }
775 }
776 } while (error);
777
778 if (actions) {
779 *actions = state->flow.actions;
780 *actions_len = state->flow.actions_len;
781 }
782 if (key) {
783 *key = state->flow.key;
784 *key_len = state->flow.key_len;
785 }
786 if (stats) {
787 dpif_linux_flow_get_stats(&state->flow, &state->stats);
788 *stats = &state->stats;
789 }
790 return error;
791 }
792
793 static int
794 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
795 {
796 struct dpif_linux_flow_state *state = state_;
797 int error = nl_dump_done(&state->dump);
798 ofpbuf_delete(state->buf);
799 free(state);
800 return error;
801 }
802
803 static int
804 dpif_linux_execute__(int dp_ifindex,
805 const struct nlattr *key, size_t key_len,
806 const struct nlattr *actions, size_t actions_len,
807 const struct ofpbuf *packet)
808 {
809 struct ovs_header *execute;
810 struct ofpbuf *buf;
811 int error;
812
813 buf = ofpbuf_new(128 + actions_len + packet->size);
814
815 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
816 OVS_PACKET_CMD_EXECUTE, 1);
817
818 execute = ofpbuf_put_uninit(buf, sizeof *execute);
819 execute->dp_ifindex = dp_ifindex;
820
821 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
822 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
823 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
824
825 error = nl_sock_transact(genl_sock, buf, NULL);
826 ofpbuf_delete(buf);
827 return error;
828 }
829
830 static int
831 dpif_linux_execute(struct dpif *dpif_,
832 const struct nlattr *key, size_t key_len,
833 const struct nlattr *actions, size_t actions_len,
834 const struct ofpbuf *packet)
835 {
836 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
837
838 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
839 actions, actions_len, packet);
840 }
841
842 static int
843 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
844 {
845 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
846 *listen_mask = dpif->listen_mask;
847 return 0;
848 }
849
850 static int
851 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
852 {
853 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
854 int error;
855 int i;
856
857 if (listen_mask == dpif->listen_mask) {
858 return 0;
859 } else if (!listen_mask) {
860 nl_sock_destroy(dpif->mc_sock);
861 dpif->mc_sock = NULL;
862 dpif->listen_mask = 0;
863 return 0;
864 } else if (!dpif->mc_sock) {
865 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
866 if (error) {
867 return error;
868 }
869 }
870
871 /* Unsubscribe from old groups. */
872 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
873 if (dpif->listen_mask & (1u << i)) {
874 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
875 }
876 }
877
878 /* Update listen_mask. */
879 dpif->listen_mask = listen_mask;
880
881 /* Subscribe to new groups. */
882 error = 0;
883 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
884 if (dpif->listen_mask & (1u << i)) {
885 int retval;
886
887 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
888 if (retval) {
889 error = retval;
890 }
891 }
892 }
893 return error;
894 }
895
896 static int
897 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
898 uint32_t *probability)
899 {
900 struct dpif_linux_dp dp;
901 struct ofpbuf *buf;
902 int error;
903
904 error = dpif_linux_dp_get(dpif_, &dp, &buf);
905 if (!error) {
906 *probability = dp.sampling ? *dp.sampling : 0;
907 ofpbuf_delete(buf);
908 }
909 return error;
910 }
911
912 static int
913 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
914 {
915 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
916 struct dpif_linux_dp dp;
917
918 dpif_linux_dp_init(&dp);
919 dp.cmd = OVS_DP_CMD_SET;
920 dp.dp_ifindex = dpif->dp_ifindex;
921 dp.sampling = &probability;
922 return dpif_linux_dp_transact(&dp, NULL, NULL);
923 }
924
925 static int
926 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
927 uint32_t queue_id, uint32_t *priority)
928 {
929 if (queue_id < 0xf000) {
930 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
931 return 0;
932 } else {
933 return EINVAL;
934 }
935 }
936
937 static int
938 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
939 int *dp_ifindex)
940 {
941 static const struct nl_policy ovs_packet_policy[] = {
942 /* Always present. */
943 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
944 .min_len = ETH_HEADER_LEN },
945 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
946
947 /* OVS_PACKET_CMD_ACTION only. */
948 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
949
950 /* OVS_PACKET_CMD_SAMPLE only. */
951 [OVS_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
952 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
953 };
954
955 struct ovs_header *ovs_header;
956 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
957 struct nlmsghdr *nlmsg;
958 struct genlmsghdr *genl;
959 struct ofpbuf b;
960 int type;
961
962 ofpbuf_use_const(&b, buf->data, buf->size);
963
964 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
965 genl = ofpbuf_try_pull(&b, sizeof *genl);
966 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
967 if (!nlmsg || !genl || !ovs_header
968 || nlmsg->nlmsg_type != ovs_packet_family
969 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
970 ARRAY_SIZE(ovs_packet_policy))) {
971 return EINVAL;
972 }
973
974 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
975 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
976 : genl->cmd == OVS_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
977 : -1);
978 if (type < 0) {
979 return EINVAL;
980 }
981
982 memset(upcall, 0, sizeof *upcall);
983 upcall->type = type;
984 upcall->packet = buf;
985 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
986 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
987 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
988 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
989 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
990 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
991 : 0);
992 upcall->sample_pool = (a[OVS_PACKET_ATTR_SAMPLE_POOL]
993 ? nl_attr_get_u32(a[OVS_PACKET_ATTR_SAMPLE_POOL])
994 : 0);
995 if (a[OVS_PACKET_ATTR_ACTIONS]) {
996 upcall->actions = (void *) nl_attr_get(a[OVS_PACKET_ATTR_ACTIONS]);
997 upcall->actions_len = nl_attr_get_size(a[OVS_PACKET_ATTR_ACTIONS]);
998 }
999
1000 *dp_ifindex = ovs_header->dp_ifindex;
1001
1002 return 0;
1003 }
1004
1005 static int
1006 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1007 {
1008 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1009 struct ofpbuf *buf;
1010 int error;
1011 int i;
1012
1013 if (!dpif->mc_sock) {
1014 return EAGAIN;
1015 }
1016
1017 for (i = 0; i < 50; i++) {
1018 int dp_ifindex;
1019
1020 error = nl_sock_recv(dpif->mc_sock, &buf, false);
1021 if (error) {
1022 return error;
1023 }
1024
1025 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1026 if (!error
1027 && dp_ifindex == dpif->dp_ifindex
1028 && dpif->listen_mask & (1u << upcall->type)) {
1029 return 0;
1030 }
1031
1032 ofpbuf_delete(buf);
1033 if (error) {
1034 return error;
1035 }
1036 }
1037
1038 return EAGAIN;
1039 }
1040
1041 static void
1042 dpif_linux_recv_wait(struct dpif *dpif_)
1043 {
1044 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1045 if (dpif->mc_sock) {
1046 nl_sock_wait(dpif->mc_sock, POLLIN);
1047 }
1048 }
1049
1050 static void
1051 dpif_linux_recv_purge(struct dpif *dpif_)
1052 {
1053 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1054
1055 if (dpif->mc_sock) {
1056 nl_sock_drain(dpif->mc_sock);
1057 }
1058 }
1059
1060 const struct dpif_class dpif_linux_class = {
1061 "system",
1062 dpif_linux_enumerate,
1063 dpif_linux_open,
1064 dpif_linux_close,
1065 dpif_linux_destroy,
1066 dpif_linux_run,
1067 dpif_linux_wait,
1068 dpif_linux_get_stats,
1069 dpif_linux_get_drop_frags,
1070 dpif_linux_set_drop_frags,
1071 dpif_linux_port_add,
1072 dpif_linux_port_del,
1073 dpif_linux_port_query_by_number,
1074 dpif_linux_port_query_by_name,
1075 dpif_linux_get_max_ports,
1076 dpif_linux_port_dump_start,
1077 dpif_linux_port_dump_next,
1078 dpif_linux_port_dump_done,
1079 dpif_linux_port_poll,
1080 dpif_linux_port_poll_wait,
1081 dpif_linux_flow_get,
1082 dpif_linux_flow_put,
1083 dpif_linux_flow_del,
1084 dpif_linux_flow_flush,
1085 dpif_linux_flow_dump_start,
1086 dpif_linux_flow_dump_next,
1087 dpif_linux_flow_dump_done,
1088 dpif_linux_execute,
1089 dpif_linux_recv_get_mask,
1090 dpif_linux_recv_set_mask,
1091 dpif_linux_get_sflow_probability,
1092 dpif_linux_set_sflow_probability,
1093 dpif_linux_queue_to_priority,
1094 dpif_linux_recv,
1095 dpif_linux_recv_wait,
1096 dpif_linux_recv_purge,
1097 };
1098 \f
1099 static int
1100 dpif_linux_init(void)
1101 {
1102 static int error = -1;
1103
1104 if (error < 0) {
1105 unsigned int ovs_vport_mcgroup;
1106
1107 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1108 &ovs_datapath_family);
1109 if (error) {
1110 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1111 "The Open vSwitch kernel module is probably not loaded.",
1112 OVS_DATAPATH_FAMILY);
1113 }
1114 if (!error) {
1115 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1116 }
1117 if (!error) {
1118 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1119 }
1120 if (!error) {
1121 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1122 &ovs_packet_family);
1123 }
1124 if (!error) {
1125 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1126 }
1127 if (!error) {
1128 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1129 &ovs_vport_mcgroup);
1130 }
1131 if (!error) {
1132 static struct dpif_linux_vport vport;
1133 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1134 dpif_linux_nln_parse, &vport);
1135 }
1136 }
1137
1138 return error;
1139 }
1140
1141 bool
1142 dpif_linux_is_internal_device(const char *name)
1143 {
1144 struct dpif_linux_vport reply;
1145 struct ofpbuf *buf;
1146 int error;
1147
1148 error = dpif_linux_vport_get(name, &reply, &buf);
1149 if (!error) {
1150 ofpbuf_delete(buf);
1151 } else if (error != ENODEV && error != ENOENT) {
1152 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1153 name, strerror(error));
1154 }
1155
1156 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1157 }
1158
1159 int
1160 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1161 const void *data, size_t size)
1162 {
1163 struct ofpbuf actions, key, packet;
1164 struct odputil_keybuf keybuf;
1165 struct flow flow;
1166 uint64_t action;
1167
1168 ofpbuf_use_const(&packet, data, size);
1169 flow_extract(&packet, htonll(0), 0, &flow);
1170
1171 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1172 odp_flow_key_from_flow(&key, &flow);
1173
1174 ofpbuf_use_stack(&actions, &action, sizeof action);
1175 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1176
1177 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1178 actions.data, actions.size, &packet);
1179 }
1180
1181 static bool
1182 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1183 {
1184 struct dpif_linux_vport *vport = vport_;
1185 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1186 }
1187
1188 static void
1189 dpif_linux_port_changed(const void *vport_, void *dpif_)
1190 {
1191 const struct dpif_linux_vport *vport = vport_;
1192 struct dpif_linux *dpif = dpif_;
1193
1194 if (vport) {
1195 if (vport->dp_ifindex == dpif->dp_ifindex
1196 && (vport->cmd == OVS_VPORT_CMD_NEW
1197 || vport->cmd == OVS_VPORT_CMD_DEL
1198 || vport->cmd == OVS_VPORT_CMD_SET)) {
1199 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1200 dpif->dpif.full_name, vport->name, vport->cmd);
1201 sset_add(&dpif->changed_ports, vport->name);
1202 }
1203 } else {
1204 dpif->change_error = true;
1205 }
1206 }
1207 \f
1208 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1209 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1210 * positive errno value.
1211 *
1212 * 'vport' will contain pointers into 'buf', so the caller should not free
1213 * 'buf' while 'vport' is still in use. */
1214 static int
1215 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1216 const struct ofpbuf *buf)
1217 {
1218 static const struct nl_policy ovs_vport_policy[] = {
1219 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1220 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1221 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1222 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1223 .min_len = sizeof(struct ovs_vport_stats),
1224 .max_len = sizeof(struct ovs_vport_stats),
1225 .optional = true },
1226 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1227 .min_len = ETH_ADDR_LEN,
1228 .max_len = ETH_ADDR_LEN,
1229 .optional = true },
1230 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1231 [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1232 };
1233
1234 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1235 struct ovs_header *ovs_header;
1236 struct nlmsghdr *nlmsg;
1237 struct genlmsghdr *genl;
1238 struct ofpbuf b;
1239
1240 dpif_linux_vport_init(vport);
1241
1242 ofpbuf_use_const(&b, buf->data, buf->size);
1243 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1244 genl = ofpbuf_try_pull(&b, sizeof *genl);
1245 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1246 if (!nlmsg || !genl || !ovs_header
1247 || nlmsg->nlmsg_type != ovs_vport_family
1248 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1249 ARRAY_SIZE(ovs_vport_policy))) {
1250 return EINVAL;
1251 }
1252
1253 vport->cmd = genl->cmd;
1254 vport->dp_ifindex = ovs_header->dp_ifindex;
1255 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1256 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1257 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1258 if (a[OVS_VPORT_ATTR_STATS]) {
1259 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1260 }
1261 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1262 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1263 }
1264 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1265 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1266 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1267 }
1268 if (a[OVS_VPORT_ATTR_IFINDEX]) {
1269 vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1270 }
1271 return 0;
1272 }
1273
1274 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1275 * followed by Netlink attributes corresponding to 'vport'. */
1276 static void
1277 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1278 struct ofpbuf *buf)
1279 {
1280 struct ovs_header *ovs_header;
1281
1282 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1283 vport->cmd, 1);
1284
1285 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1286 ovs_header->dp_ifindex = vport->dp_ifindex;
1287
1288 if (vport->port_no != UINT32_MAX) {
1289 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1290 }
1291
1292 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1293 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1294 }
1295
1296 if (vport->name) {
1297 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1298 }
1299
1300 if (vport->stats) {
1301 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1302 vport->stats, sizeof *vport->stats);
1303 }
1304
1305 if (vport->address) {
1306 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1307 vport->address, ETH_ADDR_LEN);
1308 }
1309
1310 if (vport->options) {
1311 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1312 vport->options, vport->options_len);
1313 }
1314
1315 if (vport->ifindex) {
1316 nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1317 }
1318 }
1319
1320 /* Clears 'vport' to "empty" values. */
1321 void
1322 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1323 {
1324 memset(vport, 0, sizeof *vport);
1325 vport->port_no = UINT32_MAX;
1326 }
1327
1328 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1329 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1330 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1331 * result of the command is expected to be an ovs_vport also, which is decoded
1332 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1333 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1334 int
1335 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1336 struct dpif_linux_vport *reply,
1337 struct ofpbuf **bufp)
1338 {
1339 struct ofpbuf *request_buf;
1340 int error;
1341
1342 assert((reply != NULL) == (bufp != NULL));
1343
1344 error = dpif_linux_init();
1345 if (error) {
1346 if (reply) {
1347 *bufp = NULL;
1348 dpif_linux_vport_init(reply);
1349 }
1350 return error;
1351 }
1352
1353 request_buf = ofpbuf_new(1024);
1354 dpif_linux_vport_to_ofpbuf(request, request_buf);
1355 error = nl_sock_transact(genl_sock, request_buf, bufp);
1356 ofpbuf_delete(request_buf);
1357
1358 if (reply) {
1359 if (!error) {
1360 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1361 }
1362 if (error) {
1363 dpif_linux_vport_init(reply);
1364 ofpbuf_delete(*bufp);
1365 *bufp = NULL;
1366 }
1367 }
1368 return error;
1369 }
1370
1371 /* Obtains information about the kernel vport named 'name' and stores it into
1372 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1373 * longer needed ('reply' will contain pointers into '*bufp'). */
1374 int
1375 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1376 struct ofpbuf **bufp)
1377 {
1378 struct dpif_linux_vport request;
1379
1380 dpif_linux_vport_init(&request);
1381 request.cmd = OVS_VPORT_CMD_GET;
1382 request.name = name;
1383
1384 return dpif_linux_vport_transact(&request, reply, bufp);
1385 }
1386 \f
1387 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1388 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1389 * positive errno value.
1390 *
1391 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1392 * while 'dp' is still in use. */
1393 static int
1394 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1395 {
1396 static const struct nl_policy ovs_datapath_policy[] = {
1397 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1398 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1399 .min_len = sizeof(struct ovs_dp_stats),
1400 .max_len = sizeof(struct ovs_dp_stats),
1401 .optional = true },
1402 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1403 [OVS_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1404 [OVS_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1405 };
1406
1407 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1408 struct ovs_header *ovs_header;
1409 struct nlmsghdr *nlmsg;
1410 struct genlmsghdr *genl;
1411 struct ofpbuf b;
1412
1413 dpif_linux_dp_init(dp);
1414
1415 ofpbuf_use_const(&b, buf->data, buf->size);
1416 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1417 genl = ofpbuf_try_pull(&b, sizeof *genl);
1418 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1419 if (!nlmsg || !genl || !ovs_header
1420 || nlmsg->nlmsg_type != ovs_datapath_family
1421 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1422 ARRAY_SIZE(ovs_datapath_policy))) {
1423 return EINVAL;
1424 }
1425
1426 dp->cmd = genl->cmd;
1427 dp->dp_ifindex = ovs_header->dp_ifindex;
1428 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1429 if (a[OVS_DP_ATTR_STATS]) {
1430 /* Can't use structure assignment because Netlink doesn't ensure
1431 * sufficient alignment for 64-bit members. */
1432 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1433 sizeof dp->stats);
1434 }
1435 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1436 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1437 }
1438 if (a[OVS_DP_ATTR_SAMPLING]) {
1439 dp->sampling = nl_attr_get(a[OVS_DP_ATTR_SAMPLING]);
1440 }
1441
1442 if (a[OVS_DP_ATTR_MCGROUPS]) {
1443 static const struct nl_policy ovs_mcgroup_policy[] = {
1444 [OVS_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1445 [OVS_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1446 [OVS_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1447 };
1448
1449 struct nlattr *mcgroups[ARRAY_SIZE(ovs_mcgroup_policy)];
1450
1451 if (!nl_parse_nested(a[OVS_DP_ATTR_MCGROUPS], ovs_mcgroup_policy,
1452 mcgroups, ARRAY_SIZE(ovs_mcgroup_policy))) {
1453 return EINVAL;
1454 }
1455
1456 if (mcgroups[OVS_PACKET_CMD_MISS]) {
1457 dp->mcgroups[DPIF_UC_MISS]
1458 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_MISS]);
1459 }
1460 if (mcgroups[OVS_PACKET_CMD_ACTION]) {
1461 dp->mcgroups[DPIF_UC_ACTION]
1462 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_ACTION]);
1463 }
1464 if (mcgroups[OVS_PACKET_CMD_SAMPLE]) {
1465 dp->mcgroups[DPIF_UC_SAMPLE]
1466 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_SAMPLE]);
1467 }
1468 }
1469
1470 return 0;
1471 }
1472
1473 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1474 static void
1475 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1476 {
1477 struct ovs_header *ovs_header;
1478
1479 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1480 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1481
1482 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1483 ovs_header->dp_ifindex = dp->dp_ifindex;
1484
1485 if (dp->name) {
1486 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1487 }
1488
1489 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1490
1491 if (dp->ipv4_frags) {
1492 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1493 }
1494
1495 if (dp->sampling) {
1496 nl_msg_put_u32(buf, OVS_DP_ATTR_SAMPLING, *dp->sampling);
1497 }
1498 }
1499
1500 /* Clears 'dp' to "empty" values. */
1501 static void
1502 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1503 {
1504 memset(dp, 0, sizeof *dp);
1505 }
1506
1507 static void
1508 dpif_linux_dp_dump_start(struct nl_dump *dump)
1509 {
1510 struct dpif_linux_dp request;
1511 struct ofpbuf *buf;
1512
1513 dpif_linux_dp_init(&request);
1514 request.cmd = OVS_DP_CMD_GET;
1515
1516 buf = ofpbuf_new(1024);
1517 dpif_linux_dp_to_ofpbuf(&request, buf);
1518 nl_dump_start(dump, genl_sock, buf);
1519 ofpbuf_delete(buf);
1520 }
1521
1522 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1523 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1524 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1525 * result of the command is expected to be of the same form, which is decoded
1526 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1527 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1528 static int
1529 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1530 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1531 {
1532 struct ofpbuf *request_buf;
1533 int error;
1534
1535 assert((reply != NULL) == (bufp != NULL));
1536
1537 request_buf = ofpbuf_new(1024);
1538 dpif_linux_dp_to_ofpbuf(request, request_buf);
1539 error = nl_sock_transact(genl_sock, request_buf, bufp);
1540 ofpbuf_delete(request_buf);
1541
1542 if (reply) {
1543 if (!error) {
1544 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1545 }
1546 if (error) {
1547 dpif_linux_dp_init(reply);
1548 ofpbuf_delete(*bufp);
1549 *bufp = NULL;
1550 }
1551 }
1552 return error;
1553 }
1554
1555 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1556 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1557 * will contain pointers into '*bufp'). */
1558 static int
1559 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1560 struct ofpbuf **bufp)
1561 {
1562 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1563 struct dpif_linux_dp request;
1564
1565 dpif_linux_dp_init(&request);
1566 request.cmd = OVS_DP_CMD_GET;
1567 request.dp_ifindex = dpif->dp_ifindex;
1568
1569 return dpif_linux_dp_transact(&request, reply, bufp);
1570 }
1571 \f
1572 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1573 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1574 * positive errno value.
1575 *
1576 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1577 * while 'flow' is still in use. */
1578 static int
1579 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1580 const struct ofpbuf *buf)
1581 {
1582 static const struct nl_policy ovs_flow_policy[] = {
1583 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1584 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1585 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1586 .min_len = sizeof(struct ovs_flow_stats),
1587 .max_len = sizeof(struct ovs_flow_stats),
1588 .optional = true },
1589 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1590 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1591 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1592 };
1593
1594 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1595 struct ovs_header *ovs_header;
1596 struct nlmsghdr *nlmsg;
1597 struct genlmsghdr *genl;
1598 struct ofpbuf b;
1599
1600 dpif_linux_flow_init(flow);
1601
1602 ofpbuf_use_const(&b, buf->data, buf->size);
1603 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1604 genl = ofpbuf_try_pull(&b, sizeof *genl);
1605 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1606 if (!nlmsg || !genl || !ovs_header
1607 || nlmsg->nlmsg_type != ovs_flow_family
1608 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1609 ARRAY_SIZE(ovs_flow_policy))) {
1610 return EINVAL;
1611 }
1612
1613 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1614 flow->dp_ifindex = ovs_header->dp_ifindex;
1615 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1616 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1617 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1618 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1619 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1620 }
1621 if (a[OVS_FLOW_ATTR_STATS]) {
1622 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1623 }
1624 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1625 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1626 }
1627 if (a[OVS_FLOW_ATTR_USED]) {
1628 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1629 }
1630 return 0;
1631 }
1632
1633 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1634 * followed by Netlink attributes corresponding to 'flow'. */
1635 static void
1636 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1637 struct ofpbuf *buf)
1638 {
1639 struct ovs_header *ovs_header;
1640
1641 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1642 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1643 flow->cmd, 1);
1644
1645 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1646 ovs_header->dp_ifindex = flow->dp_ifindex;
1647
1648 if (flow->key_len) {
1649 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1650 }
1651
1652 if (flow->actions || flow->actions_len) {
1653 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1654 flow->actions, flow->actions_len);
1655 }
1656
1657 /* We never need to send these to the kernel. */
1658 assert(!flow->stats);
1659 assert(!flow->tcp_flags);
1660 assert(!flow->used);
1661
1662 if (flow->clear) {
1663 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1664 }
1665 }
1666
1667 /* Clears 'flow' to "empty" values. */
1668 static void
1669 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1670 {
1671 memset(flow, 0, sizeof *flow);
1672 }
1673
1674 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1675 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1676 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1677 * result of the command is expected to be a flow also, which is decoded and
1678 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1679 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1680 static int
1681 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1682 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1683 {
1684 struct ofpbuf *request_buf;
1685 int error;
1686
1687 assert((reply != NULL) == (bufp != NULL));
1688
1689 request_buf = ofpbuf_new(1024);
1690 dpif_linux_flow_to_ofpbuf(request, request_buf);
1691 error = nl_sock_transact(genl_sock, request_buf, bufp);
1692 ofpbuf_delete(request_buf);
1693
1694 if (reply) {
1695 if (!error) {
1696 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1697 }
1698 if (error) {
1699 dpif_linux_flow_init(reply);
1700 ofpbuf_delete(*bufp);
1701 *bufp = NULL;
1702 }
1703 }
1704 return error;
1705 }
1706
1707 static void
1708 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1709 struct dpif_flow_stats *stats)
1710 {
1711 if (flow->stats) {
1712 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1713 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1714 } else {
1715 stats->n_packets = 0;
1716 stats->n_bytes = 0;
1717 }
1718 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1719 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1720 }
1721