2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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>
28 #include <linux/if_tunnel.h>
29 #include <linux/pkt_sched.h>
30 #include <linux/rtnetlink.h>
34 #include <sys/epoll.h>
39 #include "dpif-provider.h"
40 #include "openvswitch/dynamic-string.h"
42 #include "fat-rwlock.h"
44 #include "netdev-linux.h"
45 #include "netdev-vport.h"
46 #include "netlink-conntrack.h"
47 #include "netlink-notifier.h"
48 #include "netlink-socket.h"
51 #include "openvswitch/ofpbuf.h"
53 #include "poll-loop.h"
58 #include "unaligned.h"
60 #include "openvswitch/vlog.h"
62 VLOG_DEFINE_THIS_MODULE(dpif_netlink);
68 enum { MAX_PORTS = USHRT_MAX };
70 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
71 * missing if we have old headers. */
72 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
74 struct dpif_netlink_dp {
75 /* Generic Netlink header. */
78 /* struct ovs_header. */
82 const char *name; /* OVS_DP_ATTR_NAME. */
83 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
84 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
85 const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
86 const struct ovs_dp_megaflow_stats *megaflow_stats;
87 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
90 static void dpif_netlink_dp_init(struct dpif_netlink_dp *);
91 static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *,
92 const struct ofpbuf *);
93 static void dpif_netlink_dp_dump_start(struct nl_dump *);
94 static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
95 struct dpif_netlink_dp *reply,
96 struct ofpbuf **bufp);
97 static int dpif_netlink_dp_get(const struct dpif *,
98 struct dpif_netlink_dp *reply,
99 struct ofpbuf **bufp);
101 struct dpif_netlink_flow {
102 /* Generic Netlink header. */
105 /* struct ovs_header. */
106 unsigned int nlmsg_flags;
111 * The 'stats' member points to 64-bit data that might only be aligned on
112 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
115 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
116 * the Netlink version of the command, even if actions_len is zero. */
117 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
119 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
121 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
123 ovs_u128 ufid; /* OVS_FLOW_ATTR_FLOW_ID. */
124 bool ufid_present; /* Is there a UFID? */
125 bool ufid_terse; /* Skip serializing key/mask/acts? */
126 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
127 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
128 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
129 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
130 bool probe; /* OVS_FLOW_ATTR_PROBE. */
133 static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
134 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
135 const struct ofpbuf *);
136 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
138 static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
139 struct dpif_netlink_flow *reply,
140 struct ofpbuf **bufp);
141 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
142 struct dpif_flow_stats *);
143 static void dpif_netlink_flow_to_dpif_flow(struct dpif *, struct dpif_flow *,
144 const struct dpif_netlink_flow *);
146 /* One of the dpif channels between the kernel and userspace. */
147 struct dpif_channel {
148 struct nl_sock *sock; /* Netlink socket. */
149 long long int last_poll; /* Last time this channel was polled. */
153 #define VPORT_SOCK_POOL_SIZE 1
154 /* On Windows, there is no native support for epoll. There are equivalent
155 * interfaces though, that are not used currently. For simpicity, a pool of
156 * netlink sockets is used. Each socket is represented by 'struct
157 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
158 * sharing the same socket. In the future, we can add a reference count and
160 struct dpif_windows_vport_sock {
161 struct nl_sock *nl_sock; /* netlink socket. */
165 struct dpif_handler {
166 struct dpif_channel *channels;/* Array of channels for each handler. */
167 struct epoll_event *epoll_events;
168 int epoll_fd; /* epoll fd that includes channel socks. */
169 int n_events; /* Num events returned by epoll_wait(). */
170 int event_offset; /* Offset into 'epoll_events'. */
173 /* Pool of sockets. */
174 struct dpif_windows_vport_sock *vport_sock_pool;
175 size_t last_used_pool_idx; /* Index to aid in allocating a
176 socket in the pool to a port. */
180 /* Datapath interface for the openvswitch Linux kernel module. */
181 struct dpif_netlink {
185 /* Upcall messages. */
186 struct fat_rwlock upcall_lock;
187 struct dpif_handler *handlers;
188 uint32_t n_handlers; /* Num of upcall handlers. */
189 int uc_array_size; /* Size of 'handler->channels' and */
190 /* 'handler->epoll_events'. */
192 /* Change notification. */
193 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
194 bool refresh_channels;
197 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
198 uint32_t ch_idx, uint32_t handler_id);
200 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
202 /* Generic Netlink family numbers for OVS.
204 * Initialized by dpif_netlink_init(). */
205 static int ovs_datapath_family;
206 static int ovs_vport_family;
207 static int ovs_flow_family;
208 static int ovs_packet_family;
210 /* Generic Netlink multicast groups for OVS.
212 * Initialized by dpif_netlink_init(). */
213 static unsigned int ovs_vport_mcgroup;
215 static int dpif_netlink_init(void);
216 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
217 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
218 odp_port_t port_no, uint32_t hash);
219 static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
220 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
221 uint32_t n_handlers);
222 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
224 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
225 const struct ofpbuf *);
227 static struct dpif_netlink *
228 dpif_netlink_cast(const struct dpif *dpif)
230 dpif_assert_class(dpif, &dpif_netlink_class);
231 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
235 dpif_netlink_enumerate(struct sset *all_dps,
236 const struct dpif_class *dpif_class OVS_UNUSED)
239 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
240 struct ofpbuf msg, buf;
243 error = dpif_netlink_init();
248 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
249 dpif_netlink_dp_dump_start(&dump);
250 while (nl_dump_next(&dump, &msg, &buf)) {
251 struct dpif_netlink_dp dp;
253 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
254 sset_add(all_dps, dp.name);
258 return nl_dump_done(&dump);
262 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
263 bool create, struct dpif **dpifp)
265 struct dpif_netlink_dp dp_request, dp;
270 error = dpif_netlink_init();
275 /* Create or look up datapath. */
276 dpif_netlink_dp_init(&dp_request);
278 dp_request.cmd = OVS_DP_CMD_NEW;
280 dp_request.upcall_pid = &upcall_pid;
282 /* Use OVS_DP_CMD_SET to report user features */
283 dp_request.cmd = OVS_DP_CMD_SET;
285 dp_request.name = name;
286 dp_request.user_features |= OVS_DP_F_UNALIGNED;
287 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
288 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
293 error = open_dpif(&dp, dpifp);
299 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
301 struct dpif_netlink *dpif;
303 dpif = xzalloc(sizeof *dpif);
304 dpif->port_notifier = NULL;
305 fat_rwlock_init(&dpif->upcall_lock);
307 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
308 dp->dp_ifindex, dp->dp_ifindex);
310 dpif->dp_ifindex = dp->dp_ifindex;
311 *dpifp = &dpif->dpif;
316 /* Destroys the netlink sockets pointed by the elements in 'socksp'
317 * and frees the 'socksp'. */
319 vport_del_socksp__(struct nl_sock **socksp, uint32_t n_socks)
323 for (i = 0; i < n_socks; i++) {
324 nl_sock_destroy(socksp[i]);
330 /* Creates an array of netlink sockets. Returns an array of the
331 * corresponding pointers. Records the error in 'error'. */
332 static struct nl_sock **
333 vport_create_socksp__(uint32_t n_socks, int *error)
335 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
338 for (i = 0; i < n_socks; i++) {
339 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
348 vport_del_socksp__(socksp, n_socks);
355 vport_delete_sock_pool(struct dpif_handler *handler)
356 OVS_REQ_WRLOCK(dpif->upcall_lock)
358 if (handler->vport_sock_pool) {
360 struct dpif_windows_vport_sock *sock_pool =
361 handler->vport_sock_pool;
363 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
364 if (sock_pool[i].nl_sock) {
365 nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
366 nl_sock_destroy(sock_pool[i].nl_sock);
367 sock_pool[i].nl_sock = NULL;
371 free(handler->vport_sock_pool);
372 handler->vport_sock_pool = NULL;
377 vport_create_sock_pool(struct dpif_handler *handler)
378 OVS_REQ_WRLOCK(dpif->upcall_lock)
380 struct dpif_windows_vport_sock *sock_pool;
384 sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
385 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
386 error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
391 /* Enable the netlink socket to receive packets. This is equivalent to
392 * calling nl_sock_join_mcgroup() to receive events. */
393 error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
399 handler->vport_sock_pool = sock_pool;
400 handler->last_used_pool_idx = 0;
404 vport_delete_sock_pool(handler);
408 /* Returns an array pointers to netlink sockets. The sockets are picked from a
409 * pool. Records the error in 'error'. */
410 static struct nl_sock **
411 vport_create_socksp_windows(struct dpif_netlink *dpif, int *error)
412 OVS_REQ_WRLOCK(dpif->upcall_lock)
414 uint32_t n_socks = dpif->n_handlers;
415 struct nl_sock **socksp;
418 ovs_assert(n_socks <= 1);
419 socksp = xzalloc(n_socks * sizeof *socksp);
421 /* Pick netlink sockets to use in a round-robin fashion from each
422 * handler's pool of sockets. */
423 for (i = 0; i < n_socks; i++) {
424 struct dpif_handler *handler = &dpif->handlers[i];
425 struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
426 size_t index = handler->last_used_pool_idx;
428 /* A pool of sockets is allocated when the handler is initialized. */
429 if (sock_pool == NULL) {
435 ovs_assert(index < VPORT_SOCK_POOL_SIZE);
436 socksp[i] = sock_pool[index].nl_sock;
437 socksp[i] = sock_pool[index].nl_sock;
438 ovs_assert(socksp[i]);
439 index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
440 handler->last_used_pool_idx = index;
447 vport_del_socksp_windows(struct dpif_netlink *dpif, struct nl_sock **socksp)
453 static struct nl_sock **
454 vport_create_socksp(struct dpif_netlink *dpif, int *error)
457 return vport_create_socksp_windows(dpif, error);
459 return vport_create_socksp__(dpif->n_handlers, error);
464 vport_del_socksp(struct dpif_netlink *dpif, struct nl_sock **socksp)
467 vport_del_socksp_windows(dpif, socksp);
469 vport_del_socksp__(socksp, dpif->n_handlers);
473 /* Given the array of pointers to netlink sockets 'socksp', returns
474 * the array of corresponding pids. If the 'socksp' is NULL, returns
475 * a single-element array of value 0. */
477 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
482 pids = xzalloc(sizeof *pids);
486 pids = xzalloc(n_socks * sizeof *pids);
487 for (i = 0; i < n_socks; i++) {
488 pids[i] = nl_sock_pid(socksp[i]);
495 /* Given the port number 'port_idx', extracts the pids of netlink sockets
496 * associated to the port and assigns it to 'upcall_pids'. */
498 vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
499 uint32_t **upcall_pids)
504 /* Since the nl_sock can only be assigned in either all
505 * or none "dpif->handlers" channels, the following check
507 if (!dpif->handlers[0].channels[port_idx].sock) {
510 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
512 pids = xzalloc(dpif->n_handlers * sizeof *pids);
514 for (i = 0; i < dpif->n_handlers; i++) {
515 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
524 vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
525 struct nl_sock **socksp)
527 struct epoll_event event;
528 uint32_t port_idx = odp_to_u32(port_no);
532 if (dpif->handlers == NULL) {
536 /* We assume that the datapath densely chooses port numbers, which can
537 * therefore be used as an index into 'channels' and 'epoll_events' of
538 * 'dpif->handler'. */
539 if (port_idx >= dpif->uc_array_size) {
540 uint32_t new_size = port_idx + 1;
542 if (new_size > MAX_PORTS) {
543 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
544 dpif_name(&dpif->dpif), port_no);
548 for (i = 0; i < dpif->n_handlers; i++) {
549 struct dpif_handler *handler = &dpif->handlers[i];
551 handler->channels = xrealloc(handler->channels,
552 new_size * sizeof *handler->channels);
554 for (j = dpif->uc_array_size; j < new_size; j++) {
555 handler->channels[j].sock = NULL;
558 handler->epoll_events = xrealloc(handler->epoll_events,
559 new_size * sizeof *handler->epoll_events);
562 dpif->uc_array_size = new_size;
565 memset(&event, 0, sizeof event);
566 event.events = EPOLLIN;
567 event.data.u32 = port_idx;
569 for (i = 0; i < dpif->n_handlers; i++) {
570 struct dpif_handler *handler = &dpif->handlers[i];
573 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
579 dpif->handlers[i].channels[port_idx].sock = socksp[i];
580 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
586 for (j = 0; j < i; j++) {
588 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
589 nl_sock_fd(socksp[j]), NULL);
591 dpif->handlers[j].channels[port_idx].sock = NULL;
598 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
600 uint32_t port_idx = odp_to_u32(port_no);
603 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
607 /* Since the sock can only be assigned in either all or none
608 * of "dpif->handlers" channels, the following check would
610 if (!dpif->handlers[0].channels[port_idx].sock) {
614 for (i = 0; i < dpif->n_handlers; i++) {
615 struct dpif_handler *handler = &dpif->handlers[i];
617 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
618 nl_sock_fd(handler->channels[port_idx].sock), NULL);
619 nl_sock_destroy(handler->channels[port_idx].sock);
621 handler->channels[port_idx].sock = NULL;
622 handler->event_offset = handler->n_events = 0;
627 destroy_all_channels(struct dpif_netlink *dpif)
628 OVS_REQ_WRLOCK(dpif->upcall_lock)
632 if (!dpif->handlers) {
636 for (i = 0; i < dpif->uc_array_size; i++ ) {
637 struct dpif_netlink_vport vport_request;
638 uint32_t upcall_pids = 0;
640 /* Since the sock can only be assigned in either all or none
641 * of "dpif->handlers" channels, the following check would
643 if (!dpif->handlers[0].channels[i].sock) {
647 /* Turn off upcalls. */
648 dpif_netlink_vport_init(&vport_request);
649 vport_request.cmd = OVS_VPORT_CMD_SET;
650 vport_request.dp_ifindex = dpif->dp_ifindex;
651 vport_request.port_no = u32_to_odp(i);
652 vport_request.n_upcall_pids = 1;
653 vport_request.upcall_pids = &upcall_pids;
654 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
656 vport_del_channels(dpif, u32_to_odp(i));
659 for (i = 0; i < dpif->n_handlers; i++) {
660 struct dpif_handler *handler = &dpif->handlers[i];
662 dpif_netlink_handler_uninit(handler);
663 free(handler->epoll_events);
664 free(handler->channels);
667 free(dpif->handlers);
668 dpif->handlers = NULL;
669 dpif->n_handlers = 0;
670 dpif->uc_array_size = 0;
674 dpif_netlink_close(struct dpif *dpif_)
676 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
678 nl_sock_destroy(dpif->port_notifier);
680 fat_rwlock_wrlock(&dpif->upcall_lock);
681 destroy_all_channels(dpif);
682 fat_rwlock_unlock(&dpif->upcall_lock);
684 fat_rwlock_destroy(&dpif->upcall_lock);
689 dpif_netlink_destroy(struct dpif *dpif_)
691 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
692 struct dpif_netlink_dp dp;
694 dpif_netlink_dp_init(&dp);
695 dp.cmd = OVS_DP_CMD_DEL;
696 dp.dp_ifindex = dpif->dp_ifindex;
697 return dpif_netlink_dp_transact(&dp, NULL, NULL);
701 dpif_netlink_run(struct dpif *dpif_)
703 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
705 if (dpif->refresh_channels) {
706 dpif->refresh_channels = false;
707 fat_rwlock_wrlock(&dpif->upcall_lock);
708 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
709 fat_rwlock_unlock(&dpif->upcall_lock);
715 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
717 struct dpif_netlink_dp dp;
721 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
723 memset(stats, 0, sizeof *stats);
726 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
727 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
728 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
729 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
732 if (dp.megaflow_stats) {
733 stats->n_masks = dp.megaflow_stats->n_masks;
734 stats->n_mask_hit = get_32aligned_u64(
735 &dp.megaflow_stats->n_mask_hit);
737 stats->n_masks = UINT32_MAX;
738 stats->n_mask_hit = UINT64_MAX;
746 get_vport_type(const struct dpif_netlink_vport *vport)
748 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
750 switch (vport->type) {
751 case OVS_VPORT_TYPE_NETDEV: {
752 const char *type = netdev_get_type_from_name(vport->name);
754 return type ? type : "system";
757 case OVS_VPORT_TYPE_INTERNAL:
760 case OVS_VPORT_TYPE_GENEVE:
763 case OVS_VPORT_TYPE_GRE:
766 case OVS_VPORT_TYPE_VXLAN:
769 case OVS_VPORT_TYPE_LISP:
772 case OVS_VPORT_TYPE_STT:
775 case OVS_VPORT_TYPE_UNSPEC:
776 case __OVS_VPORT_TYPE_MAX:
780 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
781 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
785 static enum ovs_vport_type
786 netdev_to_ovs_vport_type(const char *type)
788 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
789 return OVS_VPORT_TYPE_NETDEV;
790 } else if (!strcmp(type, "internal")) {
791 return OVS_VPORT_TYPE_INTERNAL;
792 } else if (strstr(type, "stt")) {
793 return OVS_VPORT_TYPE_STT;
794 } else if (!strcmp(type, "geneve")) {
795 return OVS_VPORT_TYPE_GENEVE;
796 } else if (strstr(type, "gre")) {
797 return OVS_VPORT_TYPE_GRE;
798 } else if (!strcmp(type, "vxlan")) {
799 return OVS_VPORT_TYPE_VXLAN;
800 } else if (!strcmp(type, "lisp")) {
801 return OVS_VPORT_TYPE_LISP;
803 return OVS_VPORT_TYPE_UNSPEC;
808 dpif_netlink_port_add__(struct dpif_netlink *dpif, const char *name,
809 enum ovs_vport_type type,
810 struct ofpbuf *options,
811 odp_port_t *port_nop)
812 OVS_REQ_WRLOCK(dpif->upcall_lock)
814 struct dpif_netlink_vport request, reply;
816 struct nl_sock **socksp = NULL;
817 uint32_t *upcall_pids;
820 if (dpif->handlers) {
821 socksp = vport_create_socksp(dpif, &error);
827 dpif_netlink_vport_init(&request);
828 request.cmd = OVS_VPORT_CMD_NEW;
829 request.dp_ifindex = dpif->dp_ifindex;
833 request.port_no = *port_nop;
834 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
835 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
836 request.upcall_pids = upcall_pids;
839 request.options = options->data;
840 request.options_len = options->size;
843 error = dpif_netlink_vport_transact(&request, &reply, &buf);
845 *port_nop = reply.port_no;
847 if (error == EBUSY && *port_nop != ODPP_NONE) {
848 VLOG_INFO("%s: requested port %"PRIu32" is in use",
849 dpif_name(&dpif->dpif), *port_nop);
852 vport_del_socksp(dpif, socksp);
857 error = vport_add_channels(dpif, *port_nop, socksp);
859 VLOG_INFO("%s: could not add channel for port %s",
860 dpif_name(&dpif->dpif), name);
862 /* Delete the port. */
863 dpif_netlink_vport_init(&request);
864 request.cmd = OVS_VPORT_CMD_DEL;
865 request.dp_ifindex = dpif->dp_ifindex;
866 request.port_no = *port_nop;
867 dpif_netlink_vport_transact(&request, NULL, NULL);
868 vport_del_socksp(dpif, socksp);
882 dpif_netlink_port_add_compat(struct dpif_netlink *dpif, struct netdev *netdev,
883 odp_port_t *port_nop)
884 OVS_REQ_WRLOCK(dpif->upcall_lock)
886 const struct netdev_tunnel_config *tnl_cfg;
887 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
888 const char *name = netdev_vport_get_dpif_port(netdev,
889 namebuf, sizeof namebuf);
890 const char *type = netdev_get_type(netdev);
891 uint64_t options_stub[64 / 8];
892 struct ofpbuf options;
893 enum ovs_vport_type ovs_type;
895 ovs_type = netdev_to_ovs_vport_type(netdev_get_type(netdev));
896 if (ovs_type == OVS_VPORT_TYPE_UNSPEC) {
897 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
898 "unsupported type `%s'",
899 dpif_name(&dpif->dpif), name, type);
903 if (ovs_type == OVS_VPORT_TYPE_NETDEV) {
905 /* XXX : Map appropiate Windows handle */
907 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
911 tnl_cfg = netdev_get_tunnel_config(netdev);
912 if (tnl_cfg && (tnl_cfg->dst_port != 0 || tnl_cfg->exts)) {
913 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
914 if (tnl_cfg->dst_port) {
915 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
916 ntohs(tnl_cfg->dst_port));
922 ext_ofs = nl_msg_start_nested(&options, OVS_TUNNEL_ATTR_EXTENSION);
923 for (i = 0; i < 32; i++) {
924 if (tnl_cfg->exts & (1 << i)) {
925 nl_msg_put_flag(&options, i);
928 nl_msg_end_nested(&options, ext_ofs);
930 return dpif_netlink_port_add__(dpif, name, ovs_type, &options, port_nop);
932 return dpif_netlink_port_add__(dpif, name, ovs_type, NULL, port_nop);
940 netdev_linux_destroy(const char *name)
943 struct ofpbuf request, *reply;
945 ofpbuf_init(&request, 0);
946 nl_msg_put_nlmsghdr(&request, 0, RTM_DELLINK,
947 NLM_F_REQUEST | NLM_F_ACK);
948 ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg));
949 nl_msg_put_string(&request, IFLA_IFNAME, name);
951 err = nl_transact(NETLINK_ROUTE, &request, &reply);
954 ofpbuf_uninit(reply);
957 ofpbuf_uninit(&request);
962 netdev_vxlan_destroy(const char *name)
964 return netdev_linux_destroy(name);
968 netdev_gre_destroy(const char *name)
970 return netdev_linux_destroy(name);
974 * On some older systems, these enums are not defined.
977 #ifndef IFLA_VXLAN_MAX
978 #define IFLA_VXLAN_MAX 0
979 #define IFLA_VXLAN_PORT 15
981 #if IFLA_VXLAN_MAX < 20
982 #define IFLA_VXLAN_UDP_ZERO_CSUM6_RX 20
983 #define IFLA_VXLAN_GBP 23
984 #define IFLA_VXLAN_COLLECT_METADATA 25
987 #if IFLA_GRE_MAX < 18
988 #define IFLA_GRE_COLLECT_METADATA 18
992 netdev_vxlan_create(struct netdev *netdev)
995 struct ofpbuf request, *reply;
996 size_t linkinfo_off, infodata_off;
997 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
998 const char *name = netdev_vport_get_dpif_port(netdev,
999 namebuf, sizeof namebuf);
1000 struct ifinfomsg *ifinfo;
1001 const struct netdev_tunnel_config *tnl_cfg;
1002 tnl_cfg = netdev_get_tunnel_config(netdev);
1003 if (!tnl_cfg) { /* or assert? */
1007 ofpbuf_init(&request, 0);
1008 nl_msg_put_nlmsghdr(&request, 0, RTM_NEWLINK,
1009 NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE);
1010 ifinfo = ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg));
1011 ifinfo->ifi_change = ifinfo->ifi_flags = IFF_UP;
1012 nl_msg_put_string(&request, IFLA_IFNAME, name);
1013 nl_msg_put_u32(&request, IFLA_MTU, UINT16_MAX);
1014 linkinfo_off = nl_msg_start_nested(&request, IFLA_LINKINFO);
1015 nl_msg_put_string(&request, IFLA_INFO_KIND, "vxlan");
1016 infodata_off = nl_msg_start_nested(&request, IFLA_INFO_DATA);
1017 nl_msg_put_u8(&request, IFLA_VXLAN_LEARNING, 0);
1018 nl_msg_put_u8(&request, IFLA_VXLAN_COLLECT_METADATA, 1);
1019 nl_msg_put_u8(&request, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 1);
1020 if (tnl_cfg->exts & (1 << OVS_VXLAN_EXT_GBP)) {
1021 nl_msg_put_flag(&request, IFLA_VXLAN_GBP);
1023 nl_msg_put_be16(&request, IFLA_VXLAN_PORT, tnl_cfg->dst_port);
1024 nl_msg_end_nested(&request, infodata_off);
1025 nl_msg_end_nested(&request, linkinfo_off);
1027 err = nl_transact(NETLINK_ROUTE, &request, &reply);
1030 ofpbuf_uninit(reply);
1034 * Linux versions older than 4.3 will return EINVAL in case the VID is not
1035 * set, which is sufficient to verify COLLECT_METADATA is supported.
1037 if (err == EINVAL) {
1041 ofpbuf_uninit(&request);
1046 * On some Linux versions, creating the device with IFLA_GRE_COLLECT_METADATA
1047 * will succeed, even though that attribute is not supported. We need to verify
1048 * the device has been created with that attribute. In case it has not, we
1049 * destroy it and use the compat code.
1052 netdev_gre_verify(const char *name)
1055 struct ofpbuf request, *reply;
1056 struct ifinfomsg *ifmsg;
1058 static const struct nl_policy rtlink_policy[] = {
1059 [IFLA_LINKINFO] = { .type = NL_A_NESTED },
1061 static const struct nl_policy linkinfo_policy[] = {
1062 [IFLA_INFO_KIND] = { .type = NL_A_STRING },
1063 [IFLA_INFO_DATA] = { .type = NL_A_NESTED },
1065 static const struct nl_policy gre_policy[] = {
1066 [IFLA_GRE_COLLECT_METADATA] = { .type = NL_A_FLAG },
1069 ofpbuf_init(&request, 0);
1070 nl_msg_put_nlmsghdr(&request, 0, RTM_GETLINK,
1072 ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg));
1073 nl_msg_put_string(&request, IFLA_IFNAME, name);
1075 err = nl_transact(NETLINK_ROUTE, &request, &reply);
1077 struct nlattr *rtlink[ARRAY_SIZE(rtlink_policy)];
1078 struct nlattr *linkinfo[ARRAY_SIZE(linkinfo_policy)];
1079 struct nlattr *gre[ARRAY_SIZE(gre_policy)];
1082 ifmsg = ofpbuf_at(reply, NLMSG_HDRLEN, sizeof *ifmsg);
1083 if (nl_policy_parse(reply, NLMSG_HDRLEN + sizeof *ifmsg,
1084 rtlink_policy, rtlink, ARRAY_SIZE(rtlink_policy))) {
1085 if (nl_parse_nested(rtlink[IFLA_LINKINFO], linkinfo_policy,
1086 linkinfo, ARRAY_SIZE(linkinfo_policy)) &&
1087 !strcmp(nl_attr_get_string(linkinfo[IFLA_INFO_KIND]),
1089 if (nl_parse_nested(linkinfo[IFLA_INFO_DATA], gre_policy, gre,
1090 ARRAY_SIZE(gre_policy)) &&
1091 nl_attr_get_flag(gre[IFLA_GRE_COLLECT_METADATA])) {
1096 ofpbuf_uninit(reply);
1098 ofpbuf_uninit(&request);
1103 netdev_gre_create(struct netdev *netdev)
1106 struct ofpbuf request, *reply;
1107 size_t linkinfo_off, infodata_off;
1108 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1109 const char *name = netdev_vport_get_dpif_port(netdev,
1110 namebuf, sizeof namebuf);
1111 struct ifinfomsg *ifinfo;
1112 const struct netdev_tunnel_config *tnl_cfg;
1113 tnl_cfg = netdev_get_tunnel_config(netdev);
1114 if (!tnl_cfg) { /* or assert? */
1118 ofpbuf_init(&request, 0);
1119 nl_msg_put_nlmsghdr(&request, 0, RTM_NEWLINK,
1120 NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE);
1121 ifinfo = ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg));
1122 ifinfo->ifi_change = ifinfo->ifi_flags = IFF_UP;
1123 nl_msg_put_string(&request, IFLA_IFNAME, name);
1124 nl_msg_put_u32(&request, IFLA_MTU, UINT16_MAX);
1125 linkinfo_off = nl_msg_start_nested(&request, IFLA_LINKINFO);
1126 nl_msg_put_string(&request, IFLA_INFO_KIND, "gretap");
1127 infodata_off = nl_msg_start_nested(&request, IFLA_INFO_DATA);
1128 nl_msg_put_flag(&request, IFLA_GRE_COLLECT_METADATA);
1129 nl_msg_end_nested(&request, infodata_off);
1130 nl_msg_end_nested(&request, linkinfo_off);
1132 err = nl_transact(NETLINK_ROUTE, &request, &reply);
1135 ofpbuf_uninit(reply);
1138 if (!err && (err = netdev_gre_verify(name))) {
1139 netdev_gre_destroy(name);
1143 * If tunnel metadata is not supported, EEXIST will be returned for zero
1144 * addresses tunnel. We still need to verify metadata has been set as above.
1146 if (err == EINVAL || err == EEXIST) {
1150 ofpbuf_uninit(&request);
1157 netdev_vxlan_create(struct netdev *netdev OVS_UNUSED)
1163 netdev_gre_create(struct netdev *netdev OVS_UNUSED)
1169 netdev_vxlan_destroy(const char *name OVS_UNUSED)
1175 netdev_gre_destroy(const char *name OVS_UNUSED)
1183 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
1184 const char *port_name, struct dpif_port *dpif_port);
1187 dpif_netlink_port_create(struct netdev *netdev)
1189 switch (netdev_to_ovs_vport_type(netdev_get_type(netdev))) {
1190 case OVS_VPORT_TYPE_VXLAN:
1191 return netdev_vxlan_create(netdev);
1192 case OVS_VPORT_TYPE_GRE:
1193 return netdev_gre_create(netdev);
1194 case OVS_VPORT_TYPE_GENEVE:
1195 case OVS_VPORT_TYPE_NETDEV:
1196 case OVS_VPORT_TYPE_INTERNAL:
1197 case OVS_VPORT_TYPE_LISP:
1198 case OVS_VPORT_TYPE_STT:
1199 case OVS_VPORT_TYPE_UNSPEC:
1200 case __OVS_VPORT_TYPE_MAX:
1208 dpif_netlink_port_destroy(const char *name, const char *type)
1210 switch (netdev_to_ovs_vport_type(type)) {
1211 case OVS_VPORT_TYPE_VXLAN:
1212 return netdev_vxlan_destroy(name);
1213 case OVS_VPORT_TYPE_GRE:
1214 return netdev_gre_destroy(name);
1215 case OVS_VPORT_TYPE_GENEVE:
1216 case OVS_VPORT_TYPE_NETDEV:
1217 case OVS_VPORT_TYPE_INTERNAL:
1218 case OVS_VPORT_TYPE_LISP:
1219 case OVS_VPORT_TYPE_STT:
1220 case OVS_VPORT_TYPE_UNSPEC:
1221 case __OVS_VPORT_TYPE_MAX:
1229 dpif_netlink_port_create_and_add(struct dpif_netlink *dpif, struct netdev *netdev,
1230 odp_port_t *port_nop)
1231 OVS_REQ_WRLOCK(dpif->upcall_lock)
1234 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1235 const char *name = netdev_vport_get_dpif_port(netdev,
1236 namebuf, sizeof namebuf);
1238 error = dpif_netlink_port_create(netdev);
1243 error = dpif_netlink_port_add__(dpif, name, OVS_VPORT_TYPE_NETDEV, NULL, port_nop);
1245 VLOG_DBG("failed to add port, destroying: %d", error);
1246 dpif_netlink_port_destroy(name, netdev_get_type(netdev));
1252 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
1253 odp_port_t *port_nop)
1255 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1258 fat_rwlock_wrlock(&dpif->upcall_lock);
1259 error = dpif_netlink_port_create_and_add(dpif, netdev, port_nop);
1260 if (error == EOPNOTSUPP) {
1261 error = dpif_netlink_port_add_compat(dpif, netdev, port_nop);
1263 fat_rwlock_unlock(&dpif->upcall_lock);
1269 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
1270 OVS_REQ_WRLOCK(dpif->upcall_lock)
1272 struct dpif_netlink_vport vport;
1274 struct dpif_port dpif_port;
1276 error = dpif_netlink_port_query__(dpif, port_no, NULL, &dpif_port);
1281 dpif_netlink_vport_init(&vport);
1282 vport.cmd = OVS_VPORT_CMD_DEL;
1283 vport.dp_ifindex = dpif->dp_ifindex;
1284 vport.port_no = port_no;
1285 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
1287 vport_del_channels(dpif, port_no);
1289 dpif_netlink_port_destroy(dpif_port.name, dpif_port.type);
1290 dpif_port_destroy(&dpif_port);
1296 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
1298 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1301 fat_rwlock_wrlock(&dpif->upcall_lock);
1302 error = dpif_netlink_port_del__(dpif, port_no);
1303 fat_rwlock_unlock(&dpif->upcall_lock);
1309 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
1310 const char *port_name, struct dpif_port *dpif_port)
1312 struct dpif_netlink_vport request;
1313 struct dpif_netlink_vport reply;
1317 dpif_netlink_vport_init(&request);
1318 request.cmd = OVS_VPORT_CMD_GET;
1319 request.dp_ifindex = dpif->dp_ifindex;
1320 request.port_no = port_no;
1321 request.name = port_name;
1323 error = dpif_netlink_vport_transact(&request, &reply, &buf);
1325 if (reply.dp_ifindex != request.dp_ifindex) {
1326 /* A query by name reported that 'port_name' is in some datapath
1327 * other than 'dpif', but the caller wants to know about 'dpif'. */
1329 } else if (dpif_port) {
1330 dpif_port->name = xstrdup(reply.name);
1331 dpif_port->type = xstrdup(get_vport_type(&reply));
1332 dpif_port->port_no = reply.port_no;
1340 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
1341 struct dpif_port *dpif_port)
1343 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1345 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
1349 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
1350 struct dpif_port *dpif_port)
1352 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1354 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
1358 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
1359 odp_port_t port_no, uint32_t hash)
1360 OVS_REQ_RDLOCK(dpif->upcall_lock)
1362 uint32_t port_idx = odp_to_u32(port_no);
1365 if (dpif->handlers && dpif->uc_array_size > 0) {
1366 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1367 * channel, since it is not heavily loaded. */
1368 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
1369 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
1371 /* Needs to check in case the socket pointer is changed in between
1372 * the holding of upcall_lock. A known case happens when the main
1373 * thread deletes the vport while the handler thread is handling
1374 * the upcall from that port. */
1375 if (h->channels[idx].sock) {
1376 pid = nl_sock_pid(h->channels[idx].sock);
1384 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
1387 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1390 fat_rwlock_rdlock(&dpif->upcall_lock);
1391 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
1392 fat_rwlock_unlock(&dpif->upcall_lock);
1398 dpif_netlink_flow_flush(struct dpif *dpif_)
1400 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1401 struct dpif_netlink_flow flow;
1403 dpif_netlink_flow_init(&flow);
1404 flow.cmd = OVS_FLOW_CMD_DEL;
1405 flow.dp_ifindex = dpif->dp_ifindex;
1406 return dpif_netlink_flow_transact(&flow, NULL, NULL);
1409 struct dpif_netlink_port_state {
1410 struct nl_dump dump;
1415 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
1416 struct nl_dump *dump)
1418 struct dpif_netlink_vport request;
1421 dpif_netlink_vport_init(&request);
1422 request.cmd = OVS_VPORT_CMD_GET;
1423 request.dp_ifindex = dpif->dp_ifindex;
1425 buf = ofpbuf_new(1024);
1426 dpif_netlink_vport_to_ofpbuf(&request, buf);
1427 nl_dump_start(dump, NETLINK_GENERIC, buf);
1432 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
1434 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1435 struct dpif_netlink_port_state *state;
1437 *statep = state = xmalloc(sizeof *state);
1438 dpif_netlink_port_dump_start__(dpif, &state->dump);
1440 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
1445 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
1446 struct nl_dump *dump,
1447 struct dpif_netlink_vport *vport,
1448 struct ofpbuf *buffer)
1453 if (!nl_dump_next(dump, &buf, buffer)) {
1457 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
1459 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
1460 dpif_name(&dpif->dpif), ovs_strerror(error));
1466 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
1467 struct dpif_port *dpif_port)
1469 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1470 struct dpif_netlink_port_state *state = state_;
1471 struct dpif_netlink_vport vport;
1474 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
1479 dpif_port->name = CONST_CAST(char *, vport.name);
1480 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
1481 dpif_port->port_no = vport.port_no;
1486 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
1488 struct dpif_netlink_port_state *state = state_;
1489 int error = nl_dump_done(&state->dump);
1491 ofpbuf_uninit(&state->buf);
1497 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1499 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1501 /* Lazily create the Netlink socket to listen for notifications. */
1502 if (!dpif->port_notifier) {
1503 struct nl_sock *sock;
1506 error = nl_sock_create(NETLINK_GENERIC, &sock);
1511 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1513 nl_sock_destroy(sock);
1516 dpif->port_notifier = sock;
1518 /* We have no idea of the current state so report that everything
1524 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1525 uint64_t buf_stub[4096 / 8];
1529 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1530 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1532 struct dpif_netlink_vport vport;
1534 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1536 if (vport.dp_ifindex == dpif->dp_ifindex
1537 && (vport.cmd == OVS_VPORT_CMD_NEW
1538 || vport.cmd == OVS_VPORT_CMD_DEL
1539 || vport.cmd == OVS_VPORT_CMD_SET)) {
1540 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1541 dpif->dpif.full_name, vport.name, vport.cmd);
1542 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1543 dpif->refresh_channels = true;
1545 *devnamep = xstrdup(vport.name);
1546 ofpbuf_uninit(&buf);
1550 } else if (error != EAGAIN) {
1551 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1552 ovs_strerror(error));
1553 nl_sock_drain(dpif->port_notifier);
1557 ofpbuf_uninit(&buf);
1565 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1567 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1569 if (dpif->port_notifier) {
1570 nl_sock_wait(dpif->port_notifier, POLLIN);
1572 poll_immediate_wake();
1577 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow *request,
1578 const ovs_u128 *ufid, bool terse)
1581 request->ufid = *ufid;
1582 request->ufid_present = true;
1584 request->ufid_present = false;
1586 request->ufid_terse = terse;
1590 dpif_netlink_init_flow_get__(const struct dpif_netlink *dpif,
1591 const struct nlattr *key, size_t key_len,
1592 const ovs_u128 *ufid, bool terse,
1593 struct dpif_netlink_flow *request)
1595 dpif_netlink_flow_init(request);
1596 request->cmd = OVS_FLOW_CMD_GET;
1597 request->dp_ifindex = dpif->dp_ifindex;
1599 request->key_len = key_len;
1600 dpif_netlink_flow_init_ufid(request, ufid, terse);
1604 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1605 const struct dpif_flow_get *get,
1606 struct dpif_netlink_flow *request)
1608 dpif_netlink_init_flow_get__(dpif, get->key, get->key_len, get->ufid,
1613 dpif_netlink_flow_get__(const struct dpif_netlink *dpif,
1614 const struct nlattr *key, size_t key_len,
1615 const ovs_u128 *ufid, bool terse,
1616 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1618 struct dpif_netlink_flow request;
1620 dpif_netlink_init_flow_get__(dpif, key, key_len, ufid, terse, &request);
1621 return dpif_netlink_flow_transact(&request, reply, bufp);
1625 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1626 const struct dpif_netlink_flow *flow,
1627 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1629 return dpif_netlink_flow_get__(dpif, flow->key, flow->key_len,
1630 flow->ufid_present ? &flow->ufid : NULL,
1631 false, reply, bufp);
1635 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1636 const struct dpif_flow_put *put,
1637 struct dpif_netlink_flow *request)
1639 static const struct nlattr dummy_action;
1641 dpif_netlink_flow_init(request);
1642 request->cmd = (put->flags & DPIF_FP_CREATE
1643 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1644 request->dp_ifindex = dpif->dp_ifindex;
1645 request->key = put->key;
1646 request->key_len = put->key_len;
1647 request->mask = put->mask;
1648 request->mask_len = put->mask_len;
1649 dpif_netlink_flow_init_ufid(request, put->ufid, false);
1651 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1652 request->actions = (put->actions
1654 : CONST_CAST(struct nlattr *, &dummy_action));
1655 request->actions_len = put->actions_len;
1656 if (put->flags & DPIF_FP_ZERO_STATS) {
1657 request->clear = true;
1659 if (put->flags & DPIF_FP_PROBE) {
1660 request->probe = true;
1662 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1666 dpif_netlink_init_flow_del__(struct dpif_netlink *dpif,
1667 const struct nlattr *key, size_t key_len,
1668 const ovs_u128 *ufid, bool terse,
1669 struct dpif_netlink_flow *request)
1671 dpif_netlink_flow_init(request);
1672 request->cmd = OVS_FLOW_CMD_DEL;
1673 request->dp_ifindex = dpif->dp_ifindex;
1675 request->key_len = key_len;
1676 dpif_netlink_flow_init_ufid(request, ufid, terse);
1680 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1681 const struct dpif_flow_del *del,
1682 struct dpif_netlink_flow *request)
1684 dpif_netlink_init_flow_del__(dpif, del->key, del->key_len,
1685 del->ufid, del->terse, request);
1688 struct dpif_netlink_flow_dump {
1689 struct dpif_flow_dump up;
1690 struct nl_dump nl_dump;
1694 static struct dpif_netlink_flow_dump *
1695 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1697 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1700 static struct dpif_flow_dump *
1701 dpif_netlink_flow_dump_create(const struct dpif *dpif_, bool terse)
1703 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1704 struct dpif_netlink_flow_dump *dump;
1705 struct dpif_netlink_flow request;
1708 dump = xmalloc(sizeof *dump);
1709 dpif_flow_dump_init(&dump->up, dpif_);
1711 dpif_netlink_flow_init(&request);
1712 request.cmd = OVS_FLOW_CMD_GET;
1713 request.dp_ifindex = dpif->dp_ifindex;
1714 request.ufid_present = false;
1715 request.ufid_terse = terse;
1717 buf = ofpbuf_new(1024);
1718 dpif_netlink_flow_to_ofpbuf(&request, buf);
1719 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1721 atomic_init(&dump->status, 0);
1722 dump->up.terse = terse;
1728 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1730 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1731 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1734 /* No other thread has access to 'dump' at this point. */
1735 atomic_read_relaxed(&dump->status, &dump_status);
1737 return dump_status ? dump_status : nl_status;
1740 struct dpif_netlink_flow_dump_thread {
1741 struct dpif_flow_dump_thread up;
1742 struct dpif_netlink_flow_dump *dump;
1743 struct dpif_netlink_flow flow;
1744 struct dpif_flow_stats stats;
1745 struct ofpbuf nl_flows; /* Always used to store flows. */
1746 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1749 static struct dpif_netlink_flow_dump_thread *
1750 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1752 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1755 static struct dpif_flow_dump_thread *
1756 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1758 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1759 struct dpif_netlink_flow_dump_thread *thread;
1761 thread = xmalloc(sizeof *thread);
1762 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1763 thread->dump = dump;
1764 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1765 thread->nl_actions = NULL;
1771 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1773 struct dpif_netlink_flow_dump_thread *thread
1774 = dpif_netlink_flow_dump_thread_cast(thread_);
1776 ofpbuf_uninit(&thread->nl_flows);
1777 ofpbuf_delete(thread->nl_actions);
1782 dpif_netlink_flow_to_dpif_flow(struct dpif *dpif, struct dpif_flow *dpif_flow,
1783 const struct dpif_netlink_flow *datapath_flow)
1785 dpif_flow->key = datapath_flow->key;
1786 dpif_flow->key_len = datapath_flow->key_len;
1787 dpif_flow->mask = datapath_flow->mask;
1788 dpif_flow->mask_len = datapath_flow->mask_len;
1789 dpif_flow->actions = datapath_flow->actions;
1790 dpif_flow->actions_len = datapath_flow->actions_len;
1791 dpif_flow->ufid_present = datapath_flow->ufid_present;
1792 dpif_flow->pmd_id = PMD_ID_NULL;
1793 if (datapath_flow->ufid_present) {
1794 dpif_flow->ufid = datapath_flow->ufid;
1796 ovs_assert(datapath_flow->key && datapath_flow->key_len);
1797 dpif_flow_hash(dpif, datapath_flow->key, datapath_flow->key_len,
1800 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1804 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1805 struct dpif_flow *flows, int max_flows)
1807 struct dpif_netlink_flow_dump_thread *thread
1808 = dpif_netlink_flow_dump_thread_cast(thread_);
1809 struct dpif_netlink_flow_dump *dump = thread->dump;
1810 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1813 ofpbuf_delete(thread->nl_actions);
1814 thread->nl_actions = NULL;
1818 || (n_flows < max_flows && thread->nl_flows.size)) {
1819 struct dpif_netlink_flow datapath_flow;
1820 struct ofpbuf nl_flow;
1823 /* Try to grab another flow. */
1824 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1828 /* Convert the flow to our output format. */
1829 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1831 atomic_store_relaxed(&dump->status, error);
1835 if (dump->up.terse || datapath_flow.actions) {
1836 /* Common case: we don't want actions, or the flow includes
1838 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1841 /* Rare case: the flow does not include actions. Retrieve this
1842 * individual flow again to get the actions. */
1843 error = dpif_netlink_flow_get(dpif, &datapath_flow,
1844 &datapath_flow, &thread->nl_actions);
1845 if (error == ENOENT) {
1846 VLOG_DBG("dumped flow disappeared on get");
1849 VLOG_WARN("error fetching dumped flow: %s",
1850 ovs_strerror(error));
1851 atomic_store_relaxed(&dump->status, error);
1855 /* Save this flow. Then exit, because we only have one buffer to
1856 * handle this case. */
1857 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1866 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1869 struct ovs_header *k_exec;
1872 ofpbuf_prealloc_tailroom(buf, (64
1873 + dp_packet_size(d_exec->packet)
1874 + ODP_KEY_METADATA_SIZE
1875 + d_exec->actions_len));
1877 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1878 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1880 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1881 k_exec->dp_ifindex = dp_ifindex;
1883 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1884 dp_packet_data(d_exec->packet),
1885 dp_packet_size(d_exec->packet));
1887 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1888 odp_key_from_pkt_metadata(buf, &d_exec->packet->md);
1889 nl_msg_end_nested(buf, key_ofs);
1891 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1892 d_exec->actions, d_exec->actions_len);
1893 if (d_exec->probe) {
1894 nl_msg_put_flag(buf, OVS_PACKET_ATTR_PROBE);
1897 nl_msg_put_u16(buf, OVS_PACKET_ATTR_MRU, d_exec->mtu);
1901 /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1902 * Returns the number actually executed (at least 1, if 'n_ops' is
1905 dpif_netlink_operate__(struct dpif_netlink *dpif,
1906 struct dpif_op **ops, size_t n_ops)
1908 enum { MAX_OPS = 50 };
1911 struct nl_transaction txn;
1913 struct ofpbuf request;
1914 uint64_t request_stub[1024 / 8];
1916 struct ofpbuf reply;
1917 uint64_t reply_stub[1024 / 8];
1920 struct nl_transaction *txnsp[MAX_OPS];
1923 n_ops = MIN(n_ops, MAX_OPS);
1924 for (i = 0; i < n_ops; i++) {
1925 struct op_auxdata *aux = &auxes[i];
1926 struct dpif_op *op = ops[i];
1927 struct dpif_flow_put *put;
1928 struct dpif_flow_del *del;
1929 struct dpif_flow_get *get;
1930 struct dpif_netlink_flow flow;
1932 ofpbuf_use_stub(&aux->request,
1933 aux->request_stub, sizeof aux->request_stub);
1934 aux->txn.request = &aux->request;
1936 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1937 aux->txn.reply = NULL;
1940 case DPIF_OP_FLOW_PUT:
1941 put = &op->u.flow_put;
1942 dpif_netlink_init_flow_put(dpif, put, &flow);
1944 flow.nlmsg_flags |= NLM_F_ECHO;
1945 aux->txn.reply = &aux->reply;
1947 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1950 case DPIF_OP_FLOW_DEL:
1951 del = &op->u.flow_del;
1952 dpif_netlink_init_flow_del(dpif, del, &flow);
1954 flow.nlmsg_flags |= NLM_F_ECHO;
1955 aux->txn.reply = &aux->reply;
1957 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1960 case DPIF_OP_EXECUTE:
1961 /* Can't execute a packet that won't fit in a Netlink attribute. */
1962 if (OVS_UNLIKELY(nl_attr_oversized(
1963 dp_packet_size(op->u.execute.packet)))) {
1964 /* Report an error immediately if this is the first operation.
1965 * Otherwise the easiest thing to do is to postpone to the next
1966 * call (when this will be the first operation). */
1968 VLOG_ERR_RL(&error_rl,
1969 "dropping oversized %"PRIu32"-byte packet",
1970 dp_packet_size(op->u.execute.packet));
1971 op->error = ENOBUFS;
1976 dpif_netlink_encode_execute(dpif->dp_ifindex, &op->u.execute,
1981 case DPIF_OP_FLOW_GET:
1982 get = &op->u.flow_get;
1983 dpif_netlink_init_flow_get(dpif, get, &flow);
1984 aux->txn.reply = get->buffer;
1985 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1993 for (i = 0; i < n_ops; i++) {
1994 txnsp[i] = &auxes[i].txn;
1996 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1998 for (i = 0; i < n_ops; i++) {
1999 struct op_auxdata *aux = &auxes[i];
2000 struct nl_transaction *txn = &auxes[i].txn;
2001 struct dpif_op *op = ops[i];
2002 struct dpif_flow_put *put;
2003 struct dpif_flow_del *del;
2004 struct dpif_flow_get *get;
2006 op->error = txn->error;
2009 case DPIF_OP_FLOW_PUT:
2010 put = &op->u.flow_put;
2013 struct dpif_netlink_flow reply;
2015 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
2018 dpif_netlink_flow_get_stats(&reply, put->stats);
2024 case DPIF_OP_FLOW_DEL:
2025 del = &op->u.flow_del;
2028 struct dpif_netlink_flow reply;
2030 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
2033 dpif_netlink_flow_get_stats(&reply, del->stats);
2039 case DPIF_OP_EXECUTE:
2042 case DPIF_OP_FLOW_GET:
2043 get = &op->u.flow_get;
2045 struct dpif_netlink_flow reply;
2047 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
2049 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, get->flow,
2059 ofpbuf_uninit(&aux->request);
2060 ofpbuf_uninit(&aux->reply);
2067 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
2069 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2072 size_t chunk = dpif_netlink_operate__(dpif, ops, n_ops);
2080 dpif_netlink_handler_uninit(struct dpif_handler *handler)
2082 vport_delete_sock_pool(handler);
2086 dpif_netlink_handler_init(struct dpif_handler *handler)
2088 return vport_create_sock_pool(handler);
2093 dpif_netlink_handler_init(struct dpif_handler *handler)
2095 handler->epoll_fd = epoll_create(10);
2096 return handler->epoll_fd < 0 ? errno : 0;
2100 dpif_netlink_handler_uninit(struct dpif_handler *handler)
2102 close(handler->epoll_fd);
2106 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
2107 * currently in 'dpif' in the kernel, by adding a new set of channels for
2108 * any kernel vport that lacks one and deleting any channels that have no
2109 * backing kernel vports. */
2111 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
2112 OVS_REQ_WRLOCK(dpif->upcall_lock)
2114 unsigned long int *keep_channels;
2115 struct dpif_netlink_vport vport;
2116 size_t keep_channels_nbits;
2117 struct nl_dump dump;
2118 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
2123 ovs_assert(!WINDOWS || n_handlers <= 1);
2124 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
2126 if (dpif->n_handlers != n_handlers) {
2127 destroy_all_channels(dpif);
2128 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
2129 for (i = 0; i < n_handlers; i++) {
2131 struct dpif_handler *handler = &dpif->handlers[i];
2133 error = dpif_netlink_handler_init(handler);
2136 struct dpif_handler *tmp = &dpif->handlers[i];
2139 for (j = 0; j < i; j++) {
2140 dpif_netlink_handler_uninit(tmp);
2142 free(dpif->handlers);
2143 dpif->handlers = NULL;
2148 dpif->n_handlers = n_handlers;
2151 for (i = 0; i < n_handlers; i++) {
2152 struct dpif_handler *handler = &dpif->handlers[i];
2154 handler->event_offset = handler->n_events = 0;
2157 keep_channels_nbits = dpif->uc_array_size;
2158 keep_channels = bitmap_allocate(keep_channels_nbits);
2160 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
2161 dpif_netlink_port_dump_start__(dpif, &dump);
2162 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
2163 uint32_t port_no = odp_to_u32(vport.port_no);
2164 uint32_t *upcall_pids = NULL;
2167 if (port_no >= dpif->uc_array_size
2168 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
2169 struct nl_sock **socksp = vport_create_socksp(dpif, &error);
2175 error = vport_add_channels(dpif, vport.port_no, socksp);
2177 VLOG_INFO("%s: could not add channels for port %s",
2178 dpif_name(&dpif->dpif), vport.name);
2179 vport_del_socksp(dpif, socksp);
2183 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
2187 /* Configure the vport to deliver misses to 'sock'. */
2188 if (vport.upcall_pids[0] == 0
2189 || vport.n_upcall_pids != dpif->n_handlers
2190 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
2192 struct dpif_netlink_vport vport_request;
2194 dpif_netlink_vport_init(&vport_request);
2195 vport_request.cmd = OVS_VPORT_CMD_SET;
2196 vport_request.dp_ifindex = dpif->dp_ifindex;
2197 vport_request.port_no = vport.port_no;
2198 vport_request.n_upcall_pids = dpif->n_handlers;
2199 vport_request.upcall_pids = upcall_pids;
2200 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
2202 VLOG_WARN_RL(&error_rl,
2203 "%s: failed to set upcall pid on port: %s",
2204 dpif_name(&dpif->dpif), ovs_strerror(error));
2206 if (error != ENODEV && error != ENOENT) {
2209 /* The vport isn't really there, even though the dump says
2210 * it is. Probably we just hit a race after a port
2217 if (port_no < keep_channels_nbits) {
2218 bitmap_set1(keep_channels, port_no);
2225 vport_del_channels(dpif, vport.port_no);
2227 nl_dump_done(&dump);
2228 ofpbuf_uninit(&buf);
2230 /* Discard any saved channels that we didn't reuse. */
2231 for (i = 0; i < keep_channels_nbits; i++) {
2232 if (!bitmap_is_set(keep_channels, i)) {
2233 vport_del_channels(dpif, u32_to_odp(i));
2236 free(keep_channels);
2242 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
2243 OVS_REQ_WRLOCK(dpif->upcall_lock)
2245 if ((dpif->handlers != NULL) == enable) {
2247 } else if (!enable) {
2248 destroy_all_channels(dpif);
2251 return dpif_netlink_refresh_channels(dpif, 1);
2256 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
2258 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2261 fat_rwlock_wrlock(&dpif->upcall_lock);
2262 error = dpif_netlink_recv_set__(dpif, enable);
2263 fat_rwlock_unlock(&dpif->upcall_lock);
2269 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
2271 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2275 /* Multiple upcall handlers will be supported once kernel datapath supports
2277 if (n_handlers > 1) {
2282 fat_rwlock_wrlock(&dpif->upcall_lock);
2283 if (dpif->handlers) {
2284 error = dpif_netlink_refresh_channels(dpif, n_handlers);
2286 fat_rwlock_unlock(&dpif->upcall_lock);
2292 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
2293 uint32_t queue_id, uint32_t *priority)
2295 if (queue_id < 0xf000) {
2296 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
2304 parse_odp_packet(const struct dpif_netlink *dpif, struct ofpbuf *buf,
2305 struct dpif_upcall *upcall, int *dp_ifindex)
2307 static const struct nl_policy ovs_packet_policy[] = {
2308 /* Always present. */
2309 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
2310 .min_len = ETH_HEADER_LEN },
2311 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
2313 /* OVS_PACKET_CMD_ACTION only. */
2314 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
2315 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
2316 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2317 [OVS_PACKET_ATTR_MRU] = { .type = NL_A_U16, .optional = true }
2320 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2321 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2322 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2323 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2325 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
2326 if (!nlmsg || !genl || !ovs_header
2327 || nlmsg->nlmsg_type != ovs_packet_family
2328 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
2329 ARRAY_SIZE(ovs_packet_policy))) {
2333 int type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
2334 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
2340 /* (Re)set ALL fields of '*upcall' on successful return. */
2341 upcall->type = type;
2342 upcall->key = CONST_CAST(struct nlattr *,
2343 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
2344 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
2345 dpif_flow_hash(&dpif->dpif, upcall->key, upcall->key_len, &upcall->ufid);
2346 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
2347 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
2348 upcall->actions = a[OVS_PACKET_ATTR_ACTIONS];
2349 upcall->mru = a[OVS_PACKET_ATTR_MRU];
2351 /* Allow overwriting the netlink attribute header without reallocating. */
2352 dp_packet_use_stub(&upcall->packet,
2353 CONST_CAST(struct nlattr *,
2354 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
2355 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
2356 sizeof(struct nlattr));
2357 dp_packet_set_data(&upcall->packet,
2358 (char *)dp_packet_data(&upcall->packet) + sizeof(struct nlattr));
2359 dp_packet_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
2361 *dp_ifindex = ovs_header->dp_ifindex;
2367 #define PACKET_RECV_BATCH_SIZE 50
2369 dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id,
2370 struct dpif_upcall *upcall, struct ofpbuf *buf)
2371 OVS_REQ_RDLOCK(dpif->upcall_lock)
2373 struct dpif_handler *handler;
2375 struct dpif_windows_vport_sock *sock_pool;
2378 if (!dpif->handlers) {
2382 /* Only one handler is supported currently. */
2383 if (handler_id >= 1) {
2387 if (handler_id >= dpif->n_handlers) {
2391 handler = &dpif->handlers[handler_id];
2392 sock_pool = handler->vport_sock_pool;
2394 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2399 if (++read_tries > PACKET_RECV_BATCH_SIZE) {
2403 error = nl_sock_recv(sock_pool[i].nl_sock, buf, false);
2404 if (error == ENOBUFS) {
2405 /* ENOBUFS typically means that we've received so many
2406 * packets that the buffer overflowed. Try again
2407 * immediately because there's almost certainly a packet
2408 * waiting for us. */
2409 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2413 /* XXX: ch->last_poll = time_msec(); */
2415 if (error == EAGAIN) {
2421 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2422 if (!error && dp_ifindex == dpif->dp_ifindex) {
2434 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
2435 struct dpif_upcall *upcall, struct ofpbuf *buf)
2436 OVS_REQ_RDLOCK(dpif->upcall_lock)
2438 struct dpif_handler *handler;
2441 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
2445 handler = &dpif->handlers[handler_id];
2446 if (handler->event_offset >= handler->n_events) {
2449 handler->event_offset = handler->n_events = 0;
2452 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
2453 dpif->uc_array_size, 0);
2454 } while (retval < 0 && errno == EINTR);
2457 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2458 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
2459 } else if (retval > 0) {
2460 handler->n_events = retval;
2464 while (handler->event_offset < handler->n_events) {
2465 int idx = handler->epoll_events[handler->event_offset].data.u32;
2466 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
2468 handler->event_offset++;
2474 if (++read_tries > 50) {
2478 error = nl_sock_recv(ch->sock, buf, false);
2479 if (error == ENOBUFS) {
2480 /* ENOBUFS typically means that we've received so many
2481 * packets that the buffer overflowed. Try again
2482 * immediately because there's almost certainly a packet
2483 * waiting for us. */
2484 report_loss(dpif, ch, idx, handler_id);
2488 ch->last_poll = time_msec();
2490 if (error == EAGAIN) {
2496 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2497 if (!error && dp_ifindex == dpif->dp_ifindex) {
2510 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
2511 struct dpif_upcall *upcall, struct ofpbuf *buf)
2513 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2516 fat_rwlock_rdlock(&dpif->upcall_lock);
2518 error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf);
2520 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
2522 fat_rwlock_unlock(&dpif->upcall_lock);
2528 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
2529 OVS_REQ_RDLOCK(dpif->upcall_lock)
2533 struct dpif_windows_vport_sock *sock_pool =
2534 dpif->handlers[handler_id].vport_sock_pool;
2536 /* Only one handler is supported currently. */
2537 if (handler_id >= 1) {
2541 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2542 nl_sock_wait(sock_pool[i].nl_sock, POLLIN);
2545 if (dpif->handlers && handler_id < dpif->n_handlers) {
2546 struct dpif_handler *handler = &dpif->handlers[handler_id];
2548 poll_fd_wait(handler->epoll_fd, POLLIN);
2554 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
2556 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2558 fat_rwlock_rdlock(&dpif->upcall_lock);
2559 dpif_netlink_recv_wait__(dpif, handler_id);
2560 fat_rwlock_unlock(&dpif->upcall_lock);
2564 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
2565 OVS_REQ_WRLOCK(dpif->upcall_lock)
2567 if (dpif->handlers) {
2570 for (i = 0; i < dpif->uc_array_size; i++ ) {
2571 if (!dpif->handlers[0].channels[i].sock) {
2575 for (j = 0; j < dpif->n_handlers; j++) {
2576 nl_sock_drain(dpif->handlers[j].channels[i].sock);
2583 dpif_netlink_recv_purge(struct dpif *dpif_)
2585 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2587 fat_rwlock_wrlock(&dpif->upcall_lock);
2588 dpif_netlink_recv_purge__(dpif);
2589 fat_rwlock_unlock(&dpif->upcall_lock);
2593 dpif_netlink_get_datapath_version(void)
2595 char *version_str = NULL;
2599 #define MAX_VERSION_STR_SIZE 80
2600 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2603 f = fopen(LINUX_DATAPATH_VERSION_FILE, "r");
2606 char version[MAX_VERSION_STR_SIZE];
2608 if (fgets(version, MAX_VERSION_STR_SIZE, f)) {
2609 newline = strchr(version, '\n');
2613 version_str = xstrdup(version);
2623 struct dpif_netlink_ct_dump_state {
2624 struct ct_dpif_dump_state up;
2625 struct nl_ct_dump_state *nl_ct_dump;
2629 dpif_netlink_ct_dump_start(struct dpif *dpif OVS_UNUSED,
2630 struct ct_dpif_dump_state **dump_,
2631 const uint16_t *zone)
2633 struct dpif_netlink_ct_dump_state *dump;
2636 dump = xzalloc(sizeof *dump);
2637 err = nl_ct_dump_start(&dump->nl_ct_dump, zone);
2649 dpif_netlink_ct_dump_next(struct dpif *dpif OVS_UNUSED,
2650 struct ct_dpif_dump_state *dump_,
2651 struct ct_dpif_entry *entry)
2653 struct dpif_netlink_ct_dump_state *dump;
2655 INIT_CONTAINER(dump, dump_, up);
2657 return nl_ct_dump_next(dump->nl_ct_dump, entry);
2661 dpif_netlink_ct_dump_done(struct dpif *dpif OVS_UNUSED,
2662 struct ct_dpif_dump_state *dump_)
2664 struct dpif_netlink_ct_dump_state *dump;
2667 INIT_CONTAINER(dump, dump_, up);
2669 err = nl_ct_dump_done(dump->nl_ct_dump);
2675 dpif_netlink_ct_flush(struct dpif *dpif OVS_UNUSED, const uint16_t *zone)
2678 return nl_ct_flush_zone(*zone);
2680 return nl_ct_flush();
2685 const struct dpif_class dpif_netlink_class = {
2688 dpif_netlink_enumerate,
2692 dpif_netlink_destroy,
2695 dpif_netlink_get_stats,
2696 dpif_netlink_port_add,
2697 dpif_netlink_port_del,
2698 dpif_netlink_port_query_by_number,
2699 dpif_netlink_port_query_by_name,
2700 dpif_netlink_port_get_pid,
2701 dpif_netlink_port_dump_start,
2702 dpif_netlink_port_dump_next,
2703 dpif_netlink_port_dump_done,
2704 dpif_netlink_port_poll,
2705 dpif_netlink_port_poll_wait,
2706 dpif_netlink_flow_flush,
2707 dpif_netlink_flow_dump_create,
2708 dpif_netlink_flow_dump_destroy,
2709 dpif_netlink_flow_dump_thread_create,
2710 dpif_netlink_flow_dump_thread_destroy,
2711 dpif_netlink_flow_dump_next,
2712 dpif_netlink_operate,
2713 dpif_netlink_recv_set,
2714 dpif_netlink_handlers_set,
2715 NULL, /* poll_thread_set */
2716 dpif_netlink_queue_to_priority,
2718 dpif_netlink_recv_wait,
2719 dpif_netlink_recv_purge,
2720 NULL, /* register_dp_purge_cb */
2721 NULL, /* register_upcall_cb */
2722 NULL, /* enable_upcall */
2723 NULL, /* disable_upcall */
2724 dpif_netlink_get_datapath_version, /* get_datapath_version */
2726 dpif_netlink_ct_dump_start,
2727 dpif_netlink_ct_dump_next,
2728 dpif_netlink_ct_dump_done,
2729 dpif_netlink_ct_flush,
2731 NULL, /* ct_dump_start */
2732 NULL, /* ct_dump_next */
2733 NULL, /* ct_dump_done */
2734 NULL, /* ct_flush */
2739 dpif_netlink_init(void)
2741 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2744 if (ovsthread_once_start(&once)) {
2745 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
2746 &ovs_datapath_family);
2748 VLOG_WARN("Generic Netlink family '%s' does not exist. "
2749 "The Open vSwitch kernel module is probably not loaded.",
2750 OVS_DATAPATH_FAMILY);
2753 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
2756 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
2759 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
2760 &ovs_packet_family);
2763 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
2764 &ovs_vport_mcgroup);
2767 ovsthread_once_done(&once);
2774 dpif_netlink_is_internal_device(const char *name)
2776 struct dpif_netlink_vport reply;
2780 error = dpif_netlink_vport_get(name, &reply, &buf);
2783 } else if (error != ENODEV && error != ENOENT) {
2784 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
2785 name, ovs_strerror(error));
2788 return reply.type == OVS_VPORT_TYPE_INTERNAL;
2791 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2792 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
2793 * positive errno value.
2795 * 'vport' will contain pointers into 'buf', so the caller should not free
2796 * 'buf' while 'vport' is still in use. */
2798 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
2799 const struct ofpbuf *buf)
2801 static const struct nl_policy ovs_vport_policy[] = {
2802 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2803 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2804 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2805 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2806 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2808 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2811 dpif_netlink_vport_init(vport);
2813 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2814 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2815 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2816 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2818 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2819 if (!nlmsg || !genl || !ovs_header
2820 || nlmsg->nlmsg_type != ovs_vport_family
2821 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2822 ARRAY_SIZE(ovs_vport_policy))) {
2826 vport->cmd = genl->cmd;
2827 vport->dp_ifindex = ovs_header->dp_ifindex;
2828 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2829 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2830 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2831 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2832 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2833 / (sizeof *vport->upcall_pids);
2834 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2837 if (a[OVS_VPORT_ATTR_STATS]) {
2838 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2840 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2841 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2842 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2847 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2848 * followed by Netlink attributes corresponding to 'vport'. */
2850 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
2853 struct ovs_header *ovs_header;
2855 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2856 vport->cmd, OVS_VPORT_VERSION);
2858 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2859 ovs_header->dp_ifindex = vport->dp_ifindex;
2861 if (vport->port_no != ODPP_NONE) {
2862 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2865 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2866 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2870 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2873 if (vport->upcall_pids) {
2874 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2876 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2880 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2881 vport->stats, sizeof *vport->stats);
2884 if (vport->options) {
2885 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2886 vport->options, vport->options_len);
2890 /* Clears 'vport' to "empty" values. */
2892 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
2894 memset(vport, 0, sizeof *vport);
2895 vport->port_no = ODPP_NONE;
2898 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2899 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2900 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2901 * result of the command is expected to be an ovs_vport also, which is decoded
2902 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2903 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2905 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
2906 struct dpif_netlink_vport *reply,
2907 struct ofpbuf **bufp)
2909 struct ofpbuf *request_buf;
2912 ovs_assert((reply != NULL) == (bufp != NULL));
2914 error = dpif_netlink_init();
2918 dpif_netlink_vport_init(reply);
2923 request_buf = ofpbuf_new(1024);
2924 dpif_netlink_vport_to_ofpbuf(request, request_buf);
2925 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2926 ofpbuf_delete(request_buf);
2930 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
2933 dpif_netlink_vport_init(reply);
2934 ofpbuf_delete(*bufp);
2941 /* Obtains information about the kernel vport named 'name' and stores it into
2942 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2943 * longer needed ('reply' will contain pointers into '*bufp'). */
2945 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
2946 struct ofpbuf **bufp)
2948 struct dpif_netlink_vport request;
2950 dpif_netlink_vport_init(&request);
2951 request.cmd = OVS_VPORT_CMD_GET;
2952 request.name = name;
2954 return dpif_netlink_vport_transact(&request, reply, bufp);
2957 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2958 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2959 * positive errno value.
2961 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2962 * while 'dp' is still in use. */
2964 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
2966 static const struct nl_policy ovs_datapath_policy[] = {
2967 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2968 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2970 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2971 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2975 dpif_netlink_dp_init(dp);
2977 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2978 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2979 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2980 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2982 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2983 if (!nlmsg || !genl || !ovs_header
2984 || nlmsg->nlmsg_type != ovs_datapath_family
2985 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2986 ARRAY_SIZE(ovs_datapath_policy))) {
2990 dp->cmd = genl->cmd;
2991 dp->dp_ifindex = ovs_header->dp_ifindex;
2992 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2993 if (a[OVS_DP_ATTR_STATS]) {
2994 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
2997 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2998 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
3004 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
3006 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
3008 struct ovs_header *ovs_header;
3010 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
3011 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
3012 OVS_DATAPATH_VERSION);
3014 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
3015 ovs_header->dp_ifindex = dp->dp_ifindex;
3018 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
3021 if (dp->upcall_pid) {
3022 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
3025 if (dp->user_features) {
3026 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
3029 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
3032 /* Clears 'dp' to "empty" values. */
3034 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
3036 memset(dp, 0, sizeof *dp);
3040 dpif_netlink_dp_dump_start(struct nl_dump *dump)
3042 struct dpif_netlink_dp request;
3045 dpif_netlink_dp_init(&request);
3046 request.cmd = OVS_DP_CMD_GET;
3048 buf = ofpbuf_new(1024);
3049 dpif_netlink_dp_to_ofpbuf(&request, buf);
3050 nl_dump_start(dump, NETLINK_GENERIC, buf);
3054 /* Executes 'request' in the kernel datapath. If the command fails, returns a
3055 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
3056 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
3057 * result of the command is expected to be of the same form, which is decoded
3058 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
3059 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
3061 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
3062 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
3064 struct ofpbuf *request_buf;
3067 ovs_assert((reply != NULL) == (bufp != NULL));
3069 request_buf = ofpbuf_new(1024);
3070 dpif_netlink_dp_to_ofpbuf(request, request_buf);
3071 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
3072 ofpbuf_delete(request_buf);
3075 dpif_netlink_dp_init(reply);
3077 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
3080 ofpbuf_delete(*bufp);
3087 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
3088 * The caller must free '*bufp' when the reply is no longer needed ('reply'
3089 * will contain pointers into '*bufp'). */
3091 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
3092 struct ofpbuf **bufp)
3094 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
3095 struct dpif_netlink_dp request;
3097 dpif_netlink_dp_init(&request);
3098 request.cmd = OVS_DP_CMD_GET;
3099 request.dp_ifindex = dpif->dp_ifindex;
3101 return dpif_netlink_dp_transact(&request, reply, bufp);
3104 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
3105 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
3106 * positive errno value.
3108 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
3109 * while 'flow' is still in use. */
3111 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
3112 const struct ofpbuf *buf)
3114 static const struct nl_policy ovs_flow_policy[__OVS_FLOW_ATTR_MAX] = {
3115 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED, .optional = true },
3116 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
3117 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
3118 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
3120 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
3121 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
3122 [OVS_FLOW_ATTR_UFID] = { .type = NL_A_UNSPEC, .optional = true,
3123 .min_len = sizeof(ovs_u128) },
3124 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
3125 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
3126 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
3129 dpif_netlink_flow_init(flow);
3131 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
3132 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
3133 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
3134 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
3136 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
3137 if (!nlmsg || !genl || !ovs_header
3138 || nlmsg->nlmsg_type != ovs_flow_family
3139 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
3140 ARRAY_SIZE(ovs_flow_policy))) {
3143 if (!a[OVS_FLOW_ATTR_KEY] && !a[OVS_FLOW_ATTR_UFID]) {
3147 flow->nlmsg_flags = nlmsg->nlmsg_flags;
3148 flow->dp_ifindex = ovs_header->dp_ifindex;
3149 if (a[OVS_FLOW_ATTR_KEY]) {
3150 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
3151 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
3154 if (a[OVS_FLOW_ATTR_UFID]) {
3155 const ovs_u128 *ufid;
3157 ufid = nl_attr_get_unspec(a[OVS_FLOW_ATTR_UFID],
3158 nl_attr_get_size(a[OVS_FLOW_ATTR_UFID]));
3160 flow->ufid_present = true;
3162 if (a[OVS_FLOW_ATTR_MASK]) {
3163 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
3164 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
3166 if (a[OVS_FLOW_ATTR_ACTIONS]) {
3167 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
3168 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
3170 if (a[OVS_FLOW_ATTR_STATS]) {
3171 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
3173 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
3174 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
3176 if (a[OVS_FLOW_ATTR_USED]) {
3177 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
3182 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
3183 * followed by Netlink attributes corresponding to 'flow'. */
3185 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
3188 struct ovs_header *ovs_header;
3190 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
3191 NLM_F_REQUEST | flow->nlmsg_flags,
3192 flow->cmd, OVS_FLOW_VERSION);
3194 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
3195 ovs_header->dp_ifindex = flow->dp_ifindex;
3197 if (flow->ufid_present) {
3198 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_UFID, &flow->ufid,
3201 if (flow->ufid_terse) {
3202 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UFID_FLAGS,
3203 OVS_UFID_F_OMIT_KEY | OVS_UFID_F_OMIT_MASK
3204 | OVS_UFID_F_OMIT_ACTIONS);
3206 if (!flow->ufid_terse || !flow->ufid_present) {
3207 if (flow->key_len) {
3208 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY,
3209 flow->key, flow->key_len);
3212 if (flow->mask_len) {
3213 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK,
3214 flow->mask, flow->mask_len);
3216 if (flow->actions || flow->actions_len) {
3217 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
3218 flow->actions, flow->actions_len);
3222 /* We never need to send these to the kernel. */
3223 ovs_assert(!flow->stats);
3224 ovs_assert(!flow->tcp_flags);
3225 ovs_assert(!flow->used);
3228 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
3231 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
3235 /* Clears 'flow' to "empty" values. */
3237 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
3239 memset(flow, 0, sizeof *flow);
3242 /* Executes 'request' in the kernel datapath. If the command fails, returns a
3243 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
3244 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
3245 * result of the command is expected to be a flow also, which is decoded and
3246 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
3247 * is no longer needed ('reply' will contain pointers into '*bufp'). */
3249 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
3250 struct dpif_netlink_flow *reply,
3251 struct ofpbuf **bufp)
3253 struct ofpbuf *request_buf;
3256 ovs_assert((reply != NULL) == (bufp != NULL));
3259 request->nlmsg_flags |= NLM_F_ECHO;
3262 request_buf = ofpbuf_new(1024);
3263 dpif_netlink_flow_to_ofpbuf(request, request_buf);
3264 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
3265 ofpbuf_delete(request_buf);
3269 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
3272 dpif_netlink_flow_init(reply);
3273 ofpbuf_delete(*bufp);
3281 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
3282 struct dpif_flow_stats *stats)
3285 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
3286 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
3288 stats->n_packets = 0;
3291 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
3292 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
3295 /* Logs information about a packet that was recently lost in 'ch' (in
3298 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
3299 uint32_t handler_id)
3301 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
3304 if (VLOG_DROP_WARN(&rl)) {
3309 if (ch->last_poll != LLONG_MIN) {
3310 ds_put_format(&s, " (last polled %lld ms ago)",
3311 time_msec() - ch->last_poll);
3314 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
3315 dpif_name(&dpif->dpif), ch_idx, handler_id);