2 * Copyright (c) 2008-2018 Nicira, Inc.
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-netlink.h"
26 #include <linux/types.h>
27 #include <linux/pkt_sched.h>
31 #include <sys/epoll.h>
36 #include "dpif-netlink-rtnl.h"
37 #include "dpif-provider.h"
38 #include "fat-rwlock.h"
40 #include "netdev-linux.h"
41 #include "netdev-offload.h"
42 #include "netdev-provider.h"
43 #include "netdev-vport.h"
45 #include "netlink-conntrack.h"
46 #include "netlink-notifier.h"
47 #include "netlink-socket.h"
51 #include "openvswitch/dynamic-string.h"
52 #include "openvswitch/flow.h"
53 #include "openvswitch/hmap.h"
54 #include "openvswitch/match.h"
55 #include "openvswitch/ofpbuf.h"
56 #include "openvswitch/poll-loop.h"
57 #include "openvswitch/shash.h"
58 #include "openvswitch/thread.h"
59 #include "openvswitch/vlog.h"
64 #include "unaligned.h"
67 VLOG_DEFINE_THIS_MODULE(dpif_netlink
);
74 enum { MAX_PORTS
= USHRT_MAX
};
76 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
77 * missing if we have old headers. */
78 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
80 #define FLOW_DUMP_MAX_BATCH 50
81 #define OPERATE_MAX_OPS 50
83 #ifndef EPOLLEXCLUSIVE
84 #define EPOLLEXCLUSIVE (1u << 28)
87 struct dpif_netlink_dp
{
88 /* Generic Netlink header. */
91 /* struct ovs_header. */
95 const char *name
; /* OVS_DP_ATTR_NAME. */
96 const uint32_t *upcall_pid
; /* OVS_DP_ATTR_UPCALL_PID. */
97 uint32_t user_features
; /* OVS_DP_ATTR_USER_FEATURES */
98 const struct ovs_dp_stats
*stats
; /* OVS_DP_ATTR_STATS. */
99 const struct ovs_dp_megaflow_stats
*megaflow_stats
;
100 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
103 static void dpif_netlink_dp_init(struct dpif_netlink_dp
*);
104 static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp
*,
105 const struct ofpbuf
*);
106 static void dpif_netlink_dp_dump_start(struct nl_dump
*);
107 static int dpif_netlink_dp_transact(const struct dpif_netlink_dp
*request
,
108 struct dpif_netlink_dp
*reply
,
109 struct ofpbuf
**bufp
);
110 static int dpif_netlink_dp_get(const struct dpif
*,
111 struct dpif_netlink_dp
*reply
,
112 struct ofpbuf
**bufp
);
114 struct dpif_netlink_flow
{
115 /* Generic Netlink header. */
118 /* struct ovs_header. */
119 unsigned int nlmsg_flags
;
124 * The 'stats' member points to 64-bit data that might only be aligned on
125 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
128 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
129 * the Netlink version of the command, even if actions_len is zero. */
130 const struct nlattr
*key
; /* OVS_FLOW_ATTR_KEY. */
132 const struct nlattr
*mask
; /* OVS_FLOW_ATTR_MASK. */
134 const struct nlattr
*actions
; /* OVS_FLOW_ATTR_ACTIONS. */
136 ovs_u128 ufid
; /* OVS_FLOW_ATTR_FLOW_ID. */
137 bool ufid_present
; /* Is there a UFID? */
138 bool ufid_terse
; /* Skip serializing key/mask/acts? */
139 const struct ovs_flow_stats
*stats
; /* OVS_FLOW_ATTR_STATS. */
140 const uint8_t *tcp_flags
; /* OVS_FLOW_ATTR_TCP_FLAGS. */
141 const ovs_32aligned_u64
*used
; /* OVS_FLOW_ATTR_USED. */
142 bool clear
; /* OVS_FLOW_ATTR_CLEAR. */
143 bool probe
; /* OVS_FLOW_ATTR_PROBE. */
146 static void dpif_netlink_flow_init(struct dpif_netlink_flow
*);
147 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow
*,
148 const struct ofpbuf
*);
149 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow
*,
151 static int dpif_netlink_flow_transact(struct dpif_netlink_flow
*request
,
152 struct dpif_netlink_flow
*reply
,
153 struct ofpbuf
**bufp
);
154 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow
*,
155 struct dpif_flow_stats
*);
156 static void dpif_netlink_flow_to_dpif_flow(struct dpif
*, struct dpif_flow
*,
157 const struct dpif_netlink_flow
*);
159 /* One of the dpif channels between the kernel and userspace. */
160 struct dpif_channel
{
161 struct nl_sock
*sock
; /* Netlink socket. */
162 long long int last_poll
; /* Last time this channel was polled. */
166 #define VPORT_SOCK_POOL_SIZE 1
167 /* On Windows, there is no native support for epoll. There are equivalent
168 * interfaces though, that are not used currently. For simpicity, a pool of
169 * netlink sockets is used. Each socket is represented by 'struct
170 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
171 * sharing the same socket. In the future, we can add a reference count and
173 struct dpif_windows_vport_sock
{
174 struct nl_sock
*nl_sock
; /* netlink socket. */
178 struct dpif_handler
{
179 struct epoll_event
*epoll_events
;
180 int epoll_fd
; /* epoll fd that includes channel socks. */
181 int n_events
; /* Num events returned by epoll_wait(). */
182 int event_offset
; /* Offset into 'epoll_events'. */
185 /* Pool of sockets. */
186 struct dpif_windows_vport_sock
*vport_sock_pool
;
187 size_t last_used_pool_idx
; /* Index to aid in allocating a
188 socket in the pool to a port. */
192 /* Datapath interface for the openvswitch Linux kernel module. */
193 struct dpif_netlink
{
197 /* Upcall messages. */
198 struct fat_rwlock upcall_lock
;
199 struct dpif_handler
*handlers
;
200 uint32_t n_handlers
; /* Num of upcall handlers. */
201 struct dpif_channel
*channels
; /* Array of channels for each port. */
202 int uc_array_size
; /* Size of 'handler->channels' and */
203 /* 'handler->epoll_events'. */
205 /* Change notification. */
206 struct nl_sock
*port_notifier
; /* vport multicast group subscriber. */
207 bool refresh_channels
;
210 static void report_loss(struct dpif_netlink
*, struct dpif_channel
*,
211 uint32_t ch_idx
, uint32_t handler_id
);
213 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(9999, 5);
215 /* Generic Netlink family numbers for OVS.
217 * Initialized by dpif_netlink_init(). */
218 static int ovs_datapath_family
;
219 static int ovs_vport_family
;
220 static int ovs_flow_family
;
221 static int ovs_packet_family
;
222 static int ovs_meter_family
;
223 static int ovs_ct_limit_family
;
225 /* Generic Netlink multicast groups for OVS.
227 * Initialized by dpif_netlink_init(). */
228 static unsigned int ovs_vport_mcgroup
;
230 /* If true, tunnel devices are created using OVS compat/genetlink.
231 * If false, tunnel devices are created with rtnetlink and using light weight
232 * tunnels. If we fail to create the tunnel the rtnetlink+LWT, then we fallback
233 * to using the compat interface. */
234 static bool ovs_tunnels_out_of_tree
= true;
236 static int dpif_netlink_init(void);
237 static int open_dpif(const struct dpif_netlink_dp
*, struct dpif
**);
238 static uint32_t dpif_netlink_port_get_pid(const struct dpif
*,
240 static void dpif_netlink_handler_uninit(struct dpif_handler
*handler
);
241 static int dpif_netlink_refresh_channels(struct dpif_netlink
*,
242 uint32_t n_handlers
);
243 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport
*,
245 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport
*,
246 const struct ofpbuf
*);
247 static int dpif_netlink_port_query__(const struct dpif_netlink
*dpif
,
248 odp_port_t port_no
, const char *port_name
,
249 struct dpif_port
*dpif_port
);
252 create_nl_sock(struct dpif_netlink
*dpif OVS_UNUSED
, struct nl_sock
**sockp
)
253 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
256 return nl_sock_create(NETLINK_GENERIC
, sockp
);
258 /* Pick netlink sockets to use in a round-robin fashion from each
259 * handler's pool of sockets. */
260 struct dpif_handler
*handler
= &dpif
->handlers
[0];
261 struct dpif_windows_vport_sock
*sock_pool
= handler
->vport_sock_pool
;
262 size_t index
= handler
->last_used_pool_idx
;
264 /* A pool of sockets is allocated when the handler is initialized. */
265 if (sock_pool
== NULL
) {
270 ovs_assert(index
< VPORT_SOCK_POOL_SIZE
);
271 *sockp
= sock_pool
[index
].nl_sock
;
273 index
= (index
== VPORT_SOCK_POOL_SIZE
- 1) ? 0 : index
+ 1;
274 handler
->last_used_pool_idx
= index
;
280 close_nl_sock(struct nl_sock
*sock
)
283 nl_sock_destroy(sock
);
287 static struct dpif_netlink
*
288 dpif_netlink_cast(const struct dpif
*dpif
)
290 dpif_assert_class(dpif
, &dpif_netlink_class
);
291 return CONTAINER_OF(dpif
, struct dpif_netlink
, dpif
);
295 dpif_netlink_enumerate(struct sset
*all_dps
,
296 const struct dpif_class
*dpif_class OVS_UNUSED
)
299 uint64_t reply_stub
[NL_DUMP_BUFSIZE
/ 8];
300 struct ofpbuf msg
, buf
;
303 error
= dpif_netlink_init();
308 ofpbuf_use_stub(&buf
, reply_stub
, sizeof reply_stub
);
309 dpif_netlink_dp_dump_start(&dump
);
310 while (nl_dump_next(&dump
, &msg
, &buf
)) {
311 struct dpif_netlink_dp dp
;
313 if (!dpif_netlink_dp_from_ofpbuf(&dp
, &msg
)) {
314 sset_add(all_dps
, dp
.name
);
318 return nl_dump_done(&dump
);
322 dpif_netlink_open(const struct dpif_class
*class OVS_UNUSED
, const char *name
,
323 bool create
, struct dpif
**dpifp
)
325 struct dpif_netlink_dp dp_request
, dp
;
330 error
= dpif_netlink_init();
335 /* Create or look up datapath. */
336 dpif_netlink_dp_init(&dp_request
);
338 dp_request
.cmd
= OVS_DP_CMD_NEW
;
340 dp_request
.upcall_pid
= &upcall_pid
;
342 /* Use OVS_DP_CMD_SET to report user features */
343 dp_request
.cmd
= OVS_DP_CMD_SET
;
345 dp_request
.name
= name
;
346 dp_request
.user_features
|= OVS_DP_F_UNALIGNED
;
347 dp_request
.user_features
|= OVS_DP_F_VPORT_PIDS
;
348 error
= dpif_netlink_dp_transact(&dp_request
, &dp
, &buf
);
353 error
= open_dpif(&dp
, dpifp
);
359 open_dpif(const struct dpif_netlink_dp
*dp
, struct dpif
**dpifp
)
361 struct dpif_netlink
*dpif
;
363 dpif
= xzalloc(sizeof *dpif
);
364 dpif
->port_notifier
= NULL
;
365 fat_rwlock_init(&dpif
->upcall_lock
);
367 dpif_init(&dpif
->dpif
, &dpif_netlink_class
, dp
->name
,
368 dp
->dp_ifindex
, dp
->dp_ifindex
);
370 dpif
->dp_ifindex
= dp
->dp_ifindex
;
371 *dpifp
= &dpif
->dpif
;
378 vport_delete_sock_pool(struct dpif_handler
*handler
)
379 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
381 if (handler
->vport_sock_pool
) {
383 struct dpif_windows_vport_sock
*sock_pool
=
384 handler
->vport_sock_pool
;
386 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
387 if (sock_pool
[i
].nl_sock
) {
388 nl_sock_unsubscribe_packets(sock_pool
[i
].nl_sock
);
389 nl_sock_destroy(sock_pool
[i
].nl_sock
);
390 sock_pool
[i
].nl_sock
= NULL
;
394 free(handler
->vport_sock_pool
);
395 handler
->vport_sock_pool
= NULL
;
400 vport_create_sock_pool(struct dpif_handler
*handler
)
401 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
403 struct dpif_windows_vport_sock
*sock_pool
;
407 sock_pool
= xzalloc(VPORT_SOCK_POOL_SIZE
* sizeof *sock_pool
);
408 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
409 error
= nl_sock_create(NETLINK_GENERIC
, &sock_pool
[i
].nl_sock
);
414 /* Enable the netlink socket to receive packets. This is equivalent to
415 * calling nl_sock_join_mcgroup() to receive events. */
416 error
= nl_sock_subscribe_packets(sock_pool
[i
].nl_sock
);
422 handler
->vport_sock_pool
= sock_pool
;
423 handler
->last_used_pool_idx
= 0;
427 vport_delete_sock_pool(handler
);
432 /* Given the port number 'port_idx', extracts the pid of netlink socket
433 * associated to the port and assigns it to 'upcall_pid'. */
435 vport_get_pid(struct dpif_netlink
*dpif
, uint32_t port_idx
,
436 uint32_t *upcall_pid
)
438 /* Since the nl_sock can only be assigned in either all
439 * or none "dpif" channels, the following check
441 if (!dpif
->channels
[port_idx
].sock
) {
444 ovs_assert(!WINDOWS
|| dpif
->n_handlers
<= 1);
446 *upcall_pid
= nl_sock_pid(dpif
->channels
[port_idx
].sock
);
452 vport_add_channel(struct dpif_netlink
*dpif
, odp_port_t port_no
,
453 struct nl_sock
*sock
)
455 struct epoll_event event
;
456 uint32_t port_idx
= odp_to_u32(port_no
);
460 if (dpif
->handlers
== NULL
) {
465 /* We assume that the datapath densely chooses port numbers, which can
466 * therefore be used as an index into 'channels' and 'epoll_events' of
468 if (port_idx
>= dpif
->uc_array_size
) {
469 uint32_t new_size
= port_idx
+ 1;
471 if (new_size
> MAX_PORTS
) {
472 VLOG_WARN_RL(&error_rl
, "%s: datapath port %"PRIu32
" too big",
473 dpif_name(&dpif
->dpif
), port_no
);
477 dpif
->channels
= xrealloc(dpif
->channels
,
478 new_size
* sizeof *dpif
->channels
);
480 for (i
= dpif
->uc_array_size
; i
< new_size
; i
++) {
481 dpif
->channels
[i
].sock
= NULL
;
484 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
485 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
487 handler
->epoll_events
= xrealloc(handler
->epoll_events
,
488 new_size
* sizeof *handler
->epoll_events
);
491 dpif
->uc_array_size
= new_size
;
494 memset(&event
, 0, sizeof event
);
495 event
.events
= EPOLLIN
| EPOLLEXCLUSIVE
;
496 event
.data
.u32
= port_idx
;
498 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
499 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
502 if (epoll_ctl(handler
->epoll_fd
, EPOLL_CTL_ADD
, nl_sock_fd(sock
),
509 dpif
->channels
[port_idx
].sock
= sock
;
510 dpif
->channels
[port_idx
].last_poll
= LLONG_MIN
;
517 epoll_ctl(dpif
->handlers
[i
].epoll_fd
, EPOLL_CTL_DEL
,
518 nl_sock_fd(sock
), NULL
);
521 dpif
->channels
[port_idx
].sock
= NULL
;
527 vport_del_channels(struct dpif_netlink
*dpif
, odp_port_t port_no
)
529 uint32_t port_idx
= odp_to_u32(port_no
);
532 if (!dpif
->handlers
|| port_idx
>= dpif
->uc_array_size
533 || !dpif
->channels
[port_idx
].sock
) {
537 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
538 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
540 epoll_ctl(handler
->epoll_fd
, EPOLL_CTL_DEL
,
541 nl_sock_fd(dpif
->channels
[port_idx
].sock
), NULL
);
543 handler
->event_offset
= handler
->n_events
= 0;
546 nl_sock_destroy(dpif
->channels
[port_idx
].sock
);
548 dpif
->channels
[port_idx
].sock
= NULL
;
552 destroy_all_channels(struct dpif_netlink
*dpif
)
553 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
557 if (!dpif
->handlers
) {
561 for (i
= 0; i
< dpif
->uc_array_size
; i
++ ) {
562 struct dpif_netlink_vport vport_request
;
563 uint32_t upcall_pids
= 0;
565 if (!dpif
->channels
[i
].sock
) {
569 /* Turn off upcalls. */
570 dpif_netlink_vport_init(&vport_request
);
571 vport_request
.cmd
= OVS_VPORT_CMD_SET
;
572 vport_request
.dp_ifindex
= dpif
->dp_ifindex
;
573 vport_request
.port_no
= u32_to_odp(i
);
574 vport_request
.n_upcall_pids
= 1;
575 vport_request
.upcall_pids
= &upcall_pids
;
576 dpif_netlink_vport_transact(&vport_request
, NULL
, NULL
);
578 vport_del_channels(dpif
, u32_to_odp(i
));
581 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
582 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
584 dpif_netlink_handler_uninit(handler
);
585 free(handler
->epoll_events
);
587 free(dpif
->channels
);
588 free(dpif
->handlers
);
589 dpif
->handlers
= NULL
;
590 dpif
->channels
= NULL
;
591 dpif
->n_handlers
= 0;
592 dpif
->uc_array_size
= 0;
596 dpif_netlink_close(struct dpif
*dpif_
)
598 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
600 nl_sock_destroy(dpif
->port_notifier
);
602 fat_rwlock_wrlock(&dpif
->upcall_lock
);
603 destroy_all_channels(dpif
);
604 fat_rwlock_unlock(&dpif
->upcall_lock
);
606 fat_rwlock_destroy(&dpif
->upcall_lock
);
611 dpif_netlink_destroy(struct dpif
*dpif_
)
613 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
614 struct dpif_netlink_dp dp
;
616 dpif_netlink_dp_init(&dp
);
617 dp
.cmd
= OVS_DP_CMD_DEL
;
618 dp
.dp_ifindex
= dpif
->dp_ifindex
;
619 return dpif_netlink_dp_transact(&dp
, NULL
, NULL
);
623 dpif_netlink_run(struct dpif
*dpif_
)
625 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
627 if (dpif
->refresh_channels
) {
628 dpif
->refresh_channels
= false;
629 fat_rwlock_wrlock(&dpif
->upcall_lock
);
630 dpif_netlink_refresh_channels(dpif
, dpif
->n_handlers
);
631 fat_rwlock_unlock(&dpif
->upcall_lock
);
637 dpif_netlink_get_stats(const struct dpif
*dpif_
, struct dpif_dp_stats
*stats
)
639 struct dpif_netlink_dp dp
;
643 error
= dpif_netlink_dp_get(dpif_
, &dp
, &buf
);
645 memset(stats
, 0, sizeof *stats
);
648 stats
->n_hit
= get_32aligned_u64(&dp
.stats
->n_hit
);
649 stats
->n_missed
= get_32aligned_u64(&dp
.stats
->n_missed
);
650 stats
->n_lost
= get_32aligned_u64(&dp
.stats
->n_lost
);
651 stats
->n_flows
= get_32aligned_u64(&dp
.stats
->n_flows
);
654 if (dp
.megaflow_stats
) {
655 stats
->n_masks
= dp
.megaflow_stats
->n_masks
;
656 stats
->n_mask_hit
= get_32aligned_u64(
657 &dp
.megaflow_stats
->n_mask_hit
);
659 stats
->n_masks
= UINT32_MAX
;
660 stats
->n_mask_hit
= UINT64_MAX
;
668 get_vport_type(const struct dpif_netlink_vport
*vport
)
670 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
672 switch (vport
->type
) {
673 case OVS_VPORT_TYPE_NETDEV
: {
674 const char *type
= netdev_get_type_from_name(vport
->name
);
676 return type
? type
: "system";
679 case OVS_VPORT_TYPE_INTERNAL
:
682 case OVS_VPORT_TYPE_GENEVE
:
685 case OVS_VPORT_TYPE_GRE
:
688 case OVS_VPORT_TYPE_VXLAN
:
691 case OVS_VPORT_TYPE_LISP
:
694 case OVS_VPORT_TYPE_STT
:
697 case OVS_VPORT_TYPE_ERSPAN
:
700 case OVS_VPORT_TYPE_IP6ERSPAN
:
703 case OVS_VPORT_TYPE_IP6GRE
:
706 case OVS_VPORT_TYPE_UNSPEC
:
707 case __OVS_VPORT_TYPE_MAX
:
711 VLOG_WARN_RL(&rl
, "dp%d: port `%s' has unsupported type %u",
712 vport
->dp_ifindex
, vport
->name
, (unsigned int) vport
->type
);
717 netdev_to_ovs_vport_type(const char *type
)
719 if (!strcmp(type
, "tap") || !strcmp(type
, "system")) {
720 return OVS_VPORT_TYPE_NETDEV
;
721 } else if (!strcmp(type
, "internal")) {
722 return OVS_VPORT_TYPE_INTERNAL
;
723 } else if (strstr(type
, "stt")) {
724 return OVS_VPORT_TYPE_STT
;
725 } else if (!strcmp(type
, "geneve")) {
726 return OVS_VPORT_TYPE_GENEVE
;
727 } else if (!strcmp(type
, "vxlan")) {
728 return OVS_VPORT_TYPE_VXLAN
;
729 } else if (!strcmp(type
, "lisp")) {
730 return OVS_VPORT_TYPE_LISP
;
731 } else if (!strcmp(type
, "erspan")) {
732 return OVS_VPORT_TYPE_ERSPAN
;
733 } else if (!strcmp(type
, "ip6erspan")) {
734 return OVS_VPORT_TYPE_IP6ERSPAN
;
735 } else if (!strcmp(type
, "ip6gre")) {
736 return OVS_VPORT_TYPE_IP6GRE
;
737 } else if (!strcmp(type
, "gre")) {
738 return OVS_VPORT_TYPE_GRE
;
740 return OVS_VPORT_TYPE_UNSPEC
;
745 dpif_netlink_port_add__(struct dpif_netlink
*dpif
, const char *name
,
746 enum ovs_vport_type type
,
747 struct ofpbuf
*options
,
748 odp_port_t
*port_nop
)
749 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
751 struct dpif_netlink_vport request
, reply
;
753 struct nl_sock
*sock
= NULL
;
754 uint32_t upcall_pids
= 0;
757 if (dpif
->handlers
) {
758 error
= create_nl_sock(dpif
, &sock
);
764 dpif_netlink_vport_init(&request
);
765 request
.cmd
= OVS_VPORT_CMD_NEW
;
766 request
.dp_ifindex
= dpif
->dp_ifindex
;
770 request
.port_no
= *port_nop
;
772 upcall_pids
= nl_sock_pid(sock
);
774 request
.n_upcall_pids
= 1;
775 request
.upcall_pids
= &upcall_pids
;
778 request
.options
= options
->data
;
779 request
.options_len
= options
->size
;
782 error
= dpif_netlink_vport_transact(&request
, &reply
, &buf
);
784 *port_nop
= reply
.port_no
;
786 if (error
== EBUSY
&& *port_nop
!= ODPP_NONE
) {
787 VLOG_INFO("%s: requested port %"PRIu32
" is in use",
788 dpif_name(&dpif
->dpif
), *port_nop
);
795 error
= vport_add_channel(dpif
, *port_nop
, sock
);
797 VLOG_INFO("%s: could not add channel for port %s",
798 dpif_name(&dpif
->dpif
), name
);
800 /* Delete the port. */
801 dpif_netlink_vport_init(&request
);
802 request
.cmd
= OVS_VPORT_CMD_DEL
;
803 request
.dp_ifindex
= dpif
->dp_ifindex
;
804 request
.port_no
= *port_nop
;
805 dpif_netlink_vport_transact(&request
, NULL
, NULL
);
817 dpif_netlink_port_add_compat(struct dpif_netlink
*dpif
, struct netdev
*netdev
,
818 odp_port_t
*port_nop
)
819 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
821 const struct netdev_tunnel_config
*tnl_cfg
;
822 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
823 const char *type
= netdev_get_type(netdev
);
824 uint64_t options_stub
[64 / 8];
825 enum ovs_vport_type ovs_type
;
826 struct ofpbuf options
;
829 name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
831 ovs_type
= netdev_to_ovs_vport_type(netdev_get_type(netdev
));
832 if (ovs_type
== OVS_VPORT_TYPE_UNSPEC
) {
833 VLOG_WARN_RL(&error_rl
, "%s: cannot create port `%s' because it has "
834 "unsupported type `%s'",
835 dpif_name(&dpif
->dpif
), name
, type
);
839 if (ovs_type
== OVS_VPORT_TYPE_NETDEV
) {
841 /* XXX : Map appropiate Windows handle */
843 netdev_linux_ethtool_set_flag(netdev
, ETH_FLAG_LRO
, "LRO", false);
848 if (ovs_type
== OVS_VPORT_TYPE_INTERNAL
) {
849 if (!create_wmi_port(name
)){
850 VLOG_ERR("Could not create wmi internal port with name:%s", name
);
856 tnl_cfg
= netdev_get_tunnel_config(netdev
);
857 if (tnl_cfg
&& (tnl_cfg
->dst_port
!= 0 || tnl_cfg
->exts
)) {
858 ofpbuf_use_stack(&options
, options_stub
, sizeof options_stub
);
859 if (tnl_cfg
->dst_port
) {
860 nl_msg_put_u16(&options
, OVS_TUNNEL_ATTR_DST_PORT
,
861 ntohs(tnl_cfg
->dst_port
));
867 ext_ofs
= nl_msg_start_nested(&options
, OVS_TUNNEL_ATTR_EXTENSION
);
868 for (i
= 0; i
< 32; i
++) {
869 if (tnl_cfg
->exts
& (1 << i
)) {
870 nl_msg_put_flag(&options
, i
);
873 nl_msg_end_nested(&options
, ext_ofs
);
875 return dpif_netlink_port_add__(dpif
, name
, ovs_type
, &options
,
878 return dpif_netlink_port_add__(dpif
, name
, ovs_type
, NULL
, port_nop
);
884 dpif_netlink_rtnl_port_create_and_add(struct dpif_netlink
*dpif
,
885 struct netdev
*netdev
,
886 odp_port_t
*port_nop
)
887 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
889 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
890 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
894 error
= dpif_netlink_rtnl_port_create(netdev
);
896 if (error
!= EOPNOTSUPP
) {
897 VLOG_WARN_RL(&rl
, "Failed to create %s with rtnetlink: %s",
898 netdev_get_name(netdev
), ovs_strerror(error
));
903 name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
904 error
= dpif_netlink_port_add__(dpif
, name
, OVS_VPORT_TYPE_NETDEV
, NULL
,
907 dpif_netlink_rtnl_port_destroy(name
, netdev_get_type(netdev
));
913 dpif_netlink_port_add(struct dpif
*dpif_
, struct netdev
*netdev
,
914 odp_port_t
*port_nop
)
916 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
917 int error
= EOPNOTSUPP
;
919 fat_rwlock_wrlock(&dpif
->upcall_lock
);
920 if (!ovs_tunnels_out_of_tree
) {
921 error
= dpif_netlink_rtnl_port_create_and_add(dpif
, netdev
, port_nop
);
924 error
= dpif_netlink_port_add_compat(dpif
, netdev
, port_nop
);
926 fat_rwlock_unlock(&dpif
->upcall_lock
);
932 dpif_netlink_port_del__(struct dpif_netlink
*dpif
, odp_port_t port_no
)
933 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
935 struct dpif_netlink_vport vport
;
936 struct dpif_port dpif_port
;
939 error
= dpif_netlink_port_query__(dpif
, port_no
, NULL
, &dpif_port
);
944 dpif_netlink_vport_init(&vport
);
945 vport
.cmd
= OVS_VPORT_CMD_DEL
;
946 vport
.dp_ifindex
= dpif
->dp_ifindex
;
947 vport
.port_no
= port_no
;
949 if (!strcmp(dpif_port
.type
, "internal")) {
950 if (!delete_wmi_port(dpif_port
.name
)) {
951 VLOG_ERR("Could not delete wmi port with name: %s",
956 error
= dpif_netlink_vport_transact(&vport
, NULL
, NULL
);
958 vport_del_channels(dpif
, port_no
);
960 if (!error
&& !ovs_tunnels_out_of_tree
) {
961 error
= dpif_netlink_rtnl_port_destroy(dpif_port
.name
, dpif_port
.type
);
962 if (error
== EOPNOTSUPP
) {
967 dpif_port_destroy(&dpif_port
);
973 dpif_netlink_port_del(struct dpif
*dpif_
, odp_port_t port_no
)
975 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
978 fat_rwlock_wrlock(&dpif
->upcall_lock
);
979 error
= dpif_netlink_port_del__(dpif
, port_no
);
980 fat_rwlock_unlock(&dpif
->upcall_lock
);
986 dpif_netlink_port_query__(const struct dpif_netlink
*dpif
, odp_port_t port_no
,
987 const char *port_name
, struct dpif_port
*dpif_port
)
989 struct dpif_netlink_vport request
;
990 struct dpif_netlink_vport reply
;
994 dpif_netlink_vport_init(&request
);
995 request
.cmd
= OVS_VPORT_CMD_GET
;
996 request
.dp_ifindex
= dpif
->dp_ifindex
;
997 request
.port_no
= port_no
;
998 request
.name
= port_name
;
1000 error
= dpif_netlink_vport_transact(&request
, &reply
, &buf
);
1002 if (reply
.dp_ifindex
!= request
.dp_ifindex
) {
1003 /* A query by name reported that 'port_name' is in some datapath
1004 * other than 'dpif', but the caller wants to know about 'dpif'. */
1006 } else if (dpif_port
) {
1007 dpif_port
->name
= xstrdup(reply
.name
);
1008 dpif_port
->type
= xstrdup(get_vport_type(&reply
));
1009 dpif_port
->port_no
= reply
.port_no
;
1017 dpif_netlink_port_query_by_number(const struct dpif
*dpif_
, odp_port_t port_no
,
1018 struct dpif_port
*dpif_port
)
1020 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1022 return dpif_netlink_port_query__(dpif
, port_no
, NULL
, dpif_port
);
1026 dpif_netlink_port_query_by_name(const struct dpif
*dpif_
, const char *devname
,
1027 struct dpif_port
*dpif_port
)
1029 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1031 return dpif_netlink_port_query__(dpif
, 0, devname
, dpif_port
);
1035 dpif_netlink_port_get_pid__(const struct dpif_netlink
*dpif
,
1037 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
1039 uint32_t port_idx
= odp_to_u32(port_no
);
1042 if (dpif
->handlers
&& dpif
->uc_array_size
> 0) {
1043 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1044 * channel, since it is not heavily loaded. */
1045 uint32_t idx
= port_idx
>= dpif
->uc_array_size
? 0 : port_idx
;
1047 /* Needs to check in case the socket pointer is changed in between
1048 * the holding of upcall_lock. A known case happens when the main
1049 * thread deletes the vport while the handler thread is handling
1050 * the upcall from that port. */
1051 if (dpif
->channels
[idx
].sock
) {
1052 pid
= nl_sock_pid(dpif
->channels
[idx
].sock
);
1060 dpif_netlink_port_get_pid(const struct dpif
*dpif_
, odp_port_t port_no
)
1062 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1065 fat_rwlock_rdlock(&dpif
->upcall_lock
);
1066 ret
= dpif_netlink_port_get_pid__(dpif
, port_no
);
1067 fat_rwlock_unlock(&dpif
->upcall_lock
);
1073 dpif_netlink_flow_flush(struct dpif
*dpif_
)
1075 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1076 struct dpif_netlink_flow flow
;
1078 dpif_netlink_flow_init(&flow
);
1079 flow
.cmd
= OVS_FLOW_CMD_DEL
;
1080 flow
.dp_ifindex
= dpif
->dp_ifindex
;
1082 if (netdev_is_flow_api_enabled()) {
1083 netdev_ports_flow_flush(dpif_
->dpif_class
);
1086 return dpif_netlink_flow_transact(&flow
, NULL
, NULL
);
1089 struct dpif_netlink_port_state
{
1090 struct nl_dump dump
;
1095 dpif_netlink_port_dump_start__(const struct dpif_netlink
*dpif
,
1096 struct nl_dump
*dump
)
1098 struct dpif_netlink_vport request
;
1101 dpif_netlink_vport_init(&request
);
1102 request
.cmd
= OVS_VPORT_CMD_GET
;
1103 request
.dp_ifindex
= dpif
->dp_ifindex
;
1105 buf
= ofpbuf_new(1024);
1106 dpif_netlink_vport_to_ofpbuf(&request
, buf
);
1107 nl_dump_start(dump
, NETLINK_GENERIC
, buf
);
1112 dpif_netlink_port_dump_start(const struct dpif
*dpif_
, void **statep
)
1114 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1115 struct dpif_netlink_port_state
*state
;
1117 *statep
= state
= xmalloc(sizeof *state
);
1118 dpif_netlink_port_dump_start__(dpif
, &state
->dump
);
1120 ofpbuf_init(&state
->buf
, NL_DUMP_BUFSIZE
);
1125 dpif_netlink_port_dump_next__(const struct dpif_netlink
*dpif
,
1126 struct nl_dump
*dump
,
1127 struct dpif_netlink_vport
*vport
,
1128 struct ofpbuf
*buffer
)
1133 if (!nl_dump_next(dump
, &buf
, buffer
)) {
1137 error
= dpif_netlink_vport_from_ofpbuf(vport
, &buf
);
1139 VLOG_WARN_RL(&error_rl
, "%s: failed to parse vport record (%s)",
1140 dpif_name(&dpif
->dpif
), ovs_strerror(error
));
1146 dpif_netlink_port_dump_next(const struct dpif
*dpif_
, void *state_
,
1147 struct dpif_port
*dpif_port
)
1149 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1150 struct dpif_netlink_port_state
*state
= state_
;
1151 struct dpif_netlink_vport vport
;
1154 error
= dpif_netlink_port_dump_next__(dpif
, &state
->dump
, &vport
,
1159 dpif_port
->name
= CONST_CAST(char *, vport
.name
);
1160 dpif_port
->type
= CONST_CAST(char *, get_vport_type(&vport
));
1161 dpif_port
->port_no
= vport
.port_no
;
1166 dpif_netlink_port_dump_done(const struct dpif
*dpif_ OVS_UNUSED
, void *state_
)
1168 struct dpif_netlink_port_state
*state
= state_
;
1169 int error
= nl_dump_done(&state
->dump
);
1171 ofpbuf_uninit(&state
->buf
);
1177 dpif_netlink_port_poll(const struct dpif
*dpif_
, char **devnamep
)
1179 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1181 /* Lazily create the Netlink socket to listen for notifications. */
1182 if (!dpif
->port_notifier
) {
1183 struct nl_sock
*sock
;
1186 error
= nl_sock_create(NETLINK_GENERIC
, &sock
);
1191 error
= nl_sock_join_mcgroup(sock
, ovs_vport_mcgroup
);
1193 nl_sock_destroy(sock
);
1196 dpif
->port_notifier
= sock
;
1198 /* We have no idea of the current state so report that everything
1204 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1205 uint64_t buf_stub
[4096 / 8];
1209 ofpbuf_use_stub(&buf
, buf_stub
, sizeof buf_stub
);
1210 error
= nl_sock_recv(dpif
->port_notifier
, &buf
, NULL
, false);
1212 struct dpif_netlink_vport vport
;
1214 error
= dpif_netlink_vport_from_ofpbuf(&vport
, &buf
);
1216 if (vport
.dp_ifindex
== dpif
->dp_ifindex
1217 && (vport
.cmd
== OVS_VPORT_CMD_NEW
1218 || vport
.cmd
== OVS_VPORT_CMD_DEL
1219 || vport
.cmd
== OVS_VPORT_CMD_SET
)) {
1220 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8
,
1221 dpif
->dpif
.full_name
, vport
.name
, vport
.cmd
);
1222 if (vport
.cmd
== OVS_VPORT_CMD_DEL
&& dpif
->handlers
) {
1223 dpif
->refresh_channels
= true;
1225 *devnamep
= xstrdup(vport
.name
);
1226 ofpbuf_uninit(&buf
);
1230 } else if (error
!= EAGAIN
) {
1231 VLOG_WARN_RL(&rl
, "error reading or parsing netlink (%s)",
1232 ovs_strerror(error
));
1233 nl_sock_drain(dpif
->port_notifier
);
1237 ofpbuf_uninit(&buf
);
1245 dpif_netlink_port_poll_wait(const struct dpif
*dpif_
)
1247 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1249 if (dpif
->port_notifier
) {
1250 nl_sock_wait(dpif
->port_notifier
, POLLIN
);
1252 poll_immediate_wake();
1257 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow
*request
,
1258 const ovs_u128
*ufid
, bool terse
)
1261 request
->ufid
= *ufid
;
1262 request
->ufid_present
= true;
1264 request
->ufid_present
= false;
1266 request
->ufid_terse
= terse
;
1270 dpif_netlink_init_flow_get__(const struct dpif_netlink
*dpif
,
1271 const struct nlattr
*key
, size_t key_len
,
1272 const ovs_u128
*ufid
, bool terse
,
1273 struct dpif_netlink_flow
*request
)
1275 dpif_netlink_flow_init(request
);
1276 request
->cmd
= OVS_FLOW_CMD_GET
;
1277 request
->dp_ifindex
= dpif
->dp_ifindex
;
1279 request
->key_len
= key_len
;
1280 dpif_netlink_flow_init_ufid(request
, ufid
, terse
);
1284 dpif_netlink_init_flow_get(const struct dpif_netlink
*dpif
,
1285 const struct dpif_flow_get
*get
,
1286 struct dpif_netlink_flow
*request
)
1288 dpif_netlink_init_flow_get__(dpif
, get
->key
, get
->key_len
, get
->ufid
,
1293 dpif_netlink_flow_get__(const struct dpif_netlink
*dpif
,
1294 const struct nlattr
*key
, size_t key_len
,
1295 const ovs_u128
*ufid
, bool terse
,
1296 struct dpif_netlink_flow
*reply
, struct ofpbuf
**bufp
)
1298 struct dpif_netlink_flow request
;
1300 dpif_netlink_init_flow_get__(dpif
, key
, key_len
, ufid
, terse
, &request
);
1301 return dpif_netlink_flow_transact(&request
, reply
, bufp
);
1305 dpif_netlink_flow_get(const struct dpif_netlink
*dpif
,
1306 const struct dpif_netlink_flow
*flow
,
1307 struct dpif_netlink_flow
*reply
, struct ofpbuf
**bufp
)
1309 return dpif_netlink_flow_get__(dpif
, flow
->key
, flow
->key_len
,
1310 flow
->ufid_present
? &flow
->ufid
: NULL
,
1311 false, reply
, bufp
);
1315 dpif_netlink_init_flow_put(struct dpif_netlink
*dpif
,
1316 const struct dpif_flow_put
*put
,
1317 struct dpif_netlink_flow
*request
)
1319 static const struct nlattr dummy_action
;
1321 dpif_netlink_flow_init(request
);
1322 request
->cmd
= (put
->flags
& DPIF_FP_CREATE
1323 ? OVS_FLOW_CMD_NEW
: OVS_FLOW_CMD_SET
);
1324 request
->dp_ifindex
= dpif
->dp_ifindex
;
1325 request
->key
= put
->key
;
1326 request
->key_len
= put
->key_len
;
1327 request
->mask
= put
->mask
;
1328 request
->mask_len
= put
->mask_len
;
1329 dpif_netlink_flow_init_ufid(request
, put
->ufid
, false);
1331 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1332 request
->actions
= (put
->actions
1334 : CONST_CAST(struct nlattr
*, &dummy_action
));
1335 request
->actions_len
= put
->actions_len
;
1336 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
1337 request
->clear
= true;
1339 if (put
->flags
& DPIF_FP_PROBE
) {
1340 request
->probe
= true;
1342 request
->nlmsg_flags
= put
->flags
& DPIF_FP_MODIFY
? 0 : NLM_F_CREATE
;
1346 dpif_netlink_init_flow_del__(struct dpif_netlink
*dpif
,
1347 const struct nlattr
*key
, size_t key_len
,
1348 const ovs_u128
*ufid
, bool terse
,
1349 struct dpif_netlink_flow
*request
)
1351 dpif_netlink_flow_init(request
);
1352 request
->cmd
= OVS_FLOW_CMD_DEL
;
1353 request
->dp_ifindex
= dpif
->dp_ifindex
;
1355 request
->key_len
= key_len
;
1356 dpif_netlink_flow_init_ufid(request
, ufid
, terse
);
1360 dpif_netlink_init_flow_del(struct dpif_netlink
*dpif
,
1361 const struct dpif_flow_del
*del
,
1362 struct dpif_netlink_flow
*request
)
1364 dpif_netlink_init_flow_del__(dpif
, del
->key
, del
->key_len
,
1365 del
->ufid
, del
->terse
, request
);
1368 struct dpif_netlink_flow_dump
{
1369 struct dpif_flow_dump up
;
1370 struct nl_dump nl_dump
;
1372 struct netdev_flow_dump
**netdev_dumps
;
1373 int netdev_dumps_num
; /* Number of netdev_flow_dumps */
1374 struct ovs_mutex netdev_lock
; /* Guards the following. */
1375 int netdev_current_dump OVS_GUARDED
; /* Shared current dump */
1376 struct dpif_flow_dump_types types
; /* Type of dump */
1379 static struct dpif_netlink_flow_dump
*
1380 dpif_netlink_flow_dump_cast(struct dpif_flow_dump
*dump
)
1382 return CONTAINER_OF(dump
, struct dpif_netlink_flow_dump
, up
);
1386 start_netdev_dump(const struct dpif
*dpif_
,
1387 struct dpif_netlink_flow_dump
*dump
)
1389 ovs_mutex_init(&dump
->netdev_lock
);
1391 if (!(dump
->types
.netdev_flows
)) {
1392 dump
->netdev_dumps_num
= 0;
1393 dump
->netdev_dumps
= NULL
;
1397 ovs_mutex_lock(&dump
->netdev_lock
);
1398 dump
->netdev_current_dump
= 0;
1400 = netdev_ports_flow_dump_create(dpif_
->dpif_class
,
1401 &dump
->netdev_dumps_num
);
1402 ovs_mutex_unlock(&dump
->netdev_lock
);
1406 dpif_netlink_populate_flow_dump_types(struct dpif_netlink_flow_dump
*dump
,
1407 struct dpif_flow_dump_types
*types
)
1410 dump
->types
.ovs_flows
= true;
1411 dump
->types
.netdev_flows
= true;
1413 memcpy(&dump
->types
, types
, sizeof *types
);
1417 static struct dpif_flow_dump
*
1418 dpif_netlink_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
1419 struct dpif_flow_dump_types
*types
)
1421 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1422 struct dpif_netlink_flow_dump
*dump
;
1423 struct dpif_netlink_flow request
;
1426 dump
= xmalloc(sizeof *dump
);
1427 dpif_flow_dump_init(&dump
->up
, dpif_
);
1429 dpif_netlink_populate_flow_dump_types(dump
, types
);
1431 if (dump
->types
.ovs_flows
) {
1432 dpif_netlink_flow_init(&request
);
1433 request
.cmd
= OVS_FLOW_CMD_GET
;
1434 request
.dp_ifindex
= dpif
->dp_ifindex
;
1435 request
.ufid_present
= false;
1436 request
.ufid_terse
= terse
;
1438 buf
= ofpbuf_new(1024);
1439 dpif_netlink_flow_to_ofpbuf(&request
, buf
);
1440 nl_dump_start(&dump
->nl_dump
, NETLINK_GENERIC
, buf
);
1443 atomic_init(&dump
->status
, 0);
1444 dump
->up
.terse
= terse
;
1446 start_netdev_dump(dpif_
, dump
);
1452 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
1454 struct dpif_netlink_flow_dump
*dump
= dpif_netlink_flow_dump_cast(dump_
);
1455 unsigned int nl_status
= 0;
1458 if (dump
->types
.ovs_flows
) {
1459 nl_status
= nl_dump_done(&dump
->nl_dump
);
1462 for (int i
= 0; i
< dump
->netdev_dumps_num
; i
++) {
1463 int err
= netdev_flow_dump_destroy(dump
->netdev_dumps
[i
]);
1465 if (err
!= 0 && err
!= EOPNOTSUPP
) {
1466 VLOG_ERR("failed dumping netdev: %s", ovs_strerror(err
));
1470 free(dump
->netdev_dumps
);
1471 ovs_mutex_destroy(&dump
->netdev_lock
);
1473 /* No other thread has access to 'dump' at this point. */
1474 atomic_read_relaxed(&dump
->status
, &dump_status
);
1476 return dump_status
? dump_status
: nl_status
;
1479 struct dpif_netlink_flow_dump_thread
{
1480 struct dpif_flow_dump_thread up
;
1481 struct dpif_netlink_flow_dump
*dump
;
1482 struct dpif_netlink_flow flow
;
1483 struct dpif_flow_stats stats
;
1484 struct ofpbuf nl_flows
; /* Always used to store flows. */
1485 struct ofpbuf
*nl_actions
; /* Used if kernel does not supply actions. */
1486 int netdev_dump_idx
; /* This thread current netdev dump index */
1487 bool netdev_done
; /* If we are finished dumping netdevs */
1489 /* (Key/Mask/Actions) Buffers for netdev dumping */
1490 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
1491 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
1492 struct odputil_keybuf actbuf
[FLOW_DUMP_MAX_BATCH
];
1495 static struct dpif_netlink_flow_dump_thread
*
1496 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
1498 return CONTAINER_OF(thread
, struct dpif_netlink_flow_dump_thread
, up
);
1501 static struct dpif_flow_dump_thread
*
1502 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
1504 struct dpif_netlink_flow_dump
*dump
= dpif_netlink_flow_dump_cast(dump_
);
1505 struct dpif_netlink_flow_dump_thread
*thread
;
1507 thread
= xmalloc(sizeof *thread
);
1508 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
1509 thread
->dump
= dump
;
1510 ofpbuf_init(&thread
->nl_flows
, NL_DUMP_BUFSIZE
);
1511 thread
->nl_actions
= NULL
;
1512 thread
->netdev_dump_idx
= 0;
1513 thread
->netdev_done
= !(thread
->netdev_dump_idx
< dump
->netdev_dumps_num
);
1519 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
1521 struct dpif_netlink_flow_dump_thread
*thread
1522 = dpif_netlink_flow_dump_thread_cast(thread_
);
1524 ofpbuf_uninit(&thread
->nl_flows
);
1525 ofpbuf_delete(thread
->nl_actions
);
1530 dpif_netlink_flow_to_dpif_flow(struct dpif
*dpif
, struct dpif_flow
*dpif_flow
,
1531 const struct dpif_netlink_flow
*datapath_flow
)
1533 dpif_flow
->key
= datapath_flow
->key
;
1534 dpif_flow
->key_len
= datapath_flow
->key_len
;
1535 dpif_flow
->mask
= datapath_flow
->mask
;
1536 dpif_flow
->mask_len
= datapath_flow
->mask_len
;
1537 dpif_flow
->actions
= datapath_flow
->actions
;
1538 dpif_flow
->actions_len
= datapath_flow
->actions_len
;
1539 dpif_flow
->ufid_present
= datapath_flow
->ufid_present
;
1540 dpif_flow
->pmd_id
= PMD_ID_NULL
;
1541 if (datapath_flow
->ufid_present
) {
1542 dpif_flow
->ufid
= datapath_flow
->ufid
;
1544 ovs_assert(datapath_flow
->key
&& datapath_flow
->key_len
);
1545 dpif_flow_hash(dpif
, datapath_flow
->key
, datapath_flow
->key_len
,
1548 dpif_netlink_flow_get_stats(datapath_flow
, &dpif_flow
->stats
);
1549 dpif_flow
->attrs
.offloaded
= false;
1550 dpif_flow
->attrs
.dp_layer
= "ovs";
1553 /* The design is such that all threads are working together on the first dump
1554 * to the last, in order (at first they all on dump 0).
1555 * When the first thread finds that the given dump is finished,
1556 * they all move to the next. If two or more threads find the same dump
1557 * is finished at the same time, the first one will advance the shared
1558 * netdev_current_dump and the others will catch up. */
1560 dpif_netlink_advance_netdev_dump(struct dpif_netlink_flow_dump_thread
*thread
)
1562 struct dpif_netlink_flow_dump
*dump
= thread
->dump
;
1564 ovs_mutex_lock(&dump
->netdev_lock
);
1565 /* if we haven't finished (dumped everything) */
1566 if (dump
->netdev_current_dump
< dump
->netdev_dumps_num
) {
1567 /* if we are the first to find that current dump is finished
1569 if (thread
->netdev_dump_idx
== dump
->netdev_current_dump
) {
1570 thread
->netdev_dump_idx
= ++dump
->netdev_current_dump
;
1571 /* did we just finish the last dump? done. */
1572 if (dump
->netdev_current_dump
== dump
->netdev_dumps_num
) {
1573 thread
->netdev_done
= true;
1576 /* otherwise, we are behind, catch up */
1577 thread
->netdev_dump_idx
= dump
->netdev_current_dump
;
1580 /* some other thread finished */
1581 thread
->netdev_done
= true;
1583 ovs_mutex_unlock(&dump
->netdev_lock
);
1587 dpif_netlink_netdev_match_to_dpif_flow(struct match
*match
,
1588 struct ofpbuf
*key_buf
,
1589 struct ofpbuf
*mask_buf
,
1590 struct nlattr
*actions
,
1591 struct dpif_flow_stats
*stats
,
1592 struct dpif_flow_attrs
*attrs
,
1594 struct dpif_flow
*flow
,
1595 bool terse OVS_UNUSED
)
1598 struct odp_flow_key_parms odp_parms
= {
1599 .flow
= &match
->flow
,
1600 .mask
= &match
->wc
.masks
,
1602 .max_vlan_headers
= 2,
1607 memset(flow
, 0, sizeof *flow
);
1610 offset
= key_buf
->size
;
1611 flow
->key
= ofpbuf_tail(key_buf
);
1612 odp_flow_key_from_flow(&odp_parms
, key_buf
);
1613 flow
->key_len
= key_buf
->size
- offset
;
1616 offset
= mask_buf
->size
;
1617 flow
->mask
= ofpbuf_tail(mask_buf
);
1618 odp_parms
.key_buf
= key_buf
;
1619 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
1620 flow
->mask_len
= mask_buf
->size
- offset
;
1623 flow
->actions
= nl_attr_get(actions
);
1624 flow
->actions_len
= nl_attr_get_size(actions
);
1627 memcpy(&flow
->stats
, stats
, sizeof *stats
);
1630 flow
->ufid_present
= true;
1633 flow
->pmd_id
= PMD_ID_NULL
;
1635 memcpy(&flow
->attrs
, attrs
, sizeof *attrs
);
1641 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
1642 struct dpif_flow
*flows
, int max_flows
)
1644 struct dpif_netlink_flow_dump_thread
*thread
1645 = dpif_netlink_flow_dump_thread_cast(thread_
);
1646 struct dpif_netlink_flow_dump
*dump
= thread
->dump
;
1647 struct dpif_netlink
*dpif
= dpif_netlink_cast(thread
->up
.dpif
);
1650 ofpbuf_delete(thread
->nl_actions
);
1651 thread
->nl_actions
= NULL
;
1654 max_flows
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
1656 while (!thread
->netdev_done
&& n_flows
< max_flows
) {
1657 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[n_flows
];
1658 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[n_flows
];
1659 struct odputil_keybuf
*actbuf
= &thread
->actbuf
[n_flows
];
1660 struct ofpbuf key
, mask
, act
;
1661 struct dpif_flow
*f
= &flows
[n_flows
];
1662 int cur
= thread
->netdev_dump_idx
;
1663 struct netdev_flow_dump
*netdev_dump
= dump
->netdev_dumps
[cur
];
1665 struct nlattr
*actions
;
1666 struct dpif_flow_stats stats
;
1667 struct dpif_flow_attrs attrs
;
1671 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
1672 ofpbuf_use_stack(&act
, actbuf
, sizeof *actbuf
);
1673 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
1674 has_next
= netdev_flow_dump_next(netdev_dump
, &match
,
1675 &actions
, &stats
, &attrs
,
1680 dpif_netlink_netdev_match_to_dpif_flow(&match
,
1690 dpif_netlink_advance_netdev_dump(thread
);
1694 if (!(dump
->types
.ovs_flows
)) {
1699 || (n_flows
< max_flows
&& thread
->nl_flows
.size
)) {
1700 struct dpif_netlink_flow datapath_flow
;
1701 struct ofpbuf nl_flow
;
1704 /* Try to grab another flow. */
1705 if (!nl_dump_next(&dump
->nl_dump
, &nl_flow
, &thread
->nl_flows
)) {
1709 /* Convert the flow to our output format. */
1710 error
= dpif_netlink_flow_from_ofpbuf(&datapath_flow
, &nl_flow
);
1712 atomic_store_relaxed(&dump
->status
, error
);
1716 if (dump
->up
.terse
|| datapath_flow
.actions
) {
1717 /* Common case: we don't want actions, or the flow includes
1719 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, &flows
[n_flows
++],
1722 /* Rare case: the flow does not include actions. Retrieve this
1723 * individual flow again to get the actions. */
1724 error
= dpif_netlink_flow_get(dpif
, &datapath_flow
,
1725 &datapath_flow
, &thread
->nl_actions
);
1726 if (error
== ENOENT
) {
1727 VLOG_DBG("dumped flow disappeared on get");
1730 VLOG_WARN("error fetching dumped flow: %s",
1731 ovs_strerror(error
));
1732 atomic_store_relaxed(&dump
->status
, error
);
1736 /* Save this flow. Then exit, because we only have one buffer to
1737 * handle this case. */
1738 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, &flows
[n_flows
++],
1747 dpif_netlink_encode_execute(int dp_ifindex
, const struct dpif_execute
*d_exec
,
1750 struct ovs_header
*k_exec
;
1753 ofpbuf_prealloc_tailroom(buf
, (64
1754 + dp_packet_size(d_exec
->packet
)
1755 + ODP_KEY_METADATA_SIZE
1756 + d_exec
->actions_len
));
1758 nl_msg_put_genlmsghdr(buf
, 0, ovs_packet_family
, NLM_F_REQUEST
,
1759 OVS_PACKET_CMD_EXECUTE
, OVS_PACKET_VERSION
);
1761 k_exec
= ofpbuf_put_uninit(buf
, sizeof *k_exec
);
1762 k_exec
->dp_ifindex
= dp_ifindex
;
1764 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_PACKET
,
1765 dp_packet_data(d_exec
->packet
),
1766 dp_packet_size(d_exec
->packet
));
1768 key_ofs
= nl_msg_start_nested(buf
, OVS_PACKET_ATTR_KEY
);
1769 odp_key_from_dp_packet(buf
, d_exec
->packet
);
1770 nl_msg_end_nested(buf
, key_ofs
);
1772 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_ACTIONS
,
1773 d_exec
->actions
, d_exec
->actions_len
);
1774 if (d_exec
->probe
) {
1775 nl_msg_put_flag(buf
, OVS_PACKET_ATTR_PROBE
);
1778 nl_msg_put_u16(buf
, OVS_PACKET_ATTR_MRU
, d_exec
->mtu
);
1782 nl_msg_put_u64(buf
, OVS_PACKET_ATTR_HASH
, d_exec
->hash
);
1786 /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1787 * Returns the number actually executed (at least 1, if 'n_ops' is
1790 dpif_netlink_operate__(struct dpif_netlink
*dpif
,
1791 struct dpif_op
**ops
, size_t n_ops
)
1794 struct nl_transaction txn
;
1796 struct ofpbuf request
;
1797 uint64_t request_stub
[1024 / 8];
1799 struct ofpbuf reply
;
1800 uint64_t reply_stub
[1024 / 8];
1801 } auxes
[OPERATE_MAX_OPS
];
1803 struct nl_transaction
*txnsp
[OPERATE_MAX_OPS
];
1806 n_ops
= MIN(n_ops
, OPERATE_MAX_OPS
);
1807 for (i
= 0; i
< n_ops
; i
++) {
1808 struct op_auxdata
*aux
= &auxes
[i
];
1809 struct dpif_op
*op
= ops
[i
];
1810 struct dpif_flow_put
*put
;
1811 struct dpif_flow_del
*del
;
1812 struct dpif_flow_get
*get
;
1813 struct dpif_netlink_flow flow
;
1815 ofpbuf_use_stub(&aux
->request
,
1816 aux
->request_stub
, sizeof aux
->request_stub
);
1817 aux
->txn
.request
= &aux
->request
;
1819 ofpbuf_use_stub(&aux
->reply
, aux
->reply_stub
, sizeof aux
->reply_stub
);
1820 aux
->txn
.reply
= NULL
;
1823 case DPIF_OP_FLOW_PUT
:
1824 put
= &op
->flow_put
;
1825 dpif_netlink_init_flow_put(dpif
, put
, &flow
);
1827 flow
.nlmsg_flags
|= NLM_F_ECHO
;
1828 aux
->txn
.reply
= &aux
->reply
;
1830 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1833 case DPIF_OP_FLOW_DEL
:
1834 del
= &op
->flow_del
;
1835 dpif_netlink_init_flow_del(dpif
, del
, &flow
);
1837 flow
.nlmsg_flags
|= NLM_F_ECHO
;
1838 aux
->txn
.reply
= &aux
->reply
;
1840 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1843 case DPIF_OP_EXECUTE
:
1844 /* Can't execute a packet that won't fit in a Netlink attribute. */
1845 if (OVS_UNLIKELY(nl_attr_oversized(
1846 dp_packet_size(op
->execute
.packet
)))) {
1847 /* Report an error immediately if this is the first operation.
1848 * Otherwise the easiest thing to do is to postpone to the next
1849 * call (when this will be the first operation). */
1851 VLOG_ERR_RL(&error_rl
,
1852 "dropping oversized %"PRIu32
"-byte packet",
1853 dp_packet_size(op
->execute
.packet
));
1854 op
->error
= ENOBUFS
;
1859 dpif_netlink_encode_execute(dpif
->dp_ifindex
, &op
->execute
,
1864 case DPIF_OP_FLOW_GET
:
1865 get
= &op
->flow_get
;
1866 dpif_netlink_init_flow_get(dpif
, get
, &flow
);
1867 aux
->txn
.reply
= get
->buffer
;
1868 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1876 for (i
= 0; i
< n_ops
; i
++) {
1877 txnsp
[i
] = &auxes
[i
].txn
;
1879 nl_transact_multiple(NETLINK_GENERIC
, txnsp
, n_ops
);
1881 for (i
= 0; i
< n_ops
; i
++) {
1882 struct op_auxdata
*aux
= &auxes
[i
];
1883 struct nl_transaction
*txn
= &auxes
[i
].txn
;
1884 struct dpif_op
*op
= ops
[i
];
1885 struct dpif_flow_put
*put
;
1886 struct dpif_flow_del
*del
;
1887 struct dpif_flow_get
*get
;
1889 op
->error
= txn
->error
;
1892 case DPIF_OP_FLOW_PUT
:
1893 put
= &op
->flow_put
;
1896 struct dpif_netlink_flow reply
;
1898 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
,
1901 dpif_netlink_flow_get_stats(&reply
, put
->stats
);
1907 case DPIF_OP_FLOW_DEL
:
1908 del
= &op
->flow_del
;
1911 struct dpif_netlink_flow reply
;
1913 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
,
1916 dpif_netlink_flow_get_stats(&reply
, del
->stats
);
1922 case DPIF_OP_EXECUTE
:
1925 case DPIF_OP_FLOW_GET
:
1926 get
= &op
->flow_get
;
1928 struct dpif_netlink_flow reply
;
1930 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
, txn
->reply
);
1932 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, get
->flow
,
1942 ofpbuf_uninit(&aux
->request
);
1943 ofpbuf_uninit(&aux
->reply
);
1950 parse_flow_get(struct dpif_netlink
*dpif
, struct dpif_flow_get
*get
)
1952 struct dpif_flow
*dpif_flow
= get
->flow
;
1954 struct nlattr
*actions
;
1955 struct dpif_flow_stats stats
;
1956 struct dpif_flow_attrs attrs
;
1958 uint64_t act_buf
[1024 / 8];
1959 struct odputil_keybuf maskbuf
;
1960 struct odputil_keybuf keybuf
;
1961 struct odputil_keybuf actbuf
;
1962 struct ofpbuf key
, mask
, act
;
1965 ofpbuf_use_stack(&buf
, &act_buf
, sizeof act_buf
);
1966 err
= netdev_ports_flow_get(dpif
->dpif
.dpif_class
, &match
,
1967 &actions
, get
->ufid
, &stats
, &attrs
, &buf
);
1972 VLOG_DBG("found flow from netdev, translating to dpif flow");
1974 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1975 ofpbuf_use_stack(&act
, &actbuf
, sizeof actbuf
);
1976 ofpbuf_use_stack(&mask
, &maskbuf
, sizeof maskbuf
);
1977 dpif_netlink_netdev_match_to_dpif_flow(&match
, &key
, &mask
, actions
,
1979 (ovs_u128
*) get
->ufid
,
1982 ofpbuf_put(get
->buffer
, nl_attr_get(actions
), nl_attr_get_size(actions
));
1983 dpif_flow
->actions
= ofpbuf_at(get
->buffer
, 0, 0);
1984 dpif_flow
->actions_len
= nl_attr_get_size(actions
);
1990 parse_flow_put(struct dpif_netlink
*dpif
, struct dpif_flow_put
*put
)
1992 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
1993 const struct dpif_class
*dpif_class
= dpif
->dpif
.dpif_class
;
1996 const struct nlattr
*nla
;
1999 struct offload_info info
;
2000 ovs_be16 dst_port
= 0;
2001 uint8_t csum_on
= false;
2004 if (put
->flags
& DPIF_FP_PROBE
) {
2008 err
= parse_key_and_mask_to_match(put
->key
, put
->key_len
, put
->mask
,
2009 put
->mask_len
, &match
);
2014 in_port
= match
.flow
.in_port
.odp_port
;
2015 dev
= netdev_ports_get(in_port
, dpif_class
);
2020 /* Get tunnel dst port */
2021 NL_ATTR_FOR_EACH(nla
, left
, put
->actions
, put
->actions_len
) {
2022 if (nl_attr_type(nla
) == OVS_ACTION_ATTR_OUTPUT
) {
2023 const struct netdev_tunnel_config
*tnl_cfg
;
2024 struct netdev
*outdev
;
2025 odp_port_t out_port
;
2027 out_port
= nl_attr_get_odp_port(nla
);
2028 outdev
= netdev_ports_get(out_port
, dpif_class
);
2033 tnl_cfg
= netdev_get_tunnel_config(outdev
);
2034 if (tnl_cfg
&& tnl_cfg
->dst_port
!= 0) {
2035 dst_port
= tnl_cfg
->dst_port
;
2038 csum_on
= tnl_cfg
->csum
;
2040 netdev_close(outdev
);
2044 info
.dpif_class
= dpif_class
;
2045 info
.tp_dst_port
= dst_port
;
2046 info
.tunnel_csum_on
= csum_on
;
2047 err
= netdev_flow_put(dev
, &match
,
2048 CONST_CAST(struct nlattr
*, put
->actions
),
2050 CONST_CAST(ovs_u128
*, put
->ufid
),
2054 if (put
->flags
& DPIF_FP_MODIFY
) {
2055 struct dpif_op
*opp
;
2058 op
.type
= DPIF_OP_FLOW_DEL
;
2059 op
.flow_del
.key
= put
->key
;
2060 op
.flow_del
.key_len
= put
->key_len
;
2061 op
.flow_del
.ufid
= put
->ufid
;
2062 op
.flow_del
.pmd_id
= put
->pmd_id
;
2063 op
.flow_del
.stats
= NULL
;
2064 op
.flow_del
.terse
= false;
2067 dpif_netlink_operate__(dpif
, &opp
, 1);
2070 VLOG_DBG("added flow");
2071 } else if (err
!= EEXIST
) {
2072 struct netdev
*oor_netdev
= NULL
;
2073 enum vlog_level level
;
2074 if (err
== ENOSPC
&& netdev_is_offload_rebalance_policy_enabled()) {
2076 * We need to set OOR on the input netdev (i.e, 'dev') for the
2077 * flow. But if the flow has a tunnel attribute (i.e, decap action,
2078 * with a virtual device like a VxLAN interface as its in-port),
2079 * then lookup and set OOR on the underlying tunnel (real) netdev.
2081 oor_netdev
= flow_get_tunnel_netdev(&match
.flow
.tunnel
);
2083 /* Not a 'tunnel' flow */
2086 netdev_set_hw_info(oor_netdev
, HW_INFO_TYPE_OOR
, true);
2088 level
= (err
== ENOSPC
|| err
== EOPNOTSUPP
) ? VLL_DBG
: VLL_ERR
;
2089 VLOG_RL(&rl
, level
, "failed to offload flow: %s: %s",
2091 (oor_netdev
? oor_netdev
->name
: dev
->name
));
2095 if (err
&& err
!= EEXIST
&& (put
->flags
& DPIF_FP_MODIFY
)) {
2096 /* Modified rule can't be offloaded, try and delete from HW */
2097 int del_err
= netdev_flow_del(dev
, put
->ufid
, put
->stats
);
2100 /* Delete from hw success, so old flow was offloaded.
2101 * Change flags to create the flow in kernel */
2102 put
->flags
&= ~DPIF_FP_MODIFY
;
2103 put
->flags
|= DPIF_FP_CREATE
;
2104 } else if (del_err
!= ENOENT
) {
2105 VLOG_ERR_RL(&rl
, "failed to delete offloaded flow: %s",
2106 ovs_strerror(del_err
));
2107 /* stop proccesing the flow in kernel */
2118 try_send_to_netdev(struct dpif_netlink
*dpif
, struct dpif_op
*op
)
2120 int err
= EOPNOTSUPP
;
2123 case DPIF_OP_FLOW_PUT
: {
2124 struct dpif_flow_put
*put
= &op
->flow_put
;
2130 log_flow_put_message(&dpif
->dpif
, &this_module
, put
, 0);
2131 err
= parse_flow_put(dpif
, put
);
2134 case DPIF_OP_FLOW_DEL
: {
2135 struct dpif_flow_del
*del
= &op
->flow_del
;
2141 log_flow_del_message(&dpif
->dpif
, &this_module
, del
, 0);
2142 err
= netdev_ports_flow_del(dpif
->dpif
.dpif_class
, del
->ufid
,
2146 case DPIF_OP_FLOW_GET
: {
2147 struct dpif_flow_get
*get
= &op
->flow_get
;
2149 if (!op
->flow_get
.ufid
) {
2153 log_flow_get_message(&dpif
->dpif
, &this_module
, get
, 0);
2154 err
= parse_flow_get(dpif
, get
);
2157 case DPIF_OP_EXECUTE
:
2166 dpif_netlink_operate_chunks(struct dpif_netlink
*dpif
, struct dpif_op
**ops
,
2170 size_t chunk
= dpif_netlink_operate__(dpif
, ops
, n_ops
);
2178 dpif_netlink_operate(struct dpif
*dpif_
, struct dpif_op
**ops
, size_t n_ops
,
2179 enum dpif_offload_type offload_type
)
2181 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2182 struct dpif_op
*new_ops
[OPERATE_MAX_OPS
];
2187 if (offload_type
== DPIF_OFFLOAD_ALWAYS
&& !netdev_is_flow_api_enabled()) {
2188 VLOG_DBG("Invalid offload_type: %d", offload_type
);
2192 if (offload_type
!= DPIF_OFFLOAD_NEVER
&& netdev_is_flow_api_enabled()) {
2196 while (n_ops
> 0 && count
< OPERATE_MAX_OPS
) {
2197 struct dpif_op
*op
= ops
[i
++];
2199 err
= try_send_to_netdev(dpif
, op
);
2200 if (err
&& err
!= EEXIST
) {
2201 if (offload_type
== DPIF_OFFLOAD_ALWAYS
) {
2202 /* We got an error while offloading an op. Since
2203 * OFFLOAD_ALWAYS is specified, we stop further
2204 * processing and return to the caller without
2205 * invoking kernel datapath as fallback. But the
2206 * interface requires us to process all n_ops; so
2207 * return the same error in the remaining ops too.
2218 new_ops
[count
++] = op
;
2226 dpif_netlink_operate_chunks(dpif
, new_ops
, count
);
2228 } else if (offload_type
!= DPIF_OFFLOAD_ALWAYS
) {
2229 dpif_netlink_operate_chunks(dpif
, ops
, n_ops
);
2235 dpif_netlink_handler_uninit(struct dpif_handler
*handler
)
2237 vport_delete_sock_pool(handler
);
2241 dpif_netlink_handler_init(struct dpif_handler
*handler
)
2243 return vport_create_sock_pool(handler
);
2248 dpif_netlink_handler_init(struct dpif_handler
*handler
)
2250 handler
->epoll_fd
= epoll_create(10);
2251 return handler
->epoll_fd
< 0 ? errno
: 0;
2255 dpif_netlink_handler_uninit(struct dpif_handler
*handler
)
2257 close(handler
->epoll_fd
);
2261 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
2262 * currently in 'dpif' in the kernel, by adding a new set of channels for
2263 * any kernel vport that lacks one and deleting any channels that have no
2264 * backing kernel vports. */
2266 dpif_netlink_refresh_channels(struct dpif_netlink
*dpif
, uint32_t n_handlers
)
2267 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2269 unsigned long int *keep_channels
;
2270 struct dpif_netlink_vport vport
;
2271 size_t keep_channels_nbits
;
2272 struct nl_dump dump
;
2273 uint64_t reply_stub
[NL_DUMP_BUFSIZE
/ 8];
2278 ovs_assert(!WINDOWS
|| n_handlers
<= 1);
2279 ovs_assert(!WINDOWS
|| dpif
->n_handlers
<= 1);
2281 if (dpif
->n_handlers
!= n_handlers
) {
2282 destroy_all_channels(dpif
);
2283 dpif
->handlers
= xzalloc(n_handlers
* sizeof *dpif
->handlers
);
2284 for (i
= 0; i
< n_handlers
; i
++) {
2286 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
2288 error
= dpif_netlink_handler_init(handler
);
2292 for (j
= 0; j
< i
; j
++) {
2293 struct dpif_handler
*tmp
= &dpif
->handlers
[j
];
2294 dpif_netlink_handler_uninit(tmp
);
2296 free(dpif
->handlers
);
2297 dpif
->handlers
= NULL
;
2302 dpif
->n_handlers
= n_handlers
;
2305 for (i
= 0; i
< n_handlers
; i
++) {
2306 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
2308 handler
->event_offset
= handler
->n_events
= 0;
2311 keep_channels_nbits
= dpif
->uc_array_size
;
2312 keep_channels
= bitmap_allocate(keep_channels_nbits
);
2314 ofpbuf_use_stub(&buf
, reply_stub
, sizeof reply_stub
);
2315 dpif_netlink_port_dump_start__(dpif
, &dump
);
2316 while (!dpif_netlink_port_dump_next__(dpif
, &dump
, &vport
, &buf
)) {
2317 uint32_t port_no
= odp_to_u32(vport
.port_no
);
2318 uint32_t upcall_pid
;
2321 if (port_no
>= dpif
->uc_array_size
2322 || !vport_get_pid(dpif
, port_no
, &upcall_pid
)) {
2323 struct nl_sock
*sock
;
2324 error
= create_nl_sock(dpif
, &sock
);
2330 error
= vport_add_channel(dpif
, vport
.port_no
, sock
);
2332 VLOG_INFO("%s: could not add channels for port %s",
2333 dpif_name(&dpif
->dpif
), vport
.name
);
2334 nl_sock_destroy(sock
);
2338 upcall_pid
= nl_sock_pid(sock
);
2341 /* Configure the vport to deliver misses to 'sock'. */
2342 if (vport
.upcall_pids
[0] == 0
2343 || vport
.n_upcall_pids
!= 1
2344 || upcall_pid
!= vport
.upcall_pids
[0]) {
2345 struct dpif_netlink_vport vport_request
;
2347 dpif_netlink_vport_init(&vport_request
);
2348 vport_request
.cmd
= OVS_VPORT_CMD_SET
;
2349 vport_request
.dp_ifindex
= dpif
->dp_ifindex
;
2350 vport_request
.port_no
= vport
.port_no
;
2351 vport_request
.n_upcall_pids
= 1;
2352 vport_request
.upcall_pids
= &upcall_pid
;
2353 error
= dpif_netlink_vport_transact(&vport_request
, NULL
, NULL
);
2355 VLOG_WARN_RL(&error_rl
,
2356 "%s: failed to set upcall pid on port: %s",
2357 dpif_name(&dpif
->dpif
), ovs_strerror(error
));
2359 if (error
!= ENODEV
&& error
!= ENOENT
) {
2362 /* The vport isn't really there, even though the dump says
2363 * it is. Probably we just hit a race after a port
2370 if (port_no
< keep_channels_nbits
) {
2371 bitmap_set1(keep_channels
, port_no
);
2376 vport_del_channels(dpif
, vport
.port_no
);
2378 nl_dump_done(&dump
);
2379 ofpbuf_uninit(&buf
);
2381 /* Discard any saved channels that we didn't reuse. */
2382 for (i
= 0; i
< keep_channels_nbits
; i
++) {
2383 if (!bitmap_is_set(keep_channels
, i
)) {
2384 vport_del_channels(dpif
, u32_to_odp(i
));
2387 free(keep_channels
);
2393 dpif_netlink_recv_set__(struct dpif_netlink
*dpif
, bool enable
)
2394 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2396 if ((dpif
->handlers
!= NULL
) == enable
) {
2398 } else if (!enable
) {
2399 destroy_all_channels(dpif
);
2402 return dpif_netlink_refresh_channels(dpif
, 1);
2407 dpif_netlink_recv_set(struct dpif
*dpif_
, bool enable
)
2409 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2412 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2413 error
= dpif_netlink_recv_set__(dpif
, enable
);
2414 fat_rwlock_unlock(&dpif
->upcall_lock
);
2420 dpif_netlink_handlers_set(struct dpif
*dpif_
, uint32_t n_handlers
)
2422 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2426 /* Multiple upcall handlers will be supported once kernel datapath supports
2428 if (n_handlers
> 1) {
2433 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2434 if (dpif
->handlers
) {
2435 error
= dpif_netlink_refresh_channels(dpif
, n_handlers
);
2437 fat_rwlock_unlock(&dpif
->upcall_lock
);
2443 dpif_netlink_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2444 uint32_t queue_id
, uint32_t *priority
)
2446 if (queue_id
< 0xf000) {
2447 *priority
= TC_H_MAKE(1 << 16, queue_id
+ 1);
2455 parse_odp_packet(const struct dpif_netlink
*dpif
, struct ofpbuf
*buf
,
2456 struct dpif_upcall
*upcall
, int *dp_ifindex
)
2458 static const struct nl_policy ovs_packet_policy
[] = {
2459 /* Always present. */
2460 [OVS_PACKET_ATTR_PACKET
] = { .type
= NL_A_UNSPEC
,
2461 .min_len
= ETH_HEADER_LEN
},
2462 [OVS_PACKET_ATTR_KEY
] = { .type
= NL_A_NESTED
},
2464 /* OVS_PACKET_CMD_ACTION only. */
2465 [OVS_PACKET_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
, .optional
= true },
2466 [OVS_PACKET_ATTR_EGRESS_TUN_KEY
] = { .type
= NL_A_NESTED
, .optional
= true },
2467 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
2468 [OVS_PACKET_ATTR_MRU
] = { .type
= NL_A_U16
, .optional
= true },
2469 [OVS_PACKET_ATTR_HASH
] = { .type
= NL_A_U64
, .optional
= true }
2472 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
2473 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
2474 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
2475 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
2477 struct nlattr
*a
[ARRAY_SIZE(ovs_packet_policy
)];
2478 if (!nlmsg
|| !genl
|| !ovs_header
2479 || nlmsg
->nlmsg_type
!= ovs_packet_family
2480 || !nl_policy_parse(&b
, 0, ovs_packet_policy
, a
,
2481 ARRAY_SIZE(ovs_packet_policy
))) {
2485 int type
= (genl
->cmd
== OVS_PACKET_CMD_MISS
? DPIF_UC_MISS
2486 : genl
->cmd
== OVS_PACKET_CMD_ACTION
? DPIF_UC_ACTION
2492 /* (Re)set ALL fields of '*upcall' on successful return. */
2493 upcall
->type
= type
;
2494 upcall
->key
= CONST_CAST(struct nlattr
*,
2495 nl_attr_get(a
[OVS_PACKET_ATTR_KEY
]));
2496 upcall
->key_len
= nl_attr_get_size(a
[OVS_PACKET_ATTR_KEY
]);
2497 dpif_flow_hash(&dpif
->dpif
, upcall
->key
, upcall
->key_len
, &upcall
->ufid
);
2498 upcall
->userdata
= a
[OVS_PACKET_ATTR_USERDATA
];
2499 upcall
->out_tun_key
= a
[OVS_PACKET_ATTR_EGRESS_TUN_KEY
];
2500 upcall
->actions
= a
[OVS_PACKET_ATTR_ACTIONS
];
2501 upcall
->mru
= a
[OVS_PACKET_ATTR_MRU
];
2502 upcall
->hash
= a
[OVS_PACKET_ATTR_HASH
];
2504 /* Allow overwriting the netlink attribute header without reallocating. */
2505 dp_packet_use_stub(&upcall
->packet
,
2506 CONST_CAST(struct nlattr
*,
2507 nl_attr_get(a
[OVS_PACKET_ATTR_PACKET
])) - 1,
2508 nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]) +
2509 sizeof(struct nlattr
));
2510 dp_packet_set_data(&upcall
->packet
,
2511 (char *)dp_packet_data(&upcall
->packet
) + sizeof(struct nlattr
));
2512 dp_packet_set_size(&upcall
->packet
, nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]));
2514 if (nl_attr_find__(upcall
->key
, upcall
->key_len
, OVS_KEY_ATTR_ETHERNET
)) {
2515 /* Ethernet frame */
2516 upcall
->packet
.packet_type
= htonl(PT_ETH
);
2518 /* Non-Ethernet packet. Get the Ethertype from the NL attributes */
2519 ovs_be16 ethertype
= 0;
2520 const struct nlattr
*et_nla
= nl_attr_find__(upcall
->key
,
2522 OVS_KEY_ATTR_ETHERTYPE
);
2524 ethertype
= nl_attr_get_be16(et_nla
);
2526 upcall
->packet
.packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
2528 dp_packet_set_l3(&upcall
->packet
, dp_packet_data(&upcall
->packet
));
2531 *dp_ifindex
= ovs_header
->dp_ifindex
;
2537 #define PACKET_RECV_BATCH_SIZE 50
2539 dpif_netlink_recv_windows(struct dpif_netlink
*dpif
, uint32_t handler_id
,
2540 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2541 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2543 struct dpif_handler
*handler
;
2545 struct dpif_windows_vport_sock
*sock_pool
;
2548 if (!dpif
->handlers
) {
2552 /* Only one handler is supported currently. */
2553 if (handler_id
>= 1) {
2557 if (handler_id
>= dpif
->n_handlers
) {
2561 handler
= &dpif
->handlers
[handler_id
];
2562 sock_pool
= handler
->vport_sock_pool
;
2564 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
2569 if (++read_tries
> PACKET_RECV_BATCH_SIZE
) {
2573 error
= nl_sock_recv(sock_pool
[i
].nl_sock
, buf
, NULL
, false);
2574 if (error
== ENOBUFS
) {
2575 /* ENOBUFS typically means that we've received so many
2576 * packets that the buffer overflowed. Try again
2577 * immediately because there's almost certainly a packet
2578 * waiting for us. */
2579 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2583 /* XXX: ch->last_poll = time_msec(); */
2585 if (error
== EAGAIN
) {
2591 error
= parse_odp_packet(dpif
, buf
, upcall
, &dp_ifindex
);
2592 if (!error
&& dp_ifindex
== dpif
->dp_ifindex
) {
2604 dpif_netlink_recv__(struct dpif_netlink
*dpif
, uint32_t handler_id
,
2605 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2606 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2608 struct dpif_handler
*handler
;
2611 if (!dpif
->handlers
|| handler_id
>= dpif
->n_handlers
) {
2615 handler
= &dpif
->handlers
[handler_id
];
2616 if (handler
->event_offset
>= handler
->n_events
) {
2619 handler
->event_offset
= handler
->n_events
= 0;
2622 retval
= epoll_wait(handler
->epoll_fd
, handler
->epoll_events
,
2623 dpif
->uc_array_size
, 0);
2624 } while (retval
< 0 && errno
== EINTR
);
2627 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2628 VLOG_WARN_RL(&rl
, "epoll_wait failed (%s)", ovs_strerror(errno
));
2629 } else if (retval
> 0) {
2630 handler
->n_events
= retval
;
2634 while (handler
->event_offset
< handler
->n_events
) {
2635 int idx
= handler
->epoll_events
[handler
->event_offset
].data
.u32
;
2636 struct dpif_channel
*ch
= &dpif
->channels
[idx
];
2638 handler
->event_offset
++;
2644 if (++read_tries
> 50) {
2648 error
= nl_sock_recv(ch
->sock
, buf
, NULL
, false);
2649 if (error
== ENOBUFS
) {
2650 /* ENOBUFS typically means that we've received so many
2651 * packets that the buffer overflowed. Try again
2652 * immediately because there's almost certainly a packet
2653 * waiting for us. */
2654 report_loss(dpif
, ch
, idx
, handler_id
);
2658 ch
->last_poll
= time_msec();
2660 if (error
== EAGAIN
) {
2666 error
= parse_odp_packet(dpif
, buf
, upcall
, &dp_ifindex
);
2667 if (!error
&& dp_ifindex
== dpif
->dp_ifindex
) {
2680 dpif_netlink_recv(struct dpif
*dpif_
, uint32_t handler_id
,
2681 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2683 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2686 fat_rwlock_rdlock(&dpif
->upcall_lock
);
2688 error
= dpif_netlink_recv_windows(dpif
, handler_id
, upcall
, buf
);
2690 error
= dpif_netlink_recv__(dpif
, handler_id
, upcall
, buf
);
2692 fat_rwlock_unlock(&dpif
->upcall_lock
);
2698 dpif_netlink_recv_wait__(struct dpif_netlink
*dpif
, uint32_t handler_id
)
2699 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2703 struct dpif_windows_vport_sock
*sock_pool
=
2704 dpif
->handlers
[handler_id
].vport_sock_pool
;
2706 /* Only one handler is supported currently. */
2707 if (handler_id
>= 1) {
2711 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
2712 nl_sock_wait(sock_pool
[i
].nl_sock
, POLLIN
);
2715 if (dpif
->handlers
&& handler_id
< dpif
->n_handlers
) {
2716 struct dpif_handler
*handler
= &dpif
->handlers
[handler_id
];
2718 poll_fd_wait(handler
->epoll_fd
, POLLIN
);
2724 dpif_netlink_recv_wait(struct dpif
*dpif_
, uint32_t handler_id
)
2726 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2728 fat_rwlock_rdlock(&dpif
->upcall_lock
);
2729 dpif_netlink_recv_wait__(dpif
, handler_id
);
2730 fat_rwlock_unlock(&dpif
->upcall_lock
);
2734 dpif_netlink_recv_purge__(struct dpif_netlink
*dpif
)
2735 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2737 if (dpif
->handlers
) {
2740 if (!dpif
->channels
[0].sock
) {
2743 for (i
= 0; i
< dpif
->uc_array_size
; i
++ ) {
2745 nl_sock_drain(dpif
->channels
[i
].sock
);
2751 dpif_netlink_recv_purge(struct dpif
*dpif_
)
2753 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2755 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2756 dpif_netlink_recv_purge__(dpif
);
2757 fat_rwlock_unlock(&dpif
->upcall_lock
);
2761 dpif_netlink_get_datapath_version(void)
2763 char *version_str
= NULL
;
2767 #define MAX_VERSION_STR_SIZE 80
2768 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2771 f
= fopen(LINUX_DATAPATH_VERSION_FILE
, "r");
2774 char version
[MAX_VERSION_STR_SIZE
];
2776 if (fgets(version
, MAX_VERSION_STR_SIZE
, f
)) {
2777 newline
= strchr(version
, '\n');
2781 version_str
= xstrdup(version
);
2790 struct dpif_netlink_ct_dump_state
{
2791 struct ct_dpif_dump_state up
;
2792 struct nl_ct_dump_state
*nl_ct_dump
;
2796 dpif_netlink_ct_dump_start(struct dpif
*dpif OVS_UNUSED
,
2797 struct ct_dpif_dump_state
**dump_
,
2798 const uint16_t *zone
, int *ptot_bkts
)
2800 struct dpif_netlink_ct_dump_state
*dump
;
2803 dump
= xzalloc(sizeof *dump
);
2804 err
= nl_ct_dump_start(&dump
->nl_ct_dump
, zone
, ptot_bkts
);
2816 dpif_netlink_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
2817 struct ct_dpif_dump_state
*dump_
,
2818 struct ct_dpif_entry
*entry
)
2820 struct dpif_netlink_ct_dump_state
*dump
;
2822 INIT_CONTAINER(dump
, dump_
, up
);
2824 return nl_ct_dump_next(dump
->nl_ct_dump
, entry
);
2828 dpif_netlink_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
2829 struct ct_dpif_dump_state
*dump_
)
2831 struct dpif_netlink_ct_dump_state
*dump
;
2833 INIT_CONTAINER(dump
, dump_
, up
);
2835 int err
= nl_ct_dump_done(dump
->nl_ct_dump
);
2841 dpif_netlink_ct_flush(struct dpif
*dpif OVS_UNUSED
, const uint16_t *zone
,
2842 const struct ct_dpif_tuple
*tuple
)
2845 return nl_ct_flush_tuple(tuple
, zone
? *zone
: 0);
2847 return nl_ct_flush_zone(*zone
);
2849 return nl_ct_flush();
2854 dpif_netlink_ct_set_limits(struct dpif
*dpif OVS_UNUSED
,
2855 const uint32_t *default_limits
,
2856 const struct ovs_list
*zone_limits
)
2858 struct ovs_zone_limit req_zone_limit
;
2860 if (ovs_ct_limit_family
< 0) {
2864 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
2865 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
2866 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_SET
,
2867 OVS_CT_LIMIT_VERSION
);
2869 struct ovs_header
*ovs_header
;
2870 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
2871 ovs_header
->dp_ifindex
= 0;
2874 opt_offset
= nl_msg_start_nested(request
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2875 if (default_limits
) {
2876 req_zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2877 req_zone_limit
.limit
= *default_limits
;
2878 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2881 if (!ovs_list_is_empty(zone_limits
)) {
2882 struct ct_dpif_zone_limit
*zone_limit
;
2884 LIST_FOR_EACH (zone_limit
, node
, zone_limits
) {
2885 req_zone_limit
.zone_id
= zone_limit
->zone
;
2886 req_zone_limit
.limit
= zone_limit
->limit
;
2887 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2890 nl_msg_end_nested(request
, opt_offset
);
2892 int err
= nl_transact(NETLINK_GENERIC
, request
, NULL
);
2893 ofpbuf_delete(request
);
2898 dpif_netlink_zone_limits_from_ofpbuf(const struct ofpbuf
*buf
,
2899 uint32_t *default_limit
,
2900 struct ovs_list
*zone_limits
)
2902 static const struct nl_policy ovs_ct_limit_policy
[] = {
2903 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NL_A_NESTED
,
2907 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
2908 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
2909 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
2910 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
2912 struct nlattr
*attr
[ARRAY_SIZE(ovs_ct_limit_policy
)];
2914 if (!nlmsg
|| !genl
|| !ovs_header
2915 || nlmsg
->nlmsg_type
!= ovs_ct_limit_family
2916 || !nl_policy_parse(&b
, 0, ovs_ct_limit_policy
, attr
,
2917 ARRAY_SIZE(ovs_ct_limit_policy
))) {
2922 if (!attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2926 int rem
= NLA_ALIGN(
2927 nl_attr_get_size(attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]));
2928 const struct ovs_zone_limit
*zone_limit
=
2929 nl_attr_get(attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]);
2931 while (rem
>= sizeof *zone_limit
) {
2932 if (zone_limit
->zone_id
== OVS_ZONE_LIMIT_DEFAULT_ZONE
) {
2933 *default_limit
= zone_limit
->limit
;
2934 } else if (zone_limit
->zone_id
< OVS_ZONE_LIMIT_DEFAULT_ZONE
||
2935 zone_limit
->zone_id
> UINT16_MAX
) {
2937 ct_dpif_push_zone_limit(zone_limits
, zone_limit
->zone_id
,
2938 zone_limit
->limit
, zone_limit
->count
);
2940 rem
-= NLA_ALIGN(sizeof *zone_limit
);
2941 zone_limit
= ALIGNED_CAST(struct ovs_zone_limit
*,
2942 (unsigned char *) zone_limit
+ NLA_ALIGN(sizeof *zone_limit
));
2948 dpif_netlink_ct_get_limits(struct dpif
*dpif OVS_UNUSED
,
2949 uint32_t *default_limit
,
2950 const struct ovs_list
*zone_limits_request
,
2951 struct ovs_list
*zone_limits_reply
)
2953 if (ovs_ct_limit_family
< 0) {
2957 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
2958 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
2959 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_GET
,
2960 OVS_CT_LIMIT_VERSION
);
2962 struct ovs_header
*ovs_header
;
2963 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
2964 ovs_header
->dp_ifindex
= 0;
2966 if (!ovs_list_is_empty(zone_limits_request
)) {
2967 size_t opt_offset
= nl_msg_start_nested(request
,
2968 OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2970 struct ovs_zone_limit req_zone_limit
;
2971 req_zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2972 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2974 struct ct_dpif_zone_limit
*zone_limit
;
2975 LIST_FOR_EACH (zone_limit
, node
, zone_limits_request
) {
2976 req_zone_limit
.zone_id
= zone_limit
->zone
;
2977 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2980 nl_msg_end_nested(request
, opt_offset
);
2983 struct ofpbuf
*reply
;
2984 int err
= nl_transact(NETLINK_GENERIC
, request
, &reply
);
2989 err
= dpif_netlink_zone_limits_from_ofpbuf(reply
, default_limit
,
2993 ofpbuf_delete(request
);
2994 ofpbuf_delete(reply
);
2999 dpif_netlink_ct_del_limits(struct dpif
*dpif OVS_UNUSED
,
3000 const struct ovs_list
*zone_limits
)
3002 if (ovs_ct_limit_family
< 0) {
3006 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
3007 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
3008 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_DEL
,
3009 OVS_CT_LIMIT_VERSION
);
3011 struct ovs_header
*ovs_header
;
3012 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
3013 ovs_header
->dp_ifindex
= 0;
3015 if (!ovs_list_is_empty(zone_limits
)) {
3017 nl_msg_start_nested(request
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
3019 struct ct_dpif_zone_limit
*zone_limit
;
3020 LIST_FOR_EACH (zone_limit
, node
, zone_limits
) {
3021 struct ovs_zone_limit req_zone_limit
;
3022 req_zone_limit
.zone_id
= zone_limit
->zone
;
3023 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
3025 nl_msg_end_nested(request
, opt_offset
);
3028 int err
= nl_transact(NETLINK_GENERIC
, request
, NULL
);
3030 ofpbuf_delete(request
);
3034 #define NL_TP_NAME_PREFIX "ovs_tp_"
3036 struct dpif_netlink_timeout_policy_protocol
{
3041 enum OVS_PACKED_ENUM dpif_netlink_support_timeout_policy_protocol
{
3042 DPIF_NL_TP_AF_INET_TCP
,
3043 DPIF_NL_TP_AF_INET_UDP
,
3044 DPIF_NL_TP_AF_INET_ICMP
,
3045 DPIF_NL_TP_AF_INET6_TCP
,
3046 DPIF_NL_TP_AF_INET6_UDP
,
3047 DPIF_NL_TP_AF_INET6_ICMPV6
,
3051 #define DPIF_NL_ALL_TP ((1UL << DPIF_NL_TP_MAX) - 1)
3054 static struct dpif_netlink_timeout_policy_protocol tp_protos
[] = {
3055 [DPIF_NL_TP_AF_INET_TCP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_TCP
},
3056 [DPIF_NL_TP_AF_INET_UDP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_UDP
},
3057 [DPIF_NL_TP_AF_INET_ICMP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_ICMP
},
3058 [DPIF_NL_TP_AF_INET6_TCP
] = { .l3num
= AF_INET6
, .l4num
= IPPROTO_TCP
},
3059 [DPIF_NL_TP_AF_INET6_UDP
] = { .l3num
= AF_INET6
, .l4num
= IPPROTO_UDP
},
3060 [DPIF_NL_TP_AF_INET6_ICMPV6
] = { .l3num
= AF_INET6
,
3061 .l4num
= IPPROTO_ICMPV6
},
3065 dpif_netlink_format_tp_name(uint32_t id
, uint16_t l3num
, uint8_t l4num
,
3068 struct ds ds
= DS_EMPTY_INITIALIZER
;
3069 ds_put_format(&ds
, "%s%"PRIu32
"_", NL_TP_NAME_PREFIX
, id
);
3070 ct_dpif_format_ipproto(&ds
, l4num
);
3072 if (l3num
== AF_INET
) {
3073 ds_put_cstr(&ds
, "4");
3074 } else if (l3num
== AF_INET6
&& l4num
!= IPPROTO_ICMPV6
) {
3075 ds_put_cstr(&ds
, "6");
3078 ovs_assert(ds
.length
< CTNL_TIMEOUT_NAME_MAX
);
3080 *tp_name
= ds_steal_cstr(&ds
);
3084 dpif_netlink_ct_get_timeout_policy_name(struct dpif
*dpif OVS_UNUSED
,
3085 uint32_t tp_id
, uint16_t dl_type
,
3086 uint8_t nw_proto
, char **tp_name
,
3089 dpif_netlink_format_tp_name(tp_id
,
3090 dl_type
== ETH_TYPE_IP
? AF_INET
: AF_INET6
,
3092 *is_generic
= false;
3096 #define CT_DPIF_NL_TP_TCP_MAPPINGS \
3097 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT, SYN_SENT) \
3098 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_RECV, SYN_RECV) \
3099 CT_DPIF_NL_TP_MAPPING(TCP, TCP, ESTABLISHED, ESTABLISHED) \
3100 CT_DPIF_NL_TP_MAPPING(TCP, TCP, FIN_WAIT, FIN_WAIT) \
3101 CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE_WAIT, CLOSE_WAIT) \
3102 CT_DPIF_NL_TP_MAPPING(TCP, TCP, LAST_ACK, LAST_ACK) \
3103 CT_DPIF_NL_TP_MAPPING(TCP, TCP, TIME_WAIT, TIME_WAIT) \
3104 CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE, CLOSE) \
3105 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT2, SYN_SENT2) \
3106 CT_DPIF_NL_TP_MAPPING(TCP, TCP, RETRANSMIT, RETRANS) \
3107 CT_DPIF_NL_TP_MAPPING(TCP, TCP, UNACK, UNACK)
3109 #define CT_DPIF_NL_TP_UDP_MAPPINGS \
3110 CT_DPIF_NL_TP_MAPPING(UDP, UDP, SINGLE, UNREPLIED) \
3111 CT_DPIF_NL_TP_MAPPING(UDP, UDP, MULTIPLE, REPLIED)
3113 #define CT_DPIF_NL_TP_ICMP_MAPPINGS \
3114 CT_DPIF_NL_TP_MAPPING(ICMP, ICMP, FIRST, TIMEOUT)
3116 #define CT_DPIF_NL_TP_ICMPV6_MAPPINGS \
3117 CT_DPIF_NL_TP_MAPPING(ICMP, ICMPV6, FIRST, TIMEOUT)
3120 #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \
3121 if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \
3122 nl_tp->present |= 1 << CTA_TIMEOUT_##PROTO2##_##ATTR2; \
3123 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2] = \
3124 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]; \
3128 dpif_netlink_get_nl_tp_tcp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3129 struct nl_ct_timeout_policy
*nl_tp
)
3131 CT_DPIF_NL_TP_TCP_MAPPINGS
3135 dpif_netlink_get_nl_tp_udp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3136 struct nl_ct_timeout_policy
*nl_tp
)
3138 CT_DPIF_NL_TP_UDP_MAPPINGS
3142 dpif_netlink_get_nl_tp_icmp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3143 struct nl_ct_timeout_policy
*nl_tp
)
3145 CT_DPIF_NL_TP_ICMP_MAPPINGS
3149 dpif_netlink_get_nl_tp_icmpv6_attrs(const struct ct_dpif_timeout_policy
*tp
,
3150 struct nl_ct_timeout_policy
*nl_tp
)
3152 CT_DPIF_NL_TP_ICMPV6_MAPPINGS
3155 #undef CT_DPIF_NL_TP_MAPPING
3158 dpif_netlink_get_nl_tp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3159 uint8_t l4num
, struct nl_ct_timeout_policy
*nl_tp
)
3163 if (l4num
== IPPROTO_TCP
) {
3164 dpif_netlink_get_nl_tp_tcp_attrs(tp
, nl_tp
);
3165 } else if (l4num
== IPPROTO_UDP
) {
3166 dpif_netlink_get_nl_tp_udp_attrs(tp
, nl_tp
);
3167 } else if (l4num
== IPPROTO_ICMP
) {
3168 dpif_netlink_get_nl_tp_icmp_attrs(tp
, nl_tp
);
3169 } else if (l4num
== IPPROTO_ICMPV6
) {
3170 dpif_netlink_get_nl_tp_icmpv6_attrs(tp
, nl_tp
);
3174 #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \
3175 if (nl_tp->present & (1 << CTA_TIMEOUT_##PROTO2##_##ATTR2)) { \
3176 if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \
3177 if (tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] != \
3178 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]) { \
3179 VLOG_WARN_RL(&error_rl, "Inconsistent timeout policy %s " \
3180 "attribute %s=%"PRIu32" while %s=%"PRIu32, \
3181 nl_tp->name, "CTA_TIMEOUT_"#PROTO2"_"#ATTR2, \
3182 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2], \
3183 "CT_DPIF_TP_ATTR_"#PROTO1"_"#ATTR1, \
3184 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]); \
3187 tp->present |= 1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1; \
3188 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] = \
3189 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]; \
3194 dpif_netlink_set_ct_dpif_tp_tcp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3195 struct ct_dpif_timeout_policy
*tp
)
3197 CT_DPIF_NL_TP_TCP_MAPPINGS
3201 dpif_netlink_set_ct_dpif_tp_udp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3202 struct ct_dpif_timeout_policy
*tp
)
3204 CT_DPIF_NL_TP_UDP_MAPPINGS
3208 dpif_netlink_set_ct_dpif_tp_icmp_attrs(
3209 const struct nl_ct_timeout_policy
*nl_tp
,
3210 struct ct_dpif_timeout_policy
*tp
)
3212 CT_DPIF_NL_TP_ICMP_MAPPINGS
3216 dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(
3217 const struct nl_ct_timeout_policy
*nl_tp
,
3218 struct ct_dpif_timeout_policy
*tp
)
3220 CT_DPIF_NL_TP_ICMPV6_MAPPINGS
3223 #undef CT_DPIF_NL_TP_MAPPING
3226 dpif_netlink_set_ct_dpif_tp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3227 struct ct_dpif_timeout_policy
*tp
)
3229 if (nl_tp
->l4num
== IPPROTO_TCP
) {
3230 dpif_netlink_set_ct_dpif_tp_tcp_attrs(nl_tp
, tp
);
3231 } else if (nl_tp
->l4num
== IPPROTO_UDP
) {
3232 dpif_netlink_set_ct_dpif_tp_udp_attrs(nl_tp
, tp
);
3233 } else if (nl_tp
->l4num
== IPPROTO_ICMP
) {
3234 dpif_netlink_set_ct_dpif_tp_icmp_attrs(nl_tp
, tp
);
3235 } else if (nl_tp
->l4num
== IPPROTO_ICMPV6
) {
3236 dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(nl_tp
, tp
);
3242 dpif_netlink_ct_set_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3243 const struct ct_dpif_timeout_policy
*tp
)
3249 dpif_netlink_ct_get_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3251 struct ct_dpif_timeout_policy
*tp
)
3257 dpif_netlink_ct_del_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3264 dpif_netlink_ct_timeout_policy_dump_start(struct dpif
*dpif OVS_UNUSED
,
3271 dpif_netlink_ct_timeout_policy_dump_next(struct dpif
*dpif OVS_UNUSED
,
3273 struct ct_dpif_timeout_policy
**tp
)
3279 dpif_netlink_ct_timeout_policy_dump_done(struct dpif
*dpif OVS_UNUSED
,
3286 dpif_netlink_ct_set_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3287 const struct ct_dpif_timeout_policy
*tp
)
3291 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3292 struct nl_ct_timeout_policy nl_tp
;
3295 dpif_netlink_format_tp_name(tp
->id
, tp_protos
[i
].l3num
,
3296 tp_protos
[i
].l4num
, &nl_tp_name
);
3297 ovs_strlcpy(nl_tp
.name
, nl_tp_name
, sizeof nl_tp
.name
);
3300 nl_tp
.l3num
= tp_protos
[i
].l3num
;
3301 nl_tp
.l4num
= tp_protos
[i
].l4num
;
3302 dpif_netlink_get_nl_tp_attrs(tp
, tp_protos
[i
].l4num
, &nl_tp
);
3303 err
= nl_ct_set_timeout_policy(&nl_tp
);
3305 VLOG_WARN_RL(&error_rl
, "failed to add timeout policy %s (%s)",
3306 nl_tp
.name
, ovs_strerror(err
));
3316 dpif_netlink_ct_get_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3318 struct ct_dpif_timeout_policy
*tp
)
3324 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3325 struct nl_ct_timeout_policy nl_tp
;
3328 dpif_netlink_format_tp_name(tp_id
, tp_protos
[i
].l3num
,
3329 tp_protos
[i
].l4num
, &nl_tp_name
);
3330 err
= nl_ct_get_timeout_policy(nl_tp_name
, &nl_tp
);
3333 VLOG_WARN_RL(&error_rl
, "failed to get timeout policy %s (%s)",
3334 nl_tp_name
, ovs_strerror(err
));
3339 dpif_netlink_set_ct_dpif_tp_attrs(&nl_tp
, tp
);
3346 /* Returns 0 if all the sub timeout policies are deleted or not exist in the
3347 * kernel. Returns 1 if any sub timeout policy deletion failed. */
3349 dpif_netlink_ct_del_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3354 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3356 dpif_netlink_format_tp_name(tp_id
, tp_protos
[i
].l3num
,
3357 tp_protos
[i
].l4num
, &nl_tp_name
);
3358 int err
= nl_ct_del_timeout_policy(nl_tp_name
);
3359 if (err
== ENOENT
) {
3363 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(6, 6);
3364 VLOG_INFO_RL(&rl
, "failed to delete timeout policy %s (%s)",
3365 nl_tp_name
, ovs_strerror(err
));
3374 struct dpif_netlink_ct_timeout_policy_dump_state
{
3375 struct nl_ct_timeout_policy_dump_state
*nl_dump_state
;
3376 struct hmap tp_dump_map
;
3379 struct dpif_netlink_tp_dump_node
{
3380 struct hmap_node hmap_node
; /* node in tp_dump_map. */
3381 struct ct_dpif_timeout_policy
*tp
;
3382 uint32_t l3_l4_present
;
3385 static struct dpif_netlink_tp_dump_node
*
3386 get_dpif_netlink_tp_dump_node_by_tp_id(uint32_t tp_id
,
3387 struct hmap
*tp_dump_map
)
3389 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3391 HMAP_FOR_EACH_WITH_HASH (tp_dump_node
, hmap_node
, hash_int(tp_id
, 0),
3393 if (tp_dump_node
->tp
->id
== tp_id
) {
3394 return tp_dump_node
;
3401 update_dpif_netlink_tp_dump_node(
3402 const struct nl_ct_timeout_policy
*nl_tp
,
3403 struct dpif_netlink_tp_dump_node
*tp_dump_node
)
3405 dpif_netlink_set_ct_dpif_tp_attrs(nl_tp
, tp_dump_node
->tp
);
3406 for (int i
= 0; i
< DPIF_NL_TP_MAX
; ++i
) {
3407 if (nl_tp
->l3num
== tp_protos
[i
].l3num
&&
3408 nl_tp
->l4num
== tp_protos
[i
].l4num
) {
3409 tp_dump_node
->l3_l4_present
|= 1 << i
;
3416 dpif_netlink_ct_timeout_policy_dump_start(struct dpif
*dpif OVS_UNUSED
,
3419 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
;
3421 *statep
= dump_state
= xzalloc(sizeof *dump_state
);
3422 int err
= nl_ct_timeout_policy_dump_start(&dump_state
->nl_dump_state
);
3427 hmap_init(&dump_state
->tp_dump_map
);
3432 get_and_cleanup_tp_dump_node(struct hmap
*hmap
,
3433 struct dpif_netlink_tp_dump_node
*tp_dump_node
,
3434 struct ct_dpif_timeout_policy
*tp
)
3436 hmap_remove(hmap
, &tp_dump_node
->hmap_node
);
3437 *tp
= *tp_dump_node
->tp
;
3438 free(tp_dump_node
->tp
);
3443 dpif_netlink_ct_timeout_policy_dump_next(struct dpif
*dpif OVS_UNUSED
,
3445 struct ct_dpif_timeout_policy
*tp
)
3447 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
= state
;
3448 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3451 /* Dumps all the timeout policies in the kernel. */
3453 struct nl_ct_timeout_policy nl_tp
;
3456 err
= nl_ct_timeout_policy_dump_next(dump_state
->nl_dump_state
,
3462 /* We only interest in OVS installed timeout policies. */
3463 if (!ovs_scan(nl_tp
.name
, NL_TP_NAME_PREFIX
"%"PRIu32
, &tp_id
)) {
3467 tp_dump_node
= get_dpif_netlink_tp_dump_node_by_tp_id(
3468 tp_id
, &dump_state
->tp_dump_map
);
3469 if (!tp_dump_node
) {
3470 tp_dump_node
= xzalloc(sizeof *tp_dump_node
);
3471 tp_dump_node
->tp
= xzalloc(sizeof *tp_dump_node
->tp
);
3472 tp_dump_node
->tp
->id
= tp_id
;
3473 hmap_insert(&dump_state
->tp_dump_map
, &tp_dump_node
->hmap_node
,
3474 hash_int(tp_id
, 0));
3477 update_dpif_netlink_tp_dump_node(&nl_tp
, tp_dump_node
);
3479 /* Returns one ct_dpif_timeout_policy if we gather all the L3/L4
3481 if (tp_dump_node
->l3_l4_present
== DPIF_NL_ALL_TP
) {
3482 get_and_cleanup_tp_dump_node(&dump_state
->tp_dump_map
,
3488 /* Dump the incomplete timeout policies. */
3490 if (!hmap_is_empty(&dump_state
->tp_dump_map
)) {
3491 struct hmap_node
*hmap_node
= hmap_first(&dump_state
->tp_dump_map
);
3492 tp_dump_node
= CONTAINER_OF(hmap_node
,
3493 struct dpif_netlink_tp_dump_node
,
3495 get_and_cleanup_tp_dump_node(&dump_state
->tp_dump_map
,
3505 dpif_netlink_ct_timeout_policy_dump_done(struct dpif
*dpif OVS_UNUSED
,
3508 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
= state
;
3509 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3511 int err
= nl_ct_timeout_policy_dump_done(dump_state
->nl_dump_state
);
3512 HMAP_FOR_EACH_POP (tp_dump_node
, hmap_node
, &dump_state
->tp_dump_map
) {
3513 free(tp_dump_node
->tp
);
3516 hmap_destroy(&dump_state
->tp_dump_map
);
3525 /* Set of supported meter flags */
3526 #define DP_SUPPORTED_METER_FLAGS_MASK \
3527 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
3529 /* Meter support was introduced in Linux 4.15. In some versions of
3530 * Linux 4.15, 4.16, and 4.17, there was a bug that never set the id
3531 * when the meter was created, so all meters essentially had an id of
3532 * zero. Check for that condition and disable meters on those kernels. */
3533 static bool probe_broken_meters(struct dpif
*);
3536 dpif_netlink_meter_init(struct dpif_netlink
*dpif
, struct ofpbuf
*buf
,
3537 void *stub
, size_t size
, uint32_t command
)
3539 ofpbuf_use_stub(buf
, stub
, size
);
3541 nl_msg_put_genlmsghdr(buf
, 0, ovs_meter_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
3542 command
, OVS_METER_VERSION
);
3544 struct ovs_header
*ovs_header
;
3545 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
3546 ovs_header
->dp_ifindex
= dpif
->dp_ifindex
;
3549 /* Execute meter 'request' in the kernel datapath. If the command
3550 * fails, returns a positive errno value. Otherwise, stores the reply
3551 * in '*replyp', parses the policy according to 'reply_policy' into the
3552 * array of Netlink attribute in 'a', and returns 0. On success, the
3553 * caller is responsible for calling ofpbuf_delete() on '*replyp'
3554 * ('replyp' will contain pointers into 'a'). */
3556 dpif_netlink_meter_transact(struct ofpbuf
*request
, struct ofpbuf
**replyp
,
3557 const struct nl_policy
*reply_policy
,
3558 struct nlattr
**a
, size_t size_a
)
3560 int error
= nl_transact(NETLINK_GENERIC
, request
, replyp
);
3561 ofpbuf_uninit(request
);
3567 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(*replyp
, sizeof *nlmsg
);
3568 struct genlmsghdr
*genl
= ofpbuf_try_pull(*replyp
, sizeof *genl
);
3569 struct ovs_header
*ovs_header
= ofpbuf_try_pull(*replyp
,
3570 sizeof *ovs_header
);
3571 if (!nlmsg
|| !genl
|| !ovs_header
3572 || nlmsg
->nlmsg_type
!= ovs_meter_family
3573 || !nl_policy_parse(*replyp
, 0, reply_policy
, a
, size_a
)) {
3574 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3576 "Kernel module response to meter tranaction is invalid");
3583 dpif_netlink_meter_get_features(const struct dpif
*dpif_
,
3584 struct ofputil_meter_features
*features
)
3586 if (probe_broken_meters(CONST_CAST(struct dpif
*, dpif_
))) {
3591 struct ofpbuf buf
, *msg
;
3592 uint64_t stub
[1024 / 8];
3594 static const struct nl_policy ovs_meter_features_policy
[] = {
3595 [OVS_METER_ATTR_MAX_METERS
] = { .type
= NL_A_U32
},
3596 [OVS_METER_ATTR_MAX_BANDS
] = { .type
= NL_A_U32
},
3597 [OVS_METER_ATTR_BANDS
] = { .type
= NL_A_NESTED
, .optional
= true },
3599 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_features_policy
)];
3601 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3602 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
,
3603 OVS_METER_CMD_FEATURES
);
3604 if (dpif_netlink_meter_transact(&buf
, &msg
, ovs_meter_features_policy
, a
,
3605 ARRAY_SIZE(ovs_meter_features_policy
))) {
3606 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3608 "dpif_netlink_meter_transact OVS_METER_CMD_FEATURES failed");
3612 features
->max_meters
= nl_attr_get_u32(a
[OVS_METER_ATTR_MAX_METERS
]);
3613 features
->max_bands
= nl_attr_get_u32(a
[OVS_METER_ATTR_MAX_BANDS
]);
3615 /* Bands is a nested attribute of zero or more nested
3616 * band attributes. */
3617 if (a
[OVS_METER_ATTR_BANDS
]) {
3618 const struct nlattr
*nla
;
3621 NL_NESTED_FOR_EACH (nla
, left
, a
[OVS_METER_ATTR_BANDS
]) {
3622 const struct nlattr
*band_nla
;
3625 NL_NESTED_FOR_EACH (band_nla
, band_left
, nla
) {
3626 if (nl_attr_type(band_nla
) == OVS_BAND_ATTR_TYPE
) {
3627 if (nl_attr_get_size(band_nla
) == sizeof(uint32_t)) {
3628 switch (nl_attr_get_u32(band_nla
)) {
3629 case OVS_METER_BAND_TYPE_DROP
:
3630 features
->band_types
|= 1 << OFPMBT13_DROP
;
3638 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
3644 dpif_netlink_meter_set__(struct dpif
*dpif_
, ofproto_meter_id meter_id
,
3645 struct ofputil_meter_config
*config
)
3647 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3648 struct ofpbuf buf
, *msg
;
3649 uint64_t stub
[1024 / 8];
3651 static const struct nl_policy ovs_meter_set_response_policy
[] = {
3652 [OVS_METER_ATTR_ID
] = { .type
= NL_A_U32
},
3654 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_set_response_policy
)];
3656 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
) {
3657 return EBADF
; /* Unsupported flags set */
3660 for (size_t i
= 0; i
< config
->n_bands
; i
++) {
3661 switch (config
->bands
[i
].type
) {
3665 return ENODEV
; /* Unsupported band type */
3669 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
, OVS_METER_CMD_SET
);
3671 nl_msg_put_u32(&buf
, OVS_METER_ATTR_ID
, meter_id
.uint32
);
3673 if (config
->flags
& OFPMF13_KBPS
) {
3674 nl_msg_put_flag(&buf
, OVS_METER_ATTR_KBPS
);
3677 size_t bands_offset
= nl_msg_start_nested(&buf
, OVS_METER_ATTR_BANDS
);
3679 for (size_t i
= 0; i
< config
->n_bands
; ++i
) {
3680 struct ofputil_meter_band
* band
= &config
->bands
[i
];
3683 size_t band_offset
= nl_msg_start_nested(&buf
, OVS_BAND_ATTR_UNSPEC
);
3685 switch (band
->type
) {
3687 band_type
= OVS_METER_BAND_TYPE_DROP
;
3690 band_type
= OVS_METER_BAND_TYPE_UNSPEC
;
3692 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_TYPE
, band_type
);
3693 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_RATE
, band
->rate
);
3694 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_BURST
,
3695 config
->flags
& OFPMF13_BURST
?
3696 band
->burst_size
: band
->rate
);
3697 nl_msg_end_nested(&buf
, band_offset
);
3699 nl_msg_end_nested(&buf
, bands_offset
);
3701 int error
= dpif_netlink_meter_transact(&buf
, &msg
,
3702 ovs_meter_set_response_policy
, a
,
3703 ARRAY_SIZE(ovs_meter_set_response_policy
));
3705 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3707 "dpif_netlink_meter_transact OVS_METER_CMD_SET failed");
3711 if (nl_attr_get_u32(a
[OVS_METER_ATTR_ID
]) != meter_id
.uint32
) {
3712 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3714 "Kernel returned a different meter id than requested");
3721 dpif_netlink_meter_set(struct dpif
*dpif_
, ofproto_meter_id meter_id
,
3722 struct ofputil_meter_config
*config
)
3724 if (probe_broken_meters(dpif_
)) {
3728 return dpif_netlink_meter_set__(dpif_
, meter_id
, config
);
3731 /* Retrieve statistics and/or delete meter 'meter_id'. Statistics are
3732 * stored in 'stats', if it is not null. If 'command' is
3733 * OVS_METER_CMD_DEL, the meter is deleted and statistics are optionally
3734 * retrieved. If 'command' is OVS_METER_CMD_GET, then statistics are
3735 * simply retrieved. */
3737 dpif_netlink_meter_get_stats(const struct dpif
*dpif_
,
3738 ofproto_meter_id meter_id
,
3739 struct ofputil_meter_stats
*stats
,
3741 enum ovs_meter_cmd command
)
3743 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3744 struct ofpbuf buf
, *msg
;
3745 uint64_t stub
[1024 / 8];
3747 static const struct nl_policy ovs_meter_stats_policy
[] = {
3748 [OVS_METER_ATTR_ID
] = { .type
= NL_A_U32
, .optional
= true},
3749 [OVS_METER_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_flow_stats
),
3751 [OVS_METER_ATTR_BANDS
] = { .type
= NL_A_NESTED
, .optional
= true },
3753 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_stats_policy
)];
3755 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
, command
);
3757 nl_msg_put_u32(&buf
, OVS_METER_ATTR_ID
, meter_id
.uint32
);
3759 int error
= dpif_netlink_meter_transact(&buf
, &msg
,
3760 ovs_meter_stats_policy
, a
,
3761 ARRAY_SIZE(ovs_meter_stats_policy
));
3763 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3764 VLOG_INFO_RL(&rl
, "dpif_netlink_meter_transact %s failed",
3765 command
== OVS_METER_CMD_GET
? "get" : "del");
3770 && a
[OVS_METER_ATTR_ID
]
3771 && a
[OVS_METER_ATTR_STATS
]
3772 && nl_attr_get_u32(a
[OVS_METER_ATTR_ID
]) == meter_id
.uint32
) {
3774 const struct ovs_flow_stats
*stat
;
3775 const struct nlattr
*nla
;
3778 stat
= nl_attr_get(a
[OVS_METER_ATTR_STATS
]);
3779 stats
->packet_in_count
= get_32aligned_u64(&stat
->n_packets
);
3780 stats
->byte_in_count
= get_32aligned_u64(&stat
->n_bytes
);
3782 if (a
[OVS_METER_ATTR_BANDS
]) {
3784 NL_NESTED_FOR_EACH (nla
, left
, a
[OVS_METER_ATTR_BANDS
]) {
3785 const struct nlattr
*band_nla
;
3786 band_nla
= nl_attr_find_nested(nla
, OVS_BAND_ATTR_STATS
);
3787 if (band_nla
&& nl_attr_get_size(band_nla
) \
3788 == sizeof(struct ovs_flow_stats
)) {
3789 stat
= nl_attr_get(band_nla
);
3791 if (n_bands
< max_bands
) {
3792 stats
->bands
[n_bands
].packet_count
3793 = get_32aligned_u64(&stat
->n_packets
);
3794 stats
->bands
[n_bands
].byte_count
3795 = get_32aligned_u64(&stat
->n_bytes
);
3799 stats
->bands
[n_bands
].packet_count
= 0;
3800 stats
->bands
[n_bands
].byte_count
= 0;
3804 stats
->n_bands
= n_bands
;
3806 /* For a non-existent meter, return 0 stats. */
3816 dpif_netlink_meter_get(const struct dpif
*dpif
, ofproto_meter_id meter_id
,
3817 struct ofputil_meter_stats
*stats
, uint16_t max_bands
)
3819 return dpif_netlink_meter_get_stats(dpif
, meter_id
, stats
, max_bands
,
3824 dpif_netlink_meter_del(struct dpif
*dpif
, ofproto_meter_id meter_id
,
3825 struct ofputil_meter_stats
*stats
, uint16_t max_bands
)
3827 return dpif_netlink_meter_get_stats(dpif
, meter_id
, stats
, max_bands
,
3832 probe_broken_meters__(struct dpif
*dpif
)
3834 /* This test is destructive if a probe occurs while ovs-vswitchd is
3835 * running (e.g., an ovs-dpctl meter command is called), so choose a
3836 * random high meter id to make this less likely to occur. */
3837 ofproto_meter_id id1
= { 54545401 };
3838 ofproto_meter_id id2
= { 54545402 };
3839 struct ofputil_meter_band band
= {OFPMBT13_DROP
, 0, 1, 0};
3840 struct ofputil_meter_config config1
= { 1, OFPMF13_KBPS
, 1, &band
};
3841 struct ofputil_meter_config config2
= { 2, OFPMF13_KBPS
, 1, &band
};
3843 /* Try adding two meters and make sure that they both come back with
3844 * the proper meter id. Use the "__" version so that we don't cause
3845 * a recurve deadlock. */
3846 dpif_netlink_meter_set__(dpif
, id1
, &config1
);
3847 dpif_netlink_meter_set__(dpif
, id2
, &config2
);
3849 if (dpif_netlink_meter_get(dpif
, id1
, NULL
, 0)
3850 || dpif_netlink_meter_get(dpif
, id2
, NULL
, 0)) {
3851 VLOG_INFO("The kernel module has a broken meter implementation.");
3855 dpif_netlink_meter_del(dpif
, id1
, NULL
, 0);
3856 dpif_netlink_meter_del(dpif
, id2
, NULL
, 0);
3862 probe_broken_meters(struct dpif
*dpif
)
3864 /* This is a once-only test because currently OVS only has at most a single
3865 * Netlink capable datapath on any given platform. */
3866 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3868 static bool broken_meters
= false;
3869 if (ovsthread_once_start(&once
)) {
3870 broken_meters
= probe_broken_meters__(dpif
);
3871 ovsthread_once_done(&once
);
3873 return broken_meters
;
3876 const struct dpif_class dpif_netlink_class
= {
3878 false, /* cleanup_required */
3880 dpif_netlink_enumerate
,
3884 dpif_netlink_destroy
,
3887 dpif_netlink_get_stats
,
3888 dpif_netlink_port_add
,
3889 dpif_netlink_port_del
,
3890 NULL
, /* port_set_config */
3891 dpif_netlink_port_query_by_number
,
3892 dpif_netlink_port_query_by_name
,
3893 dpif_netlink_port_get_pid
,
3894 dpif_netlink_port_dump_start
,
3895 dpif_netlink_port_dump_next
,
3896 dpif_netlink_port_dump_done
,
3897 dpif_netlink_port_poll
,
3898 dpif_netlink_port_poll_wait
,
3899 dpif_netlink_flow_flush
,
3900 dpif_netlink_flow_dump_create
,
3901 dpif_netlink_flow_dump_destroy
,
3902 dpif_netlink_flow_dump_thread_create
,
3903 dpif_netlink_flow_dump_thread_destroy
,
3904 dpif_netlink_flow_dump_next
,
3905 dpif_netlink_operate
,
3906 dpif_netlink_recv_set
,
3907 dpif_netlink_handlers_set
,
3908 NULL
, /* set_config */
3909 dpif_netlink_queue_to_priority
,
3911 dpif_netlink_recv_wait
,
3912 dpif_netlink_recv_purge
,
3913 NULL
, /* register_dp_purge_cb */
3914 NULL
, /* register_upcall_cb */
3915 NULL
, /* enable_upcall */
3916 NULL
, /* disable_upcall */
3917 dpif_netlink_get_datapath_version
, /* get_datapath_version */
3918 dpif_netlink_ct_dump_start
,
3919 dpif_netlink_ct_dump_next
,
3920 dpif_netlink_ct_dump_done
,
3921 dpif_netlink_ct_flush
,
3922 NULL
, /* ct_set_maxconns */
3923 NULL
, /* ct_get_maxconns */
3924 NULL
, /* ct_get_nconns */
3925 NULL
, /* ct_set_tcp_seq_chk */
3926 NULL
, /* ct_get_tcp_seq_chk */
3927 dpif_netlink_ct_set_limits
,
3928 dpif_netlink_ct_get_limits
,
3929 dpif_netlink_ct_del_limits
,
3930 dpif_netlink_ct_set_timeout_policy
,
3931 dpif_netlink_ct_get_timeout_policy
,
3932 dpif_netlink_ct_del_timeout_policy
,
3933 dpif_netlink_ct_timeout_policy_dump_start
,
3934 dpif_netlink_ct_timeout_policy_dump_next
,
3935 dpif_netlink_ct_timeout_policy_dump_done
,
3936 dpif_netlink_ct_get_timeout_policy_name
,
3937 NULL
, /* ipf_set_enabled */
3938 NULL
, /* ipf_set_min_frag */
3939 NULL
, /* ipf_set_max_nfrags */
3940 NULL
, /* ipf_get_status */
3941 NULL
, /* ipf_dump_start */
3942 NULL
, /* ipf_dump_next */
3943 NULL
, /* ipf_dump_done */
3944 dpif_netlink_meter_get_features
,
3945 dpif_netlink_meter_set
,
3946 dpif_netlink_meter_get
,
3947 dpif_netlink_meter_del
,
3951 dpif_netlink_init(void)
3953 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3956 if (ovsthread_once_start(&once
)) {
3957 error
= nl_lookup_genl_family(OVS_DATAPATH_FAMILY
,
3958 &ovs_datapath_family
);
3960 VLOG_INFO("Generic Netlink family '%s' does not exist. "
3961 "The Open vSwitch kernel module is probably not loaded.",
3962 OVS_DATAPATH_FAMILY
);
3965 error
= nl_lookup_genl_family(OVS_VPORT_FAMILY
, &ovs_vport_family
);
3968 error
= nl_lookup_genl_family(OVS_FLOW_FAMILY
, &ovs_flow_family
);
3971 error
= nl_lookup_genl_family(OVS_PACKET_FAMILY
,
3972 &ovs_packet_family
);
3975 error
= nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY
, OVS_VPORT_MCGROUP
,
3976 &ovs_vport_mcgroup
);
3979 if (nl_lookup_genl_family(OVS_METER_FAMILY
, &ovs_meter_family
)) {
3980 VLOG_INFO("The kernel module does not support meters.");
3983 if (nl_lookup_genl_family(OVS_CT_LIMIT_FAMILY
,
3984 &ovs_ct_limit_family
) < 0) {
3985 VLOG_INFO("Generic Netlink family '%s' does not exist. "
3986 "Please update the Open vSwitch kernel module to enable "
3987 "the conntrack limit feature.", OVS_CT_LIMIT_FAMILY
);
3990 ovs_tunnels_out_of_tree
= dpif_netlink_rtnl_probe_oot_tunnels();
3992 ovsthread_once_done(&once
);
3999 dpif_netlink_is_internal_device(const char *name
)
4001 struct dpif_netlink_vport reply
;
4005 error
= dpif_netlink_vport_get(name
, &reply
, &buf
);
4008 } else if (error
!= ENODEV
&& error
!= ENOENT
) {
4009 VLOG_WARN_RL(&error_rl
, "%s: vport query failed (%s)",
4010 name
, ovs_strerror(error
));
4013 return reply
.type
== OVS_VPORT_TYPE_INTERNAL
;
4016 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4017 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
4018 * positive errno value.
4020 * 'vport' will contain pointers into 'buf', so the caller should not free
4021 * 'buf' while 'vport' is still in use. */
4023 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport
*vport
,
4024 const struct ofpbuf
*buf
)
4026 static const struct nl_policy ovs_vport_policy
[] = {
4027 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NL_A_U32
},
4028 [OVS_VPORT_ATTR_TYPE
] = { .type
= NL_A_U32
},
4029 [OVS_VPORT_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
4030 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NL_A_UNSPEC
},
4031 [OVS_VPORT_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_vport_stats
),
4033 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
4034 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NL_A_U32
, .optional
= true },
4037 dpif_netlink_vport_init(vport
);
4039 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4040 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4041 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4042 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4044 struct nlattr
*a
[ARRAY_SIZE(ovs_vport_policy
)];
4045 if (!nlmsg
|| !genl
|| !ovs_header
4046 || nlmsg
->nlmsg_type
!= ovs_vport_family
4047 || !nl_policy_parse(&b
, 0, ovs_vport_policy
, a
,
4048 ARRAY_SIZE(ovs_vport_policy
))) {
4052 vport
->cmd
= genl
->cmd
;
4053 vport
->dp_ifindex
= ovs_header
->dp_ifindex
;
4054 vport
->port_no
= nl_attr_get_odp_port(a
[OVS_VPORT_ATTR_PORT_NO
]);
4055 vport
->type
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
4056 vport
->name
= nl_attr_get_string(a
[OVS_VPORT_ATTR_NAME
]);
4057 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
4058 vport
->n_upcall_pids
= nl_attr_get_size(a
[OVS_VPORT_ATTR_UPCALL_PID
])
4059 / (sizeof *vport
->upcall_pids
);
4060 vport
->upcall_pids
= nl_attr_get(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
4063 if (a
[OVS_VPORT_ATTR_STATS
]) {
4064 vport
->stats
= nl_attr_get(a
[OVS_VPORT_ATTR_STATS
]);
4066 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
4067 vport
->options
= nl_attr_get(a
[OVS_VPORT_ATTR_OPTIONS
]);
4068 vport
->options_len
= nl_attr_get_size(a
[OVS_VPORT_ATTR_OPTIONS
]);
4070 if (a
[OVS_VPORT_ATTR_NETNSID
]) {
4071 netnsid_set(&vport
->netnsid
,
4072 nl_attr_get_u32(a
[OVS_VPORT_ATTR_NETNSID
]));
4074 netnsid_set_local(&vport
->netnsid
);
4079 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
4080 * followed by Netlink attributes corresponding to 'vport'. */
4082 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport
*vport
,
4085 struct ovs_header
*ovs_header
;
4087 nl_msg_put_genlmsghdr(buf
, 0, ovs_vport_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
4088 vport
->cmd
, OVS_VPORT_VERSION
);
4090 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4091 ovs_header
->dp_ifindex
= vport
->dp_ifindex
;
4093 if (vport
->port_no
!= ODPP_NONE
) {
4094 nl_msg_put_odp_port(buf
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
);
4097 if (vport
->type
!= OVS_VPORT_TYPE_UNSPEC
) {
4098 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_TYPE
, vport
->type
);
4102 nl_msg_put_string(buf
, OVS_VPORT_ATTR_NAME
, vport
->name
);
4105 if (vport
->upcall_pids
) {
4106 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_UPCALL_PID
,
4108 vport
->n_upcall_pids
* sizeof *vport
->upcall_pids
);
4112 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_STATS
,
4113 vport
->stats
, sizeof *vport
->stats
);
4116 if (vport
->options
) {
4117 nl_msg_put_nested(buf
, OVS_VPORT_ATTR_OPTIONS
,
4118 vport
->options
, vport
->options_len
);
4122 /* Clears 'vport' to "empty" values. */
4124 dpif_netlink_vport_init(struct dpif_netlink_vport
*vport
)
4126 memset(vport
, 0, sizeof *vport
);
4127 vport
->port_no
= ODPP_NONE
;
4130 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4131 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4132 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4133 * result of the command is expected to be an ovs_vport also, which is decoded
4134 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
4135 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
4137 dpif_netlink_vport_transact(const struct dpif_netlink_vport
*request
,
4138 struct dpif_netlink_vport
*reply
,
4139 struct ofpbuf
**bufp
)
4141 struct ofpbuf
*request_buf
;
4144 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4146 error
= dpif_netlink_init();
4150 dpif_netlink_vport_init(reply
);
4155 request_buf
= ofpbuf_new(1024);
4156 dpif_netlink_vport_to_ofpbuf(request
, request_buf
);
4157 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4158 ofpbuf_delete(request_buf
);
4162 error
= dpif_netlink_vport_from_ofpbuf(reply
, *bufp
);
4165 dpif_netlink_vport_init(reply
);
4166 ofpbuf_delete(*bufp
);
4173 /* Obtains information about the kernel vport named 'name' and stores it into
4174 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
4175 * longer needed ('reply' will contain pointers into '*bufp'). */
4177 dpif_netlink_vport_get(const char *name
, struct dpif_netlink_vport
*reply
,
4178 struct ofpbuf
**bufp
)
4180 struct dpif_netlink_vport request
;
4182 dpif_netlink_vport_init(&request
);
4183 request
.cmd
= OVS_VPORT_CMD_GET
;
4184 request
.name
= name
;
4186 return dpif_netlink_vport_transact(&request
, reply
, bufp
);
4189 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4190 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
4191 * positive errno value.
4193 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
4194 * while 'dp' is still in use. */
4196 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp
*dp
, const struct ofpbuf
*buf
)
4198 static const struct nl_policy ovs_datapath_policy
[] = {
4199 [OVS_DP_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
4200 [OVS_DP_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_dp_stats
),
4202 [OVS_DP_ATTR_MEGAFLOW_STATS
] = {
4203 NL_POLICY_FOR(struct ovs_dp_megaflow_stats
),
4207 dpif_netlink_dp_init(dp
);
4209 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4210 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4211 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4212 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4214 struct nlattr
*a
[ARRAY_SIZE(ovs_datapath_policy
)];
4215 if (!nlmsg
|| !genl
|| !ovs_header
4216 || nlmsg
->nlmsg_type
!= ovs_datapath_family
4217 || !nl_policy_parse(&b
, 0, ovs_datapath_policy
, a
,
4218 ARRAY_SIZE(ovs_datapath_policy
))) {
4222 dp
->cmd
= genl
->cmd
;
4223 dp
->dp_ifindex
= ovs_header
->dp_ifindex
;
4224 dp
->name
= nl_attr_get_string(a
[OVS_DP_ATTR_NAME
]);
4225 if (a
[OVS_DP_ATTR_STATS
]) {
4226 dp
->stats
= nl_attr_get(a
[OVS_DP_ATTR_STATS
]);
4229 if (a
[OVS_DP_ATTR_MEGAFLOW_STATS
]) {
4230 dp
->megaflow_stats
= nl_attr_get(a
[OVS_DP_ATTR_MEGAFLOW_STATS
]);
4236 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
4238 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp
*dp
, struct ofpbuf
*buf
)
4240 struct ovs_header
*ovs_header
;
4242 nl_msg_put_genlmsghdr(buf
, 0, ovs_datapath_family
,
4243 NLM_F_REQUEST
| NLM_F_ECHO
, dp
->cmd
,
4244 OVS_DATAPATH_VERSION
);
4246 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4247 ovs_header
->dp_ifindex
= dp
->dp_ifindex
;
4250 nl_msg_put_string(buf
, OVS_DP_ATTR_NAME
, dp
->name
);
4253 if (dp
->upcall_pid
) {
4254 nl_msg_put_u32(buf
, OVS_DP_ATTR_UPCALL_PID
, *dp
->upcall_pid
);
4257 if (dp
->user_features
) {
4258 nl_msg_put_u32(buf
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
);
4261 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
4264 /* Clears 'dp' to "empty" values. */
4266 dpif_netlink_dp_init(struct dpif_netlink_dp
*dp
)
4268 memset(dp
, 0, sizeof *dp
);
4272 dpif_netlink_dp_dump_start(struct nl_dump
*dump
)
4274 struct dpif_netlink_dp request
;
4277 dpif_netlink_dp_init(&request
);
4278 request
.cmd
= OVS_DP_CMD_GET
;
4280 buf
= ofpbuf_new(1024);
4281 dpif_netlink_dp_to_ofpbuf(&request
, buf
);
4282 nl_dump_start(dump
, NETLINK_GENERIC
, buf
);
4286 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4287 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4288 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4289 * result of the command is expected to be of the same form, which is decoded
4290 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
4291 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
4293 dpif_netlink_dp_transact(const struct dpif_netlink_dp
*request
,
4294 struct dpif_netlink_dp
*reply
, struct ofpbuf
**bufp
)
4296 struct ofpbuf
*request_buf
;
4299 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4301 request_buf
= ofpbuf_new(1024);
4302 dpif_netlink_dp_to_ofpbuf(request
, request_buf
);
4303 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4304 ofpbuf_delete(request_buf
);
4307 dpif_netlink_dp_init(reply
);
4309 error
= dpif_netlink_dp_from_ofpbuf(reply
, *bufp
);
4312 ofpbuf_delete(*bufp
);
4319 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
4320 * The caller must free '*bufp' when the reply is no longer needed ('reply'
4321 * will contain pointers into '*bufp'). */
4323 dpif_netlink_dp_get(const struct dpif
*dpif_
, struct dpif_netlink_dp
*reply
,
4324 struct ofpbuf
**bufp
)
4326 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
4327 struct dpif_netlink_dp request
;
4329 dpif_netlink_dp_init(&request
);
4330 request
.cmd
= OVS_DP_CMD_GET
;
4331 request
.dp_ifindex
= dpif
->dp_ifindex
;
4333 return dpif_netlink_dp_transact(&request
, reply
, bufp
);
4336 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4337 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
4338 * positive errno value.
4340 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
4341 * while 'flow' is still in use. */
4343 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow
*flow
,
4344 const struct ofpbuf
*buf
)
4346 static const struct nl_policy ovs_flow_policy
[__OVS_FLOW_ATTR_MAX
] = {
4347 [OVS_FLOW_ATTR_KEY
] = { .type
= NL_A_NESTED
, .optional
= true },
4348 [OVS_FLOW_ATTR_MASK
] = { .type
= NL_A_NESTED
, .optional
= true },
4349 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
4350 [OVS_FLOW_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_flow_stats
),
4352 [OVS_FLOW_ATTR_TCP_FLAGS
] = { .type
= NL_A_U8
, .optional
= true },
4353 [OVS_FLOW_ATTR_USED
] = { .type
= NL_A_U64
, .optional
= true },
4354 [OVS_FLOW_ATTR_UFID
] = { .type
= NL_A_U128
, .optional
= true },
4355 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
4356 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
4357 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
4360 dpif_netlink_flow_init(flow
);
4362 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4363 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4364 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4365 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4367 struct nlattr
*a
[ARRAY_SIZE(ovs_flow_policy
)];
4368 if (!nlmsg
|| !genl
|| !ovs_header
4369 || nlmsg
->nlmsg_type
!= ovs_flow_family
4370 || !nl_policy_parse(&b
, 0, ovs_flow_policy
, a
,
4371 ARRAY_SIZE(ovs_flow_policy
))) {
4374 if (!a
[OVS_FLOW_ATTR_KEY
] && !a
[OVS_FLOW_ATTR_UFID
]) {
4378 flow
->nlmsg_flags
= nlmsg
->nlmsg_flags
;
4379 flow
->dp_ifindex
= ovs_header
->dp_ifindex
;
4380 if (a
[OVS_FLOW_ATTR_KEY
]) {
4381 flow
->key
= nl_attr_get(a
[OVS_FLOW_ATTR_KEY
]);
4382 flow
->key_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_KEY
]);
4385 if (a
[OVS_FLOW_ATTR_UFID
]) {
4386 flow
->ufid
= nl_attr_get_u128(a
[OVS_FLOW_ATTR_UFID
]);
4387 flow
->ufid_present
= true;
4389 if (a
[OVS_FLOW_ATTR_MASK
]) {
4390 flow
->mask
= nl_attr_get(a
[OVS_FLOW_ATTR_MASK
]);
4391 flow
->mask_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_MASK
]);
4393 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
4394 flow
->actions
= nl_attr_get(a
[OVS_FLOW_ATTR_ACTIONS
]);
4395 flow
->actions_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_ACTIONS
]);
4397 if (a
[OVS_FLOW_ATTR_STATS
]) {
4398 flow
->stats
= nl_attr_get(a
[OVS_FLOW_ATTR_STATS
]);
4400 if (a
[OVS_FLOW_ATTR_TCP_FLAGS
]) {
4401 flow
->tcp_flags
= nl_attr_get(a
[OVS_FLOW_ATTR_TCP_FLAGS
]);
4403 if (a
[OVS_FLOW_ATTR_USED
]) {
4404 flow
->used
= nl_attr_get(a
[OVS_FLOW_ATTR_USED
]);
4411 * If PACKET_TYPE attribute is present in 'data', it filters PACKET_TYPE out.
4412 * If the flow is not Ethernet, the OVS_KEY_ATTR_PACKET_TYPE is converted to
4413 * OVS_KEY_ATTR_ETHERTYPE. Puts 'data' to 'buf'.
4416 put_exclude_packet_type(struct ofpbuf
*buf
, uint16_t type
,
4417 const struct nlattr
*data
, uint16_t data_len
)
4419 const struct nlattr
*packet_type
;
4421 packet_type
= nl_attr_find__(data
, data_len
, OVS_KEY_ATTR_PACKET_TYPE
);
4424 /* exclude PACKET_TYPE Netlink attribute. */
4425 ovs_assert(NLA_ALIGN(packet_type
->nla_len
) == NL_A_U32_SIZE
);
4426 size_t packet_type_len
= NL_A_U32_SIZE
;
4427 size_t first_chunk_size
= (uint8_t *)packet_type
- (uint8_t *)data
;
4428 size_t second_chunk_size
= data_len
- first_chunk_size
4430 struct nlattr
*next_attr
= nl_attr_next(packet_type
);
4433 ofs
= nl_msg_start_nested(buf
, type
);
4434 nl_msg_put(buf
, data
, first_chunk_size
);
4435 nl_msg_put(buf
, next_attr
, second_chunk_size
);
4436 if (!nl_attr_find__(data
, data_len
, OVS_KEY_ATTR_ETHERNET
)) {
4437 ovs_be16 pt
= pt_ns_type_be(nl_attr_get_be32(packet_type
));
4438 const struct nlattr
*nla
;
4440 nla
= nl_attr_find(buf
, ofs
+ NLA_HDRLEN
, OVS_KEY_ATTR_ETHERTYPE
);
4442 ovs_be16
*ethertype
;
4444 ethertype
= CONST_CAST(ovs_be16
*, nl_attr_get(nla
));
4447 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, pt
);
4450 nl_msg_end_nested(buf
, ofs
);
4452 nl_msg_put_unspec(buf
, type
, data
, data_len
);
4456 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
4457 * followed by Netlink attributes corresponding to 'flow'. */
4459 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow
*flow
,
4462 struct ovs_header
*ovs_header
;
4464 nl_msg_put_genlmsghdr(buf
, 0, ovs_flow_family
,
4465 NLM_F_REQUEST
| flow
->nlmsg_flags
,
4466 flow
->cmd
, OVS_FLOW_VERSION
);
4468 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4469 ovs_header
->dp_ifindex
= flow
->dp_ifindex
;
4471 if (flow
->ufid_present
) {
4472 nl_msg_put_u128(buf
, OVS_FLOW_ATTR_UFID
, flow
->ufid
);
4474 if (flow
->ufid_terse
) {
4475 nl_msg_put_u32(buf
, OVS_FLOW_ATTR_UFID_FLAGS
,
4476 OVS_UFID_F_OMIT_KEY
| OVS_UFID_F_OMIT_MASK
4477 | OVS_UFID_F_OMIT_ACTIONS
);
4479 if (!flow
->ufid_terse
|| !flow
->ufid_present
) {
4480 if (flow
->key_len
) {
4481 put_exclude_packet_type(buf
, OVS_FLOW_ATTR_KEY
, flow
->key
,
4484 if (flow
->mask_len
) {
4485 put_exclude_packet_type(buf
, OVS_FLOW_ATTR_MASK
, flow
->mask
,
4488 if (flow
->actions
|| flow
->actions_len
) {
4489 nl_msg_put_unspec(buf
, OVS_FLOW_ATTR_ACTIONS
,
4490 flow
->actions
, flow
->actions_len
);
4494 /* We never need to send these to the kernel. */
4495 ovs_assert(!flow
->stats
);
4496 ovs_assert(!flow
->tcp_flags
);
4497 ovs_assert(!flow
->used
);
4500 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_CLEAR
);
4503 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_PROBE
);
4507 /* Clears 'flow' to "empty" values. */
4509 dpif_netlink_flow_init(struct dpif_netlink_flow
*flow
)
4511 memset(flow
, 0, sizeof *flow
);
4514 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4515 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4516 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4517 * result of the command is expected to be a flow also, which is decoded and
4518 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
4519 * is no longer needed ('reply' will contain pointers into '*bufp'). */
4521 dpif_netlink_flow_transact(struct dpif_netlink_flow
*request
,
4522 struct dpif_netlink_flow
*reply
,
4523 struct ofpbuf
**bufp
)
4525 struct ofpbuf
*request_buf
;
4528 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4531 request
->nlmsg_flags
|= NLM_F_ECHO
;
4534 request_buf
= ofpbuf_new(1024);
4535 dpif_netlink_flow_to_ofpbuf(request
, request_buf
);
4536 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4537 ofpbuf_delete(request_buf
);
4541 error
= dpif_netlink_flow_from_ofpbuf(reply
, *bufp
);
4544 dpif_netlink_flow_init(reply
);
4545 ofpbuf_delete(*bufp
);
4553 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow
*flow
,
4554 struct dpif_flow_stats
*stats
)
4557 stats
->n_packets
= get_32aligned_u64(&flow
->stats
->n_packets
);
4558 stats
->n_bytes
= get_32aligned_u64(&flow
->stats
->n_bytes
);
4560 stats
->n_packets
= 0;
4563 stats
->used
= flow
->used
? get_32aligned_u64(flow
->used
) : 0;
4564 stats
->tcp_flags
= flow
->tcp_flags
? *flow
->tcp_flags
: 0;
4567 /* Logs information about a packet that was recently lost in 'ch' (in
4570 report_loss(struct dpif_netlink
*dpif
, struct dpif_channel
*ch
, uint32_t ch_idx
,
4571 uint32_t handler_id
)
4573 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
4576 if (VLOG_DROP_WARN(&rl
)) {
4581 if (ch
->last_poll
!= LLONG_MIN
) {
4582 ds_put_format(&s
, " (last polled %lld ms ago)",
4583 time_msec() - ch
->last_poll
);
4586 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
4587 dpif_name(&dpif
->dpif
), ch_idx
, handler_id
);