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 /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1783 * Returns the number actually executed (at least 1, if 'n_ops' is
1786 dpif_netlink_operate__(struct dpif_netlink
*dpif
,
1787 struct dpif_op
**ops
, size_t n_ops
)
1790 struct nl_transaction txn
;
1792 struct ofpbuf request
;
1793 uint64_t request_stub
[1024 / 8];
1795 struct ofpbuf reply
;
1796 uint64_t reply_stub
[1024 / 8];
1797 } auxes
[OPERATE_MAX_OPS
];
1799 struct nl_transaction
*txnsp
[OPERATE_MAX_OPS
];
1802 n_ops
= MIN(n_ops
, OPERATE_MAX_OPS
);
1803 for (i
= 0; i
< n_ops
; i
++) {
1804 struct op_auxdata
*aux
= &auxes
[i
];
1805 struct dpif_op
*op
= ops
[i
];
1806 struct dpif_flow_put
*put
;
1807 struct dpif_flow_del
*del
;
1808 struct dpif_flow_get
*get
;
1809 struct dpif_netlink_flow flow
;
1811 ofpbuf_use_stub(&aux
->request
,
1812 aux
->request_stub
, sizeof aux
->request_stub
);
1813 aux
->txn
.request
= &aux
->request
;
1815 ofpbuf_use_stub(&aux
->reply
, aux
->reply_stub
, sizeof aux
->reply_stub
);
1816 aux
->txn
.reply
= NULL
;
1819 case DPIF_OP_FLOW_PUT
:
1820 put
= &op
->flow_put
;
1821 dpif_netlink_init_flow_put(dpif
, put
, &flow
);
1823 flow
.nlmsg_flags
|= NLM_F_ECHO
;
1824 aux
->txn
.reply
= &aux
->reply
;
1826 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1829 case DPIF_OP_FLOW_DEL
:
1830 del
= &op
->flow_del
;
1831 dpif_netlink_init_flow_del(dpif
, del
, &flow
);
1833 flow
.nlmsg_flags
|= NLM_F_ECHO
;
1834 aux
->txn
.reply
= &aux
->reply
;
1836 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1839 case DPIF_OP_EXECUTE
:
1840 /* Can't execute a packet that won't fit in a Netlink attribute. */
1841 if (OVS_UNLIKELY(nl_attr_oversized(
1842 dp_packet_size(op
->execute
.packet
)))) {
1843 /* Report an error immediately if this is the first operation.
1844 * Otherwise the easiest thing to do is to postpone to the next
1845 * call (when this will be the first operation). */
1847 VLOG_ERR_RL(&error_rl
,
1848 "dropping oversized %"PRIu32
"-byte packet",
1849 dp_packet_size(op
->execute
.packet
));
1850 op
->error
= ENOBUFS
;
1855 dpif_netlink_encode_execute(dpif
->dp_ifindex
, &op
->execute
,
1860 case DPIF_OP_FLOW_GET
:
1861 get
= &op
->flow_get
;
1862 dpif_netlink_init_flow_get(dpif
, get
, &flow
);
1863 aux
->txn
.reply
= get
->buffer
;
1864 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1872 for (i
= 0; i
< n_ops
; i
++) {
1873 txnsp
[i
] = &auxes
[i
].txn
;
1875 nl_transact_multiple(NETLINK_GENERIC
, txnsp
, n_ops
);
1877 for (i
= 0; i
< n_ops
; i
++) {
1878 struct op_auxdata
*aux
= &auxes
[i
];
1879 struct nl_transaction
*txn
= &auxes
[i
].txn
;
1880 struct dpif_op
*op
= ops
[i
];
1881 struct dpif_flow_put
*put
;
1882 struct dpif_flow_del
*del
;
1883 struct dpif_flow_get
*get
;
1885 op
->error
= txn
->error
;
1888 case DPIF_OP_FLOW_PUT
:
1889 put
= &op
->flow_put
;
1892 struct dpif_netlink_flow reply
;
1894 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
,
1897 dpif_netlink_flow_get_stats(&reply
, put
->stats
);
1903 case DPIF_OP_FLOW_DEL
:
1904 del
= &op
->flow_del
;
1907 struct dpif_netlink_flow reply
;
1909 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
,
1912 dpif_netlink_flow_get_stats(&reply
, del
->stats
);
1918 case DPIF_OP_EXECUTE
:
1921 case DPIF_OP_FLOW_GET
:
1922 get
= &op
->flow_get
;
1924 struct dpif_netlink_flow reply
;
1926 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
, txn
->reply
);
1928 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, get
->flow
,
1938 ofpbuf_uninit(&aux
->request
);
1939 ofpbuf_uninit(&aux
->reply
);
1946 parse_flow_get(struct dpif_netlink
*dpif
, struct dpif_flow_get
*get
)
1948 struct dpif_flow
*dpif_flow
= get
->flow
;
1950 struct nlattr
*actions
;
1951 struct dpif_flow_stats stats
;
1952 struct dpif_flow_attrs attrs
;
1954 uint64_t act_buf
[1024 / 8];
1955 struct odputil_keybuf maskbuf
;
1956 struct odputil_keybuf keybuf
;
1957 struct odputil_keybuf actbuf
;
1958 struct ofpbuf key
, mask
, act
;
1961 ofpbuf_use_stack(&buf
, &act_buf
, sizeof act_buf
);
1962 err
= netdev_ports_flow_get(dpif
->dpif
.dpif_class
, &match
,
1963 &actions
, get
->ufid
, &stats
, &attrs
, &buf
);
1968 VLOG_DBG("found flow from netdev, translating to dpif flow");
1970 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1971 ofpbuf_use_stack(&act
, &actbuf
, sizeof actbuf
);
1972 ofpbuf_use_stack(&mask
, &maskbuf
, sizeof maskbuf
);
1973 dpif_netlink_netdev_match_to_dpif_flow(&match
, &key
, &mask
, actions
,
1975 (ovs_u128
*) get
->ufid
,
1978 ofpbuf_put(get
->buffer
, nl_attr_get(actions
), nl_attr_get_size(actions
));
1979 dpif_flow
->actions
= ofpbuf_at(get
->buffer
, 0, 0);
1980 dpif_flow
->actions_len
= nl_attr_get_size(actions
);
1986 parse_flow_put(struct dpif_netlink
*dpif
, struct dpif_flow_put
*put
)
1988 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
1989 const struct dpif_class
*dpif_class
= dpif
->dpif
.dpif_class
;
1992 const struct nlattr
*nla
;
1995 struct offload_info info
;
1996 ovs_be16 dst_port
= 0;
1997 uint8_t csum_on
= false;
2000 if (put
->flags
& DPIF_FP_PROBE
) {
2004 err
= parse_key_and_mask_to_match(put
->key
, put
->key_len
, put
->mask
,
2005 put
->mask_len
, &match
);
2010 in_port
= match
.flow
.in_port
.odp_port
;
2011 dev
= netdev_ports_get(in_port
, dpif_class
);
2016 /* Get tunnel dst port */
2017 NL_ATTR_FOR_EACH(nla
, left
, put
->actions
, put
->actions_len
) {
2018 if (nl_attr_type(nla
) == OVS_ACTION_ATTR_OUTPUT
) {
2019 const struct netdev_tunnel_config
*tnl_cfg
;
2020 struct netdev
*outdev
;
2021 odp_port_t out_port
;
2023 out_port
= nl_attr_get_odp_port(nla
);
2024 outdev
= netdev_ports_get(out_port
, dpif_class
);
2029 tnl_cfg
= netdev_get_tunnel_config(outdev
);
2030 if (tnl_cfg
&& tnl_cfg
->dst_port
!= 0) {
2031 dst_port
= tnl_cfg
->dst_port
;
2034 csum_on
= tnl_cfg
->csum
;
2036 netdev_close(outdev
);
2040 info
.dpif_class
= dpif_class
;
2041 info
.tp_dst_port
= dst_port
;
2042 info
.tunnel_csum_on
= csum_on
;
2043 err
= netdev_flow_put(dev
, &match
,
2044 CONST_CAST(struct nlattr
*, put
->actions
),
2046 CONST_CAST(ovs_u128
*, put
->ufid
),
2050 if (put
->flags
& DPIF_FP_MODIFY
) {
2051 struct dpif_op
*opp
;
2054 op
.type
= DPIF_OP_FLOW_DEL
;
2055 op
.flow_del
.key
= put
->key
;
2056 op
.flow_del
.key_len
= put
->key_len
;
2057 op
.flow_del
.ufid
= put
->ufid
;
2058 op
.flow_del
.pmd_id
= put
->pmd_id
;
2059 op
.flow_del
.stats
= NULL
;
2060 op
.flow_del
.terse
= false;
2063 dpif_netlink_operate__(dpif
, &opp
, 1);
2066 VLOG_DBG("added flow");
2067 } else if (err
!= EEXIST
) {
2068 struct netdev
*oor_netdev
= NULL
;
2069 enum vlog_level level
;
2070 if (err
== ENOSPC
&& netdev_is_offload_rebalance_policy_enabled()) {
2072 * We need to set OOR on the input netdev (i.e, 'dev') for the
2073 * flow. But if the flow has a tunnel attribute (i.e, decap action,
2074 * with a virtual device like a VxLAN interface as its in-port),
2075 * then lookup and set OOR on the underlying tunnel (real) netdev.
2077 oor_netdev
= flow_get_tunnel_netdev(&match
.flow
.tunnel
);
2079 /* Not a 'tunnel' flow */
2082 netdev_set_hw_info(oor_netdev
, HW_INFO_TYPE_OOR
, true);
2084 level
= (err
== ENOSPC
|| err
== EOPNOTSUPP
) ? VLL_DBG
: VLL_ERR
;
2085 VLOG_RL(&rl
, level
, "failed to offload flow: %s: %s",
2087 (oor_netdev
? oor_netdev
->name
: dev
->name
));
2091 if (err
&& err
!= EEXIST
&& (put
->flags
& DPIF_FP_MODIFY
)) {
2092 /* Modified rule can't be offloaded, try and delete from HW */
2093 int del_err
= netdev_flow_del(dev
, put
->ufid
, put
->stats
);
2096 /* Delete from hw success, so old flow was offloaded.
2097 * Change flags to create the flow in kernel */
2098 put
->flags
&= ~DPIF_FP_MODIFY
;
2099 put
->flags
|= DPIF_FP_CREATE
;
2100 } else if (del_err
!= ENOENT
) {
2101 VLOG_ERR_RL(&rl
, "failed to delete offloaded flow: %s",
2102 ovs_strerror(del_err
));
2103 /* stop proccesing the flow in kernel */
2114 try_send_to_netdev(struct dpif_netlink
*dpif
, struct dpif_op
*op
)
2116 int err
= EOPNOTSUPP
;
2119 case DPIF_OP_FLOW_PUT
: {
2120 struct dpif_flow_put
*put
= &op
->flow_put
;
2126 log_flow_put_message(&dpif
->dpif
, &this_module
, put
, 0);
2127 err
= parse_flow_put(dpif
, put
);
2130 case DPIF_OP_FLOW_DEL
: {
2131 struct dpif_flow_del
*del
= &op
->flow_del
;
2137 log_flow_del_message(&dpif
->dpif
, &this_module
, del
, 0);
2138 err
= netdev_ports_flow_del(dpif
->dpif
.dpif_class
, del
->ufid
,
2142 case DPIF_OP_FLOW_GET
: {
2143 struct dpif_flow_get
*get
= &op
->flow_get
;
2145 if (!op
->flow_get
.ufid
) {
2149 log_flow_get_message(&dpif
->dpif
, &this_module
, get
, 0);
2150 err
= parse_flow_get(dpif
, get
);
2153 case DPIF_OP_EXECUTE
:
2162 dpif_netlink_operate_chunks(struct dpif_netlink
*dpif
, struct dpif_op
**ops
,
2166 size_t chunk
= dpif_netlink_operate__(dpif
, ops
, n_ops
);
2174 dpif_netlink_operate(struct dpif
*dpif_
, struct dpif_op
**ops
, size_t n_ops
,
2175 enum dpif_offload_type offload_type
)
2177 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2178 struct dpif_op
*new_ops
[OPERATE_MAX_OPS
];
2183 if (offload_type
== DPIF_OFFLOAD_ALWAYS
&& !netdev_is_flow_api_enabled()) {
2184 VLOG_DBG("Invalid offload_type: %d", offload_type
);
2188 if (offload_type
!= DPIF_OFFLOAD_NEVER
&& netdev_is_flow_api_enabled()) {
2192 while (n_ops
> 0 && count
< OPERATE_MAX_OPS
) {
2193 struct dpif_op
*op
= ops
[i
++];
2195 err
= try_send_to_netdev(dpif
, op
);
2196 if (err
&& err
!= EEXIST
) {
2197 if (offload_type
== DPIF_OFFLOAD_ALWAYS
) {
2198 /* We got an error while offloading an op. Since
2199 * OFFLOAD_ALWAYS is specified, we stop further
2200 * processing and return to the caller without
2201 * invoking kernel datapath as fallback. But the
2202 * interface requires us to process all n_ops; so
2203 * return the same error in the remaining ops too.
2214 new_ops
[count
++] = op
;
2222 dpif_netlink_operate_chunks(dpif
, new_ops
, count
);
2224 } else if (offload_type
!= DPIF_OFFLOAD_ALWAYS
) {
2225 dpif_netlink_operate_chunks(dpif
, ops
, n_ops
);
2231 dpif_netlink_handler_uninit(struct dpif_handler
*handler
)
2233 vport_delete_sock_pool(handler
);
2237 dpif_netlink_handler_init(struct dpif_handler
*handler
)
2239 return vport_create_sock_pool(handler
);
2244 dpif_netlink_handler_init(struct dpif_handler
*handler
)
2246 handler
->epoll_fd
= epoll_create(10);
2247 return handler
->epoll_fd
< 0 ? errno
: 0;
2251 dpif_netlink_handler_uninit(struct dpif_handler
*handler
)
2253 close(handler
->epoll_fd
);
2257 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
2258 * currently in 'dpif' in the kernel, by adding a new set of channels for
2259 * any kernel vport that lacks one and deleting any channels that have no
2260 * backing kernel vports. */
2262 dpif_netlink_refresh_channels(struct dpif_netlink
*dpif
, uint32_t n_handlers
)
2263 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2265 unsigned long int *keep_channels
;
2266 struct dpif_netlink_vport vport
;
2267 size_t keep_channels_nbits
;
2268 struct nl_dump dump
;
2269 uint64_t reply_stub
[NL_DUMP_BUFSIZE
/ 8];
2274 ovs_assert(!WINDOWS
|| n_handlers
<= 1);
2275 ovs_assert(!WINDOWS
|| dpif
->n_handlers
<= 1);
2277 if (dpif
->n_handlers
!= n_handlers
) {
2278 destroy_all_channels(dpif
);
2279 dpif
->handlers
= xzalloc(n_handlers
* sizeof *dpif
->handlers
);
2280 for (i
= 0; i
< n_handlers
; i
++) {
2282 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
2284 error
= dpif_netlink_handler_init(handler
);
2288 for (j
= 0; j
< i
; j
++) {
2289 struct dpif_handler
*tmp
= &dpif
->handlers
[j
];
2290 dpif_netlink_handler_uninit(tmp
);
2292 free(dpif
->handlers
);
2293 dpif
->handlers
= NULL
;
2298 dpif
->n_handlers
= n_handlers
;
2301 for (i
= 0; i
< n_handlers
; i
++) {
2302 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
2304 handler
->event_offset
= handler
->n_events
= 0;
2307 keep_channels_nbits
= dpif
->uc_array_size
;
2308 keep_channels
= bitmap_allocate(keep_channels_nbits
);
2310 ofpbuf_use_stub(&buf
, reply_stub
, sizeof reply_stub
);
2311 dpif_netlink_port_dump_start__(dpif
, &dump
);
2312 while (!dpif_netlink_port_dump_next__(dpif
, &dump
, &vport
, &buf
)) {
2313 uint32_t port_no
= odp_to_u32(vport
.port_no
);
2314 uint32_t upcall_pid
;
2317 if (port_no
>= dpif
->uc_array_size
2318 || !vport_get_pid(dpif
, port_no
, &upcall_pid
)) {
2319 struct nl_sock
*sock
;
2320 error
= create_nl_sock(dpif
, &sock
);
2326 error
= vport_add_channel(dpif
, vport
.port_no
, sock
);
2328 VLOG_INFO("%s: could not add channels for port %s",
2329 dpif_name(&dpif
->dpif
), vport
.name
);
2330 nl_sock_destroy(sock
);
2334 upcall_pid
= nl_sock_pid(sock
);
2337 /* Configure the vport to deliver misses to 'sock'. */
2338 if (vport
.upcall_pids
[0] == 0
2339 || vport
.n_upcall_pids
!= 1
2340 || upcall_pid
!= vport
.upcall_pids
[0]) {
2341 struct dpif_netlink_vport vport_request
;
2343 dpif_netlink_vport_init(&vport_request
);
2344 vport_request
.cmd
= OVS_VPORT_CMD_SET
;
2345 vport_request
.dp_ifindex
= dpif
->dp_ifindex
;
2346 vport_request
.port_no
= vport
.port_no
;
2347 vport_request
.n_upcall_pids
= 1;
2348 vport_request
.upcall_pids
= &upcall_pid
;
2349 error
= dpif_netlink_vport_transact(&vport_request
, NULL
, NULL
);
2351 VLOG_WARN_RL(&error_rl
,
2352 "%s: failed to set upcall pid on port: %s",
2353 dpif_name(&dpif
->dpif
), ovs_strerror(error
));
2355 if (error
!= ENODEV
&& error
!= ENOENT
) {
2358 /* The vport isn't really there, even though the dump says
2359 * it is. Probably we just hit a race after a port
2366 if (port_no
< keep_channels_nbits
) {
2367 bitmap_set1(keep_channels
, port_no
);
2372 vport_del_channels(dpif
, vport
.port_no
);
2374 nl_dump_done(&dump
);
2375 ofpbuf_uninit(&buf
);
2377 /* Discard any saved channels that we didn't reuse. */
2378 for (i
= 0; i
< keep_channels_nbits
; i
++) {
2379 if (!bitmap_is_set(keep_channels
, i
)) {
2380 vport_del_channels(dpif
, u32_to_odp(i
));
2383 free(keep_channels
);
2389 dpif_netlink_recv_set__(struct dpif_netlink
*dpif
, bool enable
)
2390 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2392 if ((dpif
->handlers
!= NULL
) == enable
) {
2394 } else if (!enable
) {
2395 destroy_all_channels(dpif
);
2398 return dpif_netlink_refresh_channels(dpif
, 1);
2403 dpif_netlink_recv_set(struct dpif
*dpif_
, bool enable
)
2405 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2408 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2409 error
= dpif_netlink_recv_set__(dpif
, enable
);
2410 fat_rwlock_unlock(&dpif
->upcall_lock
);
2416 dpif_netlink_handlers_set(struct dpif
*dpif_
, uint32_t n_handlers
)
2418 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2422 /* Multiple upcall handlers will be supported once kernel datapath supports
2424 if (n_handlers
> 1) {
2429 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2430 if (dpif
->handlers
) {
2431 error
= dpif_netlink_refresh_channels(dpif
, n_handlers
);
2433 fat_rwlock_unlock(&dpif
->upcall_lock
);
2439 dpif_netlink_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2440 uint32_t queue_id
, uint32_t *priority
)
2442 if (queue_id
< 0xf000) {
2443 *priority
= TC_H_MAKE(1 << 16, queue_id
+ 1);
2451 parse_odp_packet(const struct dpif_netlink
*dpif
, struct ofpbuf
*buf
,
2452 struct dpif_upcall
*upcall
, int *dp_ifindex
)
2454 static const struct nl_policy ovs_packet_policy
[] = {
2455 /* Always present. */
2456 [OVS_PACKET_ATTR_PACKET
] = { .type
= NL_A_UNSPEC
,
2457 .min_len
= ETH_HEADER_LEN
},
2458 [OVS_PACKET_ATTR_KEY
] = { .type
= NL_A_NESTED
},
2460 /* OVS_PACKET_CMD_ACTION only. */
2461 [OVS_PACKET_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
, .optional
= true },
2462 [OVS_PACKET_ATTR_EGRESS_TUN_KEY
] = { .type
= NL_A_NESTED
, .optional
= true },
2463 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
2464 [OVS_PACKET_ATTR_MRU
] = { .type
= NL_A_U16
, .optional
= true }
2467 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
2468 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
2469 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
2470 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
2472 struct nlattr
*a
[ARRAY_SIZE(ovs_packet_policy
)];
2473 if (!nlmsg
|| !genl
|| !ovs_header
2474 || nlmsg
->nlmsg_type
!= ovs_packet_family
2475 || !nl_policy_parse(&b
, 0, ovs_packet_policy
, a
,
2476 ARRAY_SIZE(ovs_packet_policy
))) {
2480 int type
= (genl
->cmd
== OVS_PACKET_CMD_MISS
? DPIF_UC_MISS
2481 : genl
->cmd
== OVS_PACKET_CMD_ACTION
? DPIF_UC_ACTION
2487 /* (Re)set ALL fields of '*upcall' on successful return. */
2488 upcall
->type
= type
;
2489 upcall
->key
= CONST_CAST(struct nlattr
*,
2490 nl_attr_get(a
[OVS_PACKET_ATTR_KEY
]));
2491 upcall
->key_len
= nl_attr_get_size(a
[OVS_PACKET_ATTR_KEY
]);
2492 dpif_flow_hash(&dpif
->dpif
, upcall
->key
, upcall
->key_len
, &upcall
->ufid
);
2493 upcall
->userdata
= a
[OVS_PACKET_ATTR_USERDATA
];
2494 upcall
->out_tun_key
= a
[OVS_PACKET_ATTR_EGRESS_TUN_KEY
];
2495 upcall
->actions
= a
[OVS_PACKET_ATTR_ACTIONS
];
2496 upcall
->mru
= a
[OVS_PACKET_ATTR_MRU
];
2498 /* Allow overwriting the netlink attribute header without reallocating. */
2499 dp_packet_use_stub(&upcall
->packet
,
2500 CONST_CAST(struct nlattr
*,
2501 nl_attr_get(a
[OVS_PACKET_ATTR_PACKET
])) - 1,
2502 nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]) +
2503 sizeof(struct nlattr
));
2504 dp_packet_set_data(&upcall
->packet
,
2505 (char *)dp_packet_data(&upcall
->packet
) + sizeof(struct nlattr
));
2506 dp_packet_set_size(&upcall
->packet
, nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]));
2508 if (nl_attr_find__(upcall
->key
, upcall
->key_len
, OVS_KEY_ATTR_ETHERNET
)) {
2509 /* Ethernet frame */
2510 upcall
->packet
.packet_type
= htonl(PT_ETH
);
2512 /* Non-Ethernet packet. Get the Ethertype from the NL attributes */
2513 ovs_be16 ethertype
= 0;
2514 const struct nlattr
*et_nla
= nl_attr_find__(upcall
->key
,
2516 OVS_KEY_ATTR_ETHERTYPE
);
2518 ethertype
= nl_attr_get_be16(et_nla
);
2520 upcall
->packet
.packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
2522 dp_packet_set_l3(&upcall
->packet
, dp_packet_data(&upcall
->packet
));
2525 *dp_ifindex
= ovs_header
->dp_ifindex
;
2531 #define PACKET_RECV_BATCH_SIZE 50
2533 dpif_netlink_recv_windows(struct dpif_netlink
*dpif
, uint32_t handler_id
,
2534 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2535 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2537 struct dpif_handler
*handler
;
2539 struct dpif_windows_vport_sock
*sock_pool
;
2542 if (!dpif
->handlers
) {
2546 /* Only one handler is supported currently. */
2547 if (handler_id
>= 1) {
2551 if (handler_id
>= dpif
->n_handlers
) {
2555 handler
= &dpif
->handlers
[handler_id
];
2556 sock_pool
= handler
->vport_sock_pool
;
2558 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
2563 if (++read_tries
> PACKET_RECV_BATCH_SIZE
) {
2567 error
= nl_sock_recv(sock_pool
[i
].nl_sock
, buf
, NULL
, false);
2568 if (error
== ENOBUFS
) {
2569 /* ENOBUFS typically means that we've received so many
2570 * packets that the buffer overflowed. Try again
2571 * immediately because there's almost certainly a packet
2572 * waiting for us. */
2573 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2577 /* XXX: ch->last_poll = time_msec(); */
2579 if (error
== EAGAIN
) {
2585 error
= parse_odp_packet(dpif
, buf
, upcall
, &dp_ifindex
);
2586 if (!error
&& dp_ifindex
== dpif
->dp_ifindex
) {
2598 dpif_netlink_recv__(struct dpif_netlink
*dpif
, uint32_t handler_id
,
2599 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2600 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2602 struct dpif_handler
*handler
;
2605 if (!dpif
->handlers
|| handler_id
>= dpif
->n_handlers
) {
2609 handler
= &dpif
->handlers
[handler_id
];
2610 if (handler
->event_offset
>= handler
->n_events
) {
2613 handler
->event_offset
= handler
->n_events
= 0;
2616 retval
= epoll_wait(handler
->epoll_fd
, handler
->epoll_events
,
2617 dpif
->uc_array_size
, 0);
2618 } while (retval
< 0 && errno
== EINTR
);
2621 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2622 VLOG_WARN_RL(&rl
, "epoll_wait failed (%s)", ovs_strerror(errno
));
2623 } else if (retval
> 0) {
2624 handler
->n_events
= retval
;
2628 while (handler
->event_offset
< handler
->n_events
) {
2629 int idx
= handler
->epoll_events
[handler
->event_offset
].data
.u32
;
2630 struct dpif_channel
*ch
= &dpif
->channels
[idx
];
2632 handler
->event_offset
++;
2638 if (++read_tries
> 50) {
2642 error
= nl_sock_recv(ch
->sock
, buf
, NULL
, false);
2643 if (error
== ENOBUFS
) {
2644 /* ENOBUFS typically means that we've received so many
2645 * packets that the buffer overflowed. Try again
2646 * immediately because there's almost certainly a packet
2647 * waiting for us. */
2648 report_loss(dpif
, ch
, idx
, handler_id
);
2652 ch
->last_poll
= time_msec();
2654 if (error
== EAGAIN
) {
2660 error
= parse_odp_packet(dpif
, buf
, upcall
, &dp_ifindex
);
2661 if (!error
&& dp_ifindex
== dpif
->dp_ifindex
) {
2674 dpif_netlink_recv(struct dpif
*dpif_
, uint32_t handler_id
,
2675 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2677 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2680 fat_rwlock_rdlock(&dpif
->upcall_lock
);
2682 error
= dpif_netlink_recv_windows(dpif
, handler_id
, upcall
, buf
);
2684 error
= dpif_netlink_recv__(dpif
, handler_id
, upcall
, buf
);
2686 fat_rwlock_unlock(&dpif
->upcall_lock
);
2692 dpif_netlink_recv_wait__(struct dpif_netlink
*dpif
, uint32_t handler_id
)
2693 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2697 struct dpif_windows_vport_sock
*sock_pool
=
2698 dpif
->handlers
[handler_id
].vport_sock_pool
;
2700 /* Only one handler is supported currently. */
2701 if (handler_id
>= 1) {
2705 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
2706 nl_sock_wait(sock_pool
[i
].nl_sock
, POLLIN
);
2709 if (dpif
->handlers
&& handler_id
< dpif
->n_handlers
) {
2710 struct dpif_handler
*handler
= &dpif
->handlers
[handler_id
];
2712 poll_fd_wait(handler
->epoll_fd
, POLLIN
);
2718 dpif_netlink_recv_wait(struct dpif
*dpif_
, uint32_t handler_id
)
2720 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2722 fat_rwlock_rdlock(&dpif
->upcall_lock
);
2723 dpif_netlink_recv_wait__(dpif
, handler_id
);
2724 fat_rwlock_unlock(&dpif
->upcall_lock
);
2728 dpif_netlink_recv_purge__(struct dpif_netlink
*dpif
)
2729 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2731 if (dpif
->handlers
) {
2734 if (!dpif
->channels
[0].sock
) {
2737 for (i
= 0; i
< dpif
->uc_array_size
; i
++ ) {
2739 nl_sock_drain(dpif
->channels
[i
].sock
);
2745 dpif_netlink_recv_purge(struct dpif
*dpif_
)
2747 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2749 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2750 dpif_netlink_recv_purge__(dpif
);
2751 fat_rwlock_unlock(&dpif
->upcall_lock
);
2755 dpif_netlink_get_datapath_version(void)
2757 char *version_str
= NULL
;
2761 #define MAX_VERSION_STR_SIZE 80
2762 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2765 f
= fopen(LINUX_DATAPATH_VERSION_FILE
, "r");
2768 char version
[MAX_VERSION_STR_SIZE
];
2770 if (fgets(version
, MAX_VERSION_STR_SIZE
, f
)) {
2771 newline
= strchr(version
, '\n');
2775 version_str
= xstrdup(version
);
2784 struct dpif_netlink_ct_dump_state
{
2785 struct ct_dpif_dump_state up
;
2786 struct nl_ct_dump_state
*nl_ct_dump
;
2790 dpif_netlink_ct_dump_start(struct dpif
*dpif OVS_UNUSED
,
2791 struct ct_dpif_dump_state
**dump_
,
2792 const uint16_t *zone
, int *ptot_bkts
)
2794 struct dpif_netlink_ct_dump_state
*dump
;
2797 dump
= xzalloc(sizeof *dump
);
2798 err
= nl_ct_dump_start(&dump
->nl_ct_dump
, zone
, ptot_bkts
);
2810 dpif_netlink_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
2811 struct ct_dpif_dump_state
*dump_
,
2812 struct ct_dpif_entry
*entry
)
2814 struct dpif_netlink_ct_dump_state
*dump
;
2816 INIT_CONTAINER(dump
, dump_
, up
);
2818 return nl_ct_dump_next(dump
->nl_ct_dump
, entry
);
2822 dpif_netlink_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
2823 struct ct_dpif_dump_state
*dump_
)
2825 struct dpif_netlink_ct_dump_state
*dump
;
2827 INIT_CONTAINER(dump
, dump_
, up
);
2829 int err
= nl_ct_dump_done(dump
->nl_ct_dump
);
2835 dpif_netlink_ct_flush(struct dpif
*dpif OVS_UNUSED
, const uint16_t *zone
,
2836 const struct ct_dpif_tuple
*tuple
)
2839 return nl_ct_flush_tuple(tuple
, zone
? *zone
: 0);
2841 return nl_ct_flush_zone(*zone
);
2843 return nl_ct_flush();
2848 dpif_netlink_ct_set_limits(struct dpif
*dpif OVS_UNUSED
,
2849 const uint32_t *default_limits
,
2850 const struct ovs_list
*zone_limits
)
2852 struct ovs_zone_limit req_zone_limit
;
2854 if (ovs_ct_limit_family
< 0) {
2858 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
2859 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
2860 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_SET
,
2861 OVS_CT_LIMIT_VERSION
);
2863 struct ovs_header
*ovs_header
;
2864 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
2865 ovs_header
->dp_ifindex
= 0;
2868 opt_offset
= nl_msg_start_nested(request
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2869 if (default_limits
) {
2870 req_zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2871 req_zone_limit
.limit
= *default_limits
;
2872 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2875 if (!ovs_list_is_empty(zone_limits
)) {
2876 struct ct_dpif_zone_limit
*zone_limit
;
2878 LIST_FOR_EACH (zone_limit
, node
, zone_limits
) {
2879 req_zone_limit
.zone_id
= zone_limit
->zone
;
2880 req_zone_limit
.limit
= zone_limit
->limit
;
2881 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2884 nl_msg_end_nested(request
, opt_offset
);
2886 int err
= nl_transact(NETLINK_GENERIC
, request
, NULL
);
2887 ofpbuf_delete(request
);
2892 dpif_netlink_zone_limits_from_ofpbuf(const struct ofpbuf
*buf
,
2893 uint32_t *default_limit
,
2894 struct ovs_list
*zone_limits
)
2896 static const struct nl_policy ovs_ct_limit_policy
[] = {
2897 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NL_A_NESTED
,
2901 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
2902 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
2903 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
2904 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
2906 struct nlattr
*attr
[ARRAY_SIZE(ovs_ct_limit_policy
)];
2908 if (!nlmsg
|| !genl
|| !ovs_header
2909 || nlmsg
->nlmsg_type
!= ovs_ct_limit_family
2910 || !nl_policy_parse(&b
, 0, ovs_ct_limit_policy
, attr
,
2911 ARRAY_SIZE(ovs_ct_limit_policy
))) {
2916 if (!attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2920 int rem
= NLA_ALIGN(
2921 nl_attr_get_size(attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]));
2922 const struct ovs_zone_limit
*zone_limit
=
2923 nl_attr_get(attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]);
2925 while (rem
>= sizeof *zone_limit
) {
2926 if (zone_limit
->zone_id
== OVS_ZONE_LIMIT_DEFAULT_ZONE
) {
2927 *default_limit
= zone_limit
->limit
;
2928 } else if (zone_limit
->zone_id
< OVS_ZONE_LIMIT_DEFAULT_ZONE
||
2929 zone_limit
->zone_id
> UINT16_MAX
) {
2931 ct_dpif_push_zone_limit(zone_limits
, zone_limit
->zone_id
,
2932 zone_limit
->limit
, zone_limit
->count
);
2934 rem
-= NLA_ALIGN(sizeof *zone_limit
);
2935 zone_limit
= ALIGNED_CAST(struct ovs_zone_limit
*,
2936 (unsigned char *) zone_limit
+ NLA_ALIGN(sizeof *zone_limit
));
2942 dpif_netlink_ct_get_limits(struct dpif
*dpif OVS_UNUSED
,
2943 uint32_t *default_limit
,
2944 const struct ovs_list
*zone_limits_request
,
2945 struct ovs_list
*zone_limits_reply
)
2947 if (ovs_ct_limit_family
< 0) {
2951 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
2952 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
2953 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_GET
,
2954 OVS_CT_LIMIT_VERSION
);
2956 struct ovs_header
*ovs_header
;
2957 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
2958 ovs_header
->dp_ifindex
= 0;
2960 if (!ovs_list_is_empty(zone_limits_request
)) {
2961 size_t opt_offset
= nl_msg_start_nested(request
,
2962 OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2964 struct ovs_zone_limit req_zone_limit
;
2965 req_zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2966 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2968 struct ct_dpif_zone_limit
*zone_limit
;
2969 LIST_FOR_EACH (zone_limit
, node
, zone_limits_request
) {
2970 req_zone_limit
.zone_id
= zone_limit
->zone
;
2971 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2974 nl_msg_end_nested(request
, opt_offset
);
2977 struct ofpbuf
*reply
;
2978 int err
= nl_transact(NETLINK_GENERIC
, request
, &reply
);
2983 err
= dpif_netlink_zone_limits_from_ofpbuf(reply
, default_limit
,
2987 ofpbuf_delete(request
);
2988 ofpbuf_delete(reply
);
2993 dpif_netlink_ct_del_limits(struct dpif
*dpif OVS_UNUSED
,
2994 const struct ovs_list
*zone_limits
)
2996 if (ovs_ct_limit_family
< 0) {
3000 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
3001 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
3002 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_DEL
,
3003 OVS_CT_LIMIT_VERSION
);
3005 struct ovs_header
*ovs_header
;
3006 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
3007 ovs_header
->dp_ifindex
= 0;
3009 if (!ovs_list_is_empty(zone_limits
)) {
3011 nl_msg_start_nested(request
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
3013 struct ct_dpif_zone_limit
*zone_limit
;
3014 LIST_FOR_EACH (zone_limit
, node
, zone_limits
) {
3015 struct ovs_zone_limit req_zone_limit
;
3016 req_zone_limit
.zone_id
= zone_limit
->zone
;
3017 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
3019 nl_msg_end_nested(request
, opt_offset
);
3022 int err
= nl_transact(NETLINK_GENERIC
, request
, NULL
);
3024 ofpbuf_delete(request
);
3028 #define NL_TP_NAME_PREFIX "ovs_tp_"
3030 struct dpif_netlink_timeout_policy_protocol
{
3035 enum OVS_PACKED_ENUM dpif_netlink_support_timeout_policy_protocol
{
3036 DPIF_NL_TP_AF_INET_TCP
,
3037 DPIF_NL_TP_AF_INET_UDP
,
3038 DPIF_NL_TP_AF_INET_ICMP
,
3039 DPIF_NL_TP_AF_INET6_TCP
,
3040 DPIF_NL_TP_AF_INET6_UDP
,
3041 DPIF_NL_TP_AF_INET6_ICMPV6
,
3045 #define DPIF_NL_ALL_TP ((1UL << DPIF_NL_TP_MAX) - 1)
3048 static struct dpif_netlink_timeout_policy_protocol tp_protos
[] = {
3049 [DPIF_NL_TP_AF_INET_TCP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_TCP
},
3050 [DPIF_NL_TP_AF_INET_UDP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_UDP
},
3051 [DPIF_NL_TP_AF_INET_ICMP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_ICMP
},
3052 [DPIF_NL_TP_AF_INET6_TCP
] = { .l3num
= AF_INET6
, .l4num
= IPPROTO_TCP
},
3053 [DPIF_NL_TP_AF_INET6_UDP
] = { .l3num
= AF_INET6
, .l4num
= IPPROTO_UDP
},
3054 [DPIF_NL_TP_AF_INET6_ICMPV6
] = { .l3num
= AF_INET6
,
3055 .l4num
= IPPROTO_ICMPV6
},
3059 dpif_netlink_format_tp_name(uint32_t id
, uint16_t l3num
, uint8_t l4num
,
3062 struct ds ds
= DS_EMPTY_INITIALIZER
;
3063 ds_put_format(&ds
, "%s%"PRIu32
"_", NL_TP_NAME_PREFIX
, id
);
3064 ct_dpif_format_ipproto(&ds
, l4num
);
3066 if (l3num
== AF_INET
) {
3067 ds_put_cstr(&ds
, "4");
3068 } else if (l3num
== AF_INET6
&& l4num
!= IPPROTO_ICMPV6
) {
3069 ds_put_cstr(&ds
, "6");
3072 ovs_assert(ds
.length
< CTNL_TIMEOUT_NAME_MAX
);
3074 *tp_name
= ds_steal_cstr(&ds
);
3078 dpif_netlink_ct_get_timeout_policy_name(struct dpif
*dpif OVS_UNUSED
,
3079 uint32_t tp_id
, uint16_t dl_type
,
3080 uint8_t nw_proto
, char **tp_name
,
3083 dpif_netlink_format_tp_name(tp_id
,
3084 dl_type
== ETH_TYPE_IP
? AF_INET
: AF_INET6
,
3086 *is_generic
= false;
3090 #define CT_DPIF_NL_TP_TCP_MAPPINGS \
3091 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT, SYN_SENT) \
3092 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_RECV, SYN_RECV) \
3093 CT_DPIF_NL_TP_MAPPING(TCP, TCP, ESTABLISHED, ESTABLISHED) \
3094 CT_DPIF_NL_TP_MAPPING(TCP, TCP, FIN_WAIT, FIN_WAIT) \
3095 CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE_WAIT, CLOSE_WAIT) \
3096 CT_DPIF_NL_TP_MAPPING(TCP, TCP, LAST_ACK, LAST_ACK) \
3097 CT_DPIF_NL_TP_MAPPING(TCP, TCP, TIME_WAIT, TIME_WAIT) \
3098 CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE, CLOSE) \
3099 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT2, SYN_SENT2) \
3100 CT_DPIF_NL_TP_MAPPING(TCP, TCP, RETRANSMIT, RETRANS) \
3101 CT_DPIF_NL_TP_MAPPING(TCP, TCP, UNACK, UNACK)
3103 #define CT_DPIF_NL_TP_UDP_MAPPINGS \
3104 CT_DPIF_NL_TP_MAPPING(UDP, UDP, SINGLE, UNREPLIED) \
3105 CT_DPIF_NL_TP_MAPPING(UDP, UDP, MULTIPLE, REPLIED)
3107 #define CT_DPIF_NL_TP_ICMP_MAPPINGS \
3108 CT_DPIF_NL_TP_MAPPING(ICMP, ICMP, FIRST, TIMEOUT)
3110 #define CT_DPIF_NL_TP_ICMPV6_MAPPINGS \
3111 CT_DPIF_NL_TP_MAPPING(ICMP, ICMPV6, FIRST, TIMEOUT)
3114 #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \
3115 if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \
3116 nl_tp->present |= 1 << CTA_TIMEOUT_##PROTO2##_##ATTR2; \
3117 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2] = \
3118 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]; \
3122 dpif_netlink_get_nl_tp_tcp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3123 struct nl_ct_timeout_policy
*nl_tp
)
3125 CT_DPIF_NL_TP_TCP_MAPPINGS
3129 dpif_netlink_get_nl_tp_udp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3130 struct nl_ct_timeout_policy
*nl_tp
)
3132 CT_DPIF_NL_TP_UDP_MAPPINGS
3136 dpif_netlink_get_nl_tp_icmp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3137 struct nl_ct_timeout_policy
*nl_tp
)
3139 CT_DPIF_NL_TP_ICMP_MAPPINGS
3143 dpif_netlink_get_nl_tp_icmpv6_attrs(const struct ct_dpif_timeout_policy
*tp
,
3144 struct nl_ct_timeout_policy
*nl_tp
)
3146 CT_DPIF_NL_TP_ICMPV6_MAPPINGS
3149 #undef CT_DPIF_NL_TP_MAPPING
3152 dpif_netlink_get_nl_tp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3153 uint8_t l4num
, struct nl_ct_timeout_policy
*nl_tp
)
3157 if (l4num
== IPPROTO_TCP
) {
3158 dpif_netlink_get_nl_tp_tcp_attrs(tp
, nl_tp
);
3159 } else if (l4num
== IPPROTO_UDP
) {
3160 dpif_netlink_get_nl_tp_udp_attrs(tp
, nl_tp
);
3161 } else if (l4num
== IPPROTO_ICMP
) {
3162 dpif_netlink_get_nl_tp_icmp_attrs(tp
, nl_tp
);
3163 } else if (l4num
== IPPROTO_ICMPV6
) {
3164 dpif_netlink_get_nl_tp_icmpv6_attrs(tp
, nl_tp
);
3168 #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \
3169 if (nl_tp->present & (1 << CTA_TIMEOUT_##PROTO2##_##ATTR2)) { \
3170 if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \
3171 if (tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] != \
3172 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]) { \
3173 VLOG_WARN_RL(&error_rl, "Inconsistent timeout policy %s " \
3174 "attribute %s=%"PRIu32" while %s=%"PRIu32, \
3175 nl_tp->name, "CTA_TIMEOUT_"#PROTO2"_"#ATTR2, \
3176 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2], \
3177 "CT_DPIF_TP_ATTR_"#PROTO1"_"#ATTR1, \
3178 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]); \
3181 tp->present |= 1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1; \
3182 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] = \
3183 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]; \
3188 dpif_netlink_set_ct_dpif_tp_tcp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3189 struct ct_dpif_timeout_policy
*tp
)
3191 CT_DPIF_NL_TP_TCP_MAPPINGS
3195 dpif_netlink_set_ct_dpif_tp_udp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3196 struct ct_dpif_timeout_policy
*tp
)
3198 CT_DPIF_NL_TP_UDP_MAPPINGS
3202 dpif_netlink_set_ct_dpif_tp_icmp_attrs(
3203 const struct nl_ct_timeout_policy
*nl_tp
,
3204 struct ct_dpif_timeout_policy
*tp
)
3206 CT_DPIF_NL_TP_ICMP_MAPPINGS
3210 dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(
3211 const struct nl_ct_timeout_policy
*nl_tp
,
3212 struct ct_dpif_timeout_policy
*tp
)
3214 CT_DPIF_NL_TP_ICMPV6_MAPPINGS
3217 #undef CT_DPIF_NL_TP_MAPPING
3220 dpif_netlink_set_ct_dpif_tp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3221 struct ct_dpif_timeout_policy
*tp
)
3223 if (nl_tp
->l4num
== IPPROTO_TCP
) {
3224 dpif_netlink_set_ct_dpif_tp_tcp_attrs(nl_tp
, tp
);
3225 } else if (nl_tp
->l4num
== IPPROTO_UDP
) {
3226 dpif_netlink_set_ct_dpif_tp_udp_attrs(nl_tp
, tp
);
3227 } else if (nl_tp
->l4num
== IPPROTO_ICMP
) {
3228 dpif_netlink_set_ct_dpif_tp_icmp_attrs(nl_tp
, tp
);
3229 } else if (nl_tp
->l4num
== IPPROTO_ICMPV6
) {
3230 dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(nl_tp
, tp
);
3236 dpif_netlink_ct_set_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3237 const struct ct_dpif_timeout_policy
*tp
)
3243 dpif_netlink_ct_get_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3245 struct ct_dpif_timeout_policy
*tp
)
3251 dpif_netlink_ct_del_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3258 dpif_netlink_ct_timeout_policy_dump_start(struct dpif
*dpif OVS_UNUSED
,
3265 dpif_netlink_ct_timeout_policy_dump_next(struct dpif
*dpif OVS_UNUSED
,
3267 struct ct_dpif_timeout_policy
**tp
)
3273 dpif_netlink_ct_timeout_policy_dump_done(struct dpif
*dpif OVS_UNUSED
,
3280 dpif_netlink_ct_set_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3281 const struct ct_dpif_timeout_policy
*tp
)
3285 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3286 struct nl_ct_timeout_policy nl_tp
;
3289 dpif_netlink_format_tp_name(tp
->id
, tp_protos
[i
].l3num
,
3290 tp_protos
[i
].l4num
, &nl_tp_name
);
3291 ovs_strlcpy(nl_tp
.name
, nl_tp_name
, sizeof nl_tp
.name
);
3294 nl_tp
.l3num
= tp_protos
[i
].l3num
;
3295 nl_tp
.l4num
= tp_protos
[i
].l4num
;
3296 dpif_netlink_get_nl_tp_attrs(tp
, tp_protos
[i
].l4num
, &nl_tp
);
3297 err
= nl_ct_set_timeout_policy(&nl_tp
);
3299 VLOG_WARN_RL(&error_rl
, "failed to add timeout policy %s (%s)",
3300 nl_tp
.name
, ovs_strerror(err
));
3310 dpif_netlink_ct_get_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3312 struct ct_dpif_timeout_policy
*tp
)
3318 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3319 struct nl_ct_timeout_policy nl_tp
;
3322 dpif_netlink_format_tp_name(tp_id
, tp_protos
[i
].l3num
,
3323 tp_protos
[i
].l4num
, &nl_tp_name
);
3324 err
= nl_ct_get_timeout_policy(nl_tp_name
, &nl_tp
);
3327 VLOG_WARN_RL(&error_rl
, "failed to get timeout policy %s (%s)",
3328 nl_tp_name
, ovs_strerror(err
));
3333 dpif_netlink_set_ct_dpif_tp_attrs(&nl_tp
, tp
);
3340 /* Returns 0 if all the sub timeout policies are deleted or not exist in the
3341 * kernel. Returns 1 if any sub timeout policy deletion failed. */
3343 dpif_netlink_ct_del_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3348 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3350 dpif_netlink_format_tp_name(tp_id
, tp_protos
[i
].l3num
,
3351 tp_protos
[i
].l4num
, &nl_tp_name
);
3352 int err
= nl_ct_del_timeout_policy(nl_tp_name
);
3353 if (err
== ENOENT
) {
3357 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(6, 6);
3358 VLOG_INFO_RL(&rl
, "failed to delete timeout policy %s (%s)",
3359 nl_tp_name
, ovs_strerror(err
));
3368 struct dpif_netlink_ct_timeout_policy_dump_state
{
3369 struct nl_ct_timeout_policy_dump_state
*nl_dump_state
;
3370 struct hmap tp_dump_map
;
3373 struct dpif_netlink_tp_dump_node
{
3374 struct hmap_node hmap_node
; /* node in tp_dump_map. */
3375 struct ct_dpif_timeout_policy
*tp
;
3376 uint32_t l3_l4_present
;
3379 static struct dpif_netlink_tp_dump_node
*
3380 get_dpif_netlink_tp_dump_node_by_tp_id(uint32_t tp_id
,
3381 struct hmap
*tp_dump_map
)
3383 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3385 HMAP_FOR_EACH_WITH_HASH (tp_dump_node
, hmap_node
, hash_int(tp_id
, 0),
3387 if (tp_dump_node
->tp
->id
== tp_id
) {
3388 return tp_dump_node
;
3395 update_dpif_netlink_tp_dump_node(
3396 const struct nl_ct_timeout_policy
*nl_tp
,
3397 struct dpif_netlink_tp_dump_node
*tp_dump_node
)
3399 dpif_netlink_set_ct_dpif_tp_attrs(nl_tp
, tp_dump_node
->tp
);
3400 for (int i
= 0; i
< DPIF_NL_TP_MAX
; ++i
) {
3401 if (nl_tp
->l3num
== tp_protos
[i
].l3num
&&
3402 nl_tp
->l4num
== tp_protos
[i
].l4num
) {
3403 tp_dump_node
->l3_l4_present
|= 1 << i
;
3410 dpif_netlink_ct_timeout_policy_dump_start(struct dpif
*dpif OVS_UNUSED
,
3413 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
;
3415 *statep
= dump_state
= xzalloc(sizeof *dump_state
);
3416 int err
= nl_ct_timeout_policy_dump_start(&dump_state
->nl_dump_state
);
3421 hmap_init(&dump_state
->tp_dump_map
);
3426 get_and_cleanup_tp_dump_node(struct hmap
*hmap
,
3427 struct dpif_netlink_tp_dump_node
*tp_dump_node
,
3428 struct ct_dpif_timeout_policy
*tp
)
3430 hmap_remove(hmap
, &tp_dump_node
->hmap_node
);
3431 *tp
= *tp_dump_node
->tp
;
3432 free(tp_dump_node
->tp
);
3437 dpif_netlink_ct_timeout_policy_dump_next(struct dpif
*dpif OVS_UNUSED
,
3439 struct ct_dpif_timeout_policy
*tp
)
3441 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
= state
;
3442 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3445 /* Dumps all the timeout policies in the kernel. */
3447 struct nl_ct_timeout_policy nl_tp
;
3450 err
= nl_ct_timeout_policy_dump_next(dump_state
->nl_dump_state
,
3456 /* We only interest in OVS installed timeout policies. */
3457 if (!ovs_scan(nl_tp
.name
, NL_TP_NAME_PREFIX
"%"PRIu32
, &tp_id
)) {
3461 tp_dump_node
= get_dpif_netlink_tp_dump_node_by_tp_id(
3462 tp_id
, &dump_state
->tp_dump_map
);
3463 if (!tp_dump_node
) {
3464 tp_dump_node
= xzalloc(sizeof *tp_dump_node
);
3465 tp_dump_node
->tp
= xzalloc(sizeof *tp_dump_node
->tp
);
3466 tp_dump_node
->tp
->id
= tp_id
;
3467 hmap_insert(&dump_state
->tp_dump_map
, &tp_dump_node
->hmap_node
,
3468 hash_int(tp_id
, 0));
3471 update_dpif_netlink_tp_dump_node(&nl_tp
, tp_dump_node
);
3473 /* Returns one ct_dpif_timeout_policy if we gather all the L3/L4
3475 if (tp_dump_node
->l3_l4_present
== DPIF_NL_ALL_TP
) {
3476 get_and_cleanup_tp_dump_node(&dump_state
->tp_dump_map
,
3482 /* Dump the incomplete timeout policies. */
3484 if (!hmap_is_empty(&dump_state
->tp_dump_map
)) {
3485 struct hmap_node
*hmap_node
= hmap_first(&dump_state
->tp_dump_map
);
3486 tp_dump_node
= CONTAINER_OF(hmap_node
,
3487 struct dpif_netlink_tp_dump_node
,
3489 get_and_cleanup_tp_dump_node(&dump_state
->tp_dump_map
,
3499 dpif_netlink_ct_timeout_policy_dump_done(struct dpif
*dpif OVS_UNUSED
,
3502 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
= state
;
3503 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3505 int err
= nl_ct_timeout_policy_dump_done(dump_state
->nl_dump_state
);
3506 HMAP_FOR_EACH_POP (tp_dump_node
, hmap_node
, &dump_state
->tp_dump_map
) {
3507 free(tp_dump_node
->tp
);
3510 hmap_destroy(&dump_state
->tp_dump_map
);
3519 /* Set of supported meter flags */
3520 #define DP_SUPPORTED_METER_FLAGS_MASK \
3521 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
3523 /* Meter support was introduced in Linux 4.15. In some versions of
3524 * Linux 4.15, 4.16, and 4.17, there was a bug that never set the id
3525 * when the meter was created, so all meters essentially had an id of
3526 * zero. Check for that condition and disable meters on those kernels. */
3527 static bool probe_broken_meters(struct dpif
*);
3530 dpif_netlink_meter_init(struct dpif_netlink
*dpif
, struct ofpbuf
*buf
,
3531 void *stub
, size_t size
, uint32_t command
)
3533 ofpbuf_use_stub(buf
, stub
, size
);
3535 nl_msg_put_genlmsghdr(buf
, 0, ovs_meter_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
3536 command
, OVS_METER_VERSION
);
3538 struct ovs_header
*ovs_header
;
3539 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
3540 ovs_header
->dp_ifindex
= dpif
->dp_ifindex
;
3543 /* Execute meter 'request' in the kernel datapath. If the command
3544 * fails, returns a positive errno value. Otherwise, stores the reply
3545 * in '*replyp', parses the policy according to 'reply_policy' into the
3546 * array of Netlink attribute in 'a', and returns 0. On success, the
3547 * caller is responsible for calling ofpbuf_delete() on '*replyp'
3548 * ('replyp' will contain pointers into 'a'). */
3550 dpif_netlink_meter_transact(struct ofpbuf
*request
, struct ofpbuf
**replyp
,
3551 const struct nl_policy
*reply_policy
,
3552 struct nlattr
**a
, size_t size_a
)
3554 int error
= nl_transact(NETLINK_GENERIC
, request
, replyp
);
3555 ofpbuf_uninit(request
);
3561 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(*replyp
, sizeof *nlmsg
);
3562 struct genlmsghdr
*genl
= ofpbuf_try_pull(*replyp
, sizeof *genl
);
3563 struct ovs_header
*ovs_header
= ofpbuf_try_pull(*replyp
,
3564 sizeof *ovs_header
);
3565 if (!nlmsg
|| !genl
|| !ovs_header
3566 || nlmsg
->nlmsg_type
!= ovs_meter_family
3567 || !nl_policy_parse(*replyp
, 0, reply_policy
, a
, size_a
)) {
3568 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3570 "Kernel module response to meter tranaction is invalid");
3577 dpif_netlink_meter_get_features(const struct dpif
*dpif_
,
3578 struct ofputil_meter_features
*features
)
3580 if (probe_broken_meters(CONST_CAST(struct dpif
*, dpif_
))) {
3585 struct ofpbuf buf
, *msg
;
3586 uint64_t stub
[1024 / 8];
3588 static const struct nl_policy ovs_meter_features_policy
[] = {
3589 [OVS_METER_ATTR_MAX_METERS
] = { .type
= NL_A_U32
},
3590 [OVS_METER_ATTR_MAX_BANDS
] = { .type
= NL_A_U32
},
3591 [OVS_METER_ATTR_BANDS
] = { .type
= NL_A_NESTED
, .optional
= true },
3593 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_features_policy
)];
3595 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3596 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
,
3597 OVS_METER_CMD_FEATURES
);
3598 if (dpif_netlink_meter_transact(&buf
, &msg
, ovs_meter_features_policy
, a
,
3599 ARRAY_SIZE(ovs_meter_features_policy
))) {
3600 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3602 "dpif_netlink_meter_transact OVS_METER_CMD_FEATURES failed");
3606 features
->max_meters
= nl_attr_get_u32(a
[OVS_METER_ATTR_MAX_METERS
]);
3607 features
->max_bands
= nl_attr_get_u32(a
[OVS_METER_ATTR_MAX_BANDS
]);
3609 /* Bands is a nested attribute of zero or more nested
3610 * band attributes. */
3611 if (a
[OVS_METER_ATTR_BANDS
]) {
3612 const struct nlattr
*nla
;
3615 NL_NESTED_FOR_EACH (nla
, left
, a
[OVS_METER_ATTR_BANDS
]) {
3616 const struct nlattr
*band_nla
;
3619 NL_NESTED_FOR_EACH (band_nla
, band_left
, nla
) {
3620 if (nl_attr_type(band_nla
) == OVS_BAND_ATTR_TYPE
) {
3621 if (nl_attr_get_size(band_nla
) == sizeof(uint32_t)) {
3622 switch (nl_attr_get_u32(band_nla
)) {
3623 case OVS_METER_BAND_TYPE_DROP
:
3624 features
->band_types
|= 1 << OFPMBT13_DROP
;
3632 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
3638 dpif_netlink_meter_set__(struct dpif
*dpif_
, ofproto_meter_id meter_id
,
3639 struct ofputil_meter_config
*config
)
3641 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3642 struct ofpbuf buf
, *msg
;
3643 uint64_t stub
[1024 / 8];
3645 static const struct nl_policy ovs_meter_set_response_policy
[] = {
3646 [OVS_METER_ATTR_ID
] = { .type
= NL_A_U32
},
3648 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_set_response_policy
)];
3650 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
) {
3651 return EBADF
; /* Unsupported flags set */
3654 for (size_t i
= 0; i
< config
->n_bands
; i
++) {
3655 switch (config
->bands
[i
].type
) {
3659 return ENODEV
; /* Unsupported band type */
3663 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
, OVS_METER_CMD_SET
);
3665 nl_msg_put_u32(&buf
, OVS_METER_ATTR_ID
, meter_id
.uint32
);
3667 if (config
->flags
& OFPMF13_KBPS
) {
3668 nl_msg_put_flag(&buf
, OVS_METER_ATTR_KBPS
);
3671 size_t bands_offset
= nl_msg_start_nested(&buf
, OVS_METER_ATTR_BANDS
);
3673 for (size_t i
= 0; i
< config
->n_bands
; ++i
) {
3674 struct ofputil_meter_band
* band
= &config
->bands
[i
];
3677 size_t band_offset
= nl_msg_start_nested(&buf
, OVS_BAND_ATTR_UNSPEC
);
3679 switch (band
->type
) {
3681 band_type
= OVS_METER_BAND_TYPE_DROP
;
3684 band_type
= OVS_METER_BAND_TYPE_UNSPEC
;
3686 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_TYPE
, band_type
);
3687 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_RATE
, band
->rate
);
3688 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_BURST
,
3689 config
->flags
& OFPMF13_BURST
?
3690 band
->burst_size
: band
->rate
);
3691 nl_msg_end_nested(&buf
, band_offset
);
3693 nl_msg_end_nested(&buf
, bands_offset
);
3695 int error
= dpif_netlink_meter_transact(&buf
, &msg
,
3696 ovs_meter_set_response_policy
, a
,
3697 ARRAY_SIZE(ovs_meter_set_response_policy
));
3699 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3701 "dpif_netlink_meter_transact OVS_METER_CMD_SET failed");
3705 if (nl_attr_get_u32(a
[OVS_METER_ATTR_ID
]) != meter_id
.uint32
) {
3706 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3708 "Kernel returned a different meter id than requested");
3715 dpif_netlink_meter_set(struct dpif
*dpif_
, ofproto_meter_id meter_id
,
3716 struct ofputil_meter_config
*config
)
3718 if (probe_broken_meters(dpif_
)) {
3722 return dpif_netlink_meter_set__(dpif_
, meter_id
, config
);
3725 /* Retrieve statistics and/or delete meter 'meter_id'. Statistics are
3726 * stored in 'stats', if it is not null. If 'command' is
3727 * OVS_METER_CMD_DEL, the meter is deleted and statistics are optionally
3728 * retrieved. If 'command' is OVS_METER_CMD_GET, then statistics are
3729 * simply retrieved. */
3731 dpif_netlink_meter_get_stats(const struct dpif
*dpif_
,
3732 ofproto_meter_id meter_id
,
3733 struct ofputil_meter_stats
*stats
,
3735 enum ovs_meter_cmd command
)
3737 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3738 struct ofpbuf buf
, *msg
;
3739 uint64_t stub
[1024 / 8];
3741 static const struct nl_policy ovs_meter_stats_policy
[] = {
3742 [OVS_METER_ATTR_ID
] = { .type
= NL_A_U32
, .optional
= true},
3743 [OVS_METER_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_flow_stats
),
3745 [OVS_METER_ATTR_BANDS
] = { .type
= NL_A_NESTED
, .optional
= true },
3747 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_stats_policy
)];
3749 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
, command
);
3751 nl_msg_put_u32(&buf
, OVS_METER_ATTR_ID
, meter_id
.uint32
);
3753 int error
= dpif_netlink_meter_transact(&buf
, &msg
,
3754 ovs_meter_stats_policy
, a
,
3755 ARRAY_SIZE(ovs_meter_stats_policy
));
3757 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3758 VLOG_INFO_RL(&rl
, "dpif_netlink_meter_transact %s failed",
3759 command
== OVS_METER_CMD_GET
? "get" : "del");
3764 && a
[OVS_METER_ATTR_ID
]
3765 && a
[OVS_METER_ATTR_STATS
]
3766 && nl_attr_get_u32(a
[OVS_METER_ATTR_ID
]) == meter_id
.uint32
) {
3768 const struct ovs_flow_stats
*stat
;
3769 const struct nlattr
*nla
;
3772 stat
= nl_attr_get(a
[OVS_METER_ATTR_STATS
]);
3773 stats
->packet_in_count
= get_32aligned_u64(&stat
->n_packets
);
3774 stats
->byte_in_count
= get_32aligned_u64(&stat
->n_bytes
);
3776 if (a
[OVS_METER_ATTR_BANDS
]) {
3778 NL_NESTED_FOR_EACH (nla
, left
, a
[OVS_METER_ATTR_BANDS
]) {
3779 const struct nlattr
*band_nla
;
3780 band_nla
= nl_attr_find_nested(nla
, OVS_BAND_ATTR_STATS
);
3781 if (band_nla
&& nl_attr_get_size(band_nla
) \
3782 == sizeof(struct ovs_flow_stats
)) {
3783 stat
= nl_attr_get(band_nla
);
3785 if (n_bands
< max_bands
) {
3786 stats
->bands
[n_bands
].packet_count
3787 = get_32aligned_u64(&stat
->n_packets
);
3788 stats
->bands
[n_bands
].byte_count
3789 = get_32aligned_u64(&stat
->n_bytes
);
3793 stats
->bands
[n_bands
].packet_count
= 0;
3794 stats
->bands
[n_bands
].byte_count
= 0;
3798 stats
->n_bands
= n_bands
;
3800 /* For a non-existent meter, return 0 stats. */
3810 dpif_netlink_meter_get(const struct dpif
*dpif
, ofproto_meter_id meter_id
,
3811 struct ofputil_meter_stats
*stats
, uint16_t max_bands
)
3813 return dpif_netlink_meter_get_stats(dpif
, meter_id
, stats
, max_bands
,
3818 dpif_netlink_meter_del(struct dpif
*dpif
, ofproto_meter_id meter_id
,
3819 struct ofputil_meter_stats
*stats
, uint16_t max_bands
)
3821 return dpif_netlink_meter_get_stats(dpif
, meter_id
, stats
, max_bands
,
3826 probe_broken_meters__(struct dpif
*dpif
)
3828 /* This test is destructive if a probe occurs while ovs-vswitchd is
3829 * running (e.g., an ovs-dpctl meter command is called), so choose a
3830 * random high meter id to make this less likely to occur. */
3831 ofproto_meter_id id1
= { 54545401 };
3832 ofproto_meter_id id2
= { 54545402 };
3833 struct ofputil_meter_band band
= {OFPMBT13_DROP
, 0, 1, 0};
3834 struct ofputil_meter_config config1
= { 1, OFPMF13_KBPS
, 1, &band
};
3835 struct ofputil_meter_config config2
= { 2, OFPMF13_KBPS
, 1, &band
};
3837 /* Try adding two meters and make sure that they both come back with
3838 * the proper meter id. Use the "__" version so that we don't cause
3839 * a recurve deadlock. */
3840 dpif_netlink_meter_set__(dpif
, id1
, &config1
);
3841 dpif_netlink_meter_set__(dpif
, id2
, &config2
);
3843 if (dpif_netlink_meter_get(dpif
, id1
, NULL
, 0)
3844 || dpif_netlink_meter_get(dpif
, id2
, NULL
, 0)) {
3845 VLOG_INFO("The kernel module has a broken meter implementation.");
3849 dpif_netlink_meter_del(dpif
, id1
, NULL
, 0);
3850 dpif_netlink_meter_del(dpif
, id2
, NULL
, 0);
3856 probe_broken_meters(struct dpif
*dpif
)
3858 /* This is a once-only test because currently OVS only has at most a single
3859 * Netlink capable datapath on any given platform. */
3860 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3862 static bool broken_meters
= false;
3863 if (ovsthread_once_start(&once
)) {
3864 broken_meters
= probe_broken_meters__(dpif
);
3865 ovsthread_once_done(&once
);
3867 return broken_meters
;
3870 const struct dpif_class dpif_netlink_class
= {
3872 false, /* cleanup_required */
3874 dpif_netlink_enumerate
,
3878 dpif_netlink_destroy
,
3881 dpif_netlink_get_stats
,
3882 dpif_netlink_port_add
,
3883 dpif_netlink_port_del
,
3884 NULL
, /* port_set_config */
3885 dpif_netlink_port_query_by_number
,
3886 dpif_netlink_port_query_by_name
,
3887 dpif_netlink_port_get_pid
,
3888 dpif_netlink_port_dump_start
,
3889 dpif_netlink_port_dump_next
,
3890 dpif_netlink_port_dump_done
,
3891 dpif_netlink_port_poll
,
3892 dpif_netlink_port_poll_wait
,
3893 dpif_netlink_flow_flush
,
3894 dpif_netlink_flow_dump_create
,
3895 dpif_netlink_flow_dump_destroy
,
3896 dpif_netlink_flow_dump_thread_create
,
3897 dpif_netlink_flow_dump_thread_destroy
,
3898 dpif_netlink_flow_dump_next
,
3899 dpif_netlink_operate
,
3900 dpif_netlink_recv_set
,
3901 dpif_netlink_handlers_set
,
3902 NULL
, /* set_config */
3903 dpif_netlink_queue_to_priority
,
3905 dpif_netlink_recv_wait
,
3906 dpif_netlink_recv_purge
,
3907 NULL
, /* register_dp_purge_cb */
3908 NULL
, /* register_upcall_cb */
3909 NULL
, /* enable_upcall */
3910 NULL
, /* disable_upcall */
3911 dpif_netlink_get_datapath_version
, /* get_datapath_version */
3912 dpif_netlink_ct_dump_start
,
3913 dpif_netlink_ct_dump_next
,
3914 dpif_netlink_ct_dump_done
,
3915 dpif_netlink_ct_flush
,
3916 NULL
, /* ct_set_maxconns */
3917 NULL
, /* ct_get_maxconns */
3918 NULL
, /* ct_get_nconns */
3919 NULL
, /* ct_set_tcp_seq_chk */
3920 NULL
, /* ct_get_tcp_seq_chk */
3921 dpif_netlink_ct_set_limits
,
3922 dpif_netlink_ct_get_limits
,
3923 dpif_netlink_ct_del_limits
,
3924 dpif_netlink_ct_set_timeout_policy
,
3925 dpif_netlink_ct_get_timeout_policy
,
3926 dpif_netlink_ct_del_timeout_policy
,
3927 dpif_netlink_ct_timeout_policy_dump_start
,
3928 dpif_netlink_ct_timeout_policy_dump_next
,
3929 dpif_netlink_ct_timeout_policy_dump_done
,
3930 dpif_netlink_ct_get_timeout_policy_name
,
3931 NULL
, /* ipf_set_enabled */
3932 NULL
, /* ipf_set_min_frag */
3933 NULL
, /* ipf_set_max_nfrags */
3934 NULL
, /* ipf_get_status */
3935 NULL
, /* ipf_dump_start */
3936 NULL
, /* ipf_dump_next */
3937 NULL
, /* ipf_dump_done */
3938 dpif_netlink_meter_get_features
,
3939 dpif_netlink_meter_set
,
3940 dpif_netlink_meter_get
,
3941 dpif_netlink_meter_del
,
3945 dpif_netlink_init(void)
3947 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3950 if (ovsthread_once_start(&once
)) {
3951 error
= nl_lookup_genl_family(OVS_DATAPATH_FAMILY
,
3952 &ovs_datapath_family
);
3954 VLOG_INFO("Generic Netlink family '%s' does not exist. "
3955 "The Open vSwitch kernel module is probably not loaded.",
3956 OVS_DATAPATH_FAMILY
);
3959 error
= nl_lookup_genl_family(OVS_VPORT_FAMILY
, &ovs_vport_family
);
3962 error
= nl_lookup_genl_family(OVS_FLOW_FAMILY
, &ovs_flow_family
);
3965 error
= nl_lookup_genl_family(OVS_PACKET_FAMILY
,
3966 &ovs_packet_family
);
3969 error
= nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY
, OVS_VPORT_MCGROUP
,
3970 &ovs_vport_mcgroup
);
3973 if (nl_lookup_genl_family(OVS_METER_FAMILY
, &ovs_meter_family
)) {
3974 VLOG_INFO("The kernel module does not support meters.");
3977 if (nl_lookup_genl_family(OVS_CT_LIMIT_FAMILY
,
3978 &ovs_ct_limit_family
) < 0) {
3979 VLOG_INFO("Generic Netlink family '%s' does not exist. "
3980 "Please update the Open vSwitch kernel module to enable "
3981 "the conntrack limit feature.", OVS_CT_LIMIT_FAMILY
);
3984 ovs_tunnels_out_of_tree
= dpif_netlink_rtnl_probe_oot_tunnels();
3986 ovsthread_once_done(&once
);
3993 dpif_netlink_is_internal_device(const char *name
)
3995 struct dpif_netlink_vport reply
;
3999 error
= dpif_netlink_vport_get(name
, &reply
, &buf
);
4002 } else if (error
!= ENODEV
&& error
!= ENOENT
) {
4003 VLOG_WARN_RL(&error_rl
, "%s: vport query failed (%s)",
4004 name
, ovs_strerror(error
));
4007 return reply
.type
== OVS_VPORT_TYPE_INTERNAL
;
4010 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4011 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
4012 * positive errno value.
4014 * 'vport' will contain pointers into 'buf', so the caller should not free
4015 * 'buf' while 'vport' is still in use. */
4017 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport
*vport
,
4018 const struct ofpbuf
*buf
)
4020 static const struct nl_policy ovs_vport_policy
[] = {
4021 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NL_A_U32
},
4022 [OVS_VPORT_ATTR_TYPE
] = { .type
= NL_A_U32
},
4023 [OVS_VPORT_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
4024 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NL_A_UNSPEC
},
4025 [OVS_VPORT_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_vport_stats
),
4027 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
4028 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NL_A_U32
, .optional
= true },
4031 dpif_netlink_vport_init(vport
);
4033 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4034 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4035 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4036 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4038 struct nlattr
*a
[ARRAY_SIZE(ovs_vport_policy
)];
4039 if (!nlmsg
|| !genl
|| !ovs_header
4040 || nlmsg
->nlmsg_type
!= ovs_vport_family
4041 || !nl_policy_parse(&b
, 0, ovs_vport_policy
, a
,
4042 ARRAY_SIZE(ovs_vport_policy
))) {
4046 vport
->cmd
= genl
->cmd
;
4047 vport
->dp_ifindex
= ovs_header
->dp_ifindex
;
4048 vport
->port_no
= nl_attr_get_odp_port(a
[OVS_VPORT_ATTR_PORT_NO
]);
4049 vport
->type
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
4050 vport
->name
= nl_attr_get_string(a
[OVS_VPORT_ATTR_NAME
]);
4051 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
4052 vport
->n_upcall_pids
= nl_attr_get_size(a
[OVS_VPORT_ATTR_UPCALL_PID
])
4053 / (sizeof *vport
->upcall_pids
);
4054 vport
->upcall_pids
= nl_attr_get(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
4057 if (a
[OVS_VPORT_ATTR_STATS
]) {
4058 vport
->stats
= nl_attr_get(a
[OVS_VPORT_ATTR_STATS
]);
4060 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
4061 vport
->options
= nl_attr_get(a
[OVS_VPORT_ATTR_OPTIONS
]);
4062 vport
->options_len
= nl_attr_get_size(a
[OVS_VPORT_ATTR_OPTIONS
]);
4064 if (a
[OVS_VPORT_ATTR_NETNSID
]) {
4065 netnsid_set(&vport
->netnsid
,
4066 nl_attr_get_u32(a
[OVS_VPORT_ATTR_NETNSID
]));
4068 netnsid_set_local(&vport
->netnsid
);
4073 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
4074 * followed by Netlink attributes corresponding to 'vport'. */
4076 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport
*vport
,
4079 struct ovs_header
*ovs_header
;
4081 nl_msg_put_genlmsghdr(buf
, 0, ovs_vport_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
4082 vport
->cmd
, OVS_VPORT_VERSION
);
4084 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4085 ovs_header
->dp_ifindex
= vport
->dp_ifindex
;
4087 if (vport
->port_no
!= ODPP_NONE
) {
4088 nl_msg_put_odp_port(buf
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
);
4091 if (vport
->type
!= OVS_VPORT_TYPE_UNSPEC
) {
4092 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_TYPE
, vport
->type
);
4096 nl_msg_put_string(buf
, OVS_VPORT_ATTR_NAME
, vport
->name
);
4099 if (vport
->upcall_pids
) {
4100 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_UPCALL_PID
,
4102 vport
->n_upcall_pids
* sizeof *vport
->upcall_pids
);
4106 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_STATS
,
4107 vport
->stats
, sizeof *vport
->stats
);
4110 if (vport
->options
) {
4111 nl_msg_put_nested(buf
, OVS_VPORT_ATTR_OPTIONS
,
4112 vport
->options
, vport
->options_len
);
4116 /* Clears 'vport' to "empty" values. */
4118 dpif_netlink_vport_init(struct dpif_netlink_vport
*vport
)
4120 memset(vport
, 0, sizeof *vport
);
4121 vport
->port_no
= ODPP_NONE
;
4124 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4125 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4126 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4127 * result of the command is expected to be an ovs_vport also, which is decoded
4128 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
4129 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
4131 dpif_netlink_vport_transact(const struct dpif_netlink_vport
*request
,
4132 struct dpif_netlink_vport
*reply
,
4133 struct ofpbuf
**bufp
)
4135 struct ofpbuf
*request_buf
;
4138 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4140 error
= dpif_netlink_init();
4144 dpif_netlink_vport_init(reply
);
4149 request_buf
= ofpbuf_new(1024);
4150 dpif_netlink_vport_to_ofpbuf(request
, request_buf
);
4151 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4152 ofpbuf_delete(request_buf
);
4156 error
= dpif_netlink_vport_from_ofpbuf(reply
, *bufp
);
4159 dpif_netlink_vport_init(reply
);
4160 ofpbuf_delete(*bufp
);
4167 /* Obtains information about the kernel vport named 'name' and stores it into
4168 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
4169 * longer needed ('reply' will contain pointers into '*bufp'). */
4171 dpif_netlink_vport_get(const char *name
, struct dpif_netlink_vport
*reply
,
4172 struct ofpbuf
**bufp
)
4174 struct dpif_netlink_vport request
;
4176 dpif_netlink_vport_init(&request
);
4177 request
.cmd
= OVS_VPORT_CMD_GET
;
4178 request
.name
= name
;
4180 return dpif_netlink_vport_transact(&request
, reply
, bufp
);
4183 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4184 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
4185 * positive errno value.
4187 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
4188 * while 'dp' is still in use. */
4190 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp
*dp
, const struct ofpbuf
*buf
)
4192 static const struct nl_policy ovs_datapath_policy
[] = {
4193 [OVS_DP_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
4194 [OVS_DP_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_dp_stats
),
4196 [OVS_DP_ATTR_MEGAFLOW_STATS
] = {
4197 NL_POLICY_FOR(struct ovs_dp_megaflow_stats
),
4201 dpif_netlink_dp_init(dp
);
4203 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4204 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4205 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4206 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4208 struct nlattr
*a
[ARRAY_SIZE(ovs_datapath_policy
)];
4209 if (!nlmsg
|| !genl
|| !ovs_header
4210 || nlmsg
->nlmsg_type
!= ovs_datapath_family
4211 || !nl_policy_parse(&b
, 0, ovs_datapath_policy
, a
,
4212 ARRAY_SIZE(ovs_datapath_policy
))) {
4216 dp
->cmd
= genl
->cmd
;
4217 dp
->dp_ifindex
= ovs_header
->dp_ifindex
;
4218 dp
->name
= nl_attr_get_string(a
[OVS_DP_ATTR_NAME
]);
4219 if (a
[OVS_DP_ATTR_STATS
]) {
4220 dp
->stats
= nl_attr_get(a
[OVS_DP_ATTR_STATS
]);
4223 if (a
[OVS_DP_ATTR_MEGAFLOW_STATS
]) {
4224 dp
->megaflow_stats
= nl_attr_get(a
[OVS_DP_ATTR_MEGAFLOW_STATS
]);
4230 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
4232 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp
*dp
, struct ofpbuf
*buf
)
4234 struct ovs_header
*ovs_header
;
4236 nl_msg_put_genlmsghdr(buf
, 0, ovs_datapath_family
,
4237 NLM_F_REQUEST
| NLM_F_ECHO
, dp
->cmd
,
4238 OVS_DATAPATH_VERSION
);
4240 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4241 ovs_header
->dp_ifindex
= dp
->dp_ifindex
;
4244 nl_msg_put_string(buf
, OVS_DP_ATTR_NAME
, dp
->name
);
4247 if (dp
->upcall_pid
) {
4248 nl_msg_put_u32(buf
, OVS_DP_ATTR_UPCALL_PID
, *dp
->upcall_pid
);
4251 if (dp
->user_features
) {
4252 nl_msg_put_u32(buf
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
);
4255 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
4258 /* Clears 'dp' to "empty" values. */
4260 dpif_netlink_dp_init(struct dpif_netlink_dp
*dp
)
4262 memset(dp
, 0, sizeof *dp
);
4266 dpif_netlink_dp_dump_start(struct nl_dump
*dump
)
4268 struct dpif_netlink_dp request
;
4271 dpif_netlink_dp_init(&request
);
4272 request
.cmd
= OVS_DP_CMD_GET
;
4274 buf
= ofpbuf_new(1024);
4275 dpif_netlink_dp_to_ofpbuf(&request
, buf
);
4276 nl_dump_start(dump
, NETLINK_GENERIC
, buf
);
4280 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4281 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4282 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4283 * result of the command is expected to be of the same form, which is decoded
4284 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
4285 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
4287 dpif_netlink_dp_transact(const struct dpif_netlink_dp
*request
,
4288 struct dpif_netlink_dp
*reply
, struct ofpbuf
**bufp
)
4290 struct ofpbuf
*request_buf
;
4293 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4295 request_buf
= ofpbuf_new(1024);
4296 dpif_netlink_dp_to_ofpbuf(request
, request_buf
);
4297 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4298 ofpbuf_delete(request_buf
);
4301 dpif_netlink_dp_init(reply
);
4303 error
= dpif_netlink_dp_from_ofpbuf(reply
, *bufp
);
4306 ofpbuf_delete(*bufp
);
4313 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
4314 * The caller must free '*bufp' when the reply is no longer needed ('reply'
4315 * will contain pointers into '*bufp'). */
4317 dpif_netlink_dp_get(const struct dpif
*dpif_
, struct dpif_netlink_dp
*reply
,
4318 struct ofpbuf
**bufp
)
4320 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
4321 struct dpif_netlink_dp request
;
4323 dpif_netlink_dp_init(&request
);
4324 request
.cmd
= OVS_DP_CMD_GET
;
4325 request
.dp_ifindex
= dpif
->dp_ifindex
;
4327 return dpif_netlink_dp_transact(&request
, reply
, bufp
);
4330 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4331 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
4332 * positive errno value.
4334 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
4335 * while 'flow' is still in use. */
4337 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow
*flow
,
4338 const struct ofpbuf
*buf
)
4340 static const struct nl_policy ovs_flow_policy
[__OVS_FLOW_ATTR_MAX
] = {
4341 [OVS_FLOW_ATTR_KEY
] = { .type
= NL_A_NESTED
, .optional
= true },
4342 [OVS_FLOW_ATTR_MASK
] = { .type
= NL_A_NESTED
, .optional
= true },
4343 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
4344 [OVS_FLOW_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_flow_stats
),
4346 [OVS_FLOW_ATTR_TCP_FLAGS
] = { .type
= NL_A_U8
, .optional
= true },
4347 [OVS_FLOW_ATTR_USED
] = { .type
= NL_A_U64
, .optional
= true },
4348 [OVS_FLOW_ATTR_UFID
] = { .type
= NL_A_U128
, .optional
= true },
4349 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
4350 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
4351 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
4354 dpif_netlink_flow_init(flow
);
4356 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4357 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4358 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4359 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4361 struct nlattr
*a
[ARRAY_SIZE(ovs_flow_policy
)];
4362 if (!nlmsg
|| !genl
|| !ovs_header
4363 || nlmsg
->nlmsg_type
!= ovs_flow_family
4364 || !nl_policy_parse(&b
, 0, ovs_flow_policy
, a
,
4365 ARRAY_SIZE(ovs_flow_policy
))) {
4368 if (!a
[OVS_FLOW_ATTR_KEY
] && !a
[OVS_FLOW_ATTR_UFID
]) {
4372 flow
->nlmsg_flags
= nlmsg
->nlmsg_flags
;
4373 flow
->dp_ifindex
= ovs_header
->dp_ifindex
;
4374 if (a
[OVS_FLOW_ATTR_KEY
]) {
4375 flow
->key
= nl_attr_get(a
[OVS_FLOW_ATTR_KEY
]);
4376 flow
->key_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_KEY
]);
4379 if (a
[OVS_FLOW_ATTR_UFID
]) {
4380 flow
->ufid
= nl_attr_get_u128(a
[OVS_FLOW_ATTR_UFID
]);
4381 flow
->ufid_present
= true;
4383 if (a
[OVS_FLOW_ATTR_MASK
]) {
4384 flow
->mask
= nl_attr_get(a
[OVS_FLOW_ATTR_MASK
]);
4385 flow
->mask_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_MASK
]);
4387 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
4388 flow
->actions
= nl_attr_get(a
[OVS_FLOW_ATTR_ACTIONS
]);
4389 flow
->actions_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_ACTIONS
]);
4391 if (a
[OVS_FLOW_ATTR_STATS
]) {
4392 flow
->stats
= nl_attr_get(a
[OVS_FLOW_ATTR_STATS
]);
4394 if (a
[OVS_FLOW_ATTR_TCP_FLAGS
]) {
4395 flow
->tcp_flags
= nl_attr_get(a
[OVS_FLOW_ATTR_TCP_FLAGS
]);
4397 if (a
[OVS_FLOW_ATTR_USED
]) {
4398 flow
->used
= nl_attr_get(a
[OVS_FLOW_ATTR_USED
]);
4405 * If PACKET_TYPE attribute is present in 'data', it filters PACKET_TYPE out.
4406 * If the flow is not Ethernet, the OVS_KEY_ATTR_PACKET_TYPE is converted to
4407 * OVS_KEY_ATTR_ETHERTYPE. Puts 'data' to 'buf'.
4410 put_exclude_packet_type(struct ofpbuf
*buf
, uint16_t type
,
4411 const struct nlattr
*data
, uint16_t data_len
)
4413 const struct nlattr
*packet_type
;
4415 packet_type
= nl_attr_find__(data
, data_len
, OVS_KEY_ATTR_PACKET_TYPE
);
4418 /* exclude PACKET_TYPE Netlink attribute. */
4419 ovs_assert(NLA_ALIGN(packet_type
->nla_len
) == NL_A_U32_SIZE
);
4420 size_t packet_type_len
= NL_A_U32_SIZE
;
4421 size_t first_chunk_size
= (uint8_t *)packet_type
- (uint8_t *)data
;
4422 size_t second_chunk_size
= data_len
- first_chunk_size
4424 struct nlattr
*next_attr
= nl_attr_next(packet_type
);
4427 ofs
= nl_msg_start_nested(buf
, type
);
4428 nl_msg_put(buf
, data
, first_chunk_size
);
4429 nl_msg_put(buf
, next_attr
, second_chunk_size
);
4430 if (!nl_attr_find__(data
, data_len
, OVS_KEY_ATTR_ETHERNET
)) {
4431 ovs_be16 pt
= pt_ns_type_be(nl_attr_get_be32(packet_type
));
4432 const struct nlattr
*nla
;
4434 nla
= nl_attr_find(buf
, ofs
+ NLA_HDRLEN
, OVS_KEY_ATTR_ETHERTYPE
);
4436 ovs_be16
*ethertype
;
4438 ethertype
= CONST_CAST(ovs_be16
*, nl_attr_get(nla
));
4441 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, pt
);
4444 nl_msg_end_nested(buf
, ofs
);
4446 nl_msg_put_unspec(buf
, type
, data
, data_len
);
4450 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
4451 * followed by Netlink attributes corresponding to 'flow'. */
4453 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow
*flow
,
4456 struct ovs_header
*ovs_header
;
4458 nl_msg_put_genlmsghdr(buf
, 0, ovs_flow_family
,
4459 NLM_F_REQUEST
| flow
->nlmsg_flags
,
4460 flow
->cmd
, OVS_FLOW_VERSION
);
4462 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4463 ovs_header
->dp_ifindex
= flow
->dp_ifindex
;
4465 if (flow
->ufid_present
) {
4466 nl_msg_put_u128(buf
, OVS_FLOW_ATTR_UFID
, flow
->ufid
);
4468 if (flow
->ufid_terse
) {
4469 nl_msg_put_u32(buf
, OVS_FLOW_ATTR_UFID_FLAGS
,
4470 OVS_UFID_F_OMIT_KEY
| OVS_UFID_F_OMIT_MASK
4471 | OVS_UFID_F_OMIT_ACTIONS
);
4473 if (!flow
->ufid_terse
|| !flow
->ufid_present
) {
4474 if (flow
->key_len
) {
4475 put_exclude_packet_type(buf
, OVS_FLOW_ATTR_KEY
, flow
->key
,
4478 if (flow
->mask_len
) {
4479 put_exclude_packet_type(buf
, OVS_FLOW_ATTR_MASK
, flow
->mask
,
4482 if (flow
->actions
|| flow
->actions_len
) {
4483 nl_msg_put_unspec(buf
, OVS_FLOW_ATTR_ACTIONS
,
4484 flow
->actions
, flow
->actions_len
);
4488 /* We never need to send these to the kernel. */
4489 ovs_assert(!flow
->stats
);
4490 ovs_assert(!flow
->tcp_flags
);
4491 ovs_assert(!flow
->used
);
4494 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_CLEAR
);
4497 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_PROBE
);
4501 /* Clears 'flow' to "empty" values. */
4503 dpif_netlink_flow_init(struct dpif_netlink_flow
*flow
)
4505 memset(flow
, 0, sizeof *flow
);
4508 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4509 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4510 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4511 * result of the command is expected to be a flow also, which is decoded and
4512 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
4513 * is no longer needed ('reply' will contain pointers into '*bufp'). */
4515 dpif_netlink_flow_transact(struct dpif_netlink_flow
*request
,
4516 struct dpif_netlink_flow
*reply
,
4517 struct ofpbuf
**bufp
)
4519 struct ofpbuf
*request_buf
;
4522 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4525 request
->nlmsg_flags
|= NLM_F_ECHO
;
4528 request_buf
= ofpbuf_new(1024);
4529 dpif_netlink_flow_to_ofpbuf(request
, request_buf
);
4530 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4531 ofpbuf_delete(request_buf
);
4535 error
= dpif_netlink_flow_from_ofpbuf(reply
, *bufp
);
4538 dpif_netlink_flow_init(reply
);
4539 ofpbuf_delete(*bufp
);
4547 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow
*flow
,
4548 struct dpif_flow_stats
*stats
)
4551 stats
->n_packets
= get_32aligned_u64(&flow
->stats
->n_packets
);
4552 stats
->n_bytes
= get_32aligned_u64(&flow
->stats
->n_bytes
);
4554 stats
->n_packets
= 0;
4557 stats
->used
= flow
->used
? get_32aligned_u64(flow
->used
) : 0;
4558 stats
->tcp_flags
= flow
->tcp_flags
? *flow
->tcp_flags
: 0;
4561 /* Logs information about a packet that was recently lost in 'ch' (in
4564 report_loss(struct dpif_netlink
*dpif
, struct dpif_channel
*ch
, uint32_t ch_idx
,
4565 uint32_t handler_id
)
4567 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
4570 if (VLOG_DROP_WARN(&rl
)) {
4575 if (ch
->last_poll
!= LLONG_MIN
) {
4576 ds_put_format(&s
, " (last polled %lld ms ago)",
4577 time_msec() - ch
->last_poll
);
4580 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
4581 dpif_name(&dpif
->dpif
), ch_idx
, handler_id
);