2 * Copyright (c) 2008-2017 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-provider.h"
37 #include "dpif-netlink-rtnl.h"
38 #include "openvswitch/dynamic-string.h"
40 #include "fat-rwlock.h"
42 #include "netdev-provider.h"
43 #include "netdev-linux.h"
44 #include "netdev-vport.h"
45 #include "netlink-conntrack.h"
46 #include "netlink-notifier.h"
47 #include "netlink-socket.h"
50 #include "openvswitch/ofpbuf.h"
52 #include "poll-loop.h"
54 #include "openvswitch/shash.h"
57 #include "unaligned.h"
59 #include "openvswitch/vlog.h"
60 #include "openvswitch/flow.h"
62 VLOG_DEFINE_THIS_MODULE(dpif_netlink
);
69 enum { MAX_PORTS
= USHRT_MAX
};
71 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
72 * missing if we have old headers. */
73 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
75 #define FLOW_DUMP_MAX_BATCH 50
76 #define OPERATE_MAX_OPS 50
78 struct dpif_netlink_dp
{
79 /* Generic Netlink header. */
82 /* struct ovs_header. */
86 const char *name
; /* OVS_DP_ATTR_NAME. */
87 const uint32_t *upcall_pid
; /* OVS_DP_ATTR_UPCALL_PID. */
88 uint32_t user_features
; /* OVS_DP_ATTR_USER_FEATURES */
89 const struct ovs_dp_stats
*stats
; /* OVS_DP_ATTR_STATS. */
90 const struct ovs_dp_megaflow_stats
*megaflow_stats
;
91 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
94 static void dpif_netlink_dp_init(struct dpif_netlink_dp
*);
95 static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp
*,
96 const struct ofpbuf
*);
97 static void dpif_netlink_dp_dump_start(struct nl_dump
*);
98 static int dpif_netlink_dp_transact(const struct dpif_netlink_dp
*request
,
99 struct dpif_netlink_dp
*reply
,
100 struct ofpbuf
**bufp
);
101 static int dpif_netlink_dp_get(const struct dpif
*,
102 struct dpif_netlink_dp
*reply
,
103 struct ofpbuf
**bufp
);
105 struct dpif_netlink_flow
{
106 /* Generic Netlink header. */
109 /* struct ovs_header. */
110 unsigned int nlmsg_flags
;
115 * The 'stats' member points to 64-bit data that might only be aligned on
116 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
119 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
120 * the Netlink version of the command, even if actions_len is zero. */
121 const struct nlattr
*key
; /* OVS_FLOW_ATTR_KEY. */
123 const struct nlattr
*mask
; /* OVS_FLOW_ATTR_MASK. */
125 const struct nlattr
*actions
; /* OVS_FLOW_ATTR_ACTIONS. */
127 ovs_u128 ufid
; /* OVS_FLOW_ATTR_FLOW_ID. */
128 bool ufid_present
; /* Is there a UFID? */
129 bool ufid_terse
; /* Skip serializing key/mask/acts? */
130 const struct ovs_flow_stats
*stats
; /* OVS_FLOW_ATTR_STATS. */
131 const uint8_t *tcp_flags
; /* OVS_FLOW_ATTR_TCP_FLAGS. */
132 const ovs_32aligned_u64
*used
; /* OVS_FLOW_ATTR_USED. */
133 bool clear
; /* OVS_FLOW_ATTR_CLEAR. */
134 bool probe
; /* OVS_FLOW_ATTR_PROBE. */
137 static void dpif_netlink_flow_init(struct dpif_netlink_flow
*);
138 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow
*,
139 const struct ofpbuf
*);
140 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow
*,
142 static int dpif_netlink_flow_transact(struct dpif_netlink_flow
*request
,
143 struct dpif_netlink_flow
*reply
,
144 struct ofpbuf
**bufp
);
145 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow
*,
146 struct dpif_flow_stats
*);
147 static void dpif_netlink_flow_to_dpif_flow(struct dpif
*, struct dpif_flow
*,
148 const struct dpif_netlink_flow
*);
150 /* One of the dpif channels between the kernel and userspace. */
151 struct dpif_channel
{
152 struct nl_sock
*sock
; /* Netlink socket. */
153 long long int last_poll
; /* Last time this channel was polled. */
157 #define VPORT_SOCK_POOL_SIZE 1
158 /* On Windows, there is no native support for epoll. There are equivalent
159 * interfaces though, that are not used currently. For simpicity, a pool of
160 * netlink sockets is used. Each socket is represented by 'struct
161 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
162 * sharing the same socket. In the future, we can add a reference count and
164 struct dpif_windows_vport_sock
{
165 struct nl_sock
*nl_sock
; /* netlink socket. */
169 struct dpif_handler
{
170 struct dpif_channel
*channels
;/* Array of channels for each handler. */
171 struct epoll_event
*epoll_events
;
172 int epoll_fd
; /* epoll fd that includes channel socks. */
173 int n_events
; /* Num events returned by epoll_wait(). */
174 int event_offset
; /* Offset into 'epoll_events'. */
177 /* Pool of sockets. */
178 struct dpif_windows_vport_sock
*vport_sock_pool
;
179 size_t last_used_pool_idx
; /* Index to aid in allocating a
180 socket in the pool to a port. */
184 /* Datapath interface for the openvswitch Linux kernel module. */
185 struct dpif_netlink
{
189 /* Upcall messages. */
190 struct fat_rwlock upcall_lock
;
191 struct dpif_handler
*handlers
;
192 uint32_t n_handlers
; /* Num of upcall handlers. */
193 int uc_array_size
; /* Size of 'handler->channels' and */
194 /* 'handler->epoll_events'. */
196 /* Change notification. */
197 struct nl_sock
*port_notifier
; /* vport multicast group subscriber. */
198 bool refresh_channels
;
201 static void report_loss(struct dpif_netlink
*, struct dpif_channel
*,
202 uint32_t ch_idx
, uint32_t handler_id
);
204 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(9999, 5);
206 /* Generic Netlink family numbers for OVS.
208 * Initialized by dpif_netlink_init(). */
209 static int ovs_datapath_family
;
210 static int ovs_vport_family
;
211 static int ovs_flow_family
;
212 static int ovs_packet_family
;
214 /* Generic Netlink multicast groups for OVS.
216 * Initialized by dpif_netlink_init(). */
217 static unsigned int ovs_vport_mcgroup
;
219 /* If true, tunnel devices are created using OVS compat/genetlink.
220 * If false, tunnel devices are created with rtnetlink and using light weight
221 * tunnels. If we fail to create the tunnel the rtnetlink+LWT, then we fallback
222 * to using the compat interface. */
223 static bool ovs_tunnels_out_of_tree
= true;
225 static int dpif_netlink_init(void);
226 static int open_dpif(const struct dpif_netlink_dp
*, struct dpif
**);
227 static uint32_t dpif_netlink_port_get_pid(const struct dpif
*,
228 odp_port_t port_no
, uint32_t hash
);
229 static void dpif_netlink_handler_uninit(struct dpif_handler
*handler
);
230 static int dpif_netlink_refresh_channels(struct dpif_netlink
*,
231 uint32_t n_handlers
);
232 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport
*,
234 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport
*,
235 const struct ofpbuf
*);
236 static int dpif_netlink_port_query__(const struct dpif_netlink
*dpif
,
237 odp_port_t port_no
, const char *port_name
,
238 struct dpif_port
*dpif_port
);
240 static struct dpif_netlink
*
241 dpif_netlink_cast(const struct dpif
*dpif
)
243 dpif_assert_class(dpif
, &dpif_netlink_class
);
244 return CONTAINER_OF(dpif
, struct dpif_netlink
, dpif
);
248 dpif_netlink_enumerate(struct sset
*all_dps
,
249 const struct dpif_class
*dpif_class OVS_UNUSED
)
252 uint64_t reply_stub
[NL_DUMP_BUFSIZE
/ 8];
253 struct ofpbuf msg
, buf
;
256 error
= dpif_netlink_init();
261 ofpbuf_use_stub(&buf
, reply_stub
, sizeof reply_stub
);
262 dpif_netlink_dp_dump_start(&dump
);
263 while (nl_dump_next(&dump
, &msg
, &buf
)) {
264 struct dpif_netlink_dp dp
;
266 if (!dpif_netlink_dp_from_ofpbuf(&dp
, &msg
)) {
267 sset_add(all_dps
, dp
.name
);
271 return nl_dump_done(&dump
);
275 dpif_netlink_open(const struct dpif_class
*class OVS_UNUSED
, const char *name
,
276 bool create
, struct dpif
**dpifp
)
278 struct dpif_netlink_dp dp_request
, dp
;
283 error
= dpif_netlink_init();
288 /* Create or look up datapath. */
289 dpif_netlink_dp_init(&dp_request
);
291 dp_request
.cmd
= OVS_DP_CMD_NEW
;
293 dp_request
.upcall_pid
= &upcall_pid
;
295 /* Use OVS_DP_CMD_SET to report user features */
296 dp_request
.cmd
= OVS_DP_CMD_SET
;
298 dp_request
.name
= name
;
299 dp_request
.user_features
|= OVS_DP_F_UNALIGNED
;
300 dp_request
.user_features
|= OVS_DP_F_VPORT_PIDS
;
301 error
= dpif_netlink_dp_transact(&dp_request
, &dp
, &buf
);
306 error
= open_dpif(&dp
, dpifp
);
312 open_dpif(const struct dpif_netlink_dp
*dp
, struct dpif
**dpifp
)
314 struct dpif_netlink
*dpif
;
316 dpif
= xzalloc(sizeof *dpif
);
317 dpif
->port_notifier
= NULL
;
318 fat_rwlock_init(&dpif
->upcall_lock
);
320 dpif_init(&dpif
->dpif
, &dpif_netlink_class
, dp
->name
,
321 dp
->dp_ifindex
, dp
->dp_ifindex
);
323 dpif
->dp_ifindex
= dp
->dp_ifindex
;
324 *dpifp
= &dpif
->dpif
;
329 /* Destroys the netlink sockets pointed by the elements in 'socksp'
330 * and frees the 'socksp'. */
332 vport_del_socksp__(struct nl_sock
**socksp
, uint32_t n_socks
)
336 for (i
= 0; i
< n_socks
; i
++) {
337 nl_sock_destroy(socksp
[i
]);
343 /* Creates an array of netlink sockets. Returns an array of the
344 * corresponding pointers. Records the error in 'error'. */
345 static struct nl_sock
**
346 vport_create_socksp__(uint32_t n_socks
, int *error
)
348 struct nl_sock
**socksp
= xzalloc(n_socks
* sizeof *socksp
);
351 for (i
= 0; i
< n_socks
; i
++) {
352 *error
= nl_sock_create(NETLINK_GENERIC
, &socksp
[i
]);
361 vport_del_socksp__(socksp
, n_socks
);
368 vport_delete_sock_pool(struct dpif_handler
*handler
)
369 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
371 if (handler
->vport_sock_pool
) {
373 struct dpif_windows_vport_sock
*sock_pool
=
374 handler
->vport_sock_pool
;
376 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
377 if (sock_pool
[i
].nl_sock
) {
378 nl_sock_unsubscribe_packets(sock_pool
[i
].nl_sock
);
379 nl_sock_destroy(sock_pool
[i
].nl_sock
);
380 sock_pool
[i
].nl_sock
= NULL
;
384 free(handler
->vport_sock_pool
);
385 handler
->vport_sock_pool
= NULL
;
390 vport_create_sock_pool(struct dpif_handler
*handler
)
391 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
393 struct dpif_windows_vport_sock
*sock_pool
;
397 sock_pool
= xzalloc(VPORT_SOCK_POOL_SIZE
* sizeof *sock_pool
);
398 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
399 error
= nl_sock_create(NETLINK_GENERIC
, &sock_pool
[i
].nl_sock
);
404 /* Enable the netlink socket to receive packets. This is equivalent to
405 * calling nl_sock_join_mcgroup() to receive events. */
406 error
= nl_sock_subscribe_packets(sock_pool
[i
].nl_sock
);
412 handler
->vport_sock_pool
= sock_pool
;
413 handler
->last_used_pool_idx
= 0;
417 vport_delete_sock_pool(handler
);
421 /* Returns an array pointers to netlink sockets. The sockets are picked from a
422 * pool. Records the error in 'error'. */
423 static struct nl_sock
**
424 vport_create_socksp_windows(struct dpif_netlink
*dpif
, int *error
)
425 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
427 uint32_t n_socks
= dpif
->n_handlers
;
428 struct nl_sock
**socksp
;
431 ovs_assert(n_socks
<= 1);
432 socksp
= xzalloc(n_socks
* sizeof *socksp
);
434 /* Pick netlink sockets to use in a round-robin fashion from each
435 * handler's pool of sockets. */
436 for (i
= 0; i
< n_socks
; i
++) {
437 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
438 struct dpif_windows_vport_sock
*sock_pool
= handler
->vport_sock_pool
;
439 size_t index
= handler
->last_used_pool_idx
;
441 /* A pool of sockets is allocated when the handler is initialized. */
442 if (sock_pool
== NULL
) {
448 ovs_assert(index
< VPORT_SOCK_POOL_SIZE
);
449 socksp
[i
] = sock_pool
[index
].nl_sock
;
450 socksp
[i
] = sock_pool
[index
].nl_sock
;
451 ovs_assert(socksp
[i
]);
452 index
= (index
== VPORT_SOCK_POOL_SIZE
- 1) ? 0 : index
+ 1;
453 handler
->last_used_pool_idx
= index
;
460 vport_del_socksp_windows(struct dpif_netlink
*dpif
, struct nl_sock
**socksp
)
466 static struct nl_sock
**
467 vport_create_socksp(struct dpif_netlink
*dpif
, int *error
)
470 return vport_create_socksp_windows(dpif
, error
);
472 return vport_create_socksp__(dpif
->n_handlers
, error
);
477 vport_del_socksp(struct dpif_netlink
*dpif
, struct nl_sock
**socksp
)
480 vport_del_socksp_windows(dpif
, socksp
);
482 vport_del_socksp__(socksp
, dpif
->n_handlers
);
486 /* Given the array of pointers to netlink sockets 'socksp', returns
487 * the array of corresponding pids. If the 'socksp' is NULL, returns
488 * a single-element array of value 0. */
490 vport_socksp_to_pids(struct nl_sock
**socksp
, uint32_t n_socks
)
495 pids
= xzalloc(sizeof *pids
);
499 pids
= xzalloc(n_socks
* sizeof *pids
);
500 for (i
= 0; i
< n_socks
; i
++) {
501 pids
[i
] = nl_sock_pid(socksp
[i
]);
508 /* Given the port number 'port_idx', extracts the pids of netlink sockets
509 * associated to the port and assigns it to 'upcall_pids'. */
511 vport_get_pids(struct dpif_netlink
*dpif
, uint32_t port_idx
,
512 uint32_t **upcall_pids
)
517 /* Since the nl_sock can only be assigned in either all
518 * or none "dpif->handlers" channels, the following check
520 if (!dpif
->handlers
[0].channels
[port_idx
].sock
) {
523 ovs_assert(!WINDOWS
|| dpif
->n_handlers
<= 1);
525 pids
= xzalloc(dpif
->n_handlers
* sizeof *pids
);
527 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
528 pids
[i
] = nl_sock_pid(dpif
->handlers
[i
].channels
[port_idx
].sock
);
537 vport_add_channels(struct dpif_netlink
*dpif
, odp_port_t port_no
,
538 struct nl_sock
**socksp
)
540 struct epoll_event event
;
541 uint32_t port_idx
= odp_to_u32(port_no
);
545 if (dpif
->handlers
== NULL
) {
549 /* We assume that the datapath densely chooses port numbers, which can
550 * therefore be used as an index into 'channels' and 'epoll_events' of
551 * 'dpif->handler'. */
552 if (port_idx
>= dpif
->uc_array_size
) {
553 uint32_t new_size
= port_idx
+ 1;
555 if (new_size
> MAX_PORTS
) {
556 VLOG_WARN_RL(&error_rl
, "%s: datapath port %"PRIu32
" too big",
557 dpif_name(&dpif
->dpif
), port_no
);
561 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
562 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
564 handler
->channels
= xrealloc(handler
->channels
,
565 new_size
* sizeof *handler
->channels
);
567 for (j
= dpif
->uc_array_size
; j
< new_size
; j
++) {
568 handler
->channels
[j
].sock
= NULL
;
571 handler
->epoll_events
= xrealloc(handler
->epoll_events
,
572 new_size
* sizeof *handler
->epoll_events
);
575 dpif
->uc_array_size
= new_size
;
578 memset(&event
, 0, sizeof event
);
579 event
.events
= EPOLLIN
;
580 event
.data
.u32
= port_idx
;
582 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
583 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
586 if (epoll_ctl(handler
->epoll_fd
, EPOLL_CTL_ADD
, nl_sock_fd(socksp
[i
]),
592 dpif
->handlers
[i
].channels
[port_idx
].sock
= socksp
[i
];
593 dpif
->handlers
[i
].channels
[port_idx
].last_poll
= LLONG_MIN
;
599 for (j
= 0; j
< i
; j
++) {
601 epoll_ctl(dpif
->handlers
[j
].epoll_fd
, EPOLL_CTL_DEL
,
602 nl_sock_fd(socksp
[j
]), NULL
);
604 dpif
->handlers
[j
].channels
[port_idx
].sock
= NULL
;
611 vport_del_channels(struct dpif_netlink
*dpif
, odp_port_t port_no
)
613 uint32_t port_idx
= odp_to_u32(port_no
);
616 if (!dpif
->handlers
|| port_idx
>= dpif
->uc_array_size
) {
620 /* Since the sock can only be assigned in either all or none
621 * of "dpif->handlers" channels, the following check would
623 if (!dpif
->handlers
[0].channels
[port_idx
].sock
) {
627 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
628 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
630 epoll_ctl(handler
->epoll_fd
, EPOLL_CTL_DEL
,
631 nl_sock_fd(handler
->channels
[port_idx
].sock
), NULL
);
632 nl_sock_destroy(handler
->channels
[port_idx
].sock
);
634 handler
->channels
[port_idx
].sock
= NULL
;
635 handler
->event_offset
= handler
->n_events
= 0;
640 destroy_all_channels(struct dpif_netlink
*dpif
)
641 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
645 if (!dpif
->handlers
) {
649 for (i
= 0; i
< dpif
->uc_array_size
; i
++ ) {
650 struct dpif_netlink_vport vport_request
;
651 uint32_t upcall_pids
= 0;
653 /* Since the sock can only be assigned in either all or none
654 * of "dpif->handlers" channels, the following check would
656 if (!dpif
->handlers
[0].channels
[i
].sock
) {
660 /* Turn off upcalls. */
661 dpif_netlink_vport_init(&vport_request
);
662 vport_request
.cmd
= OVS_VPORT_CMD_SET
;
663 vport_request
.dp_ifindex
= dpif
->dp_ifindex
;
664 vport_request
.port_no
= u32_to_odp(i
);
665 vport_request
.n_upcall_pids
= 1;
666 vport_request
.upcall_pids
= &upcall_pids
;
667 dpif_netlink_vport_transact(&vport_request
, NULL
, NULL
);
669 vport_del_channels(dpif
, u32_to_odp(i
));
672 for (i
= 0; i
< dpif
->n_handlers
; i
++) {
673 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
675 dpif_netlink_handler_uninit(handler
);
676 free(handler
->epoll_events
);
677 free(handler
->channels
);
680 free(dpif
->handlers
);
681 dpif
->handlers
= NULL
;
682 dpif
->n_handlers
= 0;
683 dpif
->uc_array_size
= 0;
687 dpif_netlink_close(struct dpif
*dpif_
)
689 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
691 nl_sock_destroy(dpif
->port_notifier
);
693 fat_rwlock_wrlock(&dpif
->upcall_lock
);
694 destroy_all_channels(dpif
);
695 fat_rwlock_unlock(&dpif
->upcall_lock
);
697 fat_rwlock_destroy(&dpif
->upcall_lock
);
702 dpif_netlink_destroy(struct dpif
*dpif_
)
704 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
705 struct dpif_netlink_dp dp
;
707 dpif_netlink_dp_init(&dp
);
708 dp
.cmd
= OVS_DP_CMD_DEL
;
709 dp
.dp_ifindex
= dpif
->dp_ifindex
;
710 return dpif_netlink_dp_transact(&dp
, NULL
, NULL
);
714 dpif_netlink_run(struct dpif
*dpif_
)
716 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
718 if (dpif
->refresh_channels
) {
719 dpif
->refresh_channels
= false;
720 fat_rwlock_wrlock(&dpif
->upcall_lock
);
721 dpif_netlink_refresh_channels(dpif
, dpif
->n_handlers
);
722 fat_rwlock_unlock(&dpif
->upcall_lock
);
728 dpif_netlink_get_stats(const struct dpif
*dpif_
, struct dpif_dp_stats
*stats
)
730 struct dpif_netlink_dp dp
;
734 error
= dpif_netlink_dp_get(dpif_
, &dp
, &buf
);
736 memset(stats
, 0, sizeof *stats
);
739 stats
->n_hit
= get_32aligned_u64(&dp
.stats
->n_hit
);
740 stats
->n_missed
= get_32aligned_u64(&dp
.stats
->n_missed
);
741 stats
->n_lost
= get_32aligned_u64(&dp
.stats
->n_lost
);
742 stats
->n_flows
= get_32aligned_u64(&dp
.stats
->n_flows
);
745 if (dp
.megaflow_stats
) {
746 stats
->n_masks
= dp
.megaflow_stats
->n_masks
;
747 stats
->n_mask_hit
= get_32aligned_u64(
748 &dp
.megaflow_stats
->n_mask_hit
);
750 stats
->n_masks
= UINT32_MAX
;
751 stats
->n_mask_hit
= UINT64_MAX
;
759 get_vport_type(const struct dpif_netlink_vport
*vport
)
761 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
763 switch (vport
->type
) {
764 case OVS_VPORT_TYPE_NETDEV
: {
765 const char *type
= netdev_get_type_from_name(vport
->name
);
767 return type
? type
: "system";
770 case OVS_VPORT_TYPE_INTERNAL
:
773 case OVS_VPORT_TYPE_GENEVE
:
776 case OVS_VPORT_TYPE_GRE
:
779 case OVS_VPORT_TYPE_VXLAN
:
782 case OVS_VPORT_TYPE_LISP
:
785 case OVS_VPORT_TYPE_STT
:
788 case OVS_VPORT_TYPE_UNSPEC
:
789 case __OVS_VPORT_TYPE_MAX
:
793 VLOG_WARN_RL(&rl
, "dp%d: port `%s' has unsupported type %u",
794 vport
->dp_ifindex
, vport
->name
, (unsigned int) vport
->type
);
799 netdev_to_ovs_vport_type(const char *type
)
801 if (!strcmp(type
, "tap") || !strcmp(type
, "system")) {
802 return OVS_VPORT_TYPE_NETDEV
;
803 } else if (!strcmp(type
, "internal")) {
804 return OVS_VPORT_TYPE_INTERNAL
;
805 } else if (strstr(type
, "stt")) {
806 return OVS_VPORT_TYPE_STT
;
807 } else if (!strcmp(type
, "geneve")) {
808 return OVS_VPORT_TYPE_GENEVE
;
809 } else if (strstr(type
, "gre")) {
810 return OVS_VPORT_TYPE_GRE
;
811 } else if (!strcmp(type
, "vxlan")) {
812 return OVS_VPORT_TYPE_VXLAN
;
813 } else if (!strcmp(type
, "lisp")) {
814 return OVS_VPORT_TYPE_LISP
;
816 return OVS_VPORT_TYPE_UNSPEC
;
821 dpif_netlink_port_add__(struct dpif_netlink
*dpif
, const char *name
,
822 enum ovs_vport_type type
,
823 struct ofpbuf
*options
,
824 odp_port_t
*port_nop
)
825 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
827 struct dpif_netlink_vport request
, reply
;
829 struct nl_sock
**socksp
= NULL
;
830 uint32_t *upcall_pids
;
833 if (dpif
->handlers
) {
834 socksp
= vport_create_socksp(dpif
, &error
);
840 dpif_netlink_vport_init(&request
);
841 request
.cmd
= OVS_VPORT_CMD_NEW
;
842 request
.dp_ifindex
= dpif
->dp_ifindex
;
846 request
.port_no
= *port_nop
;
847 upcall_pids
= vport_socksp_to_pids(socksp
, dpif
->n_handlers
);
848 request
.n_upcall_pids
= socksp
? dpif
->n_handlers
: 1;
849 request
.upcall_pids
= upcall_pids
;
852 request
.options
= options
->data
;
853 request
.options_len
= options
->size
;
856 error
= dpif_netlink_vport_transact(&request
, &reply
, &buf
);
858 *port_nop
= reply
.port_no
;
860 if (error
== EBUSY
&& *port_nop
!= ODPP_NONE
) {
861 VLOG_INFO("%s: requested port %"PRIu32
" is in use",
862 dpif_name(&dpif
->dpif
), *port_nop
);
865 vport_del_socksp(dpif
, socksp
);
870 error
= vport_add_channels(dpif
, *port_nop
, socksp
);
872 VLOG_INFO("%s: could not add channel for port %s",
873 dpif_name(&dpif
->dpif
), name
);
875 /* Delete the port. */
876 dpif_netlink_vport_init(&request
);
877 request
.cmd
= OVS_VPORT_CMD_DEL
;
878 request
.dp_ifindex
= dpif
->dp_ifindex
;
879 request
.port_no
= *port_nop
;
880 dpif_netlink_vport_transact(&request
, NULL
, NULL
);
881 vport_del_socksp(dpif
, socksp
);
895 dpif_netlink_port_add_compat(struct dpif_netlink
*dpif
, struct netdev
*netdev
,
896 odp_port_t
*port_nop
)
897 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
899 const struct netdev_tunnel_config
*tnl_cfg
;
900 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
901 const char *type
= netdev_get_type(netdev
);
902 uint64_t options_stub
[64 / 8];
903 enum ovs_vport_type ovs_type
;
904 struct ofpbuf options
;
907 name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
909 ovs_type
= netdev_to_ovs_vport_type(netdev_get_type(netdev
));
910 if (ovs_type
== OVS_VPORT_TYPE_UNSPEC
) {
911 VLOG_WARN_RL(&error_rl
, "%s: cannot create port `%s' because it has "
912 "unsupported type `%s'",
913 dpif_name(&dpif
->dpif
), name
, type
);
917 if (ovs_type
== OVS_VPORT_TYPE_NETDEV
) {
919 /* XXX : Map appropiate Windows handle */
921 netdev_linux_ethtool_set_flag(netdev
, ETH_FLAG_LRO
, "LRO", false);
926 if (ovs_type
== OVS_VPORT_TYPE_INTERNAL
) {
927 if (!create_wmi_port(name
)){
928 VLOG_ERR("Could not create wmi internal port with name:%s", name
);
934 tnl_cfg
= netdev_get_tunnel_config(netdev
);
935 if (tnl_cfg
&& (tnl_cfg
->dst_port
!= 0 || tnl_cfg
->exts
)) {
936 ofpbuf_use_stack(&options
, options_stub
, sizeof options_stub
);
937 if (tnl_cfg
->dst_port
) {
938 nl_msg_put_u16(&options
, OVS_TUNNEL_ATTR_DST_PORT
,
939 ntohs(tnl_cfg
->dst_port
));
945 ext_ofs
= nl_msg_start_nested(&options
, OVS_TUNNEL_ATTR_EXTENSION
);
946 for (i
= 0; i
< 32; i
++) {
947 if (tnl_cfg
->exts
& (1 << i
)) {
948 nl_msg_put_flag(&options
, i
);
951 nl_msg_end_nested(&options
, ext_ofs
);
953 return dpif_netlink_port_add__(dpif
, name
, ovs_type
, &options
,
956 return dpif_netlink_port_add__(dpif
, name
, ovs_type
, NULL
, port_nop
);
962 dpif_netlink_rtnl_port_create_and_add(struct dpif_netlink
*dpif
,
963 struct netdev
*netdev
,
964 odp_port_t
*port_nop
)
965 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
967 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
968 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
972 error
= dpif_netlink_rtnl_port_create(netdev
);
974 if (error
!= EOPNOTSUPP
) {
975 VLOG_INFO_RL(&rl
, "Failed to create %s with rtnetlink: %s",
976 netdev_get_name(netdev
), ovs_strerror(error
));
981 name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
982 error
= dpif_netlink_port_add__(dpif
, name
, OVS_VPORT_TYPE_NETDEV
, NULL
,
985 dpif_netlink_rtnl_port_destroy(name
, netdev_get_type(netdev
));
991 dpif_netlink_port_add(struct dpif
*dpif_
, struct netdev
*netdev
,
992 odp_port_t
*port_nop
)
994 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
995 int error
= EOPNOTSUPP
;
997 fat_rwlock_wrlock(&dpif
->upcall_lock
);
998 if (!ovs_tunnels_out_of_tree
) {
999 error
= dpif_netlink_rtnl_port_create_and_add(dpif
, netdev
, port_nop
);
1002 error
= dpif_netlink_port_add_compat(dpif
, netdev
, port_nop
);
1004 fat_rwlock_unlock(&dpif
->upcall_lock
);
1010 dpif_netlink_port_del__(struct dpif_netlink
*dpif
, odp_port_t port_no
)
1011 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
1013 struct dpif_netlink_vport vport
;
1014 struct dpif_port dpif_port
;
1017 error
= dpif_netlink_port_query__(dpif
, port_no
, NULL
, &dpif_port
);
1022 dpif_netlink_vport_init(&vport
);
1023 vport
.cmd
= OVS_VPORT_CMD_DEL
;
1024 vport
.dp_ifindex
= dpif
->dp_ifindex
;
1025 vport
.port_no
= port_no
;
1027 if (!strcmp(dpif_port
.type
, "internal")) {
1028 if (!delete_wmi_port(dpif_port
.name
)) {
1029 VLOG_ERR("Could not delete wmi port with name: %s",
1034 error
= dpif_netlink_vport_transact(&vport
, NULL
, NULL
);
1036 vport_del_channels(dpif
, port_no
);
1038 if (!error
&& !ovs_tunnels_out_of_tree
) {
1039 error
= dpif_netlink_rtnl_port_destroy(dpif_port
.name
, dpif_port
.type
);
1040 if (error
== EOPNOTSUPP
) {
1045 dpif_port_destroy(&dpif_port
);
1051 dpif_netlink_port_del(struct dpif
*dpif_
, odp_port_t port_no
)
1053 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1056 fat_rwlock_wrlock(&dpif
->upcall_lock
);
1057 error
= dpif_netlink_port_del__(dpif
, port_no
);
1058 fat_rwlock_unlock(&dpif
->upcall_lock
);
1064 dpif_netlink_port_query__(const struct dpif_netlink
*dpif
, odp_port_t port_no
,
1065 const char *port_name
, struct dpif_port
*dpif_port
)
1067 struct dpif_netlink_vport request
;
1068 struct dpif_netlink_vport reply
;
1072 dpif_netlink_vport_init(&request
);
1073 request
.cmd
= OVS_VPORT_CMD_GET
;
1074 request
.dp_ifindex
= dpif
->dp_ifindex
;
1075 request
.port_no
= port_no
;
1076 request
.name
= port_name
;
1078 error
= dpif_netlink_vport_transact(&request
, &reply
, &buf
);
1080 if (reply
.dp_ifindex
!= request
.dp_ifindex
) {
1081 /* A query by name reported that 'port_name' is in some datapath
1082 * other than 'dpif', but the caller wants to know about 'dpif'. */
1084 } else if (dpif_port
) {
1085 dpif_port
->name
= xstrdup(reply
.name
);
1086 dpif_port
->type
= xstrdup(get_vport_type(&reply
));
1087 dpif_port
->port_no
= reply
.port_no
;
1095 dpif_netlink_port_query_by_number(const struct dpif
*dpif_
, odp_port_t port_no
,
1096 struct dpif_port
*dpif_port
)
1098 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1100 return dpif_netlink_port_query__(dpif
, port_no
, NULL
, dpif_port
);
1104 dpif_netlink_port_query_by_name(const struct dpif
*dpif_
, const char *devname
,
1105 struct dpif_port
*dpif_port
)
1107 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1109 return dpif_netlink_port_query__(dpif
, 0, devname
, dpif_port
);
1113 dpif_netlink_port_get_pid__(const struct dpif_netlink
*dpif
,
1114 odp_port_t port_no
, uint32_t hash
)
1115 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
1117 uint32_t port_idx
= odp_to_u32(port_no
);
1120 if (dpif
->handlers
&& dpif
->uc_array_size
> 0) {
1121 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1122 * channel, since it is not heavily loaded. */
1123 uint32_t idx
= port_idx
>= dpif
->uc_array_size
? 0 : port_idx
;
1124 struct dpif_handler
*h
= &dpif
->handlers
[hash
% dpif
->n_handlers
];
1126 /* Needs to check in case the socket pointer is changed in between
1127 * the holding of upcall_lock. A known case happens when the main
1128 * thread deletes the vport while the handler thread is handling
1129 * the upcall from that port. */
1130 if (h
->channels
[idx
].sock
) {
1131 pid
= nl_sock_pid(h
->channels
[idx
].sock
);
1139 dpif_netlink_port_get_pid(const struct dpif
*dpif_
, odp_port_t port_no
,
1142 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1145 fat_rwlock_rdlock(&dpif
->upcall_lock
);
1146 ret
= dpif_netlink_port_get_pid__(dpif
, port_no
, hash
);
1147 fat_rwlock_unlock(&dpif
->upcall_lock
);
1153 dpif_netlink_flow_flush(struct dpif
*dpif_
)
1155 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1156 struct dpif_netlink_flow flow
;
1158 dpif_netlink_flow_init(&flow
);
1159 flow
.cmd
= OVS_FLOW_CMD_DEL
;
1160 flow
.dp_ifindex
= dpif
->dp_ifindex
;
1162 if (netdev_is_flow_api_enabled()) {
1163 netdev_ports_flow_flush(DPIF_HMAP_KEY(dpif_
));
1166 return dpif_netlink_flow_transact(&flow
, NULL
, NULL
);
1169 struct dpif_netlink_port_state
{
1170 struct nl_dump dump
;
1175 dpif_netlink_port_dump_start__(const struct dpif_netlink
*dpif
,
1176 struct nl_dump
*dump
)
1178 struct dpif_netlink_vport request
;
1181 dpif_netlink_vport_init(&request
);
1182 request
.cmd
= OVS_VPORT_CMD_GET
;
1183 request
.dp_ifindex
= dpif
->dp_ifindex
;
1185 buf
= ofpbuf_new(1024);
1186 dpif_netlink_vport_to_ofpbuf(&request
, buf
);
1187 nl_dump_start(dump
, NETLINK_GENERIC
, buf
);
1192 dpif_netlink_port_dump_start(const struct dpif
*dpif_
, void **statep
)
1194 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1195 struct dpif_netlink_port_state
*state
;
1197 *statep
= state
= xmalloc(sizeof *state
);
1198 dpif_netlink_port_dump_start__(dpif
, &state
->dump
);
1200 ofpbuf_init(&state
->buf
, NL_DUMP_BUFSIZE
);
1205 dpif_netlink_port_dump_next__(const struct dpif_netlink
*dpif
,
1206 struct nl_dump
*dump
,
1207 struct dpif_netlink_vport
*vport
,
1208 struct ofpbuf
*buffer
)
1213 if (!nl_dump_next(dump
, &buf
, buffer
)) {
1217 error
= dpif_netlink_vport_from_ofpbuf(vport
, &buf
);
1219 VLOG_WARN_RL(&error_rl
, "%s: failed to parse vport record (%s)",
1220 dpif_name(&dpif
->dpif
), ovs_strerror(error
));
1226 dpif_netlink_port_dump_next(const struct dpif
*dpif_
, void *state_
,
1227 struct dpif_port
*dpif_port
)
1229 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1230 struct dpif_netlink_port_state
*state
= state_
;
1231 struct dpif_netlink_vport vport
;
1234 error
= dpif_netlink_port_dump_next__(dpif
, &state
->dump
, &vport
,
1239 dpif_port
->name
= CONST_CAST(char *, vport
.name
);
1240 dpif_port
->type
= CONST_CAST(char *, get_vport_type(&vport
));
1241 dpif_port
->port_no
= vport
.port_no
;
1246 dpif_netlink_port_dump_done(const struct dpif
*dpif_ OVS_UNUSED
, void *state_
)
1248 struct dpif_netlink_port_state
*state
= state_
;
1249 int error
= nl_dump_done(&state
->dump
);
1251 ofpbuf_uninit(&state
->buf
);
1257 dpif_netlink_port_poll(const struct dpif
*dpif_
, char **devnamep
)
1259 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1261 /* Lazily create the Netlink socket to listen for notifications. */
1262 if (!dpif
->port_notifier
) {
1263 struct nl_sock
*sock
;
1266 error
= nl_sock_create(NETLINK_GENERIC
, &sock
);
1271 error
= nl_sock_join_mcgroup(sock
, ovs_vport_mcgroup
);
1273 nl_sock_destroy(sock
);
1276 dpif
->port_notifier
= sock
;
1278 /* We have no idea of the current state so report that everything
1284 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1285 uint64_t buf_stub
[4096 / 8];
1289 ofpbuf_use_stub(&buf
, buf_stub
, sizeof buf_stub
);
1290 error
= nl_sock_recv(dpif
->port_notifier
, &buf
, false);
1292 struct dpif_netlink_vport vport
;
1294 error
= dpif_netlink_vport_from_ofpbuf(&vport
, &buf
);
1296 if (vport
.dp_ifindex
== dpif
->dp_ifindex
1297 && (vport
.cmd
== OVS_VPORT_CMD_NEW
1298 || vport
.cmd
== OVS_VPORT_CMD_DEL
1299 || vport
.cmd
== OVS_VPORT_CMD_SET
)) {
1300 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8
,
1301 dpif
->dpif
.full_name
, vport
.name
, vport
.cmd
);
1302 if (vport
.cmd
== OVS_VPORT_CMD_DEL
&& dpif
->handlers
) {
1303 dpif
->refresh_channels
= true;
1305 *devnamep
= xstrdup(vport
.name
);
1306 ofpbuf_uninit(&buf
);
1310 } else if (error
!= EAGAIN
) {
1311 VLOG_WARN_RL(&rl
, "error reading or parsing netlink (%s)",
1312 ovs_strerror(error
));
1313 nl_sock_drain(dpif
->port_notifier
);
1317 ofpbuf_uninit(&buf
);
1325 dpif_netlink_port_poll_wait(const struct dpif
*dpif_
)
1327 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1329 if (dpif
->port_notifier
) {
1330 nl_sock_wait(dpif
->port_notifier
, POLLIN
);
1332 poll_immediate_wake();
1337 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow
*request
,
1338 const ovs_u128
*ufid
, bool terse
)
1341 request
->ufid
= *ufid
;
1342 request
->ufid_present
= true;
1344 request
->ufid_present
= false;
1346 request
->ufid_terse
= terse
;
1350 dpif_netlink_init_flow_get__(const struct dpif_netlink
*dpif
,
1351 const struct nlattr
*key
, size_t key_len
,
1352 const ovs_u128
*ufid
, bool terse
,
1353 struct dpif_netlink_flow
*request
)
1355 dpif_netlink_flow_init(request
);
1356 request
->cmd
= OVS_FLOW_CMD_GET
;
1357 request
->dp_ifindex
= dpif
->dp_ifindex
;
1359 request
->key_len
= key_len
;
1360 dpif_netlink_flow_init_ufid(request
, ufid
, terse
);
1364 dpif_netlink_init_flow_get(const struct dpif_netlink
*dpif
,
1365 const struct dpif_flow_get
*get
,
1366 struct dpif_netlink_flow
*request
)
1368 dpif_netlink_init_flow_get__(dpif
, get
->key
, get
->key_len
, get
->ufid
,
1373 dpif_netlink_flow_get__(const struct dpif_netlink
*dpif
,
1374 const struct nlattr
*key
, size_t key_len
,
1375 const ovs_u128
*ufid
, bool terse
,
1376 struct dpif_netlink_flow
*reply
, struct ofpbuf
**bufp
)
1378 struct dpif_netlink_flow request
;
1380 dpif_netlink_init_flow_get__(dpif
, key
, key_len
, ufid
, terse
, &request
);
1381 return dpif_netlink_flow_transact(&request
, reply
, bufp
);
1385 dpif_netlink_flow_get(const struct dpif_netlink
*dpif
,
1386 const struct dpif_netlink_flow
*flow
,
1387 struct dpif_netlink_flow
*reply
, struct ofpbuf
**bufp
)
1389 return dpif_netlink_flow_get__(dpif
, flow
->key
, flow
->key_len
,
1390 flow
->ufid_present
? &flow
->ufid
: NULL
,
1391 false, reply
, bufp
);
1395 dpif_netlink_init_flow_put(struct dpif_netlink
*dpif
,
1396 const struct dpif_flow_put
*put
,
1397 struct dpif_netlink_flow
*request
)
1399 static const struct nlattr dummy_action
;
1401 dpif_netlink_flow_init(request
);
1402 request
->cmd
= (put
->flags
& DPIF_FP_CREATE
1403 ? OVS_FLOW_CMD_NEW
: OVS_FLOW_CMD_SET
);
1404 request
->dp_ifindex
= dpif
->dp_ifindex
;
1405 request
->key
= put
->key
;
1406 request
->key_len
= put
->key_len
;
1407 request
->mask
= put
->mask
;
1408 request
->mask_len
= put
->mask_len
;
1409 dpif_netlink_flow_init_ufid(request
, put
->ufid
, false);
1411 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1412 request
->actions
= (put
->actions
1414 : CONST_CAST(struct nlattr
*, &dummy_action
));
1415 request
->actions_len
= put
->actions_len
;
1416 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
1417 request
->clear
= true;
1419 if (put
->flags
& DPIF_FP_PROBE
) {
1420 request
->probe
= true;
1422 request
->nlmsg_flags
= put
->flags
& DPIF_FP_MODIFY
? 0 : NLM_F_CREATE
;
1426 dpif_netlink_init_flow_del__(struct dpif_netlink
*dpif
,
1427 const struct nlattr
*key
, size_t key_len
,
1428 const ovs_u128
*ufid
, bool terse
,
1429 struct dpif_netlink_flow
*request
)
1431 dpif_netlink_flow_init(request
);
1432 request
->cmd
= OVS_FLOW_CMD_DEL
;
1433 request
->dp_ifindex
= dpif
->dp_ifindex
;
1435 request
->key_len
= key_len
;
1436 dpif_netlink_flow_init_ufid(request
, ufid
, terse
);
1440 dpif_netlink_init_flow_del(struct dpif_netlink
*dpif
,
1441 const struct dpif_flow_del
*del
,
1442 struct dpif_netlink_flow
*request
)
1444 dpif_netlink_init_flow_del__(dpif
, del
->key
, del
->key_len
,
1445 del
->ufid
, del
->terse
, request
);
1449 DUMP_OVS_FLOWS_BIT
= 0,
1450 DUMP_OFFLOADED_FLOWS_BIT
= 1,
1454 DUMP_OVS_FLOWS
= (1 << DUMP_OVS_FLOWS_BIT
),
1455 DUMP_OFFLOADED_FLOWS
= (1 << DUMP_OFFLOADED_FLOWS_BIT
),
1458 struct dpif_netlink_flow_dump
{
1459 struct dpif_flow_dump up
;
1460 struct nl_dump nl_dump
;
1462 struct netdev_flow_dump
**netdev_dumps
;
1463 int netdev_dumps_num
; /* Number of netdev_flow_dumps */
1464 struct ovs_mutex netdev_lock
; /* Guards the following. */
1465 int netdev_current_dump OVS_GUARDED
; /* Shared current dump */
1466 int type
; /* Type of dump */
1469 static struct dpif_netlink_flow_dump
*
1470 dpif_netlink_flow_dump_cast(struct dpif_flow_dump
*dump
)
1472 return CONTAINER_OF(dump
, struct dpif_netlink_flow_dump
, up
);
1476 start_netdev_dump(const struct dpif
*dpif_
,
1477 struct dpif_netlink_flow_dump
*dump
)
1479 ovs_mutex_init(&dump
->netdev_lock
);
1481 if (!(dump
->type
& DUMP_OFFLOADED_FLOWS
)) {
1482 dump
->netdev_dumps_num
= 0;
1483 dump
->netdev_dumps
= NULL
;
1487 ovs_mutex_lock(&dump
->netdev_lock
);
1488 dump
->netdev_current_dump
= 0;
1490 = netdev_ports_flow_dump_create(DPIF_HMAP_KEY(dpif_
),
1491 &dump
->netdev_dumps_num
);
1492 ovs_mutex_unlock(&dump
->netdev_lock
);
1496 dpif_netlink_get_dump_type(char *str
) {
1499 if (!str
|| !strcmp(str
, "ovs") || !strcmp(str
, "dpctl")) {
1500 type
|= DUMP_OVS_FLOWS
;
1502 if ((netdev_is_flow_api_enabled() && !str
)
1503 || (str
&& (!strcmp(str
, "offloaded") || !strcmp(str
, "dpctl")))) {
1504 type
|= DUMP_OFFLOADED_FLOWS
;
1510 static struct dpif_flow_dump
*
1511 dpif_netlink_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
1514 const struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
1515 struct dpif_netlink_flow_dump
*dump
;
1516 struct dpif_netlink_flow request
;
1519 dump
= xmalloc(sizeof *dump
);
1520 dpif_flow_dump_init(&dump
->up
, dpif_
);
1522 dump
->type
= dpif_netlink_get_dump_type(type
);
1524 if (dump
->type
& DUMP_OVS_FLOWS
) {
1525 dpif_netlink_flow_init(&request
);
1526 request
.cmd
= OVS_FLOW_CMD_GET
;
1527 request
.dp_ifindex
= dpif
->dp_ifindex
;
1528 request
.ufid_present
= false;
1529 request
.ufid_terse
= terse
;
1531 buf
= ofpbuf_new(1024);
1532 dpif_netlink_flow_to_ofpbuf(&request
, buf
);
1533 nl_dump_start(&dump
->nl_dump
, NETLINK_GENERIC
, buf
);
1536 atomic_init(&dump
->status
, 0);
1537 dump
->up
.terse
= terse
;
1539 start_netdev_dump(dpif_
, dump
);
1545 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
1547 struct dpif_netlink_flow_dump
*dump
= dpif_netlink_flow_dump_cast(dump_
);
1548 unsigned int nl_status
= 0;
1551 if (dump
->type
& DUMP_OVS_FLOWS
) {
1552 nl_status
= nl_dump_done(&dump
->nl_dump
);
1555 for (int i
= 0; i
< dump
->netdev_dumps_num
; i
++) {
1556 int err
= netdev_flow_dump_destroy(dump
->netdev_dumps
[i
]);
1558 if (err
!= 0 && err
!= EOPNOTSUPP
) {
1559 VLOG_ERR("failed dumping netdev: %s", ovs_strerror(err
));
1563 free(dump
->netdev_dumps
);
1564 ovs_mutex_destroy(&dump
->netdev_lock
);
1566 /* No other thread has access to 'dump' at this point. */
1567 atomic_read_relaxed(&dump
->status
, &dump_status
);
1569 return dump_status
? dump_status
: nl_status
;
1572 struct dpif_netlink_flow_dump_thread
{
1573 struct dpif_flow_dump_thread up
;
1574 struct dpif_netlink_flow_dump
*dump
;
1575 struct dpif_netlink_flow flow
;
1576 struct dpif_flow_stats stats
;
1577 struct ofpbuf nl_flows
; /* Always used to store flows. */
1578 struct ofpbuf
*nl_actions
; /* Used if kernel does not supply actions. */
1579 int netdev_dump_idx
; /* This thread current netdev dump index */
1580 bool netdev_done
; /* If we are finished dumping netdevs */
1582 /* (Key/Mask/Actions) Buffers for netdev dumping */
1583 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
1584 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
1585 struct odputil_keybuf actbuf
[FLOW_DUMP_MAX_BATCH
];
1588 static struct dpif_netlink_flow_dump_thread
*
1589 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
1591 return CONTAINER_OF(thread
, struct dpif_netlink_flow_dump_thread
, up
);
1594 static struct dpif_flow_dump_thread
*
1595 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
1597 struct dpif_netlink_flow_dump
*dump
= dpif_netlink_flow_dump_cast(dump_
);
1598 struct dpif_netlink_flow_dump_thread
*thread
;
1600 thread
= xmalloc(sizeof *thread
);
1601 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
1602 thread
->dump
= dump
;
1603 ofpbuf_init(&thread
->nl_flows
, NL_DUMP_BUFSIZE
);
1604 thread
->nl_actions
= NULL
;
1605 thread
->netdev_dump_idx
= 0;
1606 thread
->netdev_done
= !(thread
->netdev_dump_idx
< dump
->netdev_dumps_num
);
1612 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
1614 struct dpif_netlink_flow_dump_thread
*thread
1615 = dpif_netlink_flow_dump_thread_cast(thread_
);
1617 ofpbuf_uninit(&thread
->nl_flows
);
1618 ofpbuf_delete(thread
->nl_actions
);
1623 dpif_netlink_flow_to_dpif_flow(struct dpif
*dpif
, struct dpif_flow
*dpif_flow
,
1624 const struct dpif_netlink_flow
*datapath_flow
)
1626 dpif_flow
->key
= datapath_flow
->key
;
1627 dpif_flow
->key_len
= datapath_flow
->key_len
;
1628 dpif_flow
->mask
= datapath_flow
->mask
;
1629 dpif_flow
->mask_len
= datapath_flow
->mask_len
;
1630 dpif_flow
->actions
= datapath_flow
->actions
;
1631 dpif_flow
->actions_len
= datapath_flow
->actions_len
;
1632 dpif_flow
->ufid_present
= datapath_flow
->ufid_present
;
1633 dpif_flow
->pmd_id
= PMD_ID_NULL
;
1634 if (datapath_flow
->ufid_present
) {
1635 dpif_flow
->ufid
= datapath_flow
->ufid
;
1637 ovs_assert(datapath_flow
->key
&& datapath_flow
->key_len
);
1638 dpif_flow_hash(dpif
, datapath_flow
->key
, datapath_flow
->key_len
,
1641 dpif_netlink_flow_get_stats(datapath_flow
, &dpif_flow
->stats
);
1642 dpif_flow
->offloaded
= false;
1645 /* The design is such that all threads are working together on the first dump
1646 * to the last, in order (at first they all on dump 0).
1647 * When the first thread finds that the given dump is finished,
1648 * they all move to the next. If two or more threads find the same dump
1649 * is finished at the same time, the first one will advance the shared
1650 * netdev_current_dump and the others will catch up. */
1652 dpif_netlink_advance_netdev_dump(struct dpif_netlink_flow_dump_thread
*thread
)
1654 struct dpif_netlink_flow_dump
*dump
= thread
->dump
;
1656 ovs_mutex_lock(&dump
->netdev_lock
);
1657 /* if we haven't finished (dumped everything) */
1658 if (dump
->netdev_current_dump
< dump
->netdev_dumps_num
) {
1659 /* if we are the first to find that current dump is finished
1661 if (thread
->netdev_dump_idx
== dump
->netdev_current_dump
) {
1662 thread
->netdev_dump_idx
= ++dump
->netdev_current_dump
;
1663 /* did we just finish the last dump? done. */
1664 if (dump
->netdev_current_dump
== dump
->netdev_dumps_num
) {
1665 thread
->netdev_done
= true;
1668 /* otherwise, we are behind, catch up */
1669 thread
->netdev_dump_idx
= dump
->netdev_current_dump
;
1672 /* some other thread finished */
1673 thread
->netdev_done
= true;
1675 ovs_mutex_unlock(&dump
->netdev_lock
);
1679 dpif_netlink_netdev_match_to_dpif_flow(struct match
*match
,
1680 struct ofpbuf
*key_buf
,
1681 struct ofpbuf
*mask_buf
,
1682 struct nlattr
*actions
,
1683 struct dpif_flow_stats
*stats
,
1685 struct dpif_flow
*flow
,
1686 bool terse OVS_UNUSED
)
1689 struct odp_flow_key_parms odp_parms
= {
1690 .flow
= &match
->flow
,
1691 .mask
= &match
->wc
.masks
,
1693 .max_vlan_headers
= 1,
1698 memset(flow
, 0, sizeof *flow
);
1701 offset
= key_buf
->size
;
1702 flow
->key
= ofpbuf_tail(key_buf
);
1703 odp_flow_key_from_flow(&odp_parms
, key_buf
);
1704 flow
->key_len
= key_buf
->size
- offset
;
1707 offset
= mask_buf
->size
;
1708 flow
->mask
= ofpbuf_tail(mask_buf
);
1709 odp_parms
.key_buf
= key_buf
;
1710 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
1711 flow
->mask_len
= mask_buf
->size
- offset
;
1714 flow
->actions
= nl_attr_get(actions
);
1715 flow
->actions_len
= nl_attr_get_size(actions
);
1718 memcpy(&flow
->stats
, stats
, sizeof *stats
);
1721 flow
->ufid_present
= true;
1724 flow
->pmd_id
= PMD_ID_NULL
;
1726 flow
->offloaded
= true;
1732 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
1733 struct dpif_flow
*flows
, int max_flows
)
1735 struct dpif_netlink_flow_dump_thread
*thread
1736 = dpif_netlink_flow_dump_thread_cast(thread_
);
1737 struct dpif_netlink_flow_dump
*dump
= thread
->dump
;
1738 struct dpif_netlink
*dpif
= dpif_netlink_cast(thread
->up
.dpif
);
1741 ofpbuf_delete(thread
->nl_actions
);
1742 thread
->nl_actions
= NULL
;
1745 max_flows
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
1747 while (!thread
->netdev_done
&& n_flows
< max_flows
) {
1748 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[n_flows
];
1749 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[n_flows
];
1750 struct odputil_keybuf
*actbuf
= &thread
->actbuf
[n_flows
];
1751 struct ofpbuf key
, mask
, act
;
1752 struct dpif_flow
*f
= &flows
[n_flows
];
1753 int cur
= thread
->netdev_dump_idx
;
1754 struct netdev_flow_dump
*netdev_dump
= dump
->netdev_dumps
[cur
];
1756 struct nlattr
*actions
;
1757 struct dpif_flow_stats stats
;
1761 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
1762 ofpbuf_use_stack(&act
, actbuf
, sizeof *actbuf
);
1763 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
1764 has_next
= netdev_flow_dump_next(netdev_dump
, &match
,
1770 dpif_netlink_netdev_match_to_dpif_flow(&match
,
1779 dpif_netlink_advance_netdev_dump(thread
);
1783 if (!(dump
->type
& DUMP_OVS_FLOWS
)) {
1788 || (n_flows
< max_flows
&& thread
->nl_flows
.size
)) {
1789 struct dpif_netlink_flow datapath_flow
;
1790 struct ofpbuf nl_flow
;
1793 /* Try to grab another flow. */
1794 if (!nl_dump_next(&dump
->nl_dump
, &nl_flow
, &thread
->nl_flows
)) {
1798 /* Convert the flow to our output format. */
1799 error
= dpif_netlink_flow_from_ofpbuf(&datapath_flow
, &nl_flow
);
1801 atomic_store_relaxed(&dump
->status
, error
);
1805 if (dump
->up
.terse
|| datapath_flow
.actions
) {
1806 /* Common case: we don't want actions, or the flow includes
1808 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, &flows
[n_flows
++],
1811 /* Rare case: the flow does not include actions. Retrieve this
1812 * individual flow again to get the actions. */
1813 error
= dpif_netlink_flow_get(dpif
, &datapath_flow
,
1814 &datapath_flow
, &thread
->nl_actions
);
1815 if (error
== ENOENT
) {
1816 VLOG_DBG("dumped flow disappeared on get");
1819 VLOG_WARN("error fetching dumped flow: %s",
1820 ovs_strerror(error
));
1821 atomic_store_relaxed(&dump
->status
, error
);
1825 /* Save this flow. Then exit, because we only have one buffer to
1826 * handle this case. */
1827 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, &flows
[n_flows
++],
1836 dpif_netlink_encode_execute(int dp_ifindex
, const struct dpif_execute
*d_exec
,
1839 struct ovs_header
*k_exec
;
1842 ofpbuf_prealloc_tailroom(buf
, (64
1843 + dp_packet_size(d_exec
->packet
)
1844 + ODP_KEY_METADATA_SIZE
1845 + d_exec
->actions_len
));
1847 nl_msg_put_genlmsghdr(buf
, 0, ovs_packet_family
, NLM_F_REQUEST
,
1848 OVS_PACKET_CMD_EXECUTE
, OVS_PACKET_VERSION
);
1850 k_exec
= ofpbuf_put_uninit(buf
, sizeof *k_exec
);
1851 k_exec
->dp_ifindex
= dp_ifindex
;
1853 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_PACKET
,
1854 dp_packet_data(d_exec
->packet
),
1855 dp_packet_size(d_exec
->packet
));
1857 key_ofs
= nl_msg_start_nested(buf
, OVS_PACKET_ATTR_KEY
);
1858 odp_key_from_dp_packet(buf
, d_exec
->packet
);
1859 nl_msg_end_nested(buf
, key_ofs
);
1861 nl_msg_put_unspec(buf
, OVS_PACKET_ATTR_ACTIONS
,
1862 d_exec
->actions
, d_exec
->actions_len
);
1863 if (d_exec
->probe
) {
1864 nl_msg_put_flag(buf
, OVS_PACKET_ATTR_PROBE
);
1867 nl_msg_put_u16(buf
, OVS_PACKET_ATTR_MRU
, d_exec
->mtu
);
1871 /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1872 * Returns the number actually executed (at least 1, if 'n_ops' is
1875 dpif_netlink_operate__(struct dpif_netlink
*dpif
,
1876 struct dpif_op
**ops
, size_t n_ops
)
1879 struct nl_transaction txn
;
1881 struct ofpbuf request
;
1882 uint64_t request_stub
[1024 / 8];
1884 struct ofpbuf reply
;
1885 uint64_t reply_stub
[1024 / 8];
1886 } auxes
[OPERATE_MAX_OPS
];
1888 struct nl_transaction
*txnsp
[OPERATE_MAX_OPS
];
1891 n_ops
= MIN(n_ops
, OPERATE_MAX_OPS
);
1892 for (i
= 0; i
< n_ops
; i
++) {
1893 struct op_auxdata
*aux
= &auxes
[i
];
1894 struct dpif_op
*op
= ops
[i
];
1895 struct dpif_flow_put
*put
;
1896 struct dpif_flow_del
*del
;
1897 struct dpif_flow_get
*get
;
1898 struct dpif_netlink_flow flow
;
1900 ofpbuf_use_stub(&aux
->request
,
1901 aux
->request_stub
, sizeof aux
->request_stub
);
1902 aux
->txn
.request
= &aux
->request
;
1904 ofpbuf_use_stub(&aux
->reply
, aux
->reply_stub
, sizeof aux
->reply_stub
);
1905 aux
->txn
.reply
= NULL
;
1908 case DPIF_OP_FLOW_PUT
:
1909 put
= &op
->u
.flow_put
;
1910 dpif_netlink_init_flow_put(dpif
, put
, &flow
);
1912 flow
.nlmsg_flags
|= NLM_F_ECHO
;
1913 aux
->txn
.reply
= &aux
->reply
;
1915 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1918 case DPIF_OP_FLOW_DEL
:
1919 del
= &op
->u
.flow_del
;
1920 dpif_netlink_init_flow_del(dpif
, del
, &flow
);
1922 flow
.nlmsg_flags
|= NLM_F_ECHO
;
1923 aux
->txn
.reply
= &aux
->reply
;
1925 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1928 case DPIF_OP_EXECUTE
:
1929 /* Can't execute a packet that won't fit in a Netlink attribute. */
1930 if (OVS_UNLIKELY(nl_attr_oversized(
1931 dp_packet_size(op
->u
.execute
.packet
)))) {
1932 /* Report an error immediately if this is the first operation.
1933 * Otherwise the easiest thing to do is to postpone to the next
1934 * call (when this will be the first operation). */
1936 VLOG_ERR_RL(&error_rl
,
1937 "dropping oversized %"PRIu32
"-byte packet",
1938 dp_packet_size(op
->u
.execute
.packet
));
1939 op
->error
= ENOBUFS
;
1944 dpif_netlink_encode_execute(dpif
->dp_ifindex
, &op
->u
.execute
,
1949 case DPIF_OP_FLOW_GET
:
1950 get
= &op
->u
.flow_get
;
1951 dpif_netlink_init_flow_get(dpif
, get
, &flow
);
1952 aux
->txn
.reply
= get
->buffer
;
1953 dpif_netlink_flow_to_ofpbuf(&flow
, &aux
->request
);
1961 for (i
= 0; i
< n_ops
; i
++) {
1962 txnsp
[i
] = &auxes
[i
].txn
;
1964 nl_transact_multiple(NETLINK_GENERIC
, txnsp
, n_ops
);
1966 for (i
= 0; i
< n_ops
; i
++) {
1967 struct op_auxdata
*aux
= &auxes
[i
];
1968 struct nl_transaction
*txn
= &auxes
[i
].txn
;
1969 struct dpif_op
*op
= ops
[i
];
1970 struct dpif_flow_put
*put
;
1971 struct dpif_flow_del
*del
;
1972 struct dpif_flow_get
*get
;
1974 op
->error
= txn
->error
;
1977 case DPIF_OP_FLOW_PUT
:
1978 put
= &op
->u
.flow_put
;
1981 struct dpif_netlink_flow reply
;
1983 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
,
1986 dpif_netlink_flow_get_stats(&reply
, put
->stats
);
1992 case DPIF_OP_FLOW_DEL
:
1993 del
= &op
->u
.flow_del
;
1996 struct dpif_netlink_flow reply
;
1998 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
,
2001 dpif_netlink_flow_get_stats(&reply
, del
->stats
);
2007 case DPIF_OP_EXECUTE
:
2010 case DPIF_OP_FLOW_GET
:
2011 get
= &op
->u
.flow_get
;
2013 struct dpif_netlink_flow reply
;
2015 op
->error
= dpif_netlink_flow_from_ofpbuf(&reply
, txn
->reply
);
2017 dpif_netlink_flow_to_dpif_flow(&dpif
->dpif
, get
->flow
,
2027 ofpbuf_uninit(&aux
->request
);
2028 ofpbuf_uninit(&aux
->reply
);
2035 parse_flow_get(struct dpif_netlink
*dpif
, struct dpif_flow_get
*get
)
2037 struct dpif_flow
*dpif_flow
= get
->flow
;
2039 struct nlattr
*actions
;
2040 struct dpif_flow_stats stats
;
2042 uint64_t act_buf
[1024 / 8];
2043 struct odputil_keybuf maskbuf
;
2044 struct odputil_keybuf keybuf
;
2045 struct odputil_keybuf actbuf
;
2046 struct ofpbuf key
, mask
, act
;
2049 ofpbuf_use_stack(&buf
, &act_buf
, sizeof act_buf
);
2050 err
= netdev_ports_flow_get(DPIF_HMAP_KEY(&dpif
->dpif
), &match
,
2051 &actions
, get
->ufid
, &stats
, &buf
);
2056 VLOG_DBG("found flow from netdev, translating to dpif flow");
2058 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2059 ofpbuf_use_stack(&act
, &actbuf
, sizeof actbuf
);
2060 ofpbuf_use_stack(&mask
, &maskbuf
, sizeof maskbuf
);
2061 dpif_netlink_netdev_match_to_dpif_flow(&match
, &key
, &mask
, actions
,
2063 (ovs_u128
*) get
->ufid
,
2066 ofpbuf_put(get
->buffer
, nl_attr_get(actions
), nl_attr_get_size(actions
));
2067 dpif_flow
->actions
= ofpbuf_at(get
->buffer
, 0, 0);
2068 dpif_flow
->actions_len
= nl_attr_get_size(actions
);
2074 parse_flow_put(struct dpif_netlink
*dpif
, struct dpif_flow_put
*put
)
2076 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
2079 const struct nlattr
*nla
;
2083 struct offload_info info
;
2084 ovs_be16 dst_port
= 0;
2087 if (put
->flags
& DPIF_FP_PROBE
) {
2091 err
= parse_key_and_mask_to_match(put
->key
, put
->key_len
, put
->mask
,
2092 put
->mask_len
, &match
);
2097 /* When we try to install a dummy flow from a probed feature. */
2098 if (match
.flow
.dl_type
== htons(0x1234)) {
2102 in_port
= match
.flow
.in_port
.odp_port
;
2103 dev
= netdev_ports_get(in_port
, DPIF_HMAP_KEY(&dpif
->dpif
));
2108 /* Get tunnel dst port and count outputs */
2109 NL_ATTR_FOR_EACH(nla
, left
, put
->actions
, put
->actions_len
) {
2110 if (nl_attr_type(nla
) == OVS_ACTION_ATTR_OUTPUT
) {
2111 const struct netdev_tunnel_config
*tnl_cfg
;
2112 struct netdev
*outdev
;
2113 odp_port_t out_port
;
2117 VLOG_DBG_RL(&rl
, "offloading multiple ports isn't supported");
2122 out_port
= nl_attr_get_odp_port(nla
);
2123 outdev
= netdev_ports_get(out_port
, DPIF_HMAP_KEY(&dpif
->dpif
));
2128 tnl_cfg
= netdev_get_tunnel_config(outdev
);
2129 if (tnl_cfg
&& tnl_cfg
->dst_port
!= 0) {
2130 dst_port
= tnl_cfg
->dst_port
;
2132 netdev_close(outdev
);
2136 info
.port_hmap_obj
= DPIF_HMAP_KEY(&dpif
->dpif
);
2137 info
.tp_dst_port
= dst_port
;
2138 err
= netdev_flow_put(dev
, &match
,
2139 CONST_CAST(struct nlattr
*, put
->actions
),
2141 CONST_CAST(ovs_u128
*, put
->ufid
),
2145 if (put
->flags
& DPIF_FP_MODIFY
) {
2146 struct dpif_op
*opp
;
2149 op
.type
= DPIF_OP_FLOW_DEL
;
2150 op
.u
.flow_del
.key
= put
->key
;
2151 op
.u
.flow_del
.key_len
= put
->key_len
;
2152 op
.u
.flow_del
.ufid
= put
->ufid
;
2153 op
.u
.flow_del
.pmd_id
= put
->pmd_id
;
2154 op
.u
.flow_del
.stats
= NULL
;
2155 op
.u
.flow_del
.terse
= false;
2158 dpif_netlink_operate__(dpif
, &opp
, 1);
2161 VLOG_DBG("added flow");
2162 } else if (err
!= EEXIST
) {
2163 VLOG_ERR_RL(&rl
, "failed to offload flow: %s", ovs_strerror(err
));
2167 if (err
&& err
!= EEXIST
&& (put
->flags
& DPIF_FP_MODIFY
)) {
2168 /* Modified rule can't be offloaded, try and delete from HW */
2169 int del_err
= netdev_flow_del(dev
, put
->ufid
, put
->stats
);
2172 /* Delete from hw success, so old flow was offloaded.
2173 * Change flags to create the flow in kernel */
2174 put
->flags
&= ~DPIF_FP_MODIFY
;
2175 put
->flags
|= DPIF_FP_CREATE
;
2176 } else if (del_err
!= ENOENT
) {
2177 VLOG_ERR_RL(&rl
, "failed to delete offloaded flow: %s",
2178 ovs_strerror(del_err
));
2179 /* stop proccesing the flow in kernel */
2190 try_send_to_netdev(struct dpif_netlink
*dpif
, struct dpif_op
*op
)
2192 int err
= EOPNOTSUPP
;
2195 case DPIF_OP_FLOW_PUT
: {
2196 struct dpif_flow_put
*put
= &op
->u
.flow_put
;
2202 log_flow_put_message(&dpif
->dpif
, &this_module
, put
, 0);
2203 err
= parse_flow_put(dpif
, put
);
2206 case DPIF_OP_FLOW_DEL
: {
2207 struct dpif_flow_del
*del
= &op
->u
.flow_del
;
2213 log_flow_del_message(&dpif
->dpif
, &this_module
, del
, 0);
2214 err
= netdev_ports_flow_del(DPIF_HMAP_KEY(&dpif
->dpif
), del
->ufid
,
2218 case DPIF_OP_FLOW_GET
: {
2219 struct dpif_flow_get
*get
= &op
->u
.flow_get
;
2221 if (!op
->u
.flow_get
.ufid
) {
2225 log_flow_get_message(&dpif
->dpif
, &this_module
, get
, 0);
2226 err
= parse_flow_get(dpif
, get
);
2229 case DPIF_OP_EXECUTE
:
2238 dpif_netlink_operate_chunks(struct dpif_netlink
*dpif
, struct dpif_op
**ops
,
2242 size_t chunk
= dpif_netlink_operate__(dpif
, ops
, n_ops
);
2250 dpif_netlink_operate(struct dpif
*dpif_
, struct dpif_op
**ops
, size_t n_ops
)
2252 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2253 struct dpif_op
*new_ops
[OPERATE_MAX_OPS
];
2258 if (netdev_is_flow_api_enabled()) {
2262 while (n_ops
> 0 && count
< OPERATE_MAX_OPS
) {
2263 struct dpif_op
*op
= ops
[i
++];
2265 err
= try_send_to_netdev(dpif
, op
);
2266 if (err
&& err
!= EEXIST
) {
2267 new_ops
[count
++] = op
;
2275 dpif_netlink_operate_chunks(dpif
, new_ops
, count
);
2278 dpif_netlink_operate_chunks(dpif
, ops
, n_ops
);
2284 dpif_netlink_handler_uninit(struct dpif_handler
*handler
)
2286 vport_delete_sock_pool(handler
);
2290 dpif_netlink_handler_init(struct dpif_handler
*handler
)
2292 return vport_create_sock_pool(handler
);
2297 dpif_netlink_handler_init(struct dpif_handler
*handler
)
2299 handler
->epoll_fd
= epoll_create(10);
2300 return handler
->epoll_fd
< 0 ? errno
: 0;
2304 dpif_netlink_handler_uninit(struct dpif_handler
*handler
)
2306 close(handler
->epoll_fd
);
2310 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
2311 * currently in 'dpif' in the kernel, by adding a new set of channels for
2312 * any kernel vport that lacks one and deleting any channels that have no
2313 * backing kernel vports. */
2315 dpif_netlink_refresh_channels(struct dpif_netlink
*dpif
, uint32_t n_handlers
)
2316 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2318 unsigned long int *keep_channels
;
2319 struct dpif_netlink_vport vport
;
2320 size_t keep_channels_nbits
;
2321 struct nl_dump dump
;
2322 uint64_t reply_stub
[NL_DUMP_BUFSIZE
/ 8];
2327 ovs_assert(!WINDOWS
|| n_handlers
<= 1);
2328 ovs_assert(!WINDOWS
|| dpif
->n_handlers
<= 1);
2330 if (dpif
->n_handlers
!= n_handlers
) {
2331 destroy_all_channels(dpif
);
2332 dpif
->handlers
= xzalloc(n_handlers
* sizeof *dpif
->handlers
);
2333 for (i
= 0; i
< n_handlers
; i
++) {
2335 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
2337 error
= dpif_netlink_handler_init(handler
);
2341 for (j
= 0; j
< i
; j
++) {
2342 struct dpif_handler
*tmp
= &dpif
->handlers
[j
];
2343 dpif_netlink_handler_uninit(tmp
);
2345 free(dpif
->handlers
);
2346 dpif
->handlers
= NULL
;
2351 dpif
->n_handlers
= n_handlers
;
2354 for (i
= 0; i
< n_handlers
; i
++) {
2355 struct dpif_handler
*handler
= &dpif
->handlers
[i
];
2357 handler
->event_offset
= handler
->n_events
= 0;
2360 keep_channels_nbits
= dpif
->uc_array_size
;
2361 keep_channels
= bitmap_allocate(keep_channels_nbits
);
2363 ofpbuf_use_stub(&buf
, reply_stub
, sizeof reply_stub
);
2364 dpif_netlink_port_dump_start__(dpif
, &dump
);
2365 while (!dpif_netlink_port_dump_next__(dpif
, &dump
, &vport
, &buf
)) {
2366 uint32_t port_no
= odp_to_u32(vport
.port_no
);
2367 uint32_t *upcall_pids
= NULL
;
2370 if (port_no
>= dpif
->uc_array_size
2371 || !vport_get_pids(dpif
, port_no
, &upcall_pids
)) {
2372 struct nl_sock
**socksp
= vport_create_socksp(dpif
, &error
);
2378 error
= vport_add_channels(dpif
, vport
.port_no
, socksp
);
2380 VLOG_INFO("%s: could not add channels for port %s",
2381 dpif_name(&dpif
->dpif
), vport
.name
);
2382 vport_del_socksp(dpif
, socksp
);
2386 upcall_pids
= vport_socksp_to_pids(socksp
, dpif
->n_handlers
);
2390 /* Configure the vport to deliver misses to 'sock'. */
2391 if (vport
.upcall_pids
[0] == 0
2392 || vport
.n_upcall_pids
!= dpif
->n_handlers
2393 || memcmp(upcall_pids
, vport
.upcall_pids
, n_handlers
* sizeof
2395 struct dpif_netlink_vport vport_request
;
2397 dpif_netlink_vport_init(&vport_request
);
2398 vport_request
.cmd
= OVS_VPORT_CMD_SET
;
2399 vport_request
.dp_ifindex
= dpif
->dp_ifindex
;
2400 vport_request
.port_no
= vport
.port_no
;
2401 vport_request
.n_upcall_pids
= dpif
->n_handlers
;
2402 vport_request
.upcall_pids
= upcall_pids
;
2403 error
= dpif_netlink_vport_transact(&vport_request
, NULL
, NULL
);
2405 VLOG_WARN_RL(&error_rl
,
2406 "%s: failed to set upcall pid on port: %s",
2407 dpif_name(&dpif
->dpif
), ovs_strerror(error
));
2409 if (error
!= ENODEV
&& error
!= ENOENT
) {
2412 /* The vport isn't really there, even though the dump says
2413 * it is. Probably we just hit a race after a port
2420 if (port_no
< keep_channels_nbits
) {
2421 bitmap_set1(keep_channels
, port_no
);
2428 vport_del_channels(dpif
, vport
.port_no
);
2430 nl_dump_done(&dump
);
2431 ofpbuf_uninit(&buf
);
2433 /* Discard any saved channels that we didn't reuse. */
2434 for (i
= 0; i
< keep_channels_nbits
; i
++) {
2435 if (!bitmap_is_set(keep_channels
, i
)) {
2436 vport_del_channels(dpif
, u32_to_odp(i
));
2439 free(keep_channels
);
2445 dpif_netlink_recv_set__(struct dpif_netlink
*dpif
, bool enable
)
2446 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2448 if ((dpif
->handlers
!= NULL
) == enable
) {
2450 } else if (!enable
) {
2451 destroy_all_channels(dpif
);
2454 return dpif_netlink_refresh_channels(dpif
, 1);
2459 dpif_netlink_recv_set(struct dpif
*dpif_
, bool enable
)
2461 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2464 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2465 error
= dpif_netlink_recv_set__(dpif
, enable
);
2466 fat_rwlock_unlock(&dpif
->upcall_lock
);
2472 dpif_netlink_handlers_set(struct dpif
*dpif_
, uint32_t n_handlers
)
2474 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2478 /* Multiple upcall handlers will be supported once kernel datapath supports
2480 if (n_handlers
> 1) {
2485 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2486 if (dpif
->handlers
) {
2487 error
= dpif_netlink_refresh_channels(dpif
, n_handlers
);
2489 fat_rwlock_unlock(&dpif
->upcall_lock
);
2495 dpif_netlink_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2496 uint32_t queue_id
, uint32_t *priority
)
2498 if (queue_id
< 0xf000) {
2499 *priority
= TC_H_MAKE(1 << 16, queue_id
+ 1);
2507 parse_odp_packet(const struct dpif_netlink
*dpif
, struct ofpbuf
*buf
,
2508 struct dpif_upcall
*upcall
, int *dp_ifindex
)
2510 static const struct nl_policy ovs_packet_policy
[] = {
2511 /* Always present. */
2512 [OVS_PACKET_ATTR_PACKET
] = { .type
= NL_A_UNSPEC
,
2513 .min_len
= ETH_HEADER_LEN
},
2514 [OVS_PACKET_ATTR_KEY
] = { .type
= NL_A_NESTED
},
2516 /* OVS_PACKET_CMD_ACTION only. */
2517 [OVS_PACKET_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
, .optional
= true },
2518 [OVS_PACKET_ATTR_EGRESS_TUN_KEY
] = { .type
= NL_A_NESTED
, .optional
= true },
2519 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
2520 [OVS_PACKET_ATTR_MRU
] = { .type
= NL_A_U16
, .optional
= true }
2523 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
2524 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
2525 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
2526 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
2528 struct nlattr
*a
[ARRAY_SIZE(ovs_packet_policy
)];
2529 if (!nlmsg
|| !genl
|| !ovs_header
2530 || nlmsg
->nlmsg_type
!= ovs_packet_family
2531 || !nl_policy_parse(&b
, 0, ovs_packet_policy
, a
,
2532 ARRAY_SIZE(ovs_packet_policy
))) {
2536 int type
= (genl
->cmd
== OVS_PACKET_CMD_MISS
? DPIF_UC_MISS
2537 : genl
->cmd
== OVS_PACKET_CMD_ACTION
? DPIF_UC_ACTION
2543 /* (Re)set ALL fields of '*upcall' on successful return. */
2544 upcall
->type
= type
;
2545 upcall
->key
= CONST_CAST(struct nlattr
*,
2546 nl_attr_get(a
[OVS_PACKET_ATTR_KEY
]));
2547 upcall
->key_len
= nl_attr_get_size(a
[OVS_PACKET_ATTR_KEY
]);
2548 dpif_flow_hash(&dpif
->dpif
, upcall
->key
, upcall
->key_len
, &upcall
->ufid
);
2549 upcall
->userdata
= a
[OVS_PACKET_ATTR_USERDATA
];
2550 upcall
->out_tun_key
= a
[OVS_PACKET_ATTR_EGRESS_TUN_KEY
];
2551 upcall
->actions
= a
[OVS_PACKET_ATTR_ACTIONS
];
2552 upcall
->mru
= a
[OVS_PACKET_ATTR_MRU
];
2554 /* Allow overwriting the netlink attribute header without reallocating. */
2555 dp_packet_use_stub(&upcall
->packet
,
2556 CONST_CAST(struct nlattr
*,
2557 nl_attr_get(a
[OVS_PACKET_ATTR_PACKET
])) - 1,
2558 nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]) +
2559 sizeof(struct nlattr
));
2560 dp_packet_set_data(&upcall
->packet
,
2561 (char *)dp_packet_data(&upcall
->packet
) + sizeof(struct nlattr
));
2562 dp_packet_set_size(&upcall
->packet
, nl_attr_get_size(a
[OVS_PACKET_ATTR_PACKET
]));
2564 if (nl_attr_find__(upcall
->key
, upcall
->key_len
, OVS_KEY_ATTR_ETHERNET
)) {
2565 /* Ethernet frame */
2566 upcall
->packet
.packet_type
= htonl(PT_ETH
);
2568 /* Non-Ethernet packet. Get the Ethertype from the NL attributes */
2569 ovs_be16 ethertype
= 0;
2570 const struct nlattr
*et_nla
= nl_attr_find__(upcall
->key
,
2572 OVS_KEY_ATTR_ETHERTYPE
);
2574 ethertype
= nl_attr_get_be16(et_nla
);
2576 upcall
->packet
.packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
2578 dp_packet_set_l3(&upcall
->packet
, dp_packet_data(&upcall
->packet
));
2581 *dp_ifindex
= ovs_header
->dp_ifindex
;
2587 #define PACKET_RECV_BATCH_SIZE 50
2589 dpif_netlink_recv_windows(struct dpif_netlink
*dpif
, uint32_t handler_id
,
2590 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2591 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2593 struct dpif_handler
*handler
;
2595 struct dpif_windows_vport_sock
*sock_pool
;
2598 if (!dpif
->handlers
) {
2602 /* Only one handler is supported currently. */
2603 if (handler_id
>= 1) {
2607 if (handler_id
>= dpif
->n_handlers
) {
2611 handler
= &dpif
->handlers
[handler_id
];
2612 sock_pool
= handler
->vport_sock_pool
;
2614 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
2619 if (++read_tries
> PACKET_RECV_BATCH_SIZE
) {
2623 error
= nl_sock_recv(sock_pool
[i
].nl_sock
, buf
, false);
2624 if (error
== ENOBUFS
) {
2625 /* ENOBUFS typically means that we've received so many
2626 * packets that the buffer overflowed. Try again
2627 * immediately because there's almost certainly a packet
2628 * waiting for us. */
2629 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2633 /* XXX: ch->last_poll = time_msec(); */
2635 if (error
== EAGAIN
) {
2641 error
= parse_odp_packet(dpif
, buf
, upcall
, &dp_ifindex
);
2642 if (!error
&& dp_ifindex
== dpif
->dp_ifindex
) {
2654 dpif_netlink_recv__(struct dpif_netlink
*dpif
, uint32_t handler_id
,
2655 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2656 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2658 struct dpif_handler
*handler
;
2661 if (!dpif
->handlers
|| handler_id
>= dpif
->n_handlers
) {
2665 handler
= &dpif
->handlers
[handler_id
];
2666 if (handler
->event_offset
>= handler
->n_events
) {
2669 handler
->event_offset
= handler
->n_events
= 0;
2672 retval
= epoll_wait(handler
->epoll_fd
, handler
->epoll_events
,
2673 dpif
->uc_array_size
, 0);
2674 } while (retval
< 0 && errno
== EINTR
);
2677 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2678 VLOG_WARN_RL(&rl
, "epoll_wait failed (%s)", ovs_strerror(errno
));
2679 } else if (retval
> 0) {
2680 handler
->n_events
= retval
;
2684 while (handler
->event_offset
< handler
->n_events
) {
2685 int idx
= handler
->epoll_events
[handler
->event_offset
].data
.u32
;
2686 struct dpif_channel
*ch
= &dpif
->handlers
[handler_id
].channels
[idx
];
2688 handler
->event_offset
++;
2694 if (++read_tries
> 50) {
2698 error
= nl_sock_recv(ch
->sock
, buf
, false);
2699 if (error
== ENOBUFS
) {
2700 /* ENOBUFS typically means that we've received so many
2701 * packets that the buffer overflowed. Try again
2702 * immediately because there's almost certainly a packet
2703 * waiting for us. */
2704 report_loss(dpif
, ch
, idx
, handler_id
);
2708 ch
->last_poll
= time_msec();
2710 if (error
== EAGAIN
) {
2716 error
= parse_odp_packet(dpif
, buf
, upcall
, &dp_ifindex
);
2717 if (!error
&& dp_ifindex
== dpif
->dp_ifindex
) {
2730 dpif_netlink_recv(struct dpif
*dpif_
, uint32_t handler_id
,
2731 struct dpif_upcall
*upcall
, struct ofpbuf
*buf
)
2733 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2736 fat_rwlock_rdlock(&dpif
->upcall_lock
);
2738 error
= dpif_netlink_recv_windows(dpif
, handler_id
, upcall
, buf
);
2740 error
= dpif_netlink_recv__(dpif
, handler_id
, upcall
, buf
);
2742 fat_rwlock_unlock(&dpif
->upcall_lock
);
2748 dpif_netlink_recv_wait__(struct dpif_netlink
*dpif
, uint32_t handler_id
)
2749 OVS_REQ_RDLOCK(dpif
->upcall_lock
)
2753 struct dpif_windows_vport_sock
*sock_pool
=
2754 dpif
->handlers
[handler_id
].vport_sock_pool
;
2756 /* Only one handler is supported currently. */
2757 if (handler_id
>= 1) {
2761 for (i
= 0; i
< VPORT_SOCK_POOL_SIZE
; i
++) {
2762 nl_sock_wait(sock_pool
[i
].nl_sock
, POLLIN
);
2765 if (dpif
->handlers
&& handler_id
< dpif
->n_handlers
) {
2766 struct dpif_handler
*handler
= &dpif
->handlers
[handler_id
];
2768 poll_fd_wait(handler
->epoll_fd
, POLLIN
);
2774 dpif_netlink_recv_wait(struct dpif
*dpif_
, uint32_t handler_id
)
2776 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2778 fat_rwlock_rdlock(&dpif
->upcall_lock
);
2779 dpif_netlink_recv_wait__(dpif
, handler_id
);
2780 fat_rwlock_unlock(&dpif
->upcall_lock
);
2784 dpif_netlink_recv_purge__(struct dpif_netlink
*dpif
)
2785 OVS_REQ_WRLOCK(dpif
->upcall_lock
)
2787 if (dpif
->handlers
) {
2790 for (i
= 0; i
< dpif
->uc_array_size
; i
++ ) {
2791 if (!dpif
->handlers
[0].channels
[i
].sock
) {
2795 for (j
= 0; j
< dpif
->n_handlers
; j
++) {
2796 nl_sock_drain(dpif
->handlers
[j
].channels
[i
].sock
);
2803 dpif_netlink_recv_purge(struct dpif
*dpif_
)
2805 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
2807 fat_rwlock_wrlock(&dpif
->upcall_lock
);
2808 dpif_netlink_recv_purge__(dpif
);
2809 fat_rwlock_unlock(&dpif
->upcall_lock
);
2813 dpif_netlink_get_datapath_version(void)
2815 char *version_str
= NULL
;
2819 #define MAX_VERSION_STR_SIZE 80
2820 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2823 f
= fopen(LINUX_DATAPATH_VERSION_FILE
, "r");
2826 char version
[MAX_VERSION_STR_SIZE
];
2828 if (fgets(version
, MAX_VERSION_STR_SIZE
, f
)) {
2829 newline
= strchr(version
, '\n');
2833 version_str
= xstrdup(version
);
2842 struct dpif_netlink_ct_dump_state
{
2843 struct ct_dpif_dump_state up
;
2844 struct nl_ct_dump_state
*nl_ct_dump
;
2848 dpif_netlink_ct_dump_start(struct dpif
*dpif OVS_UNUSED
,
2849 struct ct_dpif_dump_state
**dump_
,
2850 const uint16_t *zone
)
2852 struct dpif_netlink_ct_dump_state
*dump
;
2855 dump
= xzalloc(sizeof *dump
);
2856 err
= nl_ct_dump_start(&dump
->nl_ct_dump
, zone
);
2868 dpif_netlink_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
2869 struct ct_dpif_dump_state
*dump_
,
2870 struct ct_dpif_entry
*entry
)
2872 struct dpif_netlink_ct_dump_state
*dump
;
2874 INIT_CONTAINER(dump
, dump_
, up
);
2876 return nl_ct_dump_next(dump
->nl_ct_dump
, entry
);
2880 dpif_netlink_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
2881 struct ct_dpif_dump_state
*dump_
)
2883 struct dpif_netlink_ct_dump_state
*dump
;
2886 INIT_CONTAINER(dump
, dump_
, up
);
2888 err
= nl_ct_dump_done(dump
->nl_ct_dump
);
2894 dpif_netlink_ct_flush(struct dpif
*dpif OVS_UNUSED
, const uint16_t *zone
)
2897 return nl_ct_flush_zone(*zone
);
2899 return nl_ct_flush();
2906 dpif_netlink_meter_get_features(const struct dpif
* dpif OVS_UNUSED
,
2907 struct ofputil_meter_features
*features
)
2909 features
->max_meters
= 0;
2910 features
->band_types
= 0;
2911 features
->capabilities
= 0;
2912 features
->max_bands
= 0;
2913 features
->max_color
= 0;
2917 dpif_netlink_meter_set(struct dpif
*dpif OVS_UNUSED
,
2918 ofproto_meter_id
*meter_id OVS_UNUSED
,
2919 struct ofputil_meter_config
*config OVS_UNUSED
)
2921 return EFBIG
; /* meter_id out of range */
2925 dpif_netlink_meter_get(const struct dpif
*dpif OVS_UNUSED
,
2926 ofproto_meter_id meter_id OVS_UNUSED
,
2927 struct ofputil_meter_stats
*stats OVS_UNUSED
,
2928 uint16_t n_bands OVS_UNUSED
)
2930 return EFBIG
; /* meter_id out of range */
2934 dpif_netlink_meter_del(struct dpif
*dpif OVS_UNUSED
,
2935 ofproto_meter_id meter_id OVS_UNUSED
,
2936 struct ofputil_meter_stats
*stats OVS_UNUSED
,
2937 uint16_t n_bands OVS_UNUSED
)
2939 return EFBIG
; /* meter_id out of range */
2943 const struct dpif_class dpif_netlink_class
= {
2946 dpif_netlink_enumerate
,
2950 dpif_netlink_destroy
,
2953 dpif_netlink_get_stats
,
2954 dpif_netlink_port_add
,
2955 dpif_netlink_port_del
,
2956 NULL
, /* port_set_config */
2957 dpif_netlink_port_query_by_number
,
2958 dpif_netlink_port_query_by_name
,
2959 dpif_netlink_port_get_pid
,
2960 dpif_netlink_port_dump_start
,
2961 dpif_netlink_port_dump_next
,
2962 dpif_netlink_port_dump_done
,
2963 dpif_netlink_port_poll
,
2964 dpif_netlink_port_poll_wait
,
2965 dpif_netlink_flow_flush
,
2966 dpif_netlink_flow_dump_create
,
2967 dpif_netlink_flow_dump_destroy
,
2968 dpif_netlink_flow_dump_thread_create
,
2969 dpif_netlink_flow_dump_thread_destroy
,
2970 dpif_netlink_flow_dump_next
,
2971 dpif_netlink_operate
,
2972 dpif_netlink_recv_set
,
2973 dpif_netlink_handlers_set
,
2974 NULL
, /* set_config */
2975 dpif_netlink_queue_to_priority
,
2977 dpif_netlink_recv_wait
,
2978 dpif_netlink_recv_purge
,
2979 NULL
, /* register_dp_purge_cb */
2980 NULL
, /* register_upcall_cb */
2981 NULL
, /* enable_upcall */
2982 NULL
, /* disable_upcall */
2983 dpif_netlink_get_datapath_version
, /* get_datapath_version */
2984 dpif_netlink_ct_dump_start
,
2985 dpif_netlink_ct_dump_next
,
2986 dpif_netlink_ct_dump_done
,
2987 dpif_netlink_ct_flush
,
2988 dpif_netlink_meter_get_features
,
2989 dpif_netlink_meter_set
,
2990 dpif_netlink_meter_get
,
2991 dpif_netlink_meter_del
,
2995 dpif_netlink_init(void)
2997 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3000 if (ovsthread_once_start(&once
)) {
3001 error
= nl_lookup_genl_family(OVS_DATAPATH_FAMILY
,
3002 &ovs_datapath_family
);
3004 VLOG_WARN("Generic Netlink family '%s' does not exist. "
3005 "The Open vSwitch kernel module is probably not loaded.",
3006 OVS_DATAPATH_FAMILY
);
3009 error
= nl_lookup_genl_family(OVS_VPORT_FAMILY
, &ovs_vport_family
);
3012 error
= nl_lookup_genl_family(OVS_FLOW_FAMILY
, &ovs_flow_family
);
3015 error
= nl_lookup_genl_family(OVS_PACKET_FAMILY
,
3016 &ovs_packet_family
);
3019 error
= nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY
, OVS_VPORT_MCGROUP
,
3020 &ovs_vport_mcgroup
);
3023 ovs_tunnels_out_of_tree
= dpif_netlink_rtnl_probe_oot_tunnels();
3025 ovsthread_once_done(&once
);
3032 dpif_netlink_is_internal_device(const char *name
)
3034 struct dpif_netlink_vport reply
;
3038 error
= dpif_netlink_vport_get(name
, &reply
, &buf
);
3041 } else if (error
!= ENODEV
&& error
!= ENOENT
) {
3042 VLOG_WARN_RL(&error_rl
, "%s: vport query failed (%s)",
3043 name
, ovs_strerror(error
));
3046 return reply
.type
== OVS_VPORT_TYPE_INTERNAL
;
3049 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
3050 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
3051 * positive errno value.
3053 * 'vport' will contain pointers into 'buf', so the caller should not free
3054 * 'buf' while 'vport' is still in use. */
3056 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport
*vport
,
3057 const struct ofpbuf
*buf
)
3059 static const struct nl_policy ovs_vport_policy
[] = {
3060 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NL_A_U32
},
3061 [OVS_VPORT_ATTR_TYPE
] = { .type
= NL_A_U32
},
3062 [OVS_VPORT_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
3063 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NL_A_UNSPEC
},
3064 [OVS_VPORT_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_vport_stats
),
3066 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
3069 dpif_netlink_vport_init(vport
);
3071 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
3072 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
3073 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
3074 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
3076 struct nlattr
*a
[ARRAY_SIZE(ovs_vport_policy
)];
3077 if (!nlmsg
|| !genl
|| !ovs_header
3078 || nlmsg
->nlmsg_type
!= ovs_vport_family
3079 || !nl_policy_parse(&b
, 0, ovs_vport_policy
, a
,
3080 ARRAY_SIZE(ovs_vport_policy
))) {
3084 vport
->cmd
= genl
->cmd
;
3085 vport
->dp_ifindex
= ovs_header
->dp_ifindex
;
3086 vport
->port_no
= nl_attr_get_odp_port(a
[OVS_VPORT_ATTR_PORT_NO
]);
3087 vport
->type
= nl_attr_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
3088 vport
->name
= nl_attr_get_string(a
[OVS_VPORT_ATTR_NAME
]);
3089 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
3090 vport
->n_upcall_pids
= nl_attr_get_size(a
[OVS_VPORT_ATTR_UPCALL_PID
])
3091 / (sizeof *vport
->upcall_pids
);
3092 vport
->upcall_pids
= nl_attr_get(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
3095 if (a
[OVS_VPORT_ATTR_STATS
]) {
3096 vport
->stats
= nl_attr_get(a
[OVS_VPORT_ATTR_STATS
]);
3098 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
3099 vport
->options
= nl_attr_get(a
[OVS_VPORT_ATTR_OPTIONS
]);
3100 vport
->options_len
= nl_attr_get_size(a
[OVS_VPORT_ATTR_OPTIONS
]);
3105 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
3106 * followed by Netlink attributes corresponding to 'vport'. */
3108 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport
*vport
,
3111 struct ovs_header
*ovs_header
;
3113 nl_msg_put_genlmsghdr(buf
, 0, ovs_vport_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
3114 vport
->cmd
, OVS_VPORT_VERSION
);
3116 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
3117 ovs_header
->dp_ifindex
= vport
->dp_ifindex
;
3119 if (vport
->port_no
!= ODPP_NONE
) {
3120 nl_msg_put_odp_port(buf
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
);
3123 if (vport
->type
!= OVS_VPORT_TYPE_UNSPEC
) {
3124 nl_msg_put_u32(buf
, OVS_VPORT_ATTR_TYPE
, vport
->type
);
3128 nl_msg_put_string(buf
, OVS_VPORT_ATTR_NAME
, vport
->name
);
3131 if (vport
->upcall_pids
) {
3132 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_UPCALL_PID
,
3134 vport
->n_upcall_pids
* sizeof *vport
->upcall_pids
);
3138 nl_msg_put_unspec(buf
, OVS_VPORT_ATTR_STATS
,
3139 vport
->stats
, sizeof *vport
->stats
);
3142 if (vport
->options
) {
3143 nl_msg_put_nested(buf
, OVS_VPORT_ATTR_OPTIONS
,
3144 vport
->options
, vport
->options_len
);
3148 /* Clears 'vport' to "empty" values. */
3150 dpif_netlink_vport_init(struct dpif_netlink_vport
*vport
)
3152 memset(vport
, 0, sizeof *vport
);
3153 vport
->port_no
= ODPP_NONE
;
3156 /* Executes 'request' in the kernel datapath. If the command fails, returns a
3157 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
3158 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
3159 * result of the command is expected to be an ovs_vport also, which is decoded
3160 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
3161 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
3163 dpif_netlink_vport_transact(const struct dpif_netlink_vport
*request
,
3164 struct dpif_netlink_vport
*reply
,
3165 struct ofpbuf
**bufp
)
3167 struct ofpbuf
*request_buf
;
3170 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
3172 error
= dpif_netlink_init();
3176 dpif_netlink_vport_init(reply
);
3181 request_buf
= ofpbuf_new(1024);
3182 dpif_netlink_vport_to_ofpbuf(request
, request_buf
);
3183 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
3184 ofpbuf_delete(request_buf
);
3188 error
= dpif_netlink_vport_from_ofpbuf(reply
, *bufp
);
3191 dpif_netlink_vport_init(reply
);
3192 ofpbuf_delete(*bufp
);
3199 /* Obtains information about the kernel vport named 'name' and stores it into
3200 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
3201 * longer needed ('reply' will contain pointers into '*bufp'). */
3203 dpif_netlink_vport_get(const char *name
, struct dpif_netlink_vport
*reply
,
3204 struct ofpbuf
**bufp
)
3206 struct dpif_netlink_vport request
;
3208 dpif_netlink_vport_init(&request
);
3209 request
.cmd
= OVS_VPORT_CMD_GET
;
3210 request
.name
= name
;
3212 return dpif_netlink_vport_transact(&request
, reply
, bufp
);
3215 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
3216 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
3217 * positive errno value.
3219 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
3220 * while 'dp' is still in use. */
3222 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp
*dp
, const struct ofpbuf
*buf
)
3224 static const struct nl_policy ovs_datapath_policy
[] = {
3225 [OVS_DP_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
3226 [OVS_DP_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_dp_stats
),
3228 [OVS_DP_ATTR_MEGAFLOW_STATS
] = {
3229 NL_POLICY_FOR(struct ovs_dp_megaflow_stats
),
3233 dpif_netlink_dp_init(dp
);
3235 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
3236 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
3237 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
3238 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
3240 struct nlattr
*a
[ARRAY_SIZE(ovs_datapath_policy
)];
3241 if (!nlmsg
|| !genl
|| !ovs_header
3242 || nlmsg
->nlmsg_type
!= ovs_datapath_family
3243 || !nl_policy_parse(&b
, 0, ovs_datapath_policy
, a
,
3244 ARRAY_SIZE(ovs_datapath_policy
))) {
3248 dp
->cmd
= genl
->cmd
;
3249 dp
->dp_ifindex
= ovs_header
->dp_ifindex
;
3250 dp
->name
= nl_attr_get_string(a
[OVS_DP_ATTR_NAME
]);
3251 if (a
[OVS_DP_ATTR_STATS
]) {
3252 dp
->stats
= nl_attr_get(a
[OVS_DP_ATTR_STATS
]);
3255 if (a
[OVS_DP_ATTR_MEGAFLOW_STATS
]) {
3256 dp
->megaflow_stats
= nl_attr_get(a
[OVS_DP_ATTR_MEGAFLOW_STATS
]);
3262 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
3264 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp
*dp
, struct ofpbuf
*buf
)
3266 struct ovs_header
*ovs_header
;
3268 nl_msg_put_genlmsghdr(buf
, 0, ovs_datapath_family
,
3269 NLM_F_REQUEST
| NLM_F_ECHO
, dp
->cmd
,
3270 OVS_DATAPATH_VERSION
);
3272 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
3273 ovs_header
->dp_ifindex
= dp
->dp_ifindex
;
3276 nl_msg_put_string(buf
, OVS_DP_ATTR_NAME
, dp
->name
);
3279 if (dp
->upcall_pid
) {
3280 nl_msg_put_u32(buf
, OVS_DP_ATTR_UPCALL_PID
, *dp
->upcall_pid
);
3283 if (dp
->user_features
) {
3284 nl_msg_put_u32(buf
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
);
3287 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
3290 /* Clears 'dp' to "empty" values. */
3292 dpif_netlink_dp_init(struct dpif_netlink_dp
*dp
)
3294 memset(dp
, 0, sizeof *dp
);
3298 dpif_netlink_dp_dump_start(struct nl_dump
*dump
)
3300 struct dpif_netlink_dp request
;
3303 dpif_netlink_dp_init(&request
);
3304 request
.cmd
= OVS_DP_CMD_GET
;
3306 buf
= ofpbuf_new(1024);
3307 dpif_netlink_dp_to_ofpbuf(&request
, buf
);
3308 nl_dump_start(dump
, NETLINK_GENERIC
, buf
);
3312 /* Executes 'request' in the kernel datapath. If the command fails, returns a
3313 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
3314 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
3315 * result of the command is expected to be of the same form, which is decoded
3316 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
3317 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
3319 dpif_netlink_dp_transact(const struct dpif_netlink_dp
*request
,
3320 struct dpif_netlink_dp
*reply
, struct ofpbuf
**bufp
)
3322 struct ofpbuf
*request_buf
;
3325 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
3327 request_buf
= ofpbuf_new(1024);
3328 dpif_netlink_dp_to_ofpbuf(request
, request_buf
);
3329 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
3330 ofpbuf_delete(request_buf
);
3333 dpif_netlink_dp_init(reply
);
3335 error
= dpif_netlink_dp_from_ofpbuf(reply
, *bufp
);
3338 ofpbuf_delete(*bufp
);
3345 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
3346 * The caller must free '*bufp' when the reply is no longer needed ('reply'
3347 * will contain pointers into '*bufp'). */
3349 dpif_netlink_dp_get(const struct dpif
*dpif_
, struct dpif_netlink_dp
*reply
,
3350 struct ofpbuf
**bufp
)
3352 struct dpif_netlink
*dpif
= dpif_netlink_cast(dpif_
);
3353 struct dpif_netlink_dp request
;
3355 dpif_netlink_dp_init(&request
);
3356 request
.cmd
= OVS_DP_CMD_GET
;
3357 request
.dp_ifindex
= dpif
->dp_ifindex
;
3359 return dpif_netlink_dp_transact(&request
, reply
, bufp
);
3362 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
3363 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
3364 * positive errno value.
3366 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
3367 * while 'flow' is still in use. */
3369 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow
*flow
,
3370 const struct ofpbuf
*buf
)
3372 static const struct nl_policy ovs_flow_policy
[__OVS_FLOW_ATTR_MAX
] = {
3373 [OVS_FLOW_ATTR_KEY
] = { .type
= NL_A_NESTED
, .optional
= true },
3374 [OVS_FLOW_ATTR_MASK
] = { .type
= NL_A_NESTED
, .optional
= true },
3375 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
3376 [OVS_FLOW_ATTR_STATS
] = { NL_POLICY_FOR(struct ovs_flow_stats
),
3378 [OVS_FLOW_ATTR_TCP_FLAGS
] = { .type
= NL_A_U8
, .optional
= true },
3379 [OVS_FLOW_ATTR_USED
] = { .type
= NL_A_U64
, .optional
= true },
3380 [OVS_FLOW_ATTR_UFID
] = { .type
= NL_A_U128
, .optional
= true },
3381 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
3382 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
3383 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
3386 dpif_netlink_flow_init(flow
);
3388 struct ofpbuf b
= ofpbuf_const_initializer(buf
->data
, buf
->size
);
3389 struct nlmsghdr
*nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
3390 struct genlmsghdr
*genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
3391 struct ovs_header
*ovs_header
= ofpbuf_try_pull(&b
, sizeof *ovs_header
);
3393 struct nlattr
*a
[ARRAY_SIZE(ovs_flow_policy
)];
3394 if (!nlmsg
|| !genl
|| !ovs_header
3395 || nlmsg
->nlmsg_type
!= ovs_flow_family
3396 || !nl_policy_parse(&b
, 0, ovs_flow_policy
, a
,
3397 ARRAY_SIZE(ovs_flow_policy
))) {
3400 if (!a
[OVS_FLOW_ATTR_KEY
] && !a
[OVS_FLOW_ATTR_UFID
]) {
3404 flow
->nlmsg_flags
= nlmsg
->nlmsg_flags
;
3405 flow
->dp_ifindex
= ovs_header
->dp_ifindex
;
3406 if (a
[OVS_FLOW_ATTR_KEY
]) {
3407 flow
->key
= nl_attr_get(a
[OVS_FLOW_ATTR_KEY
]);
3408 flow
->key_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_KEY
]);
3411 if (a
[OVS_FLOW_ATTR_UFID
]) {
3412 flow
->ufid
= nl_attr_get_u128(a
[OVS_FLOW_ATTR_UFID
]);
3413 flow
->ufid_present
= true;
3415 if (a
[OVS_FLOW_ATTR_MASK
]) {
3416 flow
->mask
= nl_attr_get(a
[OVS_FLOW_ATTR_MASK
]);
3417 flow
->mask_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_MASK
]);
3419 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
3420 flow
->actions
= nl_attr_get(a
[OVS_FLOW_ATTR_ACTIONS
]);
3421 flow
->actions_len
= nl_attr_get_size(a
[OVS_FLOW_ATTR_ACTIONS
]);
3423 if (a
[OVS_FLOW_ATTR_STATS
]) {
3424 flow
->stats
= nl_attr_get(a
[OVS_FLOW_ATTR_STATS
]);
3426 if (a
[OVS_FLOW_ATTR_TCP_FLAGS
]) {
3427 flow
->tcp_flags
= nl_attr_get(a
[OVS_FLOW_ATTR_TCP_FLAGS
]);
3429 if (a
[OVS_FLOW_ATTR_USED
]) {
3430 flow
->used
= nl_attr_get(a
[OVS_FLOW_ATTR_USED
]);
3437 * If PACKET_TYPE attribute is present in 'data', it filters PACKET_TYPE out,
3438 * then puts 'data' to 'buf'.
3441 put_exclude_packet_type(struct ofpbuf
*buf
, uint16_t type
,
3442 const struct nlattr
*data
, uint16_t data_len
)
3444 const struct nlattr
*packet_type
;
3446 packet_type
= nl_attr_find__(data
, data_len
, OVS_KEY_ATTR_PACKET_TYPE
);
3449 /* exclude PACKET_TYPE Netlink attribute. */
3450 ovs_assert(NLA_ALIGN(packet_type
->nla_len
) == NL_A_U32_SIZE
);
3451 size_t packet_type_len
= NL_A_U32_SIZE
;
3452 size_t first_chunk_size
= (uint8_t *)packet_type
- (uint8_t *)data
;
3453 size_t second_chunk_size
= data_len
- first_chunk_size
3455 uint8_t *first_attr
= NULL
;
3456 struct nlattr
*next_attr
= nl_attr_next(packet_type
);
3458 first_attr
= nl_msg_put_unspec_uninit(buf
, type
,
3459 data_len
- packet_type_len
);
3460 memcpy(first_attr
, data
, first_chunk_size
);
3461 memcpy(first_attr
+ first_chunk_size
, next_attr
, second_chunk_size
);
3463 nl_msg_put_unspec(buf
, type
, data
, data_len
);
3467 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
3468 * followed by Netlink attributes corresponding to 'flow'. */
3470 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow
*flow
,
3473 struct ovs_header
*ovs_header
;
3475 nl_msg_put_genlmsghdr(buf
, 0, ovs_flow_family
,
3476 NLM_F_REQUEST
| flow
->nlmsg_flags
,
3477 flow
->cmd
, OVS_FLOW_VERSION
);
3479 ovs_header
= ofpbuf_put_uninit(buf
, sizeof *ovs_header
);
3480 ovs_header
->dp_ifindex
= flow
->dp_ifindex
;
3482 if (flow
->ufid_present
) {
3483 nl_msg_put_u128(buf
, OVS_FLOW_ATTR_UFID
, flow
->ufid
);
3485 if (flow
->ufid_terse
) {
3486 nl_msg_put_u32(buf
, OVS_FLOW_ATTR_UFID_FLAGS
,
3487 OVS_UFID_F_OMIT_KEY
| OVS_UFID_F_OMIT_MASK
3488 | OVS_UFID_F_OMIT_ACTIONS
);
3490 if (!flow
->ufid_terse
|| !flow
->ufid_present
) {
3491 if (flow
->key_len
) {
3492 put_exclude_packet_type(buf
, OVS_FLOW_ATTR_KEY
, flow
->key
,
3495 if (flow
->mask_len
) {
3496 put_exclude_packet_type(buf
, OVS_FLOW_ATTR_MASK
, flow
->mask
,
3499 if (flow
->actions
|| flow
->actions_len
) {
3500 nl_msg_put_unspec(buf
, OVS_FLOW_ATTR_ACTIONS
,
3501 flow
->actions
, flow
->actions_len
);
3505 /* We never need to send these to the kernel. */
3506 ovs_assert(!flow
->stats
);
3507 ovs_assert(!flow
->tcp_flags
);
3508 ovs_assert(!flow
->used
);
3511 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_CLEAR
);
3514 nl_msg_put_flag(buf
, OVS_FLOW_ATTR_PROBE
);
3518 /* Clears 'flow' to "empty" values. */
3520 dpif_netlink_flow_init(struct dpif_netlink_flow
*flow
)
3522 memset(flow
, 0, sizeof *flow
);
3525 /* Executes 'request' in the kernel datapath. If the command fails, returns a
3526 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
3527 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
3528 * result of the command is expected to be a flow also, which is decoded and
3529 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
3530 * is no longer needed ('reply' will contain pointers into '*bufp'). */
3532 dpif_netlink_flow_transact(struct dpif_netlink_flow
*request
,
3533 struct dpif_netlink_flow
*reply
,
3534 struct ofpbuf
**bufp
)
3536 struct ofpbuf
*request_buf
;
3539 ovs_assert((reply
!= NULL
) == (bufp
!= NULL
));
3542 request
->nlmsg_flags
|= NLM_F_ECHO
;
3545 request_buf
= ofpbuf_new(1024);
3546 dpif_netlink_flow_to_ofpbuf(request
, request_buf
);
3547 error
= nl_transact(NETLINK_GENERIC
, request_buf
, bufp
);
3548 ofpbuf_delete(request_buf
);
3552 error
= dpif_netlink_flow_from_ofpbuf(reply
, *bufp
);
3555 dpif_netlink_flow_init(reply
);
3556 ofpbuf_delete(*bufp
);
3564 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow
*flow
,
3565 struct dpif_flow_stats
*stats
)
3568 stats
->n_packets
= get_32aligned_u64(&flow
->stats
->n_packets
);
3569 stats
->n_bytes
= get_32aligned_u64(&flow
->stats
->n_bytes
);
3571 stats
->n_packets
= 0;
3574 stats
->used
= flow
->used
? get_32aligned_u64(flow
->used
) : 0;
3575 stats
->tcp_flags
= flow
->tcp_flags
? *flow
->tcp_flags
: 0;
3578 /* Logs information about a packet that was recently lost in 'ch' (in
3581 report_loss(struct dpif_netlink
*dpif
, struct dpif_channel
*ch
, uint32_t ch_idx
,
3582 uint32_t handler_id
)
3584 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
3587 if (VLOG_DROP_WARN(&rl
)) {
3592 if (ch
->last_poll
!= LLONG_MIN
) {
3593 ds_put_format(&s
, " (last polled %lld ms ago)",
3594 time_msec() - ch
->last_poll
);
3597 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
3598 dpif_name(&dpif
->dpif
), ch_idx
, handler_id
);