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
**socksp
)
253 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
256 return nl_sock_create(NETLINK_GENERIC
, socksp
);
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 *socksp
= 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
*socksp
)
283 nl_sock_destroy(socksp
);
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
*socksp
)
455 struct epoll_event event
;
456 uint32_t port_idx
= odp_to_u32(port_no
);
460 if (dpif
->handlers
== NULL
) {
464 /* We assume that the datapath densely chooses port numbers, which can
465 * therefore be used as an index into 'channels' and 'epoll_events' of
467 if (port_idx
>= dpif
->uc_array_size
) {
468 uint32_t new_size
= port_idx
+ 1;
470 if (new_size
> MAX_PORTS
) {
471 VLOG_WARN_RL(&error_rl
, "%s: datapath port %"PRIu32
" too big",
472 dpif_name(&dpif
->dpif
), port_no
);
476 dpif
->channels
= xrealloc(dpif
->channels
,
477 new_size
* sizeof *dpif
->channels
);
479 for (i
= dpif
->uc_array_size
; i
< new_size
; i
++) {
480 dpif
->channels
[i
].sock
= NULL
;
483 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
484 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
486 handler
->epoll_events
= xrealloc(handler
->epoll_events
,
487 new_size
* sizeof *handler
->epoll_events
);
490 dpif
->uc_array_size
= new_size
;
493 memset(&event
, 0, sizeof event
);
494 event
.events
= EPOLLIN
| EPOLLEXCLUSIVE
;
495 event
.data
.u32
= port_idx
;
497 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
498 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
501 if (epoll_ctl(handler
->epoll_fd
, EPOLL_CTL_ADD
, nl_sock_fd(socksp
),
508 dpif
->channels
[port_idx
].sock
= socksp
;
509 dpif
->channels
[port_idx
].last_poll
= LLONG_MIN
;
516 epoll_ctl(dpif
->handlers
[i
].epoll_fd
, EPOLL_CTL_DEL
,
517 nl_sock_fd(socksp
), NULL
);
520 dpif
->channels
[port_idx
].sock
= NULL
;
526 vport_del_channels(struct dpif_netlink
*dpif
, odp_port_t port_no
)
528 uint32_t port_idx
= odp_to_u32(port_no
);
531 if (!dpif
->handlers
|| port_idx
>= dpif
->uc_array_size
532 || !dpif
->channels
[port_idx
].sock
) {
536 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
537 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
539 epoll_ctl(handler
->epoll_fd
, EPOLL_CTL_DEL
,
540 nl_sock_fd(dpif
->channels
[port_idx
].sock
), NULL
);
542 handler
->event_offset
= handler
->n_events
= 0;
545 nl_sock_destroy(dpif
->channels
[port_idx
].sock
);
547 dpif
->channels
[port_idx
].sock
= NULL
;
551 destroy_all_channels(struct dpif_netlink
*dpif
)
552 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
556 if (!dpif
->handlers
) {
560 for (i
= 0; i
< dpif
->uc_array_size
; i
++ ) {
561 struct dpif_netlink_vport vport_request
;
562 uint32_t upcall_pids
= 0;
564 if (!dpif
->channels
[i
].sock
) {
568 /* Turn off upcalls. */
569 dpif_netlink_vport_init(&vport_request
);
570 vport_request
.cmd
= OVS_VPORT_CMD_SET
;
571 vport_request
.dp_ifindex
= dpif
->dp_ifindex
;
572 vport_request
.port_no
= u32_to_odp(i
);
573 vport_request
.n_upcall_pids
= 1;
574 vport_request
.upcall_pids
= &upcall_pids
;
575 dpif_netlink_vport_transact(&vport_request
, NULL
, NULL
);
577 vport_del_channels(dpif
, u32_to_odp(i
));
580 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
581 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
583 dpif_netlink_handler_uninit(handler
);
584 free(handler
->epoll_events
);
586 free(dpif
->channels
);
587 free(dpif
->handlers
);
588 dpif
->handlers
= NULL
;
589 dpif
->channels
= NULL
;
590 dpif
->n_handlers
= 0;
591 dpif
->uc_array_size
= 0;
595 dpif_netlink_close(struct dpif
*dpif_
)
597 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
599 nl_sock_destroy(dpif
->port_notifier
);
601 fat_rwlock_wrlock(&dpif
->upcall_lock
);
602 destroy_all_channels(dpif
);
603 fat_rwlock_unlock(&dpif
->upcall_lock
);
605 fat_rwlock_destroy(&dpif
->upcall_lock
);
610 dpif_netlink_destroy(struct dpif
*dpif_
)
612 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
613 struct dpif_netlink_dp dp
;
615 dpif_netlink_dp_init(&dp
);
616 dp
.cmd
= OVS_DP_CMD_DEL
;
617 dp
.dp_ifindex
= dpif
->dp_ifindex
;
618 return dpif_netlink_dp_transact(&dp
, NULL
, NULL
);
622 dpif_netlink_run(struct dpif
*dpif_
)
624 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
626 if (dpif
->refresh_channels
) {
627 dpif
->refresh_channels
= false;
628 fat_rwlock_wrlock(&dpif
->upcall_lock
);
629 dpif_netlink_refresh_channels(dpif
, dpif
->n_handlers
);
630 fat_rwlock_unlock(&dpif
->upcall_lock
);
636 dpif_netlink_get_stats(const struct dpif
*dpif_
, struct dpif_dp_stats
*stats
)
638 struct dpif_netlink_dp dp
;
642 error
= dpif_netlink_dp_get(dpif_
, &dp
, &buf
);
644 memset(stats
, 0, sizeof *stats
);
647 stats
->n_hit
= get_32aligned_u64(&dp
.stats
->n_hit
);
648 stats
->n_missed
= get_32aligned_u64(&dp
.stats
->n_missed
);
649 stats
->n_lost
= get_32aligned_u64(&dp
.stats
->n_lost
);
650 stats
->n_flows
= get_32aligned_u64(&dp
.stats
->n_flows
);
653 if (dp
.megaflow_stats
) {
654 stats
->n_masks
= dp
.megaflow_stats
->n_masks
;
655 stats
->n_mask_hit
= get_32aligned_u64(
656 &dp
.megaflow_stats
->n_mask_hit
);
658 stats
->n_masks
= UINT32_MAX
;
659 stats
->n_mask_hit
= UINT64_MAX
;
667 get_vport_type(const struct dpif_netlink_vport
*vport
)
669 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
671 switch (vport
->type
) {
672 case OVS_VPORT_TYPE_NETDEV
: {
673 const char *type
= netdev_get_type_from_name(vport
->name
);
675 return type
? type
: "system";
678 case OVS_VPORT_TYPE_INTERNAL
:
681 case OVS_VPORT_TYPE_GENEVE
:
684 case OVS_VPORT_TYPE_GRE
:
687 case OVS_VPORT_TYPE_VXLAN
:
690 case OVS_VPORT_TYPE_LISP
:
693 case OVS_VPORT_TYPE_STT
:
696 case OVS_VPORT_TYPE_ERSPAN
:
699 case OVS_VPORT_TYPE_IP6ERSPAN
:
702 case OVS_VPORT_TYPE_IP6GRE
:
705 case OVS_VPORT_TYPE_UNSPEC
:
706 case __OVS_VPORT_TYPE_MAX
:
710 VLOG_WARN_RL(&rl
, "dp%d: port `%s' has unsupported type %u",
711 vport
->dp_ifindex
, vport
->name
, (unsigned int) vport
->type
);
716 netdev_to_ovs_vport_type(const char *type
)
718 if (!strcmp(type
, "tap") || !strcmp(type
, "system")) {
719 return OVS_VPORT_TYPE_NETDEV
;
720 } else if (!strcmp(type
, "internal")) {
721 return OVS_VPORT_TYPE_INTERNAL
;
722 } else if (strstr(type
, "stt")) {
723 return OVS_VPORT_TYPE_STT
;
724 } else if (!strcmp(type
, "geneve")) {
725 return OVS_VPORT_TYPE_GENEVE
;
726 } else if (!strcmp(type
, "vxlan")) {
727 return OVS_VPORT_TYPE_VXLAN
;
728 } else if (!strcmp(type
, "lisp")) {
729 return OVS_VPORT_TYPE_LISP
;
730 } else if (!strcmp(type
, "erspan")) {
731 return OVS_VPORT_TYPE_ERSPAN
;
732 } else if (!strcmp(type
, "ip6erspan")) {
733 return OVS_VPORT_TYPE_IP6ERSPAN
;
734 } else if (!strcmp(type
, "ip6gre")) {
735 return OVS_VPORT_TYPE_IP6GRE
;
736 } else if (!strcmp(type
, "gre")) {
737 return OVS_VPORT_TYPE_GRE
;
739 return OVS_VPORT_TYPE_UNSPEC
;
744 dpif_netlink_port_add__(struct dpif_netlink
*dpif
, const char *name
,
745 enum ovs_vport_type type
,
746 struct ofpbuf
*options
,
747 odp_port_t
*port_nop
)
748 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
750 struct dpif_netlink_vport request
, reply
;
752 struct nl_sock
*socksp
= NULL
;
753 uint32_t upcall_pids
= 0;
756 if (dpif
->handlers
) {
757 error
= create_nl_sock(dpif
, &socksp
);
763 dpif_netlink_vport_init(&request
);
764 request
.cmd
= OVS_VPORT_CMD_NEW
;
765 request
.dp_ifindex
= dpif
->dp_ifindex
;
769 request
.port_no
= *port_nop
;
771 upcall_pids
= nl_sock_pid(socksp
);
773 request
.n_upcall_pids
= 1;
774 request
.upcall_pids
= &upcall_pids
;
777 request
.options
= options
->data
;
778 request
.options_len
= options
->size
;
781 error
= dpif_netlink_vport_transact(&request
, &reply
, &buf
);
783 *port_nop
= reply
.port_no
;
785 if (error
== EBUSY
&& *port_nop
!= ODPP_NONE
) {
786 VLOG_INFO("%s: requested port %"PRIu32
" is in use",
787 dpif_name(&dpif
->dpif
), *port_nop
);
790 close_nl_sock(socksp
);
794 error
= vport_add_channel(dpif
, *port_nop
, socksp
);
796 VLOG_INFO("%s: could not add channel for port %s",
797 dpif_name(&dpif
->dpif
), name
);
799 /* Delete the port. */
800 dpif_netlink_vport_init(&request
);
801 request
.cmd
= OVS_VPORT_CMD_DEL
;
802 request
.dp_ifindex
= dpif
->dp_ifindex
;
803 request
.port_no
= *port_nop
;
804 dpif_netlink_vport_transact(&request
, NULL
, NULL
);
805 close_nl_sock(socksp
);
816 dpif_netlink_port_add_compat(struct dpif_netlink
*dpif
, struct netdev
*netdev
,
817 odp_port_t
*port_nop
)
818 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
820 const struct netdev_tunnel_config
*tnl_cfg
;
821 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
822 const char *type
= netdev_get_type(netdev
);
823 uint64_t options_stub
[64 / 8];
824 enum ovs_vport_type ovs_type
;
825 struct ofpbuf options
;
828 name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
830 ovs_type
= netdev_to_ovs_vport_type(netdev_get_type(netdev
));
831 if (ovs_type
== OVS_VPORT_TYPE_UNSPEC
) {
832 VLOG_WARN_RL(&error_rl
, "%s: cannot create port `%s' because it has "
833 "unsupported type `%s'",
834 dpif_name(&dpif
->dpif
), name
, type
);
838 if (ovs_type
== OVS_VPORT_TYPE_NETDEV
) {
840 /* XXX : Map appropiate Windows handle */
842 netdev_linux_ethtool_set_flag(netdev
, ETH_FLAG_LRO
, "LRO", false);
847 if (ovs_type
== OVS_VPORT_TYPE_INTERNAL
) {
848 if (!create_wmi_port(name
)){
849 VLOG_ERR("Could not create wmi internal port with name:%s", name
);
855 tnl_cfg
= netdev_get_tunnel_config(netdev
);
856 if (tnl_cfg
&& (tnl_cfg
->dst_port
!= 0 || tnl_cfg
->exts
)) {
857 ofpbuf_use_stack(&options
, options_stub
, sizeof options_stub
);
858 if (tnl_cfg
->dst_port
) {
859 nl_msg_put_u16(&options
, OVS_TUNNEL_ATTR_DST_PORT
,
860 ntohs(tnl_cfg
->dst_port
));
866 ext_ofs
= nl_msg_start_nested(&options
, OVS_TUNNEL_ATTR_EXTENSION
);
867 for (i
= 0; i
< 32; i
++) {
868 if (tnl_cfg
->exts
& (1 << i
)) {
869 nl_msg_put_flag(&options
, i
);
872 nl_msg_end_nested(&options
, ext_ofs
);
874 return dpif_netlink_port_add__(dpif
, name
, ovs_type
, &options
,
877 return dpif_netlink_port_add__(dpif
, name
, ovs_type
, NULL
, port_nop
);
883 dpif_netlink_rtnl_port_create_and_add(struct dpif_netlink
*dpif
,
884 struct netdev
*netdev
,
885 odp_port_t
*port_nop
)
886 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
888 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
889 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
893 error
= dpif_netlink_rtnl_port_create(netdev
);
895 if (error
!= EOPNOTSUPP
) {
896 VLOG_WARN_RL(&rl
, "Failed to create %s with rtnetlink: %s",
897 netdev_get_name(netdev
), ovs_strerror(error
));
902 name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
903 error
= dpif_netlink_port_add__(dpif
, name
, OVS_VPORT_TYPE_NETDEV
, NULL
,
906 dpif_netlink_rtnl_port_destroy(name
, netdev_get_type(netdev
));
912 dpif_netlink_port_add(struct dpif
*dpif_
, struct netdev
*netdev
,
913 odp_port_t
*port_nop
)
915 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
916 int error
= EOPNOTSUPP
;
918 fat_rwlock_wrlock(&dpif
->upcall_lock
);
919 if (!ovs_tunnels_out_of_tree
) {
920 error
= dpif_netlink_rtnl_port_create_and_add(dpif
, netdev
, port_nop
);
923 error
= dpif_netlink_port_add_compat(dpif
, netdev
, port_nop
);
925 fat_rwlock_unlock(&dpif
->upcall_lock
);
931 dpif_netlink_port_del__(struct dpif_netlink
*dpif
, odp_port_t port_no
)
932 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
934 struct dpif_netlink_vport vport
;
935 struct dpif_port dpif_port
;
938 error
= dpif_netlink_port_query__(dpif
, port_no
, NULL
, &dpif_port
);
943 dpif_netlink_vport_init(&vport
);
944 vport
.cmd
= OVS_VPORT_CMD_DEL
;
945 vport
.dp_ifindex
= dpif
->dp_ifindex
;
946 vport
.port_no
= port_no
;
948 if (!strcmp(dpif_port
.type
, "internal")) {
949 if (!delete_wmi_port(dpif_port
.name
)) {
950 VLOG_ERR("Could not delete wmi port with name: %s",
955 error
= dpif_netlink_vport_transact(&vport
, NULL
, NULL
);
957 vport_del_channels(dpif
, port_no
);
959 if (!error
&& !ovs_tunnels_out_of_tree
) {
960 error
= dpif_netlink_rtnl_port_destroy(dpif_port
.name
, dpif_port
.type
);
961 if (error
== EOPNOTSUPP
) {
966 dpif_port_destroy(&dpif_port
);
972 dpif_netlink_port_del(struct dpif
*dpif_
, odp_port_t port_no
)
974 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
977 fat_rwlock_wrlock(&dpif
->upcall_lock
);
978 error
= dpif_netlink_port_del__(dpif
, port_no
);
979 fat_rwlock_unlock(&dpif
->upcall_lock
);
985 dpif_netlink_port_query__(const struct dpif_netlink
*dpif
, odp_port_t port_no
,
986 const char *port_name
, struct dpif_port
*dpif_port
)
988 struct dpif_netlink_vport request
;
989 struct dpif_netlink_vport reply
;
993 dpif_netlink_vport_init(&request
);
994 request
.cmd
= OVS_VPORT_CMD_GET
;
995 request
.dp_ifindex
= dpif
->dp_ifindex
;
996 request
.port_no
= port_no
;
997 request
.name
= port_name
;
999 error
= dpif_netlink_vport_transact(&request
, &reply
, &buf
);
1001 if (reply
.dp_ifindex
!= request
.dp_ifindex
) {
1002 /* A query by name reported that 'port_name' is in some datapath
1003 * other than 'dpif', but the caller wants to know about 'dpif'. */
1005 } else if (dpif_port
) {
1006 dpif_port
->name
= xstrdup(reply
.name
);
1007 dpif_port
->type
= xstrdup(get_vport_type(&reply
));
1008 dpif_port
->port_no
= reply
.port_no
;
1016 dpif_netlink_port_query_by_number(const struct dpif
*dpif_
, odp_port_t port_no
,
1017 struct dpif_port
*dpif_port
)
1019 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1021 return dpif_netlink_port_query__(dpif
, port_no
, NULL
, dpif_port
);
1025 dpif_netlink_port_query_by_name(const struct dpif
*dpif_
, const char *devname
,
1026 struct dpif_port
*dpif_port
)
1028 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1030 return dpif_netlink_port_query__(dpif
, 0, devname
, dpif_port
);
1034 dpif_netlink_port_get_pid__(const struct dpif_netlink
*dpif
,
1036 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
1038 uint32_t port_idx
= odp_to_u32(port_no
);
1041 if (dpif
->handlers
&& dpif
->uc_array_size
> 0) {
1042 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1043 * channel, since it is not heavily loaded. */
1044 uint32_t idx
= port_idx
>= dpif
->uc_array_size
? 0 : port_idx
;
1046 /* Needs to check in case the socket pointer is changed in between
1047 * the holding of upcall_lock. A known case happens when the main
1048 * thread deletes the vport while the handler thread is handling
1049 * the upcall from that port. */
1050 if (dpif
->channels
[idx
].sock
) {
1051 pid
= nl_sock_pid(dpif
->channels
[idx
].sock
);
1059 dpif_netlink_port_get_pid(const struct dpif
*dpif_
, odp_port_t port_no
)
1061 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1064 fat_rwlock_rdlock(&dpif
->upcall_lock
);
1065 ret
= dpif_netlink_port_get_pid__(dpif
, port_no
);
1066 fat_rwlock_unlock(&dpif
->upcall_lock
);
1072 dpif_netlink_flow_flush(struct dpif
*dpif_
)
1074 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1075 struct dpif_netlink_flow flow
;
1077 dpif_netlink_flow_init(&flow
);
1078 flow
.cmd
= OVS_FLOW_CMD_DEL
;
1079 flow
.dp_ifindex
= dpif
->dp_ifindex
;
1081 if (netdev_is_flow_api_enabled()) {
1082 netdev_ports_flow_flush(dpif_
->dpif_class
);
1085 return dpif_netlink_flow_transact(&flow
, NULL
, NULL
);
1088 struct dpif_netlink_port_state
{
1089 struct nl_dump dump
;
1094 dpif_netlink_port_dump_start__(const struct dpif_netlink
*dpif
,
1095 struct nl_dump
*dump
)
1097 struct dpif_netlink_vport request
;
1100 dpif_netlink_vport_init(&request
);
1101 request
.cmd
= OVS_VPORT_CMD_GET
;
1102 request
.dp_ifindex
= dpif
->dp_ifindex
;
1104 buf
= ofpbuf_new(1024);
1105 dpif_netlink_vport_to_ofpbuf(&request
, buf
);
1106 nl_dump_start(dump
, NETLINK_GENERIC
, buf
);
1111 dpif_netlink_port_dump_start(const struct dpif
*dpif_
, void **statep
)
1113 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1114 struct dpif_netlink_port_state
*state
;
1116 *statep
= state
= xmalloc(sizeof *state
);
1117 dpif_netlink_port_dump_start__(dpif
, &state
->dump
);
1119 ofpbuf_init(&state
->buf
, NL_DUMP_BUFSIZE
);
1124 dpif_netlink_port_dump_next__(const struct dpif_netlink
*dpif
,
1125 struct nl_dump
*dump
,
1126 struct dpif_netlink_vport
*vport
,
1127 struct ofpbuf
*buffer
)
1132 if (!nl_dump_next(dump
, &buf
, buffer
)) {
1136 error
= dpif_netlink_vport_from_ofpbuf(vport
, &buf
);
1138 VLOG_WARN_RL(&error_rl
, "%s: failed to parse vport record (%s)",
1139 dpif_name(&dpif
->dpif
), ovs_strerror(error
));
1145 dpif_netlink_port_dump_next(const struct dpif
*dpif_
, void *state_
,
1146 struct dpif_port
*dpif_port
)
1148 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1149 struct dpif_netlink_port_state
*state
= state_
;
1150 struct dpif_netlink_vport vport
;
1153 error
= dpif_netlink_port_dump_next__(dpif
, &state
->dump
, &vport
,
1158 dpif_port
->name
= CONST_CAST(char *, vport
.name
);
1159 dpif_port
->type
= CONST_CAST(char *, get_vport_type(&vport
));
1160 dpif_port
->port_no
= vport
.port_no
;
1165 dpif_netlink_port_dump_done(const struct dpif
*dpif_ OVS_UNUSED
, void *state_
)
1167 struct dpif_netlink_port_state
*state
= state_
;
1168 int error
= nl_dump_done(&state
->dump
);
1170 ofpbuf_uninit(&state
->buf
);
1176 dpif_netlink_port_poll(const struct dpif
*dpif_
, char **devnamep
)
1178 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1180 /* Lazily create the Netlink socket to listen for notifications. */
1181 if (!dpif
->port_notifier
) {
1182 struct nl_sock
*sock
;
1185 error
= nl_sock_create(NETLINK_GENERIC
, &sock
);
1190 error
= nl_sock_join_mcgroup(sock
, ovs_vport_mcgroup
);
1192 nl_sock_destroy(sock
);
1195 dpif
->port_notifier
= sock
;
1197 /* We have no idea of the current state so report that everything
1203 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1204 uint64_t buf_stub
[4096 / 8];
1208 ofpbuf_use_stub(&buf
, buf_stub
, sizeof buf_stub
);
1209 error
= nl_sock_recv(dpif
->port_notifier
, &buf
, NULL
, false);
1211 struct dpif_netlink_vport vport
;
1213 error
= dpif_netlink_vport_from_ofpbuf(&vport
, &buf
);
1215 if (vport
.dp_ifindex
== dpif
->dp_ifindex
1216 && (vport
.cmd
== OVS_VPORT_CMD_NEW
1217 || vport
.cmd
== OVS_VPORT_CMD_DEL
1218 || vport
.cmd
== OVS_VPORT_CMD_SET
)) {
1219 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8
,
1220 dpif
->dpif
.full_name
, vport
.name
, vport
.cmd
);
1221 if (vport
.cmd
== OVS_VPORT_CMD_DEL
&& dpif
->handlers
) {
1222 dpif
->refresh_channels
= true;
1224 *devnamep
= xstrdup(vport
.name
);
1225 ofpbuf_uninit(&buf
);
1229 } else if (error
!= EAGAIN
) {
1230 VLOG_WARN_RL(&rl
, "error reading or parsing netlink (%s)",
1231 ovs_strerror(error
));
1232 nl_sock_drain(dpif
->port_notifier
);
1236 ofpbuf_uninit(&buf
);
1244 dpif_netlink_port_poll_wait(const struct dpif
*dpif_
)
1246 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1248 if (dpif
->port_notifier
) {
1249 nl_sock_wait(dpif
->port_notifier
, POLLIN
);
1251 poll_immediate_wake();
1256 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow
*request
,
1257 const ovs_u128
*ufid
, bool terse
)
1260 request
->ufid
= *ufid
;
1261 request
->ufid_present
= true;
1263 request
->ufid_present
= false;
1265 request
->ufid_terse
= terse
;
1269 dpif_netlink_init_flow_get__(const struct dpif_netlink
*dpif
,
1270 const struct nlattr
*key
, size_t key_len
,
1271 const ovs_u128
*ufid
, bool terse
,
1272 struct dpif_netlink_flow
*request
)
1274 dpif_netlink_flow_init(request
);
1275 request
->cmd
= OVS_FLOW_CMD_GET
;
1276 request
->dp_ifindex
= dpif
->dp_ifindex
;
1278 request
->key_len
= key_len
;
1279 dpif_netlink_flow_init_ufid(request
, ufid
, terse
);
1283 dpif_netlink_init_flow_get(const struct dpif_netlink
*dpif
,
1284 const struct dpif_flow_get
*get
,
1285 struct dpif_netlink_flow
*request
)
1287 dpif_netlink_init_flow_get__(dpif
, get
->key
, get
->key_len
, get
->ufid
,
1292 dpif_netlink_flow_get__(const struct dpif_netlink
*dpif
,
1293 const struct nlattr
*key
, size_t key_len
,
1294 const ovs_u128
*ufid
, bool terse
,
1295 struct dpif_netlink_flow
*reply
, struct ofpbuf
**bufp
)
1297 struct dpif_netlink_flow request
;
1299 dpif_netlink_init_flow_get__(dpif
, key
, key_len
, ufid
, terse
, &request
);
1300 return dpif_netlink_flow_transact(&request
, reply
, bufp
);
1304 dpif_netlink_flow_get(const struct dpif_netlink
*dpif
,
1305 const struct dpif_netlink_flow
*flow
,
1306 struct dpif_netlink_flow
*reply
, struct ofpbuf
**bufp
)
1308 return dpif_netlink_flow_get__(dpif
, flow
->key
, flow
->key_len
,
1309 flow
->ufid_present
? &flow
->ufid
: NULL
,
1310 false, reply
, bufp
);
1314 dpif_netlink_init_flow_put(struct dpif_netlink
*dpif
,
1315 const struct dpif_flow_put
*put
,
1316 struct dpif_netlink_flow
*request
)
1318 static const struct nlattr dummy_action
;
1320 dpif_netlink_flow_init(request
);
1321 request
->cmd
= (put
->flags
& DPIF_FP_CREATE
1322 ? OVS_FLOW_CMD_NEW
: OVS_FLOW_CMD_SET
);
1323 request
->dp_ifindex
= dpif
->dp_ifindex
;
1324 request
->key
= put
->key
;
1325 request
->key_len
= put
->key_len
;
1326 request
->mask
= put
->mask
;
1327 request
->mask_len
= put
->mask_len
;
1328 dpif_netlink_flow_init_ufid(request
, put
->ufid
, false);
1330 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1331 request
->actions
= (put
->actions
1333 : CONST_CAST(struct nlattr
*, &dummy_action
));
1334 request
->actions_len
= put
->actions_len
;
1335 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
1336 request
->clear
= true;
1338 if (put
->flags
& DPIF_FP_PROBE
) {
1339 request
->probe
= true;
1341 request
->nlmsg_flags
= put
->flags
& DPIF_FP_MODIFY
? 0 : NLM_F_CREATE
;
1345 dpif_netlink_init_flow_del__(struct dpif_netlink
*dpif
,
1346 const struct nlattr
*key
, size_t key_len
,
1347 const ovs_u128
*ufid
, bool terse
,
1348 struct dpif_netlink_flow
*request
)
1350 dpif_netlink_flow_init(request
);
1351 request
->cmd
= OVS_FLOW_CMD_DEL
;
1352 request
->dp_ifindex
= dpif
->dp_ifindex
;
1354 request
->key_len
= key_len
;
1355 dpif_netlink_flow_init_ufid(request
, ufid
, terse
);
1359 dpif_netlink_init_flow_del(struct dpif_netlink
*dpif
,
1360 const struct dpif_flow_del
*del
,
1361 struct dpif_netlink_flow
*request
)
1363 dpif_netlink_init_flow_del__(dpif
, del
->key
, del
->key_len
,
1364 del
->ufid
, del
->terse
, request
);
1367 struct dpif_netlink_flow_dump
{
1368 struct dpif_flow_dump up
;
1369 struct nl_dump nl_dump
;
1371 struct netdev_flow_dump
**netdev_dumps
;
1372 int netdev_dumps_num
; /* Number of netdev_flow_dumps */
1373 struct ovs_mutex netdev_lock
; /* Guards the following. */
1374 int netdev_current_dump OVS_GUARDED
; /* Shared current dump */
1375 struct dpif_flow_dump_types types
; /* Type of dump */
1378 static struct dpif_netlink_flow_dump
*
1379 dpif_netlink_flow_dump_cast(struct dpif_flow_dump
*dump
)
1381 return CONTAINER_OF(dump
, struct dpif_netlink_flow_dump
, up
);
1385 start_netdev_dump(const struct dpif
*dpif_
,
1386 struct dpif_netlink_flow_dump
*dump
)
1388 ovs_mutex_init(&dump
->netdev_lock
);
1390 if (!(dump
->types
.netdev_flows
)) {
1391 dump
->netdev_dumps_num
= 0;
1392 dump
->netdev_dumps
= NULL
;
1396 ovs_mutex_lock(&dump
->netdev_lock
);
1397 dump
->netdev_current_dump
= 0;
1399 = netdev_ports_flow_dump_create(dpif_
->dpif_class
,
1400 &dump
->netdev_dumps_num
);
1401 ovs_mutex_unlock(&dump
->netdev_lock
);
1405 dpif_netlink_populate_flow_dump_types(struct dpif_netlink_flow_dump
*dump
,
1406 struct dpif_flow_dump_types
*types
)
1409 dump
->types
.ovs_flows
= true;
1410 dump
->types
.netdev_flows
= true;
1412 memcpy(&dump
->types
, types
, sizeof *types
);
1416 static struct dpif_flow_dump
*
1417 dpif_netlink_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
1418 struct dpif_flow_dump_types
*types
)
1420 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1421 struct dpif_netlink_flow_dump
*dump
;
1422 struct dpif_netlink_flow request
;
1425 dump
= xmalloc(sizeof *dump
);
1426 dpif_flow_dump_init(&dump
->up
, dpif_
);
1428 dpif_netlink_populate_flow_dump_types(dump
, types
);
1430 if (dump
->types
.ovs_flows
) {
1431 dpif_netlink_flow_init(&request
);
1432 request
.cmd
= OVS_FLOW_CMD_GET
;
1433 request
.dp_ifindex
= dpif
->dp_ifindex
;
1434 request
.ufid_present
= false;
1435 request
.ufid_terse
= terse
;
1437 buf
= ofpbuf_new(1024);
1438 dpif_netlink_flow_to_ofpbuf(&request
, buf
);
1439 nl_dump_start(&dump
->nl_dump
, NETLINK_GENERIC
, buf
);
1442 atomic_init(&dump
->status
, 0);
1443 dump
->up
.terse
= terse
;
1445 start_netdev_dump(dpif_
, dump
);
1451 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
1453 struct dpif_netlink_flow_dump
*dump
= dpif_netlink_flow_dump_cast(dump_
);
1454 unsigned int nl_status
= 0;
1457 if (dump
->types
.ovs_flows
) {
1458 nl_status
= nl_dump_done(&dump
->nl_dump
);
1461 for (int i
= 0; i
< dump
->netdev_dumps_num
; i
++) {
1462 int err
= netdev_flow_dump_destroy(dump
->netdev_dumps
[i
]);
1464 if (err
!= 0 && err
!= EOPNOTSUPP
) {
1465 VLOG_ERR("failed dumping netdev: %s", ovs_strerror(err
));
1469 free(dump
->netdev_dumps
);
1470 ovs_mutex_destroy(&dump
->netdev_lock
);
1472 /* No other thread has access to 'dump' at this point. */
1473 atomic_read_relaxed(&dump
->status
, &dump_status
);
1475 return dump_status
? dump_status
: nl_status
;
1478 struct dpif_netlink_flow_dump_thread
{
1479 struct dpif_flow_dump_thread up
;
1480 struct dpif_netlink_flow_dump
*dump
;
1481 struct dpif_netlink_flow flow
;
1482 struct dpif_flow_stats stats
;
1483 struct ofpbuf nl_flows
; /* Always used to store flows. */
1484 struct ofpbuf
*nl_actions
; /* Used if kernel does not supply actions. */
1485 int netdev_dump_idx
; /* This thread current netdev dump index */
1486 bool netdev_done
; /* If we are finished dumping netdevs */
1488 /* (Key/Mask/Actions) Buffers for netdev dumping */
1489 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
1490 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
1491 struct odputil_keybuf actbuf
[FLOW_DUMP_MAX_BATCH
];
1494 static struct dpif_netlink_flow_dump_thread
*
1495 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
1497 return CONTAINER_OF(thread
, struct dpif_netlink_flow_dump_thread
, up
);
1500 static struct dpif_flow_dump_thread
*
1501 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
1503 struct dpif_netlink_flow_dump
*dump
= dpif_netlink_flow_dump_cast(dump_
);
1504 struct dpif_netlink_flow_dump_thread
*thread
;
1506 thread
= xmalloc(sizeof *thread
);
1507 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
1508 thread
->dump
= dump
;
1509 ofpbuf_init(&thread
->nl_flows
, NL_DUMP_BUFSIZE
);
1510 thread
->nl_actions
= NULL
;
1511 thread
->netdev_dump_idx
= 0;
1512 thread
->netdev_done
= !(thread
->netdev_dump_idx
< dump
->netdev_dumps_num
);
1518 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
1520 struct dpif_netlink_flow_dump_thread
*thread
1521 = dpif_netlink_flow_dump_thread_cast(thread_
);
1523 ofpbuf_uninit(&thread
->nl_flows
);
1524 ofpbuf_delete(thread
->nl_actions
);
1529 dpif_netlink_flow_to_dpif_flow(struct dpif
*dpif
, struct dpif_flow
*dpif_flow
,
1530 const struct dpif_netlink_flow
*datapath_flow
)
1532 dpif_flow
->key
= datapath_flow
->key
;
1533 dpif_flow
->key_len
= datapath_flow
->key_len
;
1534 dpif_flow
->mask
= datapath_flow
->mask
;
1535 dpif_flow
->mask_len
= datapath_flow
->mask_len
;
1536 dpif_flow
->actions
= datapath_flow
->actions
;
1537 dpif_flow
->actions_len
= datapath_flow
->actions_len
;
1538 dpif_flow
->ufid_present
= datapath_flow
->ufid_present
;
1539 dpif_flow
->pmd_id
= PMD_ID_NULL
;
1540 if (datapath_flow
->ufid_present
) {
1541 dpif_flow
->ufid
= datapath_flow
->ufid
;
1543 ovs_assert(datapath_flow
->key
&& datapath_flow
->key_len
);
1544 dpif_flow_hash(dpif
, datapath_flow
->key
, datapath_flow
->key_len
,
1547 dpif_netlink_flow_get_stats(datapath_flow
, &dpif_flow
->stats
);
1548 dpif_flow
->attrs
.offloaded
= false;
1549 dpif_flow
->attrs
.dp_layer
= "ovs";
1552 /* The design is such that all threads are working together on the first dump
1553 * to the last, in order (at first they all on dump 0).
1554 * When the first thread finds that the given dump is finished,
1555 * they all move to the next. If two or more threads find the same dump
1556 * is finished at the same time, the first one will advance the shared
1557 * netdev_current_dump and the others will catch up. */
1559 dpif_netlink_advance_netdev_dump(struct dpif_netlink_flow_dump_thread
*thread
)
1561 struct dpif_netlink_flow_dump
*dump
= thread
->dump
;
1563 ovs_mutex_lock(&dump
->netdev_lock
);
1564 /* if we haven't finished (dumped everything) */
1565 if (dump
->netdev_current_dump
< dump
->netdev_dumps_num
) {
1566 /* if we are the first to find that current dump is finished
1568 if (thread
->netdev_dump_idx
== dump
->netdev_current_dump
) {
1569 thread
->netdev_dump_idx
= ++dump
->netdev_current_dump
;
1570 /* did we just finish the last dump? done. */
1571 if (dump
->netdev_current_dump
== dump
->netdev_dumps_num
) {
1572 thread
->netdev_done
= true;
1575 /* otherwise, we are behind, catch up */
1576 thread
->netdev_dump_idx
= dump
->netdev_current_dump
;
1579 /* some other thread finished */
1580 thread
->netdev_done
= true;
1582 ovs_mutex_unlock(&dump
->netdev_lock
);
1586 dpif_netlink_netdev_match_to_dpif_flow(struct match
*match
,
1587 struct ofpbuf
*key_buf
,
1588 struct ofpbuf
*mask_buf
,
1589 struct nlattr
*actions
,
1590 struct dpif_flow_stats
*stats
,
1591 struct dpif_flow_attrs
*attrs
,
1593 struct dpif_flow
*flow
,
1594 bool terse OVS_UNUSED
)
1597 struct odp_flow_key_parms odp_parms
= {
1598 .flow
= &match
->flow
,
1599 .mask
= &match
->wc
.masks
,
1601 .max_vlan_headers
= 2,
1606 memset(flow
, 0, sizeof *flow
);
1609 offset
= key_buf
->size
;
1610 flow
->key
= ofpbuf_tail(key_buf
);
1611 odp_flow_key_from_flow(&odp_parms
, key_buf
);
1612 flow
->key_len
= key_buf
->size
- offset
;
1615 offset
= mask_buf
->size
;
1616 flow
->mask
= ofpbuf_tail(mask_buf
);
1617 odp_parms
.key_buf
= key_buf
;
1618 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
1619 flow
->mask_len
= mask_buf
->size
- offset
;
1622 flow
->actions
= nl_attr_get(actions
);
1623 flow
->actions_len
= nl_attr_get_size(actions
);
1626 memcpy(&flow
->stats
, stats
, sizeof *stats
);
1629 flow
->ufid_present
= true;
1632 flow
->pmd_id
= PMD_ID_NULL
;
1634 memcpy(&flow
->attrs
, attrs
, sizeof *attrs
);
1640 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
1641 struct dpif_flow
*flows
, int max_flows
)
1643 struct dpif_netlink_flow_dump_thread
*thread
1644 = dpif_netlink_flow_dump_thread_cast(thread_
);
1645 struct dpif_netlink_flow_dump
*dump
= thread
->dump
;
1646 struct dpif_netlink
*dpif
= dpif_netlink_cast(thread
->up
.dpif
);
1649 ofpbuf_delete(thread
->nl_actions
);
1650 thread
->nl_actions
= NULL
;
1653 max_flows
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
1655 while (!thread
->netdev_done
&& n_flows
< max_flows
) {
1656 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[n_flows
];
1657 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[n_flows
];
1658 struct odputil_keybuf
*actbuf
= &thread
->actbuf
[n_flows
];
1659 struct ofpbuf key
, mask
, act
;
1660 struct dpif_flow
*f
= &flows
[n_flows
];
1661 int cur
= thread
->netdev_dump_idx
;
1662 struct netdev_flow_dump
*netdev_dump
= dump
->netdev_dumps
[cur
];
1664 struct nlattr
*actions
;
1665 struct dpif_flow_stats stats
;
1666 struct dpif_flow_attrs attrs
;
1670 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
1671 ofpbuf_use_stack(&act
, actbuf
, sizeof *actbuf
);
1672 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
1673 has_next
= netdev_flow_dump_next(netdev_dump
, &match
,
1674 &actions
, &stats
, &attrs
,
1679 dpif_netlink_netdev_match_to_dpif_flow(&match
,
1689 dpif_netlink_advance_netdev_dump(thread
);
1693 if (!(dump
->types
.ovs_flows
)) {
1698 || (n_flows
< max_flows
&& thread
->nl_flows
.size
)) {
1699 struct dpif_netlink_flow datapath_flow
;
1700 struct ofpbuf nl_flow
;
1703 /* Try to grab another flow. */
1704 if (!nl_dump_next(&dump
->nl_dump
, &nl_flow
, &thread
->nl_flows
)) {
1708 /* Convert the flow to our output format. */
1709 error
= dpif_netlink_flow_from_ofpbuf(&datapath_flow
, &nl_flow
);
1711 atomic_store_relaxed(&dump
->status
, error
);
1715 if (dump
->up
.terse
|| datapath_flow
.actions
) {
1716 /* Common case: we don't want actions, or the flow includes
1718 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, &flows
[n_flows
++],
1721 /* Rare case: the flow does not include actions. Retrieve this
1722 * individual flow again to get the actions. */
1723 error
= dpif_netlink_flow_get(dpif
, &datapath_flow
,
1724 &datapath_flow
, &thread
->nl_actions
);
1725 if (error
== ENOENT
) {
1726 VLOG_DBG("dumped flow disappeared on get");
1729 VLOG_WARN("error fetching dumped flow: %s",
1730 ovs_strerror(error
));
1731 atomic_store_relaxed(&dump
->status
, error
);
1735 /* Save this flow. Then exit, because we only have one buffer to
1736 * handle this case. */
1737 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, &flows
[n_flows
++],
1746 dpif_netlink_encode_execute(int dp_ifindex
, const struct dpif_execute
*d_exec
,
1749 struct ovs_header
*k_exec
;
1752 ofpbuf_prealloc_tailroom(buf
, (64
1753 + dp_packet_size(d_exec
->packet
)
1754 + ODP_KEY_METADATA_SIZE
1755 + d_exec
->actions_len
));
1757 nl_msg_put_genlmsghdr(buf
, 0, ovs_packet_family
, NLM_F_REQUEST
,
1758 OVS_PACKET_CMD_EXECUTE
, OVS_PACKET_VERSION
);
1760 k_exec
= ofpbuf_put_uninit(buf
, sizeof *k_exec
);
1761 k_exec
->dp_ifindex
= dp_ifindex
;
1763 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_PACKET
,
1764 dp_packet_data(d_exec
->packet
),
1765 dp_packet_size(d_exec
->packet
));
1767 key_ofs
= nl_msg_start_nested(buf
, OVS_PACKET_ATTR_KEY
);
1768 odp_key_from_dp_packet(buf
, d_exec
->packet
);
1769 nl_msg_end_nested(buf
, key_ofs
);
1771 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_ACTIONS
,
1772 d_exec
->actions
, d_exec
->actions_len
);
1773 if (d_exec
->probe
) {
1774 nl_msg_put_flag(buf
, OVS_PACKET_ATTR_PROBE
);
1777 nl_msg_put_u16(buf
, OVS_PACKET_ATTR_MRU
, d_exec
->mtu
);
1781 /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1782 * Returns the number actually executed (at least 1, if 'n_ops' is
1785 dpif_netlink_operate__(struct dpif_netlink
*dpif
,
1786 struct dpif_op
**ops
, size_t n_ops
)
1789 struct nl_transaction txn
;
1791 struct ofpbuf request
;
1792 uint64_t request_stub
[1024 / 8];
1794 struct ofpbuf reply
;
1795 uint64_t reply_stub
[1024 / 8];
1796 } auxes
[OPERATE_MAX_OPS
];
1798 struct nl_transaction
*txnsp
[OPERATE_MAX_OPS
];
1801 n_ops
= MIN(n_ops
, OPERATE_MAX_OPS
);
1802 for (i
= 0; i
< n_ops
; i
++) {
1803 struct op_auxdata
*aux
= &auxes
[i
];
1804 struct dpif_op
*op
= ops
[i
];
1805 struct dpif_flow_put
*put
;
1806 struct dpif_flow_del
*del
;
1807 struct dpif_flow_get
*get
;
1808 struct dpif_netlink_flow flow
;
1810 ofpbuf_use_stub(&aux
->request
,
1811 aux
->request_stub
, sizeof aux
->request_stub
);
1812 aux
->txn
.request
= &aux
->request
;
1814 ofpbuf_use_stub(&aux
->reply
, aux
->reply_stub
, sizeof aux
->reply_stub
);
1815 aux
->txn
.reply
= NULL
;
1818 case DPIF_OP_FLOW_PUT
:
1819 put
= &op
->flow_put
;
1820 dpif_netlink_init_flow_put(dpif
, put
, &flow
);
1822 flow
.nlmsg_flags
|= NLM_F_ECHO
;
1823 aux
->txn
.reply
= &aux
->reply
;
1825 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1828 case DPIF_OP_FLOW_DEL
:
1829 del
= &op
->flow_del
;
1830 dpif_netlink_init_flow_del(dpif
, del
, &flow
);
1832 flow
.nlmsg_flags
|= NLM_F_ECHO
;
1833 aux
->txn
.reply
= &aux
->reply
;
1835 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1838 case DPIF_OP_EXECUTE
:
1839 /* Can't execute a packet that won't fit in a Netlink attribute. */
1840 if (OVS_UNLIKELY(nl_attr_oversized(
1841 dp_packet_size(op
->execute
.packet
)))) {
1842 /* Report an error immediately if this is the first operation.
1843 * Otherwise the easiest thing to do is to postpone to the next
1844 * call (when this will be the first operation). */
1846 VLOG_ERR_RL(&error_rl
,
1847 "dropping oversized %"PRIu32
"-byte packet",
1848 dp_packet_size(op
->execute
.packet
));
1849 op
->error
= ENOBUFS
;
1854 dpif_netlink_encode_execute(dpif
->dp_ifindex
, &op
->execute
,
1859 case DPIF_OP_FLOW_GET
:
1860 get
= &op
->flow_get
;
1861 dpif_netlink_init_flow_get(dpif
, get
, &flow
);
1862 aux
->txn
.reply
= get
->buffer
;
1863 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1871 for (i
= 0; i
< n_ops
; i
++) {
1872 txnsp
[i
] = &auxes
[i
].txn
;
1874 nl_transact_multiple(NETLINK_GENERIC
, txnsp
, n_ops
);
1876 for (i
= 0; i
< n_ops
; i
++) {
1877 struct op_auxdata
*aux
= &auxes
[i
];
1878 struct nl_transaction
*txn
= &auxes
[i
].txn
;
1879 struct dpif_op
*op
= ops
[i
];
1880 struct dpif_flow_put
*put
;
1881 struct dpif_flow_del
*del
;
1882 struct dpif_flow_get
*get
;
1884 op
->error
= txn
->error
;
1887 case DPIF_OP_FLOW_PUT
:
1888 put
= &op
->flow_put
;
1891 struct dpif_netlink_flow reply
;
1893 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
,
1896 dpif_netlink_flow_get_stats(&reply
, put
->stats
);
1902 case DPIF_OP_FLOW_DEL
:
1903 del
= &op
->flow_del
;
1906 struct dpif_netlink_flow reply
;
1908 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
,
1911 dpif_netlink_flow_get_stats(&reply
, del
->stats
);
1917 case DPIF_OP_EXECUTE
:
1920 case DPIF_OP_FLOW_GET
:
1921 get
= &op
->flow_get
;
1923 struct dpif_netlink_flow reply
;
1925 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
, txn
->reply
);
1927 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, get
->flow
,
1937 ofpbuf_uninit(&aux
->request
);
1938 ofpbuf_uninit(&aux
->reply
);
1945 parse_flow_get(struct dpif_netlink
*dpif
, struct dpif_flow_get
*get
)
1947 struct dpif_flow
*dpif_flow
= get
->flow
;
1949 struct nlattr
*actions
;
1950 struct dpif_flow_stats stats
;
1951 struct dpif_flow_attrs attrs
;
1953 uint64_t act_buf
[1024 / 8];
1954 struct odputil_keybuf maskbuf
;
1955 struct odputil_keybuf keybuf
;
1956 struct odputil_keybuf actbuf
;
1957 struct ofpbuf key
, mask
, act
;
1960 ofpbuf_use_stack(&buf
, &act_buf
, sizeof act_buf
);
1961 err
= netdev_ports_flow_get(dpif
->dpif
.dpif_class
, &match
,
1962 &actions
, get
->ufid
, &stats
, &attrs
, &buf
);
1967 VLOG_DBG("found flow from netdev, translating to dpif flow");
1969 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1970 ofpbuf_use_stack(&act
, &actbuf
, sizeof actbuf
);
1971 ofpbuf_use_stack(&mask
, &maskbuf
, sizeof maskbuf
);
1972 dpif_netlink_netdev_match_to_dpif_flow(&match
, &key
, &mask
, actions
,
1974 (ovs_u128
*) get
->ufid
,
1977 ofpbuf_put(get
->buffer
, nl_attr_get(actions
), nl_attr_get_size(actions
));
1978 dpif_flow
->actions
= ofpbuf_at(get
->buffer
, 0, 0);
1979 dpif_flow
->actions_len
= nl_attr_get_size(actions
);
1985 parse_flow_put(struct dpif_netlink
*dpif
, struct dpif_flow_put
*put
)
1987 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
1988 const struct dpif_class
*dpif_class
= dpif
->dpif
.dpif_class
;
1991 const struct nlattr
*nla
;
1994 struct offload_info info
;
1995 ovs_be16 dst_port
= 0;
1996 uint8_t csum_on
= false;
1999 if (put
->flags
& DPIF_FP_PROBE
) {
2003 err
= parse_key_and_mask_to_match(put
->key
, put
->key_len
, put
->mask
,
2004 put
->mask_len
, &match
);
2009 in_port
= match
.flow
.in_port
.odp_port
;
2010 dev
= netdev_ports_get(in_port
, dpif_class
);
2015 /* Get tunnel dst port */
2016 NL_ATTR_FOR_EACH(nla
, left
, put
->actions
, put
->actions_len
) {
2017 if (nl_attr_type(nla
) == OVS_ACTION_ATTR_OUTPUT
) {
2018 const struct netdev_tunnel_config
*tnl_cfg
;
2019 struct netdev
*outdev
;
2020 odp_port_t out_port
;
2022 out_port
= nl_attr_get_odp_port(nla
);
2023 outdev
= netdev_ports_get(out_port
, dpif_class
);
2028 tnl_cfg
= netdev_get_tunnel_config(outdev
);
2029 if (tnl_cfg
&& tnl_cfg
->dst_port
!= 0) {
2030 dst_port
= tnl_cfg
->dst_port
;
2033 csum_on
= tnl_cfg
->csum
;
2035 netdev_close(outdev
);
2039 info
.dpif_class
= dpif_class
;
2040 info
.tp_dst_port
= dst_port
;
2041 info
.tunnel_csum_on
= csum_on
;
2042 err
= netdev_flow_put(dev
, &match
,
2043 CONST_CAST(struct nlattr
*, put
->actions
),
2045 CONST_CAST(ovs_u128
*, put
->ufid
),
2049 if (put
->flags
& DPIF_FP_MODIFY
) {
2050 struct dpif_op
*opp
;
2053 op
.type
= DPIF_OP_FLOW_DEL
;
2054 op
.flow_del
.key
= put
->key
;
2055 op
.flow_del
.key_len
= put
->key_len
;
2056 op
.flow_del
.ufid
= put
->ufid
;
2057 op
.flow_del
.pmd_id
= put
->pmd_id
;
2058 op
.flow_del
.stats
= NULL
;
2059 op
.flow_del
.terse
= false;
2062 dpif_netlink_operate__(dpif
, &opp
, 1);
2065 VLOG_DBG("added flow");
2066 } else if (err
!= EEXIST
) {
2067 struct netdev
*oor_netdev
= NULL
;
2068 enum vlog_level level
;
2069 if (err
== ENOSPC
&& netdev_is_offload_rebalance_policy_enabled()) {
2071 * We need to set OOR on the input netdev (i.e, 'dev') for the
2072 * flow. But if the flow has a tunnel attribute (i.e, decap action,
2073 * with a virtual device like a VxLAN interface as its in-port),
2074 * then lookup and set OOR on the underlying tunnel (real) netdev.
2076 oor_netdev
= flow_get_tunnel_netdev(&match
.flow
.tunnel
);
2078 /* Not a 'tunnel' flow */
2081 netdev_set_hw_info(oor_netdev
, HW_INFO_TYPE_OOR
, true);
2083 level
= (err
== ENOSPC
|| err
== EOPNOTSUPP
) ? VLL_DBG
: VLL_ERR
;
2084 VLOG_RL(&rl
, level
, "failed to offload flow: %s: %s",
2086 (oor_netdev
? oor_netdev
->name
: dev
->name
));
2090 if (err
&& err
!= EEXIST
&& (put
->flags
& DPIF_FP_MODIFY
)) {
2091 /* Modified rule can't be offloaded, try and delete from HW */
2092 int del_err
= netdev_flow_del(dev
, put
->ufid
, put
->stats
);
2095 /* Delete from hw success, so old flow was offloaded.
2096 * Change flags to create the flow in kernel */
2097 put
->flags
&= ~DPIF_FP_MODIFY
;
2098 put
->flags
|= DPIF_FP_CREATE
;
2099 } else if (del_err
!= ENOENT
) {
2100 VLOG_ERR_RL(&rl
, "failed to delete offloaded flow: %s",
2101 ovs_strerror(del_err
));
2102 /* stop proccesing the flow in kernel */
2113 try_send_to_netdev(struct dpif_netlink
*dpif
, struct dpif_op
*op
)
2115 int err
= EOPNOTSUPP
;
2118 case DPIF_OP_FLOW_PUT
: {
2119 struct dpif_flow_put
*put
= &op
->flow_put
;
2125 log_flow_put_message(&dpif
->dpif
, &this_module
, put
, 0);
2126 err
= parse_flow_put(dpif
, put
);
2129 case DPIF_OP_FLOW_DEL
: {
2130 struct dpif_flow_del
*del
= &op
->flow_del
;
2136 log_flow_del_message(&dpif
->dpif
, &this_module
, del
, 0);
2137 err
= netdev_ports_flow_del(dpif
->dpif
.dpif_class
, del
->ufid
,
2141 case DPIF_OP_FLOW_GET
: {
2142 struct dpif_flow_get
*get
= &op
->flow_get
;
2144 if (!op
->flow_get
.ufid
) {
2148 log_flow_get_message(&dpif
->dpif
, &this_module
, get
, 0);
2149 err
= parse_flow_get(dpif
, get
);
2152 case DPIF_OP_EXECUTE
:
2161 dpif_netlink_operate_chunks(struct dpif_netlink
*dpif
, struct dpif_op
**ops
,
2165 size_t chunk
= dpif_netlink_operate__(dpif
, ops
, n_ops
);
2173 dpif_netlink_operate(struct dpif
*dpif_
, struct dpif_op
**ops
, size_t n_ops
,
2174 enum dpif_offload_type offload_type
)
2176 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2177 struct dpif_op
*new_ops
[OPERATE_MAX_OPS
];
2182 if (offload_type
== DPIF_OFFLOAD_ALWAYS
&& !netdev_is_flow_api_enabled()) {
2183 VLOG_DBG("Invalid offload_type: %d", offload_type
);
2187 if (offload_type
!= DPIF_OFFLOAD_NEVER
&& netdev_is_flow_api_enabled()) {
2191 while (n_ops
> 0 && count
< OPERATE_MAX_OPS
) {
2192 struct dpif_op
*op
= ops
[i
++];
2194 err
= try_send_to_netdev(dpif
, op
);
2195 if (err
&& err
!= EEXIST
) {
2196 if (offload_type
== DPIF_OFFLOAD_ALWAYS
) {
2197 /* We got an error while offloading an op. Since
2198 * OFFLOAD_ALWAYS is specified, we stop further
2199 * processing and return to the caller without
2200 * invoking kernel datapath as fallback. But the
2201 * interface requires us to process all n_ops; so
2202 * return the same error in the remaining ops too.
2213 new_ops
[count
++] = op
;
2221 dpif_netlink_operate_chunks(dpif
, new_ops
, count
);
2223 } else if (offload_type
!= DPIF_OFFLOAD_ALWAYS
) {
2224 dpif_netlink_operate_chunks(dpif
, ops
, n_ops
);
2230 dpif_netlink_handler_uninit(struct dpif_handler
*handler
)
2232 vport_delete_sock_pool(handler
);
2236 dpif_netlink_handler_init(struct dpif_handler
*handler
)
2238 return vport_create_sock_pool(handler
);
2243 dpif_netlink_handler_init(struct dpif_handler
*handler
)
2245 handler
->epoll_fd
= epoll_create(10);
2246 return handler
->epoll_fd
< 0 ? errno
: 0;
2250 dpif_netlink_handler_uninit(struct dpif_handler
*handler
)
2252 close(handler
->epoll_fd
);
2256 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
2257 * currently in 'dpif' in the kernel, by adding a new set of channels for
2258 * any kernel vport that lacks one and deleting any channels that have no
2259 * backing kernel vports. */
2261 dpif_netlink_refresh_channels(struct dpif_netlink
*dpif
, uint32_t n_handlers
)
2262 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2264 unsigned long int *keep_channels
;
2265 struct dpif_netlink_vport vport
;
2266 size_t keep_channels_nbits
;
2267 struct nl_dump dump
;
2268 uint64_t reply_stub
[NL_DUMP_BUFSIZE
/ 8];
2273 ovs_assert(!WINDOWS
|| n_handlers
<= 1);
2274 ovs_assert(!WINDOWS
|| dpif
->n_handlers
<= 1);
2276 if (dpif
->n_handlers
!= n_handlers
) {
2277 destroy_all_channels(dpif
);
2278 dpif
->handlers
= xzalloc(n_handlers
* sizeof *dpif
->handlers
);
2279 for (i
= 0; i
< n_handlers
; i
++) {
2281 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
2283 error
= dpif_netlink_handler_init(handler
);
2287 for (j
= 0; j
< i
; j
++) {
2288 struct dpif_handler
*tmp
= &dpif
->handlers
[j
];
2289 dpif_netlink_handler_uninit(tmp
);
2291 free(dpif
->handlers
);
2292 dpif
->handlers
= NULL
;
2297 dpif
->n_handlers
= n_handlers
;
2300 for (i
= 0; i
< n_handlers
; i
++) {
2301 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
2303 handler
->event_offset
= handler
->n_events
= 0;
2306 keep_channels_nbits
= dpif
->uc_array_size
;
2307 keep_channels
= bitmap_allocate(keep_channels_nbits
);
2309 ofpbuf_use_stub(&buf
, reply_stub
, sizeof reply_stub
);
2310 dpif_netlink_port_dump_start__(dpif
, &dump
);
2311 while (!dpif_netlink_port_dump_next__(dpif
, &dump
, &vport
, &buf
)) {
2312 uint32_t port_no
= odp_to_u32(vport
.port_no
);
2313 uint32_t upcall_pid
;
2316 if (port_no
>= dpif
->uc_array_size
2317 || !vport_get_pid(dpif
, port_no
, &upcall_pid
)) {
2318 struct nl_sock
*socksp
;
2319 error
= create_nl_sock(dpif
, &socksp
);
2325 error
= vport_add_channel(dpif
, vport
.port_no
, socksp
);
2327 VLOG_INFO("%s: could not add channels for port %s",
2328 dpif_name(&dpif
->dpif
), vport
.name
);
2329 nl_sock_destroy(socksp
);
2333 upcall_pid
= nl_sock_pid(socksp
);
2336 /* Configure the vport to deliver misses to 'sock'. */
2337 if (vport
.upcall_pids
[0] == 0
2338 || vport
.n_upcall_pids
!= 1
2339 || upcall_pid
!= vport
.upcall_pids
[0]) {
2340 struct dpif_netlink_vport vport_request
;
2342 dpif_netlink_vport_init(&vport_request
);
2343 vport_request
.cmd
= OVS_VPORT_CMD_SET
;
2344 vport_request
.dp_ifindex
= dpif
->dp_ifindex
;
2345 vport_request
.port_no
= vport
.port_no
;
2346 vport_request
.n_upcall_pids
= 1;
2347 vport_request
.upcall_pids
= &upcall_pid
;
2348 error
= dpif_netlink_vport_transact(&vport_request
, NULL
, NULL
);
2350 VLOG_WARN_RL(&error_rl
,
2351 "%s: failed to set upcall pid on port: %s",
2352 dpif_name(&dpif
->dpif
), ovs_strerror(error
));
2354 if (error
!= ENODEV
&& error
!= ENOENT
) {
2357 /* The vport isn't really there, even though the dump says
2358 * it is. Probably we just hit a race after a port
2365 if (port_no
< keep_channels_nbits
) {
2366 bitmap_set1(keep_channels
, port_no
);
2371 vport_del_channels(dpif
, vport
.port_no
);
2373 nl_dump_done(&dump
);
2374 ofpbuf_uninit(&buf
);
2376 /* Discard any saved channels that we didn't reuse. */
2377 for (i
= 0; i
< keep_channels_nbits
; i
++) {
2378 if (!bitmap_is_set(keep_channels
, i
)) {
2379 vport_del_channels(dpif
, u32_to_odp(i
));
2382 free(keep_channels
);
2388 dpif_netlink_recv_set__(struct dpif_netlink
*dpif
, bool enable
)
2389 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2391 if ((dpif
->handlers
!= NULL
) == enable
) {
2393 } else if (!enable
) {
2394 destroy_all_channels(dpif
);
2397 return dpif_netlink_refresh_channels(dpif
, 1);
2402 dpif_netlink_recv_set(struct dpif
*dpif_
, bool enable
)
2404 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2407 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2408 error
= dpif_netlink_recv_set__(dpif
, enable
);
2409 fat_rwlock_unlock(&dpif
->upcall_lock
);
2415 dpif_netlink_handlers_set(struct dpif
*dpif_
, uint32_t n_handlers
)
2417 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2421 /* Multiple upcall handlers will be supported once kernel datapath supports
2423 if (n_handlers
> 1) {
2428 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2429 if (dpif
->handlers
) {
2430 error
= dpif_netlink_refresh_channels(dpif
, n_handlers
);
2432 fat_rwlock_unlock(&dpif
->upcall_lock
);
2438 dpif_netlink_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2439 uint32_t queue_id
, uint32_t *priority
)
2441 if (queue_id
< 0xf000) {
2442 *priority
= TC_H_MAKE(1 << 16, queue_id
+ 1);
2450 parse_odp_packet(const struct dpif_netlink
*dpif
, struct ofpbuf
*buf
,
2451 struct dpif_upcall
*upcall
, int *dp_ifindex
)
2453 static const struct nl_policy ovs_packet_policy
[] = {
2454 /* Always present. */
2455 [OVS_PACKET_ATTR_PACKET
] = { .type
= NL_A_UNSPEC
,
2456 .min_len
= ETH_HEADER_LEN
},
2457 [OVS_PACKET_ATTR_KEY
] = { .type
= NL_A_NESTED
},
2459 /* OVS_PACKET_CMD_ACTION only. */
2460 [OVS_PACKET_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
, .optional
= true },
2461 [OVS_PACKET_ATTR_EGRESS_TUN_KEY
] = { .type
= NL_A_NESTED
, .optional
= true },
2462 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
2463 [OVS_PACKET_ATTR_MRU
] = { .type
= NL_A_U16
, .optional
= true }
2466 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
2467 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
2468 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
2469 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
2471 struct nlattr
*a
[ARRAY_SIZE(ovs_packet_policy
)];
2472 if (!nlmsg
|| !genl
|| !ovs_header
2473 || nlmsg
->nlmsg_type
!= ovs_packet_family
2474 || !nl_policy_parse(&b
, 0, ovs_packet_policy
, a
,
2475 ARRAY_SIZE(ovs_packet_policy
))) {
2479 int type
= (genl
->cmd
== OVS_PACKET_CMD_MISS
? DPIF_UC_MISS
2480 : genl
->cmd
== OVS_PACKET_CMD_ACTION
? DPIF_UC_ACTION
2486 /* (Re)set ALL fields of '*upcall' on successful return. */
2487 upcall
->type
= type
;
2488 upcall
->key
= CONST_CAST(struct nlattr
*,
2489 nl_attr_get(a
[OVS_PACKET_ATTR_KEY
]));
2490 upcall
->key_len
= nl_attr_get_size(a
[OVS_PACKET_ATTR_KEY
]);
2491 dpif_flow_hash(&dpif
->dpif
, upcall
->key
, upcall
->key_len
, &upcall
->ufid
);
2492 upcall
->userdata
= a
[OVS_PACKET_ATTR_USERDATA
];
2493 upcall
->out_tun_key
= a
[OVS_PACKET_ATTR_EGRESS_TUN_KEY
];
2494 upcall
->actions
= a
[OVS_PACKET_ATTR_ACTIONS
];
2495 upcall
->mru
= a
[OVS_PACKET_ATTR_MRU
];
2497 /* Allow overwriting the netlink attribute header without reallocating. */
2498 dp_packet_use_stub(&upcall
->packet
,
2499 CONST_CAST(struct nlattr
*,
2500 nl_attr_get(a
[OVS_PACKET_ATTR_PACKET
])) - 1,
2501 nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]) +
2502 sizeof(struct nlattr
));
2503 dp_packet_set_data(&upcall
->packet
,
2504 (char *)dp_packet_data(&upcall
->packet
) + sizeof(struct nlattr
));
2505 dp_packet_set_size(&upcall
->packet
, nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]));
2507 if (nl_attr_find__(upcall
->key
, upcall
->key_len
, OVS_KEY_ATTR_ETHERNET
)) {
2508 /* Ethernet frame */
2509 upcall
->packet
.packet_type
= htonl(PT_ETH
);
2511 /* Non-Ethernet packet. Get the Ethertype from the NL attributes */
2512 ovs_be16 ethertype
= 0;
2513 const struct nlattr
*et_nla
= nl_attr_find__(upcall
->key
,
2515 OVS_KEY_ATTR_ETHERTYPE
);
2517 ethertype
= nl_attr_get_be16(et_nla
);
2519 upcall
->packet
.packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
2521 dp_packet_set_l3(&upcall
->packet
, dp_packet_data(&upcall
->packet
));
2524 *dp_ifindex
= ovs_header
->dp_ifindex
;
2530 #define PACKET_RECV_BATCH_SIZE 50
2532 dpif_netlink_recv_windows(struct dpif_netlink
*dpif
, uint32_t handler_id
,
2533 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2534 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2536 struct dpif_handler
*handler
;
2538 struct dpif_windows_vport_sock
*sock_pool
;
2541 if (!dpif
->handlers
) {
2545 /* Only one handler is supported currently. */
2546 if (handler_id
>= 1) {
2550 if (handler_id
>= dpif
->n_handlers
) {
2554 handler
= &dpif
->handlers
[handler_id
];
2555 sock_pool
= handler
->vport_sock_pool
;
2557 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
2562 if (++read_tries
> PACKET_RECV_BATCH_SIZE
) {
2566 error
= nl_sock_recv(sock_pool
[i
].nl_sock
, buf
, NULL
, false);
2567 if (error
== ENOBUFS
) {
2568 /* ENOBUFS typically means that we've received so many
2569 * packets that the buffer overflowed. Try again
2570 * immediately because there's almost certainly a packet
2571 * waiting for us. */
2572 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2576 /* XXX: ch->last_poll = time_msec(); */
2578 if (error
== EAGAIN
) {
2584 error
= parse_odp_packet(dpif
, buf
, upcall
, &dp_ifindex
);
2585 if (!error
&& dp_ifindex
== dpif
->dp_ifindex
) {
2597 dpif_netlink_recv__(struct dpif_netlink
*dpif
, uint32_t handler_id
,
2598 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2599 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2601 struct dpif_handler
*handler
;
2604 if (!dpif
->handlers
|| handler_id
>= dpif
->n_handlers
) {
2608 handler
= &dpif
->handlers
[handler_id
];
2609 if (handler
->event_offset
>= handler
->n_events
) {
2612 handler
->event_offset
= handler
->n_events
= 0;
2615 retval
= epoll_wait(handler
->epoll_fd
, handler
->epoll_events
,
2616 dpif
->uc_array_size
, 0);
2617 } while (retval
< 0 && errno
== EINTR
);
2620 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2621 VLOG_WARN_RL(&rl
, "epoll_wait failed (%s)", ovs_strerror(errno
));
2622 } else if (retval
> 0) {
2623 handler
->n_events
= retval
;
2627 while (handler
->event_offset
< handler
->n_events
) {
2628 int idx
= handler
->epoll_events
[handler
->event_offset
].data
.u32
;
2629 struct dpif_channel
*ch
= &dpif
->channels
[idx
];
2631 handler
->event_offset
++;
2637 if (++read_tries
> 50) {
2641 error
= nl_sock_recv(ch
->sock
, buf
, NULL
, false);
2642 if (error
== ENOBUFS
) {
2643 /* ENOBUFS typically means that we've received so many
2644 * packets that the buffer overflowed. Try again
2645 * immediately because there's almost certainly a packet
2646 * waiting for us. */
2647 report_loss(dpif
, ch
, idx
, handler_id
);
2651 ch
->last_poll
= time_msec();
2653 if (error
== EAGAIN
) {
2659 error
= parse_odp_packet(dpif
, buf
, upcall
, &dp_ifindex
);
2660 if (!error
&& dp_ifindex
== dpif
->dp_ifindex
) {
2673 dpif_netlink_recv(struct dpif
*dpif_
, uint32_t handler_id
,
2674 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2676 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2679 fat_rwlock_rdlock(&dpif
->upcall_lock
);
2681 error
= dpif_netlink_recv_windows(dpif
, handler_id
, upcall
, buf
);
2683 error
= dpif_netlink_recv__(dpif
, handler_id
, upcall
, buf
);
2685 fat_rwlock_unlock(&dpif
->upcall_lock
);
2691 dpif_netlink_recv_wait__(struct dpif_netlink
*dpif
, uint32_t handler_id
)
2692 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2696 struct dpif_windows_vport_sock
*sock_pool
=
2697 dpif
->handlers
[handler_id
].vport_sock_pool
;
2699 /* Only one handler is supported currently. */
2700 if (handler_id
>= 1) {
2704 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
2705 nl_sock_wait(sock_pool
[i
].nl_sock
, POLLIN
);
2708 if (dpif
->handlers
&& handler_id
< dpif
->n_handlers
) {
2709 struct dpif_handler
*handler
= &dpif
->handlers
[handler_id
];
2711 poll_fd_wait(handler
->epoll_fd
, POLLIN
);
2717 dpif_netlink_recv_wait(struct dpif
*dpif_
, uint32_t handler_id
)
2719 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2721 fat_rwlock_rdlock(&dpif
->upcall_lock
);
2722 dpif_netlink_recv_wait__(dpif
, handler_id
);
2723 fat_rwlock_unlock(&dpif
->upcall_lock
);
2727 dpif_netlink_recv_purge__(struct dpif_netlink
*dpif
)
2728 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2730 if (dpif
->handlers
) {
2733 if (!dpif
->channels
[0].sock
) {
2736 for (i
= 0; i
< dpif
->uc_array_size
; i
++ ) {
2738 nl_sock_drain(dpif
->channels
[i
].sock
);
2744 dpif_netlink_recv_purge(struct dpif
*dpif_
)
2746 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2748 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2749 dpif_netlink_recv_purge__(dpif
);
2750 fat_rwlock_unlock(&dpif
->upcall_lock
);
2754 dpif_netlink_get_datapath_version(void)
2756 char *version_str
= NULL
;
2760 #define MAX_VERSION_STR_SIZE 80
2761 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2764 f
= fopen(LINUX_DATAPATH_VERSION_FILE
, "r");
2767 char version
[MAX_VERSION_STR_SIZE
];
2769 if (fgets(version
, MAX_VERSION_STR_SIZE
, f
)) {
2770 newline
= strchr(version
, '\n');
2774 version_str
= xstrdup(version
);
2783 struct dpif_netlink_ct_dump_state
{
2784 struct ct_dpif_dump_state up
;
2785 struct nl_ct_dump_state
*nl_ct_dump
;
2789 dpif_netlink_ct_dump_start(struct dpif
*dpif OVS_UNUSED
,
2790 struct ct_dpif_dump_state
**dump_
,
2791 const uint16_t *zone
, int *ptot_bkts
)
2793 struct dpif_netlink_ct_dump_state
*dump
;
2796 dump
= xzalloc(sizeof *dump
);
2797 err
= nl_ct_dump_start(&dump
->nl_ct_dump
, zone
, ptot_bkts
);
2809 dpif_netlink_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
2810 struct ct_dpif_dump_state
*dump_
,
2811 struct ct_dpif_entry
*entry
)
2813 struct dpif_netlink_ct_dump_state
*dump
;
2815 INIT_CONTAINER(dump
, dump_
, up
);
2817 return nl_ct_dump_next(dump
->nl_ct_dump
, entry
);
2821 dpif_netlink_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
2822 struct ct_dpif_dump_state
*dump_
)
2824 struct dpif_netlink_ct_dump_state
*dump
;
2826 INIT_CONTAINER(dump
, dump_
, up
);
2828 int err
= nl_ct_dump_done(dump
->nl_ct_dump
);
2834 dpif_netlink_ct_flush(struct dpif
*dpif OVS_UNUSED
, const uint16_t *zone
,
2835 const struct ct_dpif_tuple
*tuple
)
2838 return nl_ct_flush_tuple(tuple
, zone
? *zone
: 0);
2840 return nl_ct_flush_zone(*zone
);
2842 return nl_ct_flush();
2847 dpif_netlink_ct_set_limits(struct dpif
*dpif OVS_UNUSED
,
2848 const uint32_t *default_limits
,
2849 const struct ovs_list
*zone_limits
)
2851 struct ovs_zone_limit req_zone_limit
;
2853 if (ovs_ct_limit_family
< 0) {
2857 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
2858 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
2859 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_SET
,
2860 OVS_CT_LIMIT_VERSION
);
2862 struct ovs_header
*ovs_header
;
2863 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
2864 ovs_header
->dp_ifindex
= 0;
2867 opt_offset
= nl_msg_start_nested(request
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2868 if (default_limits
) {
2869 req_zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2870 req_zone_limit
.limit
= *default_limits
;
2871 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2874 if (!ovs_list_is_empty(zone_limits
)) {
2875 struct ct_dpif_zone_limit
*zone_limit
;
2877 LIST_FOR_EACH (zone_limit
, node
, zone_limits
) {
2878 req_zone_limit
.zone_id
= zone_limit
->zone
;
2879 req_zone_limit
.limit
= zone_limit
->limit
;
2880 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2883 nl_msg_end_nested(request
, opt_offset
);
2885 int err
= nl_transact(NETLINK_GENERIC
, request
, NULL
);
2886 ofpbuf_delete(request
);
2891 dpif_netlink_zone_limits_from_ofpbuf(const struct ofpbuf
*buf
,
2892 uint32_t *default_limit
,
2893 struct ovs_list
*zone_limits
)
2895 static const struct nl_policy ovs_ct_limit_policy
[] = {
2896 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT
] = { .type
= NL_A_NESTED
,
2900 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
2901 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
2902 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
2903 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
2905 struct nlattr
*attr
[ARRAY_SIZE(ovs_ct_limit_policy
)];
2907 if (!nlmsg
|| !genl
|| !ovs_header
2908 || nlmsg
->nlmsg_type
!= ovs_ct_limit_family
2909 || !nl_policy_parse(&b
, 0, ovs_ct_limit_policy
, attr
,
2910 ARRAY_SIZE(ovs_ct_limit_policy
))) {
2915 if (!attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]) {
2919 int rem
= NLA_ALIGN(
2920 nl_attr_get_size(attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]));
2921 const struct ovs_zone_limit
*zone_limit
=
2922 nl_attr_get(attr
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT
]);
2924 while (rem
>= sizeof *zone_limit
) {
2925 if (zone_limit
->zone_id
== OVS_ZONE_LIMIT_DEFAULT_ZONE
) {
2926 *default_limit
= zone_limit
->limit
;
2927 } else if (zone_limit
->zone_id
< OVS_ZONE_LIMIT_DEFAULT_ZONE
||
2928 zone_limit
->zone_id
> UINT16_MAX
) {
2930 ct_dpif_push_zone_limit(zone_limits
, zone_limit
->zone_id
,
2931 zone_limit
->limit
, zone_limit
->count
);
2933 rem
-= NLA_ALIGN(sizeof *zone_limit
);
2934 zone_limit
= ALIGNED_CAST(struct ovs_zone_limit
*,
2935 (unsigned char *) zone_limit
+ NLA_ALIGN(sizeof *zone_limit
));
2941 dpif_netlink_ct_get_limits(struct dpif
*dpif OVS_UNUSED
,
2942 uint32_t *default_limit
,
2943 const struct ovs_list
*zone_limits_request
,
2944 struct ovs_list
*zone_limits_reply
)
2946 if (ovs_ct_limit_family
< 0) {
2950 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
2951 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
2952 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_GET
,
2953 OVS_CT_LIMIT_VERSION
);
2955 struct ovs_header
*ovs_header
;
2956 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
2957 ovs_header
->dp_ifindex
= 0;
2959 if (!ovs_list_is_empty(zone_limits_request
)) {
2960 size_t opt_offset
= nl_msg_start_nested(request
,
2961 OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
2963 struct ovs_zone_limit req_zone_limit
;
2964 req_zone_limit
.zone_id
= OVS_ZONE_LIMIT_DEFAULT_ZONE
;
2965 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2967 struct ct_dpif_zone_limit
*zone_limit
;
2968 LIST_FOR_EACH (zone_limit
, node
, zone_limits_request
) {
2969 req_zone_limit
.zone_id
= zone_limit
->zone
;
2970 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
2973 nl_msg_end_nested(request
, opt_offset
);
2976 struct ofpbuf
*reply
;
2977 int err
= nl_transact(NETLINK_GENERIC
, request
, &reply
);
2982 err
= dpif_netlink_zone_limits_from_ofpbuf(reply
, default_limit
,
2986 ofpbuf_delete(request
);
2987 ofpbuf_delete(reply
);
2992 dpif_netlink_ct_del_limits(struct dpif
*dpif OVS_UNUSED
,
2993 const struct ovs_list
*zone_limits
)
2995 if (ovs_ct_limit_family
< 0) {
2999 struct ofpbuf
*request
= ofpbuf_new(NL_DUMP_BUFSIZE
);
3000 nl_msg_put_genlmsghdr(request
, 0, ovs_ct_limit_family
,
3001 NLM_F_REQUEST
| NLM_F_ECHO
, OVS_CT_LIMIT_CMD_DEL
,
3002 OVS_CT_LIMIT_VERSION
);
3004 struct ovs_header
*ovs_header
;
3005 ovs_header
= ofpbuf_put_uninit(request
, sizeof *ovs_header
);
3006 ovs_header
->dp_ifindex
= 0;
3008 if (!ovs_list_is_empty(zone_limits
)) {
3010 nl_msg_start_nested(request
, OVS_CT_LIMIT_ATTR_ZONE_LIMIT
);
3012 struct ct_dpif_zone_limit
*zone_limit
;
3013 LIST_FOR_EACH (zone_limit
, node
, zone_limits
) {
3014 struct ovs_zone_limit req_zone_limit
;
3015 req_zone_limit
.zone_id
= zone_limit
->zone
;
3016 nl_msg_put(request
, &req_zone_limit
, sizeof req_zone_limit
);
3018 nl_msg_end_nested(request
, opt_offset
);
3021 int err
= nl_transact(NETLINK_GENERIC
, request
, NULL
);
3023 ofpbuf_delete(request
);
3027 #define NL_TP_NAME_PREFIX "ovs_tp_"
3029 struct dpif_netlink_timeout_policy_protocol
{
3034 enum OVS_PACKED_ENUM dpif_netlink_support_timeout_policy_protocol
{
3035 DPIF_NL_TP_AF_INET_TCP
,
3036 DPIF_NL_TP_AF_INET_UDP
,
3037 DPIF_NL_TP_AF_INET_ICMP
,
3038 DPIF_NL_TP_AF_INET6_TCP
,
3039 DPIF_NL_TP_AF_INET6_UDP
,
3040 DPIF_NL_TP_AF_INET6_ICMPV6
,
3044 #define DPIF_NL_ALL_TP ((1UL << DPIF_NL_TP_MAX) - 1)
3047 static struct dpif_netlink_timeout_policy_protocol tp_protos
[] = {
3048 [DPIF_NL_TP_AF_INET_TCP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_TCP
},
3049 [DPIF_NL_TP_AF_INET_UDP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_UDP
},
3050 [DPIF_NL_TP_AF_INET_ICMP
] = { .l3num
= AF_INET
, .l4num
= IPPROTO_ICMP
},
3051 [DPIF_NL_TP_AF_INET6_TCP
] = { .l3num
= AF_INET6
, .l4num
= IPPROTO_TCP
},
3052 [DPIF_NL_TP_AF_INET6_UDP
] = { .l3num
= AF_INET6
, .l4num
= IPPROTO_UDP
},
3053 [DPIF_NL_TP_AF_INET6_ICMPV6
] = { .l3num
= AF_INET6
,
3054 .l4num
= IPPROTO_ICMPV6
},
3058 dpif_netlink_format_tp_name(uint32_t id
, uint16_t l3num
, uint8_t l4num
,
3062 ds_put_format(tp_name
, "%s%"PRIu32
"_", NL_TP_NAME_PREFIX
, id
);
3063 ct_dpif_format_ipproto(tp_name
, l4num
);
3065 if (l3num
== AF_INET
) {
3066 ds_put_cstr(tp_name
, "4");
3067 } else if (l3num
== AF_INET6
&& l4num
!= IPPROTO_ICMPV6
) {
3068 ds_put_cstr(tp_name
, "6");
3071 ovs_assert(tp_name
->length
< CTNL_TIMEOUT_NAME_MAX
);
3074 #define CT_DPIF_NL_TP_TCP_MAPPINGS \
3075 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT, SYN_SENT) \
3076 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_RECV, SYN_RECV) \
3077 CT_DPIF_NL_TP_MAPPING(TCP, TCP, ESTABLISHED, ESTABLISHED) \
3078 CT_DPIF_NL_TP_MAPPING(TCP, TCP, FIN_WAIT, FIN_WAIT) \
3079 CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE_WAIT, CLOSE_WAIT) \
3080 CT_DPIF_NL_TP_MAPPING(TCP, TCP, LAST_ACK, LAST_ACK) \
3081 CT_DPIF_NL_TP_MAPPING(TCP, TCP, TIME_WAIT, TIME_WAIT) \
3082 CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE, CLOSE) \
3083 CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT2, SYN_SENT2) \
3084 CT_DPIF_NL_TP_MAPPING(TCP, TCP, RETRANSMIT, RETRANS) \
3085 CT_DPIF_NL_TP_MAPPING(TCP, TCP, UNACK, UNACK)
3087 #define CT_DPIF_NL_TP_UDP_MAPPINGS \
3088 CT_DPIF_NL_TP_MAPPING(UDP, UDP, SINGLE, UNREPLIED) \
3089 CT_DPIF_NL_TP_MAPPING(UDP, UDP, MULTIPLE, REPLIED)
3091 #define CT_DPIF_NL_TP_ICMP_MAPPINGS \
3092 CT_DPIF_NL_TP_MAPPING(ICMP, ICMP, FIRST, TIMEOUT)
3094 #define CT_DPIF_NL_TP_ICMPV6_MAPPINGS \
3095 CT_DPIF_NL_TP_MAPPING(ICMP, ICMPV6, FIRST, TIMEOUT)
3098 #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \
3099 if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \
3100 nl_tp->present |= 1 << CTA_TIMEOUT_##PROTO2##_##ATTR2; \
3101 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2] = \
3102 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]; \
3106 dpif_netlink_get_nl_tp_tcp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3107 struct nl_ct_timeout_policy
*nl_tp
)
3109 CT_DPIF_NL_TP_TCP_MAPPINGS
3113 dpif_netlink_get_nl_tp_udp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3114 struct nl_ct_timeout_policy
*nl_tp
)
3116 CT_DPIF_NL_TP_UDP_MAPPINGS
3120 dpif_netlink_get_nl_tp_icmp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3121 struct nl_ct_timeout_policy
*nl_tp
)
3123 CT_DPIF_NL_TP_ICMP_MAPPINGS
3127 dpif_netlink_get_nl_tp_icmpv6_attrs(const struct ct_dpif_timeout_policy
*tp
,
3128 struct nl_ct_timeout_policy
*nl_tp
)
3130 CT_DPIF_NL_TP_ICMPV6_MAPPINGS
3133 #undef CT_DPIF_NL_TP_MAPPING
3136 dpif_netlink_get_nl_tp_attrs(const struct ct_dpif_timeout_policy
*tp
,
3137 uint8_t l4num
, struct nl_ct_timeout_policy
*nl_tp
)
3141 if (l4num
== IPPROTO_TCP
) {
3142 dpif_netlink_get_nl_tp_tcp_attrs(tp
, nl_tp
);
3143 } else if (l4num
== IPPROTO_UDP
) {
3144 dpif_netlink_get_nl_tp_udp_attrs(tp
, nl_tp
);
3145 } else if (l4num
== IPPROTO_ICMP
) {
3146 dpif_netlink_get_nl_tp_icmp_attrs(tp
, nl_tp
);
3147 } else if (l4num
== IPPROTO_ICMPV6
) {
3148 dpif_netlink_get_nl_tp_icmpv6_attrs(tp
, nl_tp
);
3152 #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \
3153 if (nl_tp->present & (1 << CTA_TIMEOUT_##PROTO2##_##ATTR2)) { \
3154 if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \
3155 if (tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] != \
3156 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]) { \
3157 VLOG_WARN_RL(&error_rl, "Inconsistent timeout policy %s " \
3158 "attribute %s=%"PRIu32" while %s=%"PRIu32, \
3159 nl_tp->name, "CTA_TIMEOUT_"#PROTO2"_"#ATTR2, \
3160 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2], \
3161 "CT_DPIF_TP_ATTR_"#PROTO1"_"#ATTR1, \
3162 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]); \
3165 tp->present |= 1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1; \
3166 tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] = \
3167 nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]; \
3172 dpif_netlink_set_ct_dpif_tp_tcp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3173 struct ct_dpif_timeout_policy
*tp
)
3175 CT_DPIF_NL_TP_TCP_MAPPINGS
3179 dpif_netlink_set_ct_dpif_tp_udp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3180 struct ct_dpif_timeout_policy
*tp
)
3182 CT_DPIF_NL_TP_UDP_MAPPINGS
3186 dpif_netlink_set_ct_dpif_tp_icmp_attrs(
3187 const struct nl_ct_timeout_policy
*nl_tp
,
3188 struct ct_dpif_timeout_policy
*tp
)
3190 CT_DPIF_NL_TP_ICMP_MAPPINGS
3194 dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(
3195 const struct nl_ct_timeout_policy
*nl_tp
,
3196 struct ct_dpif_timeout_policy
*tp
)
3198 CT_DPIF_NL_TP_ICMPV6_MAPPINGS
3201 #undef CT_DPIF_NL_TP_MAPPING
3204 dpif_netlink_set_ct_dpif_tp_attrs(const struct nl_ct_timeout_policy
*nl_tp
,
3205 struct ct_dpif_timeout_policy
*tp
)
3207 if (nl_tp
->l4num
== IPPROTO_TCP
) {
3208 dpif_netlink_set_ct_dpif_tp_tcp_attrs(nl_tp
, tp
);
3209 } else if (nl_tp
->l4num
== IPPROTO_UDP
) {
3210 dpif_netlink_set_ct_dpif_tp_udp_attrs(nl_tp
, tp
);
3211 } else if (nl_tp
->l4num
== IPPROTO_ICMP
) {
3212 dpif_netlink_set_ct_dpif_tp_icmp_attrs(nl_tp
, tp
);
3213 } else if (nl_tp
->l4num
== IPPROTO_ICMPV6
) {
3214 dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(nl_tp
, tp
);
3220 dpif_netlink_ct_set_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3221 const struct ct_dpif_timeout_policy
*tp
)
3227 dpif_netlink_ct_get_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3229 struct ct_dpif_timeout_policy
*tp
)
3235 dpif_netlink_ct_del_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3242 dpif_netlink_ct_timeout_policy_dump_start(struct dpif
*dpif OVS_UNUSED
,
3249 dpif_netlink_ct_timeout_policy_dump_next(struct dpif
*dpif OVS_UNUSED
,
3251 struct ct_dpif_timeout_policy
**tp
)
3257 dpif_netlink_ct_timeout_policy_dump_done(struct dpif
*dpif OVS_UNUSED
,
3264 dpif_netlink_ct_set_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3265 const struct ct_dpif_timeout_policy
*tp
)
3267 struct nl_ct_timeout_policy nl_tp
;
3268 struct ds nl_tp_name
= DS_EMPTY_INITIALIZER
;
3271 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3272 dpif_netlink_format_tp_name(tp
->id
, tp_protos
[i
].l3num
,
3273 tp_protos
[i
].l4num
, &nl_tp_name
);
3274 ovs_strlcpy(nl_tp
.name
, ds_cstr(&nl_tp_name
), sizeof nl_tp
.name
);
3275 nl_tp
.l3num
= tp_protos
[i
].l3num
;
3276 nl_tp
.l4num
= tp_protos
[i
].l4num
;
3277 dpif_netlink_get_nl_tp_attrs(tp
, tp_protos
[i
].l4num
, &nl_tp
);
3278 err
= nl_ct_set_timeout_policy(&nl_tp
);
3280 VLOG_WARN_RL(&error_rl
, "failed to add timeout policy %s (%s)",
3281 nl_tp
.name
, ovs_strerror(err
));
3287 ds_destroy(&nl_tp_name
);
3292 dpif_netlink_ct_get_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3294 struct ct_dpif_timeout_policy
*tp
)
3296 struct nl_ct_timeout_policy nl_tp
;
3297 struct ds nl_tp_name
= DS_EMPTY_INITIALIZER
;
3302 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3303 dpif_netlink_format_tp_name(tp_id
, tp_protos
[i
].l3num
,
3304 tp_protos
[i
].l4num
, &nl_tp_name
);
3305 err
= nl_ct_get_timeout_policy(ds_cstr(&nl_tp_name
), &nl_tp
);
3308 VLOG_WARN_RL(&error_rl
, "failed to get timeout policy %s (%s)",
3309 nl_tp
.name
, ovs_strerror(err
));
3312 dpif_netlink_set_ct_dpif_tp_attrs(&nl_tp
, tp
);
3316 ds_destroy(&nl_tp_name
);
3320 /* Returns 0 if all the sub timeout policies are deleted or not exist in the
3321 * kernel. Returns 1 if any sub timeout policy deletion failed. */
3323 dpif_netlink_ct_del_timeout_policy(struct dpif
*dpif OVS_UNUSED
,
3326 struct ds nl_tp_name
= DS_EMPTY_INITIALIZER
;
3329 for (int i
= 0; i
< ARRAY_SIZE(tp_protos
); ++i
) {
3330 dpif_netlink_format_tp_name(tp_id
, tp_protos
[i
].l3num
,
3331 tp_protos
[i
].l4num
, &nl_tp_name
);
3332 int err
= nl_ct_del_timeout_policy(ds_cstr(&nl_tp_name
));
3333 if (err
== ENOENT
) {
3337 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(6, 6);
3338 VLOG_INFO_RL(&rl
, "failed to delete timeout policy %s (%s)",
3339 ds_cstr(&nl_tp_name
), ovs_strerror(err
));
3344 ds_destroy(&nl_tp_name
);
3348 struct dpif_netlink_ct_timeout_policy_dump_state
{
3349 struct nl_ct_timeout_policy_dump_state
*nl_dump_state
;
3350 struct hmap tp_dump_map
;
3353 struct dpif_netlink_tp_dump_node
{
3354 struct hmap_node hmap_node
; /* node in tp_dump_map. */
3355 struct ct_dpif_timeout_policy
*tp
;
3356 uint32_t l3_l4_present
;
3359 static struct dpif_netlink_tp_dump_node
*
3360 get_dpif_netlink_tp_dump_node_by_tp_id(uint32_t tp_id
,
3361 struct hmap
*tp_dump_map
)
3363 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3365 HMAP_FOR_EACH_WITH_HASH (tp_dump_node
, hmap_node
, hash_int(tp_id
, 0),
3367 if (tp_dump_node
->tp
->id
== tp_id
) {
3368 return tp_dump_node
;
3375 update_dpif_netlink_tp_dump_node(
3376 const struct nl_ct_timeout_policy
*nl_tp
,
3377 struct dpif_netlink_tp_dump_node
*tp_dump_node
)
3379 dpif_netlink_set_ct_dpif_tp_attrs(nl_tp
, tp_dump_node
->tp
);
3380 for (int i
= 0; i
< DPIF_NL_TP_MAX
; ++i
) {
3381 if (nl_tp
->l3num
== tp_protos
[i
].l3num
&&
3382 nl_tp
->l4num
== tp_protos
[i
].l4num
) {
3383 tp_dump_node
->l3_l4_present
|= 1 << i
;
3390 dpif_netlink_ct_timeout_policy_dump_start(struct dpif
*dpif OVS_UNUSED
,
3393 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
;
3395 *statep
= dump_state
= xzalloc(sizeof *dump_state
);
3396 int err
= nl_ct_timeout_policy_dump_start(&dump_state
->nl_dump_state
);
3401 hmap_init(&dump_state
->tp_dump_map
);
3406 get_and_cleanup_tp_dump_node(struct hmap
*hmap
,
3407 struct dpif_netlink_tp_dump_node
*tp_dump_node
,
3408 struct ct_dpif_timeout_policy
*tp
)
3410 hmap_remove(hmap
, &tp_dump_node
->hmap_node
);
3411 *tp
= *tp_dump_node
->tp
;
3412 free(tp_dump_node
->tp
);
3417 dpif_netlink_ct_timeout_policy_dump_next(struct dpif
*dpif OVS_UNUSED
,
3419 struct ct_dpif_timeout_policy
*tp
)
3421 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
= state
;
3422 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3425 /* Dumps all the timeout policies in the kernel. */
3427 struct nl_ct_timeout_policy nl_tp
;
3430 err
= nl_ct_timeout_policy_dump_next(dump_state
->nl_dump_state
,
3436 /* We only interest in OVS installed timeout policies. */
3437 if (!ovs_scan(nl_tp
.name
, NL_TP_NAME_PREFIX
"%"PRIu32
, &tp_id
)) {
3441 tp_dump_node
= get_dpif_netlink_tp_dump_node_by_tp_id(
3442 tp_id
, &dump_state
->tp_dump_map
);
3443 if (!tp_dump_node
) {
3444 tp_dump_node
= xzalloc(sizeof *tp_dump_node
);
3445 tp_dump_node
->tp
= xzalloc(sizeof *tp_dump_node
->tp
);
3446 tp_dump_node
->tp
->id
= tp_id
;
3447 hmap_insert(&dump_state
->tp_dump_map
, &tp_dump_node
->hmap_node
,
3448 hash_int(tp_id
, 0));
3451 update_dpif_netlink_tp_dump_node(&nl_tp
, tp_dump_node
);
3453 /* Returns one ct_dpif_timeout_policy if we gather all the L3/L4
3455 if (tp_dump_node
->l3_l4_present
== DPIF_NL_ALL_TP
) {
3456 get_and_cleanup_tp_dump_node(&dump_state
->tp_dump_map
,
3462 /* Dump the incomplete timeout policies. */
3464 if (!hmap_is_empty(&dump_state
->tp_dump_map
)) {
3465 struct hmap_node
*hmap_node
= hmap_first(&dump_state
->tp_dump_map
);
3466 tp_dump_node
= CONTAINER_OF(hmap_node
,
3467 struct dpif_netlink_tp_dump_node
,
3469 get_and_cleanup_tp_dump_node(&dump_state
->tp_dump_map
,
3479 dpif_netlink_ct_timeout_policy_dump_done(struct dpif
*dpif OVS_UNUSED
,
3482 struct dpif_netlink_ct_timeout_policy_dump_state
*dump_state
= state
;
3483 struct dpif_netlink_tp_dump_node
*tp_dump_node
;
3485 int err
= nl_ct_timeout_policy_dump_done(dump_state
->nl_dump_state
);
3486 HMAP_FOR_EACH_POP (tp_dump_node
, hmap_node
, &dump_state
->tp_dump_map
) {
3487 free(tp_dump_node
->tp
);
3490 hmap_destroy(&dump_state
->tp_dump_map
);
3499 /* Set of supported meter flags */
3500 #define DP_SUPPORTED_METER_FLAGS_MASK \
3501 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
3503 /* Meter support was introduced in Linux 4.15. In some versions of
3504 * Linux 4.15, 4.16, and 4.17, there was a bug that never set the id
3505 * when the meter was created, so all meters essentially had an id of
3506 * zero. Check for that condition and disable meters on those kernels. */
3507 static bool probe_broken_meters(struct dpif
*);
3510 dpif_netlink_meter_init(struct dpif_netlink
*dpif
, struct ofpbuf
*buf
,
3511 void *stub
, size_t size
, uint32_t command
)
3513 ofpbuf_use_stub(buf
, stub
, size
);
3515 nl_msg_put_genlmsghdr(buf
, 0, ovs_meter_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
3516 command
, OVS_METER_VERSION
);
3518 struct ovs_header
*ovs_header
;
3519 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
3520 ovs_header
->dp_ifindex
= dpif
->dp_ifindex
;
3523 /* Execute meter 'request' in the kernel datapath. If the command
3524 * fails, returns a positive errno value. Otherwise, stores the reply
3525 * in '*replyp', parses the policy according to 'reply_policy' into the
3526 * array of Netlink attribute in 'a', and returns 0. On success, the
3527 * caller is responsible for calling ofpbuf_delete() on '*replyp'
3528 * ('replyp' will contain pointers into 'a'). */
3530 dpif_netlink_meter_transact(struct ofpbuf
*request
, struct ofpbuf
**replyp
,
3531 const struct nl_policy
*reply_policy
,
3532 struct nlattr
**a
, size_t size_a
)
3534 int error
= nl_transact(NETLINK_GENERIC
, request
, replyp
);
3535 ofpbuf_uninit(request
);
3541 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(*replyp
, sizeof *nlmsg
);
3542 struct genlmsghdr
*genl
= ofpbuf_try_pull(*replyp
, sizeof *genl
);
3543 struct ovs_header
*ovs_header
= ofpbuf_try_pull(*replyp
,
3544 sizeof *ovs_header
);
3545 if (!nlmsg
|| !genl
|| !ovs_header
3546 || nlmsg
->nlmsg_type
!= ovs_meter_family
3547 || !nl_policy_parse(*replyp
, 0, reply_policy
, a
, size_a
)) {
3548 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3550 "Kernel module response to meter tranaction is invalid");
3557 dpif_netlink_meter_get_features(const struct dpif
*dpif_
,
3558 struct ofputil_meter_features
*features
)
3560 if (probe_broken_meters(CONST_CAST(struct dpif
*, dpif_
))) {
3565 struct ofpbuf buf
, *msg
;
3566 uint64_t stub
[1024 / 8];
3568 static const struct nl_policy ovs_meter_features_policy
[] = {
3569 [OVS_METER_ATTR_MAX_METERS
] = { .type
= NL_A_U32
},
3570 [OVS_METER_ATTR_MAX_BANDS
] = { .type
= NL_A_U32
},
3571 [OVS_METER_ATTR_BANDS
] = { .type
= NL_A_NESTED
, .optional
= true },
3573 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_features_policy
)];
3575 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3576 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
,
3577 OVS_METER_CMD_FEATURES
);
3578 if (dpif_netlink_meter_transact(&buf
, &msg
, ovs_meter_features_policy
, a
,
3579 ARRAY_SIZE(ovs_meter_features_policy
))) {
3580 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3582 "dpif_netlink_meter_transact OVS_METER_CMD_FEATURES failed");
3586 features
->max_meters
= nl_attr_get_u32(a
[OVS_METER_ATTR_MAX_METERS
]);
3587 features
->max_bands
= nl_attr_get_u32(a
[OVS_METER_ATTR_MAX_BANDS
]);
3589 /* Bands is a nested attribute of zero or more nested
3590 * band attributes. */
3591 if (a
[OVS_METER_ATTR_BANDS
]) {
3592 const struct nlattr
*nla
;
3595 NL_NESTED_FOR_EACH (nla
, left
, a
[OVS_METER_ATTR_BANDS
]) {
3596 const struct nlattr
*band_nla
;
3599 NL_NESTED_FOR_EACH (band_nla
, band_left
, nla
) {
3600 if (nl_attr_type(band_nla
) == OVS_BAND_ATTR_TYPE
) {
3601 if (nl_attr_get_size(band_nla
) == sizeof(uint32_t)) {
3602 switch (nl_attr_get_u32(band_nla
)) {
3603 case OVS_METER_BAND_TYPE_DROP
:
3604 features
->band_types
|= 1 << OFPMBT13_DROP
;
3612 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
3618 dpif_netlink_meter_set__(struct dpif
*dpif_
, ofproto_meter_id meter_id
,
3619 struct ofputil_meter_config
*config
)
3621 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3622 struct ofpbuf buf
, *msg
;
3623 uint64_t stub
[1024 / 8];
3625 static const struct nl_policy ovs_meter_set_response_policy
[] = {
3626 [OVS_METER_ATTR_ID
] = { .type
= NL_A_U32
},
3628 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_set_response_policy
)];
3630 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
) {
3631 return EBADF
; /* Unsupported flags set */
3634 for (size_t i
= 0; i
< config
->n_bands
; i
++) {
3635 switch (config
->bands
[i
].type
) {
3639 return ENODEV
; /* Unsupported band type */
3643 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
, OVS_METER_CMD_SET
);
3645 nl_msg_put_u32(&buf
, OVS_METER_ATTR_ID
, meter_id
.uint32
);
3647 if (config
->flags
& OFPMF13_KBPS
) {
3648 nl_msg_put_flag(&buf
, OVS_METER_ATTR_KBPS
);
3651 size_t bands_offset
= nl_msg_start_nested(&buf
, OVS_METER_ATTR_BANDS
);
3653 for (size_t i
= 0; i
< config
->n_bands
; ++i
) {
3654 struct ofputil_meter_band
* band
= &config
->bands
[i
];
3657 size_t band_offset
= nl_msg_start_nested(&buf
, OVS_BAND_ATTR_UNSPEC
);
3659 switch (band
->type
) {
3661 band_type
= OVS_METER_BAND_TYPE_DROP
;
3664 band_type
= OVS_METER_BAND_TYPE_UNSPEC
;
3666 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_TYPE
, band_type
);
3667 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_RATE
, band
->rate
);
3668 nl_msg_put_u32(&buf
, OVS_BAND_ATTR_BURST
,
3669 config
->flags
& OFPMF13_BURST
?
3670 band
->burst_size
: band
->rate
);
3671 nl_msg_end_nested(&buf
, band_offset
);
3673 nl_msg_end_nested(&buf
, bands_offset
);
3675 int error
= dpif_netlink_meter_transact(&buf
, &msg
,
3676 ovs_meter_set_response_policy
, a
,
3677 ARRAY_SIZE(ovs_meter_set_response_policy
));
3679 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3681 "dpif_netlink_meter_transact OVS_METER_CMD_SET failed");
3685 if (nl_attr_get_u32(a
[OVS_METER_ATTR_ID
]) != meter_id
.uint32
) {
3686 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3688 "Kernel returned a different meter id than requested");
3695 dpif_netlink_meter_set(struct dpif
*dpif_
, ofproto_meter_id meter_id
,
3696 struct ofputil_meter_config
*config
)
3698 if (probe_broken_meters(dpif_
)) {
3702 return dpif_netlink_meter_set__(dpif_
, meter_id
, config
);
3705 /* Retrieve statistics and/or delete meter 'meter_id'. Statistics are
3706 * stored in 'stats', if it is not null. If 'command' is
3707 * OVS_METER_CMD_DEL, the meter is deleted and statistics are optionally
3708 * retrieved. If 'command' is OVS_METER_CMD_GET, then statistics are
3709 * simply retrieved. */
3711 dpif_netlink_meter_get_stats(const struct dpif
*dpif_
,
3712 ofproto_meter_id meter_id
,
3713 struct ofputil_meter_stats
*stats
,
3715 enum ovs_meter_cmd command
)
3717 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3718 struct ofpbuf buf
, *msg
;
3719 uint64_t stub
[1024 / 8];
3721 static const struct nl_policy ovs_meter_stats_policy
[] = {
3722 [OVS_METER_ATTR_ID
] = { .type
= NL_A_U32
, .optional
= true},
3723 [OVS_METER_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_flow_stats
),
3725 [OVS_METER_ATTR_BANDS
] = { .type
= NL_A_NESTED
, .optional
= true },
3727 struct nlattr
*a
[ARRAY_SIZE(ovs_meter_stats_policy
)];
3729 dpif_netlink_meter_init(dpif
, &buf
, stub
, sizeof stub
, command
);
3731 nl_msg_put_u32(&buf
, OVS_METER_ATTR_ID
, meter_id
.uint32
);
3733 int error
= dpif_netlink_meter_transact(&buf
, &msg
,
3734 ovs_meter_stats_policy
, a
,
3735 ARRAY_SIZE(ovs_meter_stats_policy
));
3737 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3738 VLOG_INFO_RL(&rl
, "dpif_netlink_meter_transact %s failed",
3739 command
== OVS_METER_CMD_GET
? "get" : "del");
3744 && a
[OVS_METER_ATTR_ID
]
3745 && a
[OVS_METER_ATTR_STATS
]
3746 && nl_attr_get_u32(a
[OVS_METER_ATTR_ID
]) == meter_id
.uint32
) {
3748 const struct ovs_flow_stats
*stat
;
3749 const struct nlattr
*nla
;
3752 stat
= nl_attr_get(a
[OVS_METER_ATTR_STATS
]);
3753 stats
->packet_in_count
= get_32aligned_u64(&stat
->n_packets
);
3754 stats
->byte_in_count
= get_32aligned_u64(&stat
->n_bytes
);
3756 if (a
[OVS_METER_ATTR_BANDS
]) {
3758 NL_NESTED_FOR_EACH (nla
, left
, a
[OVS_METER_ATTR_BANDS
]) {
3759 const struct nlattr
*band_nla
;
3760 band_nla
= nl_attr_find_nested(nla
, OVS_BAND_ATTR_STATS
);
3761 if (band_nla
&& nl_attr_get_size(band_nla
) \
3762 == sizeof(struct ovs_flow_stats
)) {
3763 stat
= nl_attr_get(band_nla
);
3765 if (n_bands
< max_bands
) {
3766 stats
->bands
[n_bands
].packet_count
3767 = get_32aligned_u64(&stat
->n_packets
);
3768 stats
->bands
[n_bands
].byte_count
3769 = get_32aligned_u64(&stat
->n_bytes
);
3773 stats
->bands
[n_bands
].packet_count
= 0;
3774 stats
->bands
[n_bands
].byte_count
= 0;
3778 stats
->n_bands
= n_bands
;
3780 /* For a non-existent meter, return 0 stats. */
3790 dpif_netlink_meter_get(const struct dpif
*dpif
, ofproto_meter_id meter_id
,
3791 struct ofputil_meter_stats
*stats
, uint16_t max_bands
)
3793 return dpif_netlink_meter_get_stats(dpif
, meter_id
, stats
, max_bands
,
3798 dpif_netlink_meter_del(struct dpif
*dpif
, ofproto_meter_id meter_id
,
3799 struct ofputil_meter_stats
*stats
, uint16_t max_bands
)
3801 return dpif_netlink_meter_get_stats(dpif
, meter_id
, stats
, max_bands
,
3806 probe_broken_meters__(struct dpif
*dpif
)
3808 /* This test is destructive if a probe occurs while ovs-vswitchd is
3809 * running (e.g., an ovs-dpctl meter command is called), so choose a
3810 * random high meter id to make this less likely to occur. */
3811 ofproto_meter_id id1
= { 54545401 };
3812 ofproto_meter_id id2
= { 54545402 };
3813 struct ofputil_meter_band band
= {OFPMBT13_DROP
, 0, 1, 0};
3814 struct ofputil_meter_config config1
= { 1, OFPMF13_KBPS
, 1, &band
};
3815 struct ofputil_meter_config config2
= { 2, OFPMF13_KBPS
, 1, &band
};
3817 /* Try adding two meters and make sure that they both come back with
3818 * the proper meter id. Use the "__" version so that we don't cause
3819 * a recurve deadlock. */
3820 dpif_netlink_meter_set__(dpif
, id1
, &config1
);
3821 dpif_netlink_meter_set__(dpif
, id2
, &config2
);
3823 if (dpif_netlink_meter_get(dpif
, id1
, NULL
, 0)
3824 || dpif_netlink_meter_get(dpif
, id2
, NULL
, 0)) {
3825 VLOG_INFO("The kernel module has a broken meter implementation.");
3829 dpif_netlink_meter_del(dpif
, id1
, NULL
, 0);
3830 dpif_netlink_meter_del(dpif
, id2
, NULL
, 0);
3836 probe_broken_meters(struct dpif
*dpif
)
3838 /* This is a once-only test because currently OVS only has at most a single
3839 * Netlink capable datapath on any given platform. */
3840 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3842 static bool broken_meters
= false;
3843 if (ovsthread_once_start(&once
)) {
3844 broken_meters
= probe_broken_meters__(dpif
);
3845 ovsthread_once_done(&once
);
3847 return broken_meters
;
3850 const struct dpif_class dpif_netlink_class
= {
3852 false, /* cleanup_required */
3854 dpif_netlink_enumerate
,
3858 dpif_netlink_destroy
,
3861 dpif_netlink_get_stats
,
3862 dpif_netlink_port_add
,
3863 dpif_netlink_port_del
,
3864 NULL
, /* port_set_config */
3865 dpif_netlink_port_query_by_number
,
3866 dpif_netlink_port_query_by_name
,
3867 dpif_netlink_port_get_pid
,
3868 dpif_netlink_port_dump_start
,
3869 dpif_netlink_port_dump_next
,
3870 dpif_netlink_port_dump_done
,
3871 dpif_netlink_port_poll
,
3872 dpif_netlink_port_poll_wait
,
3873 dpif_netlink_flow_flush
,
3874 dpif_netlink_flow_dump_create
,
3875 dpif_netlink_flow_dump_destroy
,
3876 dpif_netlink_flow_dump_thread_create
,
3877 dpif_netlink_flow_dump_thread_destroy
,
3878 dpif_netlink_flow_dump_next
,
3879 dpif_netlink_operate
,
3880 dpif_netlink_recv_set
,
3881 dpif_netlink_handlers_set
,
3882 NULL
, /* set_config */
3883 dpif_netlink_queue_to_priority
,
3885 dpif_netlink_recv_wait
,
3886 dpif_netlink_recv_purge
,
3887 NULL
, /* register_dp_purge_cb */
3888 NULL
, /* register_upcall_cb */
3889 NULL
, /* enable_upcall */
3890 NULL
, /* disable_upcall */
3891 dpif_netlink_get_datapath_version
, /* get_datapath_version */
3892 dpif_netlink_ct_dump_start
,
3893 dpif_netlink_ct_dump_next
,
3894 dpif_netlink_ct_dump_done
,
3895 dpif_netlink_ct_flush
,
3896 NULL
, /* ct_set_maxconns */
3897 NULL
, /* ct_get_maxconns */
3898 NULL
, /* ct_get_nconns */
3899 NULL
, /* ct_set_tcp_seq_chk */
3900 NULL
, /* ct_get_tcp_seq_chk */
3901 dpif_netlink_ct_set_limits
,
3902 dpif_netlink_ct_get_limits
,
3903 dpif_netlink_ct_del_limits
,
3904 dpif_netlink_ct_set_timeout_policy
,
3905 dpif_netlink_ct_get_timeout_policy
,
3906 dpif_netlink_ct_del_timeout_policy
,
3907 dpif_netlink_ct_timeout_policy_dump_start
,
3908 dpif_netlink_ct_timeout_policy_dump_next
,
3909 dpif_netlink_ct_timeout_policy_dump_done
,
3910 NULL
, /* ipf_set_enabled */
3911 NULL
, /* ipf_set_min_frag */
3912 NULL
, /* ipf_set_max_nfrags */
3913 NULL
, /* ipf_get_status */
3914 NULL
, /* ipf_dump_start */
3915 NULL
, /* ipf_dump_next */
3916 NULL
, /* ipf_dump_done */
3917 dpif_netlink_meter_get_features
,
3918 dpif_netlink_meter_set
,
3919 dpif_netlink_meter_get
,
3920 dpif_netlink_meter_del
,
3924 dpif_netlink_init(void)
3926 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3929 if (ovsthread_once_start(&once
)) {
3930 error
= nl_lookup_genl_family(OVS_DATAPATH_FAMILY
,
3931 &ovs_datapath_family
);
3933 VLOG_INFO("Generic Netlink family '%s' does not exist. "
3934 "The Open vSwitch kernel module is probably not loaded.",
3935 OVS_DATAPATH_FAMILY
);
3938 error
= nl_lookup_genl_family(OVS_VPORT_FAMILY
, &ovs_vport_family
);
3941 error
= nl_lookup_genl_family(OVS_FLOW_FAMILY
, &ovs_flow_family
);
3944 error
= nl_lookup_genl_family(OVS_PACKET_FAMILY
,
3945 &ovs_packet_family
);
3948 error
= nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY
, OVS_VPORT_MCGROUP
,
3949 &ovs_vport_mcgroup
);
3952 if (nl_lookup_genl_family(OVS_METER_FAMILY
, &ovs_meter_family
)) {
3953 VLOG_INFO("The kernel module does not support meters.");
3956 if (nl_lookup_genl_family(OVS_CT_LIMIT_FAMILY
,
3957 &ovs_ct_limit_family
) < 0) {
3958 VLOG_INFO("Generic Netlink family '%s' does not exist. "
3959 "Please update the Open vSwitch kernel module to enable "
3960 "the conntrack limit feature.", OVS_CT_LIMIT_FAMILY
);
3963 ovs_tunnels_out_of_tree
= dpif_netlink_rtnl_probe_oot_tunnels();
3965 ovsthread_once_done(&once
);
3972 dpif_netlink_is_internal_device(const char *name
)
3974 struct dpif_netlink_vport reply
;
3978 error
= dpif_netlink_vport_get(name
, &reply
, &buf
);
3981 } else if (error
!= ENODEV
&& error
!= ENOENT
) {
3982 VLOG_WARN_RL(&error_rl
, "%s: vport query failed (%s)",
3983 name
, ovs_strerror(error
));
3986 return reply
.type
== OVS_VPORT_TYPE_INTERNAL
;
3989 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
3990 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
3991 * positive errno value.
3993 * 'vport' will contain pointers into 'buf', so the caller should not free
3994 * 'buf' while 'vport' is still in use. */
3996 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport
*vport
,
3997 const struct ofpbuf
*buf
)
3999 static const struct nl_policy ovs_vport_policy
[] = {
4000 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NL_A_U32
},
4001 [OVS_VPORT_ATTR_TYPE
] = { .type
= NL_A_U32
},
4002 [OVS_VPORT_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
4003 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NL_A_UNSPEC
},
4004 [OVS_VPORT_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_vport_stats
),
4006 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
4007 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NL_A_U32
, .optional
= true },
4010 dpif_netlink_vport_init(vport
);
4012 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4013 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4014 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4015 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4017 struct nlattr
*a
[ARRAY_SIZE(ovs_vport_policy
)];
4018 if (!nlmsg
|| !genl
|| !ovs_header
4019 || nlmsg
->nlmsg_type
!= ovs_vport_family
4020 || !nl_policy_parse(&b
, 0, ovs_vport_policy
, a
,
4021 ARRAY_SIZE(ovs_vport_policy
))) {
4025 vport
->cmd
= genl
->cmd
;
4026 vport
->dp_ifindex
= ovs_header
->dp_ifindex
;
4027 vport
->port_no
= nl_attr_get_odp_port(a
[OVS_VPORT_ATTR_PORT_NO
]);
4028 vport
->type
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
4029 vport
->name
= nl_attr_get_string(a
[OVS_VPORT_ATTR_NAME
]);
4030 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
4031 vport
->n_upcall_pids
= nl_attr_get_size(a
[OVS_VPORT_ATTR_UPCALL_PID
])
4032 / (sizeof *vport
->upcall_pids
);
4033 vport
->upcall_pids
= nl_attr_get(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
4036 if (a
[OVS_VPORT_ATTR_STATS
]) {
4037 vport
->stats
= nl_attr_get(a
[OVS_VPORT_ATTR_STATS
]);
4039 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
4040 vport
->options
= nl_attr_get(a
[OVS_VPORT_ATTR_OPTIONS
]);
4041 vport
->options_len
= nl_attr_get_size(a
[OVS_VPORT_ATTR_OPTIONS
]);
4043 if (a
[OVS_VPORT_ATTR_NETNSID
]) {
4044 netnsid_set(&vport
->netnsid
,
4045 nl_attr_get_u32(a
[OVS_VPORT_ATTR_NETNSID
]));
4047 netnsid_set_local(&vport
->netnsid
);
4052 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
4053 * followed by Netlink attributes corresponding to 'vport'. */
4055 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport
*vport
,
4058 struct ovs_header
*ovs_header
;
4060 nl_msg_put_genlmsghdr(buf
, 0, ovs_vport_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
4061 vport
->cmd
, OVS_VPORT_VERSION
);
4063 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4064 ovs_header
->dp_ifindex
= vport
->dp_ifindex
;
4066 if (vport
->port_no
!= ODPP_NONE
) {
4067 nl_msg_put_odp_port(buf
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
);
4070 if (vport
->type
!= OVS_VPORT_TYPE_UNSPEC
) {
4071 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_TYPE
, vport
->type
);
4075 nl_msg_put_string(buf
, OVS_VPORT_ATTR_NAME
, vport
->name
);
4078 if (vport
->upcall_pids
) {
4079 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_UPCALL_PID
,
4081 vport
->n_upcall_pids
* sizeof *vport
->upcall_pids
);
4085 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_STATS
,
4086 vport
->stats
, sizeof *vport
->stats
);
4089 if (vport
->options
) {
4090 nl_msg_put_nested(buf
, OVS_VPORT_ATTR_OPTIONS
,
4091 vport
->options
, vport
->options_len
);
4095 /* Clears 'vport' to "empty" values. */
4097 dpif_netlink_vport_init(struct dpif_netlink_vport
*vport
)
4099 memset(vport
, 0, sizeof *vport
);
4100 vport
->port_no
= ODPP_NONE
;
4103 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4104 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4105 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4106 * result of the command is expected to be an ovs_vport also, which is decoded
4107 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
4108 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
4110 dpif_netlink_vport_transact(const struct dpif_netlink_vport
*request
,
4111 struct dpif_netlink_vport
*reply
,
4112 struct ofpbuf
**bufp
)
4114 struct ofpbuf
*request_buf
;
4117 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4119 error
= dpif_netlink_init();
4123 dpif_netlink_vport_init(reply
);
4128 request_buf
= ofpbuf_new(1024);
4129 dpif_netlink_vport_to_ofpbuf(request
, request_buf
);
4130 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4131 ofpbuf_delete(request_buf
);
4135 error
= dpif_netlink_vport_from_ofpbuf(reply
, *bufp
);
4138 dpif_netlink_vport_init(reply
);
4139 ofpbuf_delete(*bufp
);
4146 /* Obtains information about the kernel vport named 'name' and stores it into
4147 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
4148 * longer needed ('reply' will contain pointers into '*bufp'). */
4150 dpif_netlink_vport_get(const char *name
, struct dpif_netlink_vport
*reply
,
4151 struct ofpbuf
**bufp
)
4153 struct dpif_netlink_vport request
;
4155 dpif_netlink_vport_init(&request
);
4156 request
.cmd
= OVS_VPORT_CMD_GET
;
4157 request
.name
= name
;
4159 return dpif_netlink_vport_transact(&request
, reply
, bufp
);
4162 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4163 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
4164 * positive errno value.
4166 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
4167 * while 'dp' is still in use. */
4169 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp
*dp
, const struct ofpbuf
*buf
)
4171 static const struct nl_policy ovs_datapath_policy
[] = {
4172 [OVS_DP_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
4173 [OVS_DP_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_dp_stats
),
4175 [OVS_DP_ATTR_MEGAFLOW_STATS
] = {
4176 NL_POLICY_FOR(struct ovs_dp_megaflow_stats
),
4180 dpif_netlink_dp_init(dp
);
4182 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4183 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4184 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4185 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4187 struct nlattr
*a
[ARRAY_SIZE(ovs_datapath_policy
)];
4188 if (!nlmsg
|| !genl
|| !ovs_header
4189 || nlmsg
->nlmsg_type
!= ovs_datapath_family
4190 || !nl_policy_parse(&b
, 0, ovs_datapath_policy
, a
,
4191 ARRAY_SIZE(ovs_datapath_policy
))) {
4195 dp
->cmd
= genl
->cmd
;
4196 dp
->dp_ifindex
= ovs_header
->dp_ifindex
;
4197 dp
->name
= nl_attr_get_string(a
[OVS_DP_ATTR_NAME
]);
4198 if (a
[OVS_DP_ATTR_STATS
]) {
4199 dp
->stats
= nl_attr_get(a
[OVS_DP_ATTR_STATS
]);
4202 if (a
[OVS_DP_ATTR_MEGAFLOW_STATS
]) {
4203 dp
->megaflow_stats
= nl_attr_get(a
[OVS_DP_ATTR_MEGAFLOW_STATS
]);
4209 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
4211 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp
*dp
, struct ofpbuf
*buf
)
4213 struct ovs_header
*ovs_header
;
4215 nl_msg_put_genlmsghdr(buf
, 0, ovs_datapath_family
,
4216 NLM_F_REQUEST
| NLM_F_ECHO
, dp
->cmd
,
4217 OVS_DATAPATH_VERSION
);
4219 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4220 ovs_header
->dp_ifindex
= dp
->dp_ifindex
;
4223 nl_msg_put_string(buf
, OVS_DP_ATTR_NAME
, dp
->name
);
4226 if (dp
->upcall_pid
) {
4227 nl_msg_put_u32(buf
, OVS_DP_ATTR_UPCALL_PID
, *dp
->upcall_pid
);
4230 if (dp
->user_features
) {
4231 nl_msg_put_u32(buf
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
);
4234 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
4237 /* Clears 'dp' to "empty" values. */
4239 dpif_netlink_dp_init(struct dpif_netlink_dp
*dp
)
4241 memset(dp
, 0, sizeof *dp
);
4245 dpif_netlink_dp_dump_start(struct nl_dump
*dump
)
4247 struct dpif_netlink_dp request
;
4250 dpif_netlink_dp_init(&request
);
4251 request
.cmd
= OVS_DP_CMD_GET
;
4253 buf
= ofpbuf_new(1024);
4254 dpif_netlink_dp_to_ofpbuf(&request
, buf
);
4255 nl_dump_start(dump
, NETLINK_GENERIC
, buf
);
4259 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4260 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4261 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4262 * result of the command is expected to be of the same form, which is decoded
4263 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
4264 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
4266 dpif_netlink_dp_transact(const struct dpif_netlink_dp
*request
,
4267 struct dpif_netlink_dp
*reply
, struct ofpbuf
**bufp
)
4269 struct ofpbuf
*request_buf
;
4272 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4274 request_buf
= ofpbuf_new(1024);
4275 dpif_netlink_dp_to_ofpbuf(request
, request_buf
);
4276 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4277 ofpbuf_delete(request_buf
);
4280 dpif_netlink_dp_init(reply
);
4282 error
= dpif_netlink_dp_from_ofpbuf(reply
, *bufp
);
4285 ofpbuf_delete(*bufp
);
4292 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
4293 * The caller must free '*bufp' when the reply is no longer needed ('reply'
4294 * will contain pointers into '*bufp'). */
4296 dpif_netlink_dp_get(const struct dpif
*dpif_
, struct dpif_netlink_dp
*reply
,
4297 struct ofpbuf
**bufp
)
4299 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
4300 struct dpif_netlink_dp request
;
4302 dpif_netlink_dp_init(&request
);
4303 request
.cmd
= OVS_DP_CMD_GET
;
4304 request
.dp_ifindex
= dpif
->dp_ifindex
;
4306 return dpif_netlink_dp_transact(&request
, reply
, bufp
);
4309 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
4310 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
4311 * positive errno value.
4313 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
4314 * while 'flow' is still in use. */
4316 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow
*flow
,
4317 const struct ofpbuf
*buf
)
4319 static const struct nl_policy ovs_flow_policy
[__OVS_FLOW_ATTR_MAX
] = {
4320 [OVS_FLOW_ATTR_KEY
] = { .type
= NL_A_NESTED
, .optional
= true },
4321 [OVS_FLOW_ATTR_MASK
] = { .type
= NL_A_NESTED
, .optional
= true },
4322 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
4323 [OVS_FLOW_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_flow_stats
),
4325 [OVS_FLOW_ATTR_TCP_FLAGS
] = { .type
= NL_A_U8
, .optional
= true },
4326 [OVS_FLOW_ATTR_USED
] = { .type
= NL_A_U64
, .optional
= true },
4327 [OVS_FLOW_ATTR_UFID
] = { .type
= NL_A_U128
, .optional
= true },
4328 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
4329 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
4330 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
4333 dpif_netlink_flow_init(flow
);
4335 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
4336 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
4337 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
4338 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
4340 struct nlattr
*a
[ARRAY_SIZE(ovs_flow_policy
)];
4341 if (!nlmsg
|| !genl
|| !ovs_header
4342 || nlmsg
->nlmsg_type
!= ovs_flow_family
4343 || !nl_policy_parse(&b
, 0, ovs_flow_policy
, a
,
4344 ARRAY_SIZE(ovs_flow_policy
))) {
4347 if (!a
[OVS_FLOW_ATTR_KEY
] && !a
[OVS_FLOW_ATTR_UFID
]) {
4351 flow
->nlmsg_flags
= nlmsg
->nlmsg_flags
;
4352 flow
->dp_ifindex
= ovs_header
->dp_ifindex
;
4353 if (a
[OVS_FLOW_ATTR_KEY
]) {
4354 flow
->key
= nl_attr_get(a
[OVS_FLOW_ATTR_KEY
]);
4355 flow
->key_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_KEY
]);
4358 if (a
[OVS_FLOW_ATTR_UFID
]) {
4359 flow
->ufid
= nl_attr_get_u128(a
[OVS_FLOW_ATTR_UFID
]);
4360 flow
->ufid_present
= true;
4362 if (a
[OVS_FLOW_ATTR_MASK
]) {
4363 flow
->mask
= nl_attr_get(a
[OVS_FLOW_ATTR_MASK
]);
4364 flow
->mask_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_MASK
]);
4366 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
4367 flow
->actions
= nl_attr_get(a
[OVS_FLOW_ATTR_ACTIONS
]);
4368 flow
->actions_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_ACTIONS
]);
4370 if (a
[OVS_FLOW_ATTR_STATS
]) {
4371 flow
->stats
= nl_attr_get(a
[OVS_FLOW_ATTR_STATS
]);
4373 if (a
[OVS_FLOW_ATTR_TCP_FLAGS
]) {
4374 flow
->tcp_flags
= nl_attr_get(a
[OVS_FLOW_ATTR_TCP_FLAGS
]);
4376 if (a
[OVS_FLOW_ATTR_USED
]) {
4377 flow
->used
= nl_attr_get(a
[OVS_FLOW_ATTR_USED
]);
4384 * If PACKET_TYPE attribute is present in 'data', it filters PACKET_TYPE out.
4385 * If the flow is not Ethernet, the OVS_KEY_ATTR_PACKET_TYPE is converted to
4386 * OVS_KEY_ATTR_ETHERTYPE. Puts 'data' to 'buf'.
4389 put_exclude_packet_type(struct ofpbuf
*buf
, uint16_t type
,
4390 const struct nlattr
*data
, uint16_t data_len
)
4392 const struct nlattr
*packet_type
;
4394 packet_type
= nl_attr_find__(data
, data_len
, OVS_KEY_ATTR_PACKET_TYPE
);
4397 /* exclude PACKET_TYPE Netlink attribute. */
4398 ovs_assert(NLA_ALIGN(packet_type
->nla_len
) == NL_A_U32_SIZE
);
4399 size_t packet_type_len
= NL_A_U32_SIZE
;
4400 size_t first_chunk_size
= (uint8_t *)packet_type
- (uint8_t *)data
;
4401 size_t second_chunk_size
= data_len
- first_chunk_size
4403 struct nlattr
*next_attr
= nl_attr_next(packet_type
);
4406 ofs
= nl_msg_start_nested(buf
, type
);
4407 nl_msg_put(buf
, data
, first_chunk_size
);
4408 nl_msg_put(buf
, next_attr
, second_chunk_size
);
4409 if (!nl_attr_find__(data
, data_len
, OVS_KEY_ATTR_ETHERNET
)) {
4410 ovs_be16 pt
= pt_ns_type_be(nl_attr_get_be32(packet_type
));
4411 const struct nlattr
*nla
;
4413 nla
= nl_attr_find(buf
, ofs
+ NLA_HDRLEN
, OVS_KEY_ATTR_ETHERTYPE
);
4415 ovs_be16
*ethertype
;
4417 ethertype
= CONST_CAST(ovs_be16
*, nl_attr_get(nla
));
4420 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, pt
);
4423 nl_msg_end_nested(buf
, ofs
);
4425 nl_msg_put_unspec(buf
, type
, data
, data_len
);
4429 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
4430 * followed by Netlink attributes corresponding to 'flow'. */
4432 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow
*flow
,
4435 struct ovs_header
*ovs_header
;
4437 nl_msg_put_genlmsghdr(buf
, 0, ovs_flow_family
,
4438 NLM_F_REQUEST
| flow
->nlmsg_flags
,
4439 flow
->cmd
, OVS_FLOW_VERSION
);
4441 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
4442 ovs_header
->dp_ifindex
= flow
->dp_ifindex
;
4444 if (flow
->ufid_present
) {
4445 nl_msg_put_u128(buf
, OVS_FLOW_ATTR_UFID
, flow
->ufid
);
4447 if (flow
->ufid_terse
) {
4448 nl_msg_put_u32(buf
, OVS_FLOW_ATTR_UFID_FLAGS
,
4449 OVS_UFID_F_OMIT_KEY
| OVS_UFID_F_OMIT_MASK
4450 | OVS_UFID_F_OMIT_ACTIONS
);
4452 if (!flow
->ufid_terse
|| !flow
->ufid_present
) {
4453 if (flow
->key_len
) {
4454 put_exclude_packet_type(buf
, OVS_FLOW_ATTR_KEY
, flow
->key
,
4457 if (flow
->mask_len
) {
4458 put_exclude_packet_type(buf
, OVS_FLOW_ATTR_MASK
, flow
->mask
,
4461 if (flow
->actions
|| flow
->actions_len
) {
4462 nl_msg_put_unspec(buf
, OVS_FLOW_ATTR_ACTIONS
,
4463 flow
->actions
, flow
->actions_len
);
4467 /* We never need to send these to the kernel. */
4468 ovs_assert(!flow
->stats
);
4469 ovs_assert(!flow
->tcp_flags
);
4470 ovs_assert(!flow
->used
);
4473 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_CLEAR
);
4476 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_PROBE
);
4480 /* Clears 'flow' to "empty" values. */
4482 dpif_netlink_flow_init(struct dpif_netlink_flow
*flow
)
4484 memset(flow
, 0, sizeof *flow
);
4487 /* Executes 'request' in the kernel datapath. If the command fails, returns a
4488 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
4489 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
4490 * result of the command is expected to be a flow also, which is decoded and
4491 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
4492 * is no longer needed ('reply' will contain pointers into '*bufp'). */
4494 dpif_netlink_flow_transact(struct dpif_netlink_flow
*request
,
4495 struct dpif_netlink_flow
*reply
,
4496 struct ofpbuf
**bufp
)
4498 struct ofpbuf
*request_buf
;
4501 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
4504 request
->nlmsg_flags
|= NLM_F_ECHO
;
4507 request_buf
= ofpbuf_new(1024);
4508 dpif_netlink_flow_to_ofpbuf(request
, request_buf
);
4509 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
4510 ofpbuf_delete(request_buf
);
4514 error
= dpif_netlink_flow_from_ofpbuf(reply
, *bufp
);
4517 dpif_netlink_flow_init(reply
);
4518 ofpbuf_delete(*bufp
);
4526 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow
*flow
,
4527 struct dpif_flow_stats
*stats
)
4530 stats
->n_packets
= get_32aligned_u64(&flow
->stats
->n_packets
);
4531 stats
->n_bytes
= get_32aligned_u64(&flow
->stats
->n_bytes
);
4533 stats
->n_packets
= 0;
4536 stats
->used
= flow
->used
? get_32aligned_u64(flow
->used
) : 0;
4537 stats
->tcp_flags
= flow
->tcp_flags
? *flow
->tcp_flags
: 0;
4540 /* Logs information about a packet that was recently lost in 'ch' (in
4543 report_loss(struct dpif_netlink
*dpif
, struct dpif_channel
*ch
, uint32_t ch_idx
,
4544 uint32_t handler_id
)
4546 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
4549 if (VLOG_DROP_WARN(&rl
)) {
4554 if (ch
->last_poll
!= LLONG_MIN
) {
4555 ds_put_format(&s
, " (last polled %lld ms ago)",
4556 time_msec() - ch
->last_poll
);
4559 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
4560 dpif_name(&dpif
->dpif
), ch_idx
, handler_id
);