2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "dpif-linux.h"
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-socket.h"
46 #include "openvswitch/tunnel.h"
48 #include "poll-loop.h"
49 #include "rtnetlink.h"
50 #include "rtnetlink-link.h"
53 #include "unaligned.h"
57 VLOG_DEFINE_THIS_MODULE(dpif_linux
);
59 enum { LRU_MAX_PORTS
= 1024 };
60 enum { LRU_MASK
= LRU_MAX_PORTS
- 1};
61 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS
));
63 struct dpif_linux_dp
{
64 /* Generic Netlink header. */
67 /* struct ovs_header. */
71 const char *name
; /* OVS_DP_ATTR_NAME. */
72 struct ovs_dp_stats stats
; /* OVS_DP_ATTR_STATS. */
73 enum ovs_frag_handling ipv4_frags
; /* OVS_DP_ATTR_IPV4_FRAGS. */
74 const uint32_t *sampling
; /* OVS_DP_ATTR_SAMPLING. */
75 uint32_t mcgroups
[DPIF_N_UC_TYPES
]; /* OVS_DP_ATTR_MCGROUPS. */
78 static void dpif_linux_dp_init(struct dpif_linux_dp
*);
79 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp
*,
80 const struct ofpbuf
*);
81 static void dpif_linux_dp_dump_start(struct nl_dump
*);
82 static int dpif_linux_dp_transact(const struct dpif_linux_dp
*request
,
83 struct dpif_linux_dp
*reply
,
84 struct ofpbuf
**bufp
);
85 static int dpif_linux_dp_get(const struct dpif
*, struct dpif_linux_dp
*reply
,
86 struct ofpbuf
**bufp
);
88 struct dpif_linux_flow
{
89 /* Generic Netlink header. */
92 /* struct ovs_header. */
93 unsigned int nlmsg_flags
;
98 * The 'stats' and 'used' members point to 64-bit data that might only be
99 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
100 * access their values.
102 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
103 * the Netlink version of the command, even if actions_len is zero. */
104 const struct nlattr
*key
; /* OVS_FLOW_ATTR_KEY. */
106 const struct nlattr
*actions
; /* OVS_FLOW_ATTR_ACTIONS. */
108 const struct ovs_flow_stats
*stats
; /* OVS_FLOW_ATTR_STATS. */
109 const uint8_t *tcp_flags
; /* OVS_FLOW_ATTR_TCP_FLAGS. */
110 const uint64_t *used
; /* OVS_FLOW_ATTR_USED. */
111 bool clear
; /* OVS_FLOW_ATTR_CLEAR. */
114 static void dpif_linux_flow_init(struct dpif_linux_flow
*);
115 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow
*,
116 const struct ofpbuf
*);
117 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow
*,
119 static int dpif_linux_flow_transact(const struct dpif_linux_flow
*request
,
120 struct dpif_linux_flow
*reply
,
121 struct ofpbuf
**bufp
);
122 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow
*,
123 struct dpif_flow_stats
*);
125 /* Datapath interface for the openvswitch Linux kernel module. */
130 /* Multicast group messages. */
131 struct nl_sock
*mc_sock
;
132 uint32_t mcgroups
[DPIF_N_UC_TYPES
];
133 unsigned int listen_mask
;
135 /* Change notification. */
136 struct sset changed_ports
; /* Ports that have changed. */
137 struct rtnetlink_notifier port_notifier
;
140 /* Queue of unused ports. */
141 unsigned long *lru_bitmap
;
142 uint16_t lru_ports
[LRU_MAX_PORTS
];
147 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(9999, 5);
149 /* Generic Netlink family numbers for OVS. */
150 static int ovs_datapath_family
;
151 static int ovs_vport_family
;
152 static int ovs_flow_family
;
153 static int ovs_packet_family
;
155 /* Generic Netlink socket. */
156 static struct nl_sock
*genl_sock
;
158 static int dpif_linux_init(void);
159 static int open_dpif(const struct dpif_linux_dp
*, struct dpif
**);
160 static void dpif_linux_port_changed(const struct rtnetlink_link_change
*,
163 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport
*,
165 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport
*,
166 const struct ofpbuf
*);
168 static struct dpif_linux
*
169 dpif_linux_cast(const struct dpif
*dpif
)
171 dpif_assert_class(dpif
, &dpif_linux_class
);
172 return CONTAINER_OF(dpif
, struct dpif_linux
, dpif
);
176 dpif_linux_push_port(struct dpif_linux
*dp
, uint16_t port
)
178 if (port
< LRU_MAX_PORTS
&& !bitmap_is_set(dp
->lru_bitmap
, port
)) {
179 bitmap_set1(dp
->lru_bitmap
, port
);
180 dp
->lru_ports
[dp
->lru_head
++ & LRU_MASK
] = port
;
185 dpif_linux_pop_port(struct dpif_linux
*dp
)
189 if (dp
->lru_head
== dp
->lru_tail
) {
193 port
= dp
->lru_ports
[dp
->lru_tail
++ & LRU_MASK
];
194 bitmap_set0(dp
->lru_bitmap
, port
);
199 dpif_linux_enumerate(struct sset
*all_dps
)
205 error
= dpif_linux_init();
210 dpif_linux_dp_dump_start(&dump
);
211 while (nl_dump_next(&dump
, &msg
)) {
212 struct dpif_linux_dp dp
;
214 if (!dpif_linux_dp_from_ofpbuf(&dp
, &msg
)) {
215 sset_add(all_dps
, dp
.name
);
218 return nl_dump_done(&dump
);
222 dpif_linux_open(const struct dpif_class
*class OVS_UNUSED
, const char *name
,
223 bool create
, struct dpif
**dpifp
)
225 struct dpif_linux_dp dp_request
, dp
;
229 error
= dpif_linux_init();
234 /* Create or look up datapath. */
235 dpif_linux_dp_init(&dp_request
);
236 dp_request
.cmd
= create
? OVS_DP_CMD_NEW
: OVS_DP_CMD_GET
;
237 dp_request
.name
= name
;
238 error
= dpif_linux_dp_transact(&dp_request
, &dp
, &buf
);
242 error
= open_dpif(&dp
, dpifp
);
249 open_dpif(const struct dpif_linux_dp
*dp
, struct dpif
**dpifp
)
251 struct dpif_linux
*dpif
;
255 dpif
= xmalloc(sizeof *dpif
);
256 error
= rtnetlink_link_notifier_register(&dpif
->port_notifier
,
257 dpif_linux_port_changed
, dpif
);
262 dpif_init(&dpif
->dpif
, &dpif_linux_class
, dp
->name
,
263 dp
->dp_ifindex
, dp
->dp_ifindex
);
265 dpif
->mc_sock
= NULL
;
266 for (i
= 0; i
< DPIF_N_UC_TYPES
; i
++) {
267 dpif
->mcgroups
[i
] = dp
->mcgroups
[i
];
269 dpif
->listen_mask
= 0;
270 dpif
->dp_ifindex
= dp
->dp_ifindex
;
271 sset_init(&dpif
->changed_ports
);
272 dpif
->change_error
= false;
273 *dpifp
= &dpif
->dpif
;
275 dpif
->lru_head
= dpif
->lru_tail
= 0;
276 dpif
->lru_bitmap
= bitmap_allocate(LRU_MAX_PORTS
);
277 bitmap_set1(dpif
->lru_bitmap
, OVSP_LOCAL
);
278 for (i
= 1; i
< LRU_MAX_PORTS
; i
++) {
279 dpif_linux_push_port(dpif
, i
);
289 dpif_linux_close(struct dpif
*dpif_
)
291 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
292 nl_sock_destroy(dpif
->mc_sock
);
293 rtnetlink_link_notifier_unregister(&dpif
->port_notifier
);
294 sset_destroy(&dpif
->changed_ports
);
295 free(dpif
->lru_bitmap
);
300 dpif_linux_destroy(struct dpif
*dpif_
)
302 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
303 struct dpif_linux_dp dp
;
305 dpif_linux_dp_init(&dp
);
306 dp
.cmd
= OVS_DP_CMD_DEL
;
307 dp
.dp_ifindex
= dpif
->dp_ifindex
;
308 return dpif_linux_dp_transact(&dp
, NULL
, NULL
);
312 dpif_linux_run(struct dpif
*dpif OVS_UNUSED
)
314 rtnetlink_link_notifier_run();
318 dpif_linux_wait(struct dpif
*dpif OVS_UNUSED
)
320 rtnetlink_link_notifier_wait();
324 dpif_linux_get_stats(const struct dpif
*dpif_
, struct ovs_dp_stats
*stats
)
326 struct dpif_linux_dp dp
;
330 error
= dpif_linux_dp_get(dpif_
, &dp
, &buf
);
339 dpif_linux_get_drop_frags(const struct dpif
*dpif_
, bool *drop_fragsp
)
341 struct dpif_linux_dp dp
;
345 error
= dpif_linux_dp_get(dpif_
, &dp
, &buf
);
347 *drop_fragsp
= dp
.ipv4_frags
== OVS_DP_FRAG_DROP
;
354 dpif_linux_set_drop_frags(struct dpif
*dpif_
, bool drop_frags
)
356 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
357 struct dpif_linux_dp dp
;
359 dpif_linux_dp_init(&dp
);
360 dp
.cmd
= OVS_DP_CMD_SET
;
361 dp
.dp_ifindex
= dpif
->dp_ifindex
;
362 dp
.ipv4_frags
= drop_frags
? OVS_DP_FRAG_DROP
: OVS_DP_FRAG_ZERO
;
363 return dpif_linux_dp_transact(&dp
, NULL
, NULL
);
367 dpif_linux_port_add(struct dpif
*dpif_
, struct netdev
*netdev
,
370 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
371 const char *name
= netdev_get_name(netdev
);
372 const char *type
= netdev_get_type(netdev
);
373 struct dpif_linux_vport request
, reply
;
374 const struct ofpbuf
*options
;
378 dpif_linux_vport_init(&request
);
379 request
.cmd
= OVS_VPORT_CMD_NEW
;
380 request
.dp_ifindex
= dpif
->dp_ifindex
;
381 request
.type
= netdev_vport_get_vport_type(netdev
);
382 if (request
.type
== OVS_VPORT_TYPE_UNSPEC
) {
383 VLOG_WARN_RL(&error_rl
, "%s: cannot create port `%s' because it has "
384 "unsupported type `%s'",
385 dpif_name(dpif_
), name
, type
);
390 options
= netdev_vport_get_options(netdev
);
391 if (options
&& options
->size
) {
392 request
.options
= options
->data
;
393 request
.options_len
= options
->size
;
396 /* Loop until we find a port that isn't used. */
398 request
.port_no
= dpif_linux_pop_port(dpif
);
399 error
= dpif_linux_vport_transact(&request
, &reply
, &buf
);
402 *port_nop
= reply
.port_no
;
405 } while (request
.port_no
!= UINT32_MAX
406 && (error
== EBUSY
|| error
== EFBIG
));
412 dpif_linux_port_del(struct dpif
*dpif_
, uint16_t port_no
)
414 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
415 struct dpif_linux_vport vport
;
418 dpif_linux_vport_init(&vport
);
419 vport
.cmd
= OVS_VPORT_CMD_DEL
;
420 vport
.dp_ifindex
= dpif
->dp_ifindex
;
421 vport
.port_no
= port_no
;
422 error
= dpif_linux_vport_transact(&vport
, NULL
, NULL
);
425 dpif_linux_push_port(dpif
, port_no
);
431 dpif_linux_port_query__(const struct dpif
*dpif
, uint32_t port_no
,
432 const char *port_name
, struct dpif_port
*dpif_port
)
434 struct dpif_linux_vport request
;
435 struct dpif_linux_vport reply
;
439 dpif_linux_vport_init(&request
);
440 request
.cmd
= OVS_VPORT_CMD_GET
;
441 request
.dp_ifindex
= dpif_linux_cast(dpif
)->dp_ifindex
;
442 request
.port_no
= port_no
;
443 request
.name
= port_name
;
445 error
= dpif_linux_vport_transact(&request
, &reply
, &buf
);
447 dpif_port
->name
= xstrdup(reply
.name
);
448 dpif_port
->type
= xstrdup(netdev_vport_get_netdev_type(&reply
));
449 dpif_port
->port_no
= reply
.port_no
;
451 netdev_stats_from_rtnl_link_stats64(&dpif_port
->stats
,
454 memset(&dpif_port
->stats
, 0xff, sizeof dpif_port
->stats
);
462 dpif_linux_port_query_by_number(const struct dpif
*dpif
, uint16_t port_no
,
463 struct dpif_port
*dpif_port
)
465 return dpif_linux_port_query__(dpif
, port_no
, NULL
, dpif_port
);
469 dpif_linux_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
470 struct dpif_port
*dpif_port
)
472 return dpif_linux_port_query__(dpif
, 0, devname
, dpif_port
);
476 dpif_linux_get_max_ports(const struct dpif
*dpif OVS_UNUSED
)
478 /* If the datapath increases its range of supported ports, then it should
479 * start reporting that. */
484 dpif_linux_flow_flush(struct dpif
*dpif_
)
486 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
487 struct dpif_linux_flow flow
;
489 dpif_linux_flow_init(&flow
);
490 flow
.cmd
= OVS_FLOW_CMD_DEL
;
491 flow
.dp_ifindex
= dpif
->dp_ifindex
;
492 return dpif_linux_flow_transact(&flow
, NULL
, NULL
);
495 struct dpif_linux_port_state
{
497 unsigned long *port_bitmap
; /* Ports in the datapath. */
498 bool complete
; /* Dump completed without error. */
502 dpif_linux_port_dump_start(const struct dpif
*dpif_
, void **statep
)
504 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
505 struct dpif_linux_port_state
*state
;
506 struct dpif_linux_vport request
;
509 *statep
= state
= xmalloc(sizeof *state
);
510 state
->port_bitmap
= bitmap_allocate(LRU_MAX_PORTS
);
511 state
->complete
= false;
513 dpif_linux_vport_init(&request
);
514 request
.cmd
= OVS_DP_CMD_GET
;
515 request
.dp_ifindex
= dpif
->dp_ifindex
;
517 buf
= ofpbuf_new(1024);
518 dpif_linux_vport_to_ofpbuf(&request
, buf
);
519 nl_dump_start(&state
->dump
, genl_sock
, buf
);
526 dpif_linux_port_dump_next(const struct dpif
*dpif OVS_UNUSED
, void *state_
,
527 struct dpif_port
*dpif_port
)
529 struct dpif_linux_port_state
*state
= state_
;
530 struct dpif_linux_vport vport
;
534 if (!nl_dump_next(&state
->dump
, &buf
)) {
535 state
->complete
= true;
539 error
= dpif_linux_vport_from_ofpbuf(&vport
, &buf
);
544 if (vport
.port_no
< LRU_MAX_PORTS
) {
545 bitmap_set1(state
->port_bitmap
, vport
.port_no
);
548 dpif_port
->name
= (char *) vport
.name
;
549 dpif_port
->type
= (char *) netdev_vport_get_netdev_type(&vport
);
550 dpif_port
->port_no
= vport
.port_no
;
552 netdev_stats_from_rtnl_link_stats64(&dpif_port
->stats
, vport
.stats
);
554 memset(&dpif_port
->stats
, 0xff, sizeof dpif_port
->stats
);
560 dpif_linux_port_dump_done(const struct dpif
*dpif_
, void *state_
)
562 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
563 struct dpif_linux_port_state
*state
= state_
;
564 int error
= nl_dump_done(&state
->dump
);
566 if (state
->complete
) {
569 for (i
= 0; i
< LRU_MAX_PORTS
; i
++) {
570 if (!bitmap_is_set(state
->port_bitmap
, i
)) {
571 dpif_linux_push_port(dpif
, i
);
576 free(state
->port_bitmap
);
582 dpif_linux_port_poll(const struct dpif
*dpif_
, char **devnamep
)
584 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
586 if (dpif
->change_error
) {
587 dpif
->change_error
= false;
588 sset_clear(&dpif
->changed_ports
);
590 } else if (!sset_is_empty(&dpif
->changed_ports
)) {
591 *devnamep
= sset_pop(&dpif
->changed_ports
);
599 dpif_linux_port_poll_wait(const struct dpif
*dpif_
)
601 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
602 if (!sset_is_empty(&dpif
->changed_ports
) || dpif
->change_error
) {
603 poll_immediate_wake();
608 dpif_linux_flow_get__(const struct dpif
*dpif_
,
609 const struct nlattr
*key
, size_t key_len
,
610 struct dpif_linux_flow
*reply
, struct ofpbuf
**bufp
)
612 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
613 struct dpif_linux_flow request
;
615 dpif_linux_flow_init(&request
);
616 request
.cmd
= OVS_FLOW_CMD_GET
;
617 request
.dp_ifindex
= dpif
->dp_ifindex
;
619 request
.key_len
= key_len
;
620 return dpif_linux_flow_transact(&request
, reply
, bufp
);
624 dpif_linux_flow_get(const struct dpif
*dpif_
,
625 const struct nlattr
*key
, size_t key_len
,
626 struct ofpbuf
**actionsp
, struct dpif_flow_stats
*stats
)
628 struct dpif_linux_flow reply
;
632 error
= dpif_linux_flow_get__(dpif_
, key
, key_len
, &reply
, &buf
);
635 dpif_linux_flow_get_stats(&reply
, stats
);
638 buf
->data
= (void *) reply
.actions
;
639 buf
->size
= reply
.actions_len
;
649 dpif_linux_flow_put(struct dpif
*dpif_
, enum dpif_flow_put_flags flags
,
650 const struct nlattr
*key
, size_t key_len
,
651 const struct nlattr
*actions
, size_t actions_len
,
652 struct dpif_flow_stats
*stats
)
654 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
655 struct dpif_linux_flow request
, reply
;
656 struct nlattr dummy_action
;
660 dpif_linux_flow_init(&request
);
661 request
.cmd
= flags
& DPIF_FP_CREATE
? OVS_FLOW_CMD_NEW
: OVS_FLOW_CMD_SET
;
662 request
.dp_ifindex
= dpif
->dp_ifindex
;
664 request
.key_len
= key_len
;
665 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
666 request
.actions
= actions
? actions
: &dummy_action
;
667 request
.actions_len
= actions_len
;
668 if (flags
& DPIF_FP_ZERO_STATS
) {
669 request
.clear
= true;
671 request
.nlmsg_flags
= flags
& DPIF_FP_MODIFY
? 0 : NLM_F_CREATE
;
672 error
= dpif_linux_flow_transact(&request
,
673 stats
? &reply
: NULL
,
674 stats
? &buf
: NULL
);
675 if (!error
&& stats
) {
676 dpif_linux_flow_get_stats(&reply
, stats
);
683 dpif_linux_flow_del(struct dpif
*dpif_
,
684 const struct nlattr
*key
, size_t key_len
,
685 struct dpif_flow_stats
*stats
)
687 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
688 struct dpif_linux_flow request
, reply
;
692 dpif_linux_flow_init(&request
);
693 request
.cmd
= OVS_FLOW_CMD_DEL
;
694 request
.dp_ifindex
= dpif
->dp_ifindex
;
696 request
.key_len
= key_len
;
697 error
= dpif_linux_flow_transact(&request
,
698 stats
? &reply
: NULL
,
699 stats
? &buf
: NULL
);
700 if (!error
&& stats
) {
701 dpif_linux_flow_get_stats(&reply
, stats
);
707 struct dpif_linux_flow_state
{
709 struct dpif_linux_flow flow
;
710 struct dpif_flow_stats stats
;
715 dpif_linux_flow_dump_start(const struct dpif
*dpif_
, void **statep
)
717 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
718 struct dpif_linux_flow_state
*state
;
719 struct dpif_linux_flow request
;
722 *statep
= state
= xmalloc(sizeof *state
);
724 dpif_linux_flow_init(&request
);
725 request
.cmd
= OVS_DP_CMD_GET
;
726 request
.dp_ifindex
= dpif
->dp_ifindex
;
728 buf
= ofpbuf_new(1024);
729 dpif_linux_flow_to_ofpbuf(&request
, buf
);
730 nl_dump_start(&state
->dump
, genl_sock
, buf
);
739 dpif_linux_flow_dump_next(const struct dpif
*dpif_ OVS_UNUSED
, void *state_
,
740 const struct nlattr
**key
, size_t *key_len
,
741 const struct nlattr
**actions
, size_t *actions_len
,
742 const struct dpif_flow_stats
**stats
)
744 struct dpif_linux_flow_state
*state
= state_
;
749 ofpbuf_delete(state
->buf
);
752 if (!nl_dump_next(&state
->dump
, &buf
)) {
756 error
= dpif_linux_flow_from_ofpbuf(&state
->flow
, &buf
);
761 if (actions
&& !state
->flow
.actions
) {
762 error
= dpif_linux_flow_get__(dpif_
, state
->flow
.key
,
764 &state
->flow
, &state
->buf
);
765 if (error
== ENOENT
) {
766 VLOG_DBG("dumped flow disappeared on get");
768 VLOG_WARN("error fetching dumped flow: %s", strerror(error
));
774 *actions
= state
->flow
.actions
;
775 *actions_len
= state
->flow
.actions_len
;
778 *key
= state
->flow
.key
;
779 *key_len
= state
->flow
.key_len
;
782 dpif_linux_flow_get_stats(&state
->flow
, &state
->stats
);
783 *stats
= &state
->stats
;
789 dpif_linux_flow_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
791 struct dpif_linux_flow_state
*state
= state_
;
792 int error
= nl_dump_done(&state
->dump
);
793 ofpbuf_delete(state
->buf
);
799 dpif_linux_execute__(int dp_ifindex
,
800 const struct nlattr
*key
, size_t key_len
,
801 const struct nlattr
*actions
, size_t actions_len
,
802 const struct ofpbuf
*packet
)
804 struct ovs_header
*execute
;
808 buf
= ofpbuf_new(128 + actions_len
+ packet
->size
);
810 nl_msg_put_genlmsghdr(buf
, 0, ovs_packet_family
, NLM_F_REQUEST
,
811 OVS_PACKET_CMD_EXECUTE
, 1);
813 execute
= ofpbuf_put_uninit(buf
, sizeof *execute
);
814 execute
->dp_ifindex
= dp_ifindex
;
816 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_PACKET
, packet
->data
, packet
->size
);
817 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_KEY
, key
, key_len
);
818 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_ACTIONS
, actions
, actions_len
);
820 error
= nl_sock_transact(genl_sock
, buf
, NULL
);
826 dpif_linux_execute(struct dpif
*dpif_
,
827 const struct nlattr
*key
, size_t key_len
,
828 const struct nlattr
*actions
, size_t actions_len
,
829 const struct ofpbuf
*packet
)
831 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
833 return dpif_linux_execute__(dpif
->dp_ifindex
, key
, key_len
,
834 actions
, actions_len
, packet
);
838 dpif_linux_recv_get_mask(const struct dpif
*dpif_
, int *listen_mask
)
840 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
841 *listen_mask
= dpif
->listen_mask
;
846 dpif_linux_recv_set_mask(struct dpif
*dpif_
, int listen_mask
)
848 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
852 if (listen_mask
== dpif
->listen_mask
) {
854 } else if (!listen_mask
) {
855 nl_sock_destroy(dpif
->mc_sock
);
856 dpif
->mc_sock
= NULL
;
857 dpif
->listen_mask
= 0;
859 } else if (!dpif
->mc_sock
) {
860 error
= nl_sock_create(NETLINK_GENERIC
, &dpif
->mc_sock
);
866 /* Unsubscribe from old groups. */
867 for (i
= 0; i
< DPIF_N_UC_TYPES
; i
++) {
868 if (dpif
->listen_mask
& (1u << i
)) {
869 nl_sock_leave_mcgroup(dpif
->mc_sock
, dpif
->mcgroups
[i
]);
873 /* Update listen_mask. */
874 dpif
->listen_mask
= listen_mask
;
876 /* Subscribe to new groups. */
878 for (i
= 0; i
< DPIF_N_UC_TYPES
; i
++) {
879 if (dpif
->listen_mask
& (1u << i
)) {
882 retval
= nl_sock_join_mcgroup(dpif
->mc_sock
, dpif
->mcgroups
[i
]);
892 dpif_linux_get_sflow_probability(const struct dpif
*dpif_
,
893 uint32_t *probability
)
895 struct dpif_linux_dp dp
;
899 error
= dpif_linux_dp_get(dpif_
, &dp
, &buf
);
901 *probability
= dp
.sampling
? *dp
.sampling
: 0;
908 dpif_linux_set_sflow_probability(struct dpif
*dpif_
, uint32_t probability
)
910 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
911 struct dpif_linux_dp dp
;
913 dpif_linux_dp_init(&dp
);
914 dp
.cmd
= OVS_DP_CMD_SET
;
915 dp
.dp_ifindex
= dpif
->dp_ifindex
;
916 dp
.sampling
= &probability
;
917 return dpif_linux_dp_transact(&dp
, NULL
, NULL
);
921 dpif_linux_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
922 uint32_t queue_id
, uint32_t *priority
)
924 if (queue_id
< 0xf000) {
925 *priority
= TC_H_MAKE(1 << 16, queue_id
+ 1);
933 parse_odp_packet(struct ofpbuf
*buf
, struct dpif_upcall
*upcall
,
936 static const struct nl_policy ovs_packet_policy
[] = {
937 /* Always present. */
938 [OVS_PACKET_ATTR_PACKET
] = { .type
= NL_A_UNSPEC
,
939 .min_len
= ETH_HEADER_LEN
},
940 [OVS_PACKET_ATTR_KEY
] = { .type
= NL_A_NESTED
},
942 /* OVS_PACKET_CMD_ACTION only. */
943 [OVS_PACKET_ATTR_USERDATA
] = { .type
= NL_A_U64
, .optional
= true },
945 /* OVS_PACKET_CMD_SAMPLE only. */
946 [OVS_PACKET_ATTR_SAMPLE_POOL
] = { .type
= NL_A_U32
, .optional
= true },
947 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
950 struct ovs_header
*ovs_header
;
951 struct nlattr
*a
[ARRAY_SIZE(ovs_packet_policy
)];
952 struct nlmsghdr
*nlmsg
;
953 struct genlmsghdr
*genl
;
957 ofpbuf_use_const(&b
, buf
->data
, buf
->size
);
959 nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
960 genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
961 ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
962 if (!nlmsg
|| !genl
|| !ovs_header
963 || nlmsg
->nlmsg_type
!= ovs_packet_family
964 || !nl_policy_parse(&b
, 0, ovs_packet_policy
, a
,
965 ARRAY_SIZE(ovs_packet_policy
))) {
969 type
= (genl
->cmd
== OVS_PACKET_CMD_MISS
? DPIF_UC_MISS
970 : genl
->cmd
== OVS_PACKET_CMD_ACTION
? DPIF_UC_ACTION
971 : genl
->cmd
== OVS_PACKET_CMD_SAMPLE
? DPIF_UC_SAMPLE
977 memset(upcall
, 0, sizeof *upcall
);
979 upcall
->packet
= buf
;
980 upcall
->packet
->data
= (void *) nl_attr_get(a
[OVS_PACKET_ATTR_PACKET
]);
981 upcall
->packet
->size
= nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]);
982 upcall
->key
= (void *) nl_attr_get(a
[OVS_PACKET_ATTR_KEY
]);
983 upcall
->key_len
= nl_attr_get_size(a
[OVS_PACKET_ATTR_KEY
]);
984 upcall
->userdata
= (a
[OVS_PACKET_ATTR_USERDATA
]
985 ? nl_attr_get_u64(a
[OVS_PACKET_ATTR_USERDATA
])
987 upcall
->sample_pool
= (a
[OVS_PACKET_ATTR_SAMPLE_POOL
]
988 ? nl_attr_get_u32(a
[OVS_PACKET_ATTR_SAMPLE_POOL
])
990 if (a
[OVS_PACKET_ATTR_ACTIONS
]) {
991 upcall
->actions
= (void *) nl_attr_get(a
[OVS_PACKET_ATTR_ACTIONS
]);
992 upcall
->actions_len
= nl_attr_get_size(a
[OVS_PACKET_ATTR_ACTIONS
]);
995 *dp_ifindex
= ovs_header
->dp_ifindex
;
1001 dpif_linux_recv(struct dpif
*dpif_
, struct dpif_upcall
*upcall
)
1003 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
1008 if (!dpif
->mc_sock
) {
1012 for (i
= 0; i
< 50; i
++) {
1015 error
= nl_sock_recv(dpif
->mc_sock
, &buf
, false);
1020 error
= parse_odp_packet(buf
, upcall
, &dp_ifindex
);
1022 && dp_ifindex
== dpif
->dp_ifindex
1023 && dpif
->listen_mask
& (1u << upcall
->type
)) {
1037 dpif_linux_recv_wait(struct dpif
*dpif_
)
1039 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
1040 if (dpif
->mc_sock
) {
1041 nl_sock_wait(dpif
->mc_sock
, POLLIN
);
1046 dpif_linux_recv_purge(struct dpif
*dpif_
)
1048 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
1050 if (dpif
->mc_sock
) {
1051 nl_sock_drain(dpif
->mc_sock
);
1055 const struct dpif_class dpif_linux_class
= {
1057 dpif_linux_enumerate
,
1063 dpif_linux_get_stats
,
1064 dpif_linux_get_drop_frags
,
1065 dpif_linux_set_drop_frags
,
1066 dpif_linux_port_add
,
1067 dpif_linux_port_del
,
1068 dpif_linux_port_query_by_number
,
1069 dpif_linux_port_query_by_name
,
1070 dpif_linux_get_max_ports
,
1071 dpif_linux_port_dump_start
,
1072 dpif_linux_port_dump_next
,
1073 dpif_linux_port_dump_done
,
1074 dpif_linux_port_poll
,
1075 dpif_linux_port_poll_wait
,
1076 dpif_linux_flow_get
,
1077 dpif_linux_flow_put
,
1078 dpif_linux_flow_del
,
1079 dpif_linux_flow_flush
,
1080 dpif_linux_flow_dump_start
,
1081 dpif_linux_flow_dump_next
,
1082 dpif_linux_flow_dump_done
,
1084 dpif_linux_recv_get_mask
,
1085 dpif_linux_recv_set_mask
,
1086 dpif_linux_get_sflow_probability
,
1087 dpif_linux_set_sflow_probability
,
1088 dpif_linux_queue_to_priority
,
1090 dpif_linux_recv_wait
,
1091 dpif_linux_recv_purge
,
1095 dpif_linux_init(void)
1097 static int error
= -1;
1100 error
= nl_lookup_genl_family(OVS_DATAPATH_FAMILY
,
1101 &ovs_datapath_family
);
1103 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1104 "The Open vSwitch kernel module is probably not loaded.",
1105 OVS_DATAPATH_FAMILY
);
1108 error
= nl_lookup_genl_family(OVS_VPORT_FAMILY
, &ovs_vport_family
);
1111 error
= nl_lookup_genl_family(OVS_FLOW_FAMILY
, &ovs_flow_family
);
1114 error
= nl_lookup_genl_family(OVS_PACKET_FAMILY
,
1115 &ovs_packet_family
);
1118 error
= nl_sock_create(NETLINK_GENERIC
, &genl_sock
);
1126 dpif_linux_is_internal_device(const char *name
)
1128 struct dpif_linux_vport reply
;
1132 error
= dpif_linux_vport_get(name
, &reply
, &buf
);
1135 } else if (error
!= ENODEV
&& error
!= ENOENT
) {
1136 VLOG_WARN_RL(&error_rl
, "%s: vport query failed (%s)",
1137 name
, strerror(error
));
1140 return reply
.type
== OVS_VPORT_TYPE_INTERNAL
;
1144 dpif_linux_vport_send(int dp_ifindex
, uint32_t port_no
,
1145 const void *data
, size_t size
)
1147 struct ofpbuf actions
, key
, packet
;
1148 struct odputil_keybuf keybuf
;
1152 ofpbuf_use_const(&packet
, data
, size
);
1153 flow_extract(&packet
, htonll(0), 0, &flow
);
1155 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1156 odp_flow_key_from_flow(&key
, &flow
);
1158 ofpbuf_use_stack(&actions
, &action
, sizeof action
);
1159 nl_msg_put_u32(&actions
, OVS_ACTION_ATTR_OUTPUT
, port_no
);
1161 return dpif_linux_execute__(dp_ifindex
, key
.data
, key
.size
,
1162 actions
.data
, actions
.size
, &packet
);
1166 dpif_linux_port_changed(const struct rtnetlink_link_change
*change
,
1169 struct dpif_linux
*dpif
= dpif_
;
1172 if (change
->master_ifindex
== dpif
->dp_ifindex
1173 && (change
->nlmsg_type
== RTM_NEWLINK
1174 || change
->nlmsg_type
== RTM_DELLINK
))
1176 /* Our datapath changed, either adding a new port or deleting an
1178 sset_add(&dpif
->changed_ports
, change
->ifname
);
1181 dpif
->change_error
= true;
1185 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1186 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1187 * positive errno value.
1189 * 'vport' will contain pointers into 'buf', so the caller should not free
1190 * 'buf' while 'vport' is still in use. */
1192 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport
*vport
,
1193 const struct ofpbuf
*buf
)
1195 static const struct nl_policy ovs_vport_policy
[] = {
1196 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NL_A_U32
},
1197 [OVS_VPORT_ATTR_TYPE
] = { .type
= NL_A_U32
},
1198 [OVS_VPORT_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
1199 [OVS_VPORT_ATTR_STATS
] = { .type
= NL_A_UNSPEC
,
1200 .min_len
= sizeof(struct rtnl_link_stats64
),
1201 .max_len
= sizeof(struct rtnl_link_stats64
),
1203 [OVS_VPORT_ATTR_ADDRESS
] = { .type
= NL_A_UNSPEC
,
1204 .min_len
= ETH_ADDR_LEN
,
1205 .max_len
= ETH_ADDR_LEN
,
1207 [OVS_VPORT_ATTR_MTU
] = { .type
= NL_A_U32
, .optional
= true },
1208 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
1209 [OVS_VPORT_ATTR_IFINDEX
] = { .type
= NL_A_U32
, .optional
= true },
1210 [OVS_VPORT_ATTR_IFLINK
] = { .type
= NL_A_U32
, .optional
= true },
1213 struct nlattr
*a
[ARRAY_SIZE(ovs_vport_policy
)];
1214 struct ovs_header
*ovs_header
;
1215 struct nlmsghdr
*nlmsg
;
1216 struct genlmsghdr
*genl
;
1219 dpif_linux_vport_init(vport
);
1221 ofpbuf_use_const(&b
, buf
->data
, buf
->size
);
1222 nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
1223 genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
1224 ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
1225 if (!nlmsg
|| !genl
|| !ovs_header
1226 || nlmsg
->nlmsg_type
!= ovs_vport_family
1227 || !nl_policy_parse(&b
, 0, ovs_vport_policy
, a
,
1228 ARRAY_SIZE(ovs_vport_policy
))) {
1232 vport
->cmd
= genl
->cmd
;
1233 vport
->dp_ifindex
= ovs_header
->dp_ifindex
;
1234 vport
->port_no
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1235 vport
->type
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
1236 vport
->name
= nl_attr_get_string(a
[OVS_VPORT_ATTR_NAME
]);
1237 if (a
[OVS_VPORT_ATTR_STATS
]) {
1238 vport
->stats
= nl_attr_get(a
[OVS_VPORT_ATTR_STATS
]);
1240 if (a
[OVS_VPORT_ATTR_ADDRESS
]) {
1241 vport
->address
= nl_attr_get(a
[OVS_VPORT_ATTR_ADDRESS
]);
1243 if (a
[OVS_VPORT_ATTR_MTU
]) {
1244 vport
->mtu
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_MTU
]);
1246 vport
->mtu
= INT_MAX
;
1248 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
1249 vport
->options
= nl_attr_get(a
[OVS_VPORT_ATTR_OPTIONS
]);
1250 vport
->options_len
= nl_attr_get_size(a
[OVS_VPORT_ATTR_OPTIONS
]);
1252 if (a
[OVS_VPORT_ATTR_IFINDEX
]) {
1253 vport
->ifindex
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_IFINDEX
]);
1255 if (a
[OVS_VPORT_ATTR_IFLINK
]) {
1256 vport
->iflink
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_IFLINK
]);
1261 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1262 * followed by Netlink attributes corresponding to 'vport'. */
1264 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport
*vport
,
1267 struct ovs_header
*ovs_header
;
1269 nl_msg_put_genlmsghdr(buf
, 0, ovs_vport_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
1272 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
1273 ovs_header
->dp_ifindex
= vport
->dp_ifindex
;
1275 if (vport
->port_no
!= UINT32_MAX
) {
1276 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
);
1279 if (vport
->type
!= OVS_VPORT_TYPE_UNSPEC
) {
1280 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_TYPE
, vport
->type
);
1284 nl_msg_put_string(buf
, OVS_VPORT_ATTR_NAME
, vport
->name
);
1288 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_STATS
,
1289 vport
->stats
, sizeof *vport
->stats
);
1292 if (vport
->address
) {
1293 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_ADDRESS
,
1294 vport
->address
, ETH_ADDR_LEN
);
1297 if (vport
->mtu
&& vport
->mtu
!= INT_MAX
) {
1298 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_MTU
, vport
->mtu
);
1301 if (vport
->options
) {
1302 nl_msg_put_nested(buf
, OVS_VPORT_ATTR_OPTIONS
,
1303 vport
->options
, vport
->options_len
);
1306 if (vport
->ifindex
) {
1307 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_IFINDEX
, vport
->ifindex
);
1310 if (vport
->iflink
) {
1311 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_IFLINK
, vport
->iflink
);
1315 /* Clears 'vport' to "empty" values. */
1317 dpif_linux_vport_init(struct dpif_linux_vport
*vport
)
1319 memset(vport
, 0, sizeof *vport
);
1320 vport
->port_no
= UINT32_MAX
;
1323 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1324 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1325 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1326 * result of the command is expected to be an ovs_vport also, which is decoded
1327 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1328 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1330 dpif_linux_vport_transact(const struct dpif_linux_vport
*request
,
1331 struct dpif_linux_vport
*reply
,
1332 struct ofpbuf
**bufp
)
1334 struct ofpbuf
*request_buf
;
1337 assert((reply
!= NULL
) == (bufp
!= NULL
));
1339 error
= dpif_linux_init();
1343 dpif_linux_vport_init(reply
);
1348 request_buf
= ofpbuf_new(1024);
1349 dpif_linux_vport_to_ofpbuf(request
, request_buf
);
1350 error
= nl_sock_transact(genl_sock
, request_buf
, bufp
);
1351 ofpbuf_delete(request_buf
);
1355 error
= dpif_linux_vport_from_ofpbuf(reply
, *bufp
);
1358 dpif_linux_vport_init(reply
);
1359 ofpbuf_delete(*bufp
);
1366 /* Obtains information about the kernel vport named 'name' and stores it into
1367 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1368 * longer needed ('reply' will contain pointers into '*bufp'). */
1370 dpif_linux_vport_get(const char *name
, struct dpif_linux_vport
*reply
,
1371 struct ofpbuf
**bufp
)
1373 struct dpif_linux_vport request
;
1375 dpif_linux_vport_init(&request
);
1376 request
.cmd
= OVS_VPORT_CMD_GET
;
1377 request
.name
= name
;
1379 return dpif_linux_vport_transact(&request
, reply
, bufp
);
1382 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1383 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1384 * positive errno value.
1386 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1387 * while 'dp' is still in use. */
1389 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp
*dp
, const struct ofpbuf
*buf
)
1391 static const struct nl_policy ovs_datapath_policy
[] = {
1392 [OVS_DP_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
1393 [OVS_DP_ATTR_STATS
] = { .type
= NL_A_UNSPEC
,
1394 .min_len
= sizeof(struct ovs_dp_stats
),
1395 .max_len
= sizeof(struct ovs_dp_stats
),
1397 [OVS_DP_ATTR_IPV4_FRAGS
] = { .type
= NL_A_U32
, .optional
= true },
1398 [OVS_DP_ATTR_SAMPLING
] = { .type
= NL_A_U32
, .optional
= true },
1399 [OVS_DP_ATTR_MCGROUPS
] = { .type
= NL_A_NESTED
, .optional
= true },
1402 struct nlattr
*a
[ARRAY_SIZE(ovs_datapath_policy
)];
1403 struct ovs_header
*ovs_header
;
1404 struct nlmsghdr
*nlmsg
;
1405 struct genlmsghdr
*genl
;
1408 dpif_linux_dp_init(dp
);
1410 ofpbuf_use_const(&b
, buf
->data
, buf
->size
);
1411 nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
1412 genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
1413 ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
1414 if (!nlmsg
|| !genl
|| !ovs_header
1415 || nlmsg
->nlmsg_type
!= ovs_datapath_family
1416 || !nl_policy_parse(&b
, 0, ovs_datapath_policy
, a
,
1417 ARRAY_SIZE(ovs_datapath_policy
))) {
1421 dp
->cmd
= genl
->cmd
;
1422 dp
->dp_ifindex
= ovs_header
->dp_ifindex
;
1423 dp
->name
= nl_attr_get_string(a
[OVS_DP_ATTR_NAME
]);
1424 if (a
[OVS_DP_ATTR_STATS
]) {
1425 /* Can't use structure assignment because Netlink doesn't ensure
1426 * sufficient alignment for 64-bit members. */
1427 memcpy(&dp
->stats
, nl_attr_get(a
[OVS_DP_ATTR_STATS
]),
1430 if (a
[OVS_DP_ATTR_IPV4_FRAGS
]) {
1431 dp
->ipv4_frags
= nl_attr_get_u32(a
[OVS_DP_ATTR_IPV4_FRAGS
]);
1433 if (a
[OVS_DP_ATTR_SAMPLING
]) {
1434 dp
->sampling
= nl_attr_get(a
[OVS_DP_ATTR_SAMPLING
]);
1437 if (a
[OVS_DP_ATTR_MCGROUPS
]) {
1438 static const struct nl_policy ovs_mcgroup_policy
[] = {
1439 [OVS_PACKET_CMD_MISS
] = { .type
= NL_A_U32
, .optional
= true },
1440 [OVS_PACKET_CMD_ACTION
] = { .type
= NL_A_U32
, .optional
= true },
1441 [OVS_PACKET_CMD_SAMPLE
] = { .type
= NL_A_U32
, .optional
= true },
1444 struct nlattr
*mcgroups
[ARRAY_SIZE(ovs_mcgroup_policy
)];
1446 if (!nl_parse_nested(a
[OVS_DP_ATTR_MCGROUPS
], ovs_mcgroup_policy
,
1447 mcgroups
, ARRAY_SIZE(ovs_mcgroup_policy
))) {
1451 if (mcgroups
[OVS_PACKET_CMD_MISS
]) {
1452 dp
->mcgroups
[DPIF_UC_MISS
]
1453 = nl_attr_get_u32(mcgroups
[OVS_PACKET_CMD_MISS
]);
1455 if (mcgroups
[OVS_PACKET_CMD_ACTION
]) {
1456 dp
->mcgroups
[DPIF_UC_ACTION
]
1457 = nl_attr_get_u32(mcgroups
[OVS_PACKET_CMD_ACTION
]);
1459 if (mcgroups
[OVS_PACKET_CMD_SAMPLE
]) {
1460 dp
->mcgroups
[DPIF_UC_SAMPLE
]
1461 = nl_attr_get_u32(mcgroups
[OVS_PACKET_CMD_SAMPLE
]);
1468 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1470 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp
*dp
, struct ofpbuf
*buf
)
1472 struct ovs_header
*ovs_header
;
1474 nl_msg_put_genlmsghdr(buf
, 0, ovs_datapath_family
,
1475 NLM_F_REQUEST
| NLM_F_ECHO
, dp
->cmd
, 1);
1477 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
1478 ovs_header
->dp_ifindex
= dp
->dp_ifindex
;
1481 nl_msg_put_string(buf
, OVS_DP_ATTR_NAME
, dp
->name
);
1484 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1486 if (dp
->ipv4_frags
) {
1487 nl_msg_put_u32(buf
, OVS_DP_ATTR_IPV4_FRAGS
, dp
->ipv4_frags
);
1491 nl_msg_put_u32(buf
, OVS_DP_ATTR_SAMPLING
, *dp
->sampling
);
1495 /* Clears 'dp' to "empty" values. */
1497 dpif_linux_dp_init(struct dpif_linux_dp
*dp
)
1499 memset(dp
, 0, sizeof *dp
);
1503 dpif_linux_dp_dump_start(struct nl_dump
*dump
)
1505 struct dpif_linux_dp request
;
1508 dpif_linux_dp_init(&request
);
1509 request
.cmd
= OVS_DP_CMD_GET
;
1511 buf
= ofpbuf_new(1024);
1512 dpif_linux_dp_to_ofpbuf(&request
, buf
);
1513 nl_dump_start(dump
, genl_sock
, buf
);
1517 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1518 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1519 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1520 * result of the command is expected to be of the same form, which is decoded
1521 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1522 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1524 dpif_linux_dp_transact(const struct dpif_linux_dp
*request
,
1525 struct dpif_linux_dp
*reply
, struct ofpbuf
**bufp
)
1527 struct ofpbuf
*request_buf
;
1530 assert((reply
!= NULL
) == (bufp
!= NULL
));
1532 request_buf
= ofpbuf_new(1024);
1533 dpif_linux_dp_to_ofpbuf(request
, request_buf
);
1534 error
= nl_sock_transact(genl_sock
, request_buf
, bufp
);
1535 ofpbuf_delete(request_buf
);
1539 error
= dpif_linux_dp_from_ofpbuf(reply
, *bufp
);
1542 dpif_linux_dp_init(reply
);
1543 ofpbuf_delete(*bufp
);
1550 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1551 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1552 * will contain pointers into '*bufp'). */
1554 dpif_linux_dp_get(const struct dpif
*dpif_
, struct dpif_linux_dp
*reply
,
1555 struct ofpbuf
**bufp
)
1557 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
1558 struct dpif_linux_dp request
;
1560 dpif_linux_dp_init(&request
);
1561 request
.cmd
= OVS_DP_CMD_GET
;
1562 request
.dp_ifindex
= dpif
->dp_ifindex
;
1564 return dpif_linux_dp_transact(&request
, reply
, bufp
);
1567 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1568 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1569 * positive errno value.
1571 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1572 * while 'flow' is still in use. */
1574 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow
*flow
,
1575 const struct ofpbuf
*buf
)
1577 static const struct nl_policy ovs_flow_policy
[] = {
1578 [OVS_FLOW_ATTR_KEY
] = { .type
= NL_A_NESTED
},
1579 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
1580 [OVS_FLOW_ATTR_STATS
] = { .type
= NL_A_UNSPEC
,
1581 .min_len
= sizeof(struct ovs_flow_stats
),
1582 .max_len
= sizeof(struct ovs_flow_stats
),
1584 [OVS_FLOW_ATTR_TCP_FLAGS
] = { .type
= NL_A_U8
, .optional
= true },
1585 [OVS_FLOW_ATTR_USED
] = { .type
= NL_A_U64
, .optional
= true },
1586 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1589 struct nlattr
*a
[ARRAY_SIZE(ovs_flow_policy
)];
1590 struct ovs_header
*ovs_header
;
1591 struct nlmsghdr
*nlmsg
;
1592 struct genlmsghdr
*genl
;
1595 dpif_linux_flow_init(flow
);
1597 ofpbuf_use_const(&b
, buf
->data
, buf
->size
);
1598 nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
1599 genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
1600 ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
1601 if (!nlmsg
|| !genl
|| !ovs_header
1602 || nlmsg
->nlmsg_type
!= ovs_flow_family
1603 || !nl_policy_parse(&b
, 0, ovs_flow_policy
, a
,
1604 ARRAY_SIZE(ovs_flow_policy
))) {
1608 flow
->nlmsg_flags
= nlmsg
->nlmsg_flags
;
1609 flow
->dp_ifindex
= ovs_header
->dp_ifindex
;
1610 flow
->key
= nl_attr_get(a
[OVS_FLOW_ATTR_KEY
]);
1611 flow
->key_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_KEY
]);
1612 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1613 flow
->actions
= nl_attr_get(a
[OVS_FLOW_ATTR_ACTIONS
]);
1614 flow
->actions_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_ACTIONS
]);
1616 if (a
[OVS_FLOW_ATTR_STATS
]) {
1617 flow
->stats
= nl_attr_get(a
[OVS_FLOW_ATTR_STATS
]);
1619 if (a
[OVS_FLOW_ATTR_TCP_FLAGS
]) {
1620 flow
->tcp_flags
= nl_attr_get(a
[OVS_FLOW_ATTR_TCP_FLAGS
]);
1622 if (a
[OVS_FLOW_ATTR_USED
]) {
1623 flow
->used
= nl_attr_get(a
[OVS_FLOW_ATTR_USED
]);
1628 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1629 * followed by Netlink attributes corresponding to 'flow'. */
1631 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow
*flow
,
1634 struct ovs_header
*ovs_header
;
1636 nl_msg_put_genlmsghdr(buf
, 0, ovs_flow_family
,
1637 NLM_F_REQUEST
| NLM_F_ECHO
| flow
->nlmsg_flags
,
1640 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
1641 ovs_header
->dp_ifindex
= flow
->dp_ifindex
;
1643 if (flow
->key_len
) {
1644 nl_msg_put_unspec(buf
, OVS_FLOW_ATTR_KEY
, flow
->key
, flow
->key_len
);
1647 if (flow
->actions
|| flow
->actions_len
) {
1648 nl_msg_put_unspec(buf
, OVS_FLOW_ATTR_ACTIONS
,
1649 flow
->actions
, flow
->actions_len
);
1652 /* We never need to send these to the kernel. */
1653 assert(!flow
->stats
);
1654 assert(!flow
->tcp_flags
);
1655 assert(!flow
->used
);
1658 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_CLEAR
);
1662 /* Clears 'flow' to "empty" values. */
1664 dpif_linux_flow_init(struct dpif_linux_flow
*flow
)
1666 memset(flow
, 0, sizeof *flow
);
1669 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1670 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1671 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1672 * result of the command is expected to be a flow also, which is decoded and
1673 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1674 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1676 dpif_linux_flow_transact(const struct dpif_linux_flow
*request
,
1677 struct dpif_linux_flow
*reply
, struct ofpbuf
**bufp
)
1679 struct ofpbuf
*request_buf
;
1682 assert((reply
!= NULL
) == (bufp
!= NULL
));
1684 request_buf
= ofpbuf_new(1024);
1685 dpif_linux_flow_to_ofpbuf(request
, request_buf
);
1686 error
= nl_sock_transact(genl_sock
, request_buf
, bufp
);
1687 ofpbuf_delete(request_buf
);
1691 error
= dpif_linux_flow_from_ofpbuf(reply
, *bufp
);
1694 dpif_linux_flow_init(reply
);
1695 ofpbuf_delete(*bufp
);
1703 dpif_linux_flow_get_stats(const struct dpif_linux_flow
*flow
,
1704 struct dpif_flow_stats
*stats
)
1707 stats
->n_packets
= get_unaligned_u64(&flow
->stats
->n_packets
);
1708 stats
->n_bytes
= get_unaligned_u64(&flow
->stats
->n_bytes
);
1710 stats
->n_packets
= 0;
1713 stats
->used
= flow
->used
? get_unaligned_u64(flow
->used
) : 0;
1714 stats
->tcp_flags
= flow
->tcp_flags
? *flow
->tcp_flags
: 0;