2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 "dynamic-string.h"
39 #include "fat-rwlock.h"
41 #include "netdev-linux.h"
42 #include "netdev-vport.h"
43 #include "netlink-notifier.h"
44 #include "netlink-socket.h"
49 #include "poll-loop.h"
54 #include "unaligned.h"
58 VLOG_DEFINE_THIS_MODULE(dpif_netlink);
64 enum { MAX_PORTS = USHRT_MAX };
66 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
67 * missing if we have old headers. */
68 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
70 struct dpif_netlink_dp {
71 /* Generic Netlink header. */
74 /* struct ovs_header. */
78 const char *name; /* OVS_DP_ATTR_NAME. */
79 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
80 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
81 const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
82 const struct ovs_dp_megaflow_stats *megaflow_stats;
83 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
86 static void dpif_netlink_dp_init(struct dpif_netlink_dp *);
87 static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *,
88 const struct ofpbuf *);
89 static void dpif_netlink_dp_dump_start(struct nl_dump *);
90 static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
91 struct dpif_netlink_dp *reply,
92 struct ofpbuf **bufp);
93 static int dpif_netlink_dp_get(const struct dpif *,
94 struct dpif_netlink_dp *reply,
95 struct ofpbuf **bufp);
97 struct dpif_netlink_flow {
98 /* Generic Netlink header. */
101 /* struct ovs_header. */
102 unsigned int nlmsg_flags;
107 * The 'stats' member points to 64-bit data that might only be aligned on
108 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
111 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
112 * the Netlink version of the command, even if actions_len is zero. */
113 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
115 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
117 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
119 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
120 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
121 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
122 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
123 bool probe; /* OVS_FLOW_ATTR_PROBE. */
126 static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
127 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
128 const struct ofpbuf *);
129 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
131 static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
132 struct dpif_netlink_flow *reply,
133 struct ofpbuf **bufp);
134 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
135 struct dpif_flow_stats *);
136 static void dpif_netlink_flow_to_dpif_flow(struct dpif_flow *,
137 const struct dpif_netlink_flow *);
139 /* One of the dpif channels between the kernel and userspace. */
140 struct dpif_channel {
141 struct nl_sock *sock; /* Netlink socket. */
142 long long int last_poll; /* Last time this channel was polled. */
146 #define VPORT_SOCK_POOL_SIZE 1
147 /* On Windows, there is no native support for epoll. There are equivalent
148 * interfaces though, that are not used currently. For simpicity, a pool of
149 * netlink sockets is used. Each socket is represented by 'struct
150 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
151 * sharing the same socket. In the future, we can add a reference count and
153 struct dpif_windows_vport_sock {
154 struct nl_sock *nl_sock; /* netlink socket. */
158 struct dpif_handler {
159 struct dpif_channel *channels;/* Array of channels for each handler. */
160 struct epoll_event *epoll_events;
161 int epoll_fd; /* epoll fd that includes channel socks. */
162 int n_events; /* Num events returned by epoll_wait(). */
163 int event_offset; /* Offset into 'epoll_events'. */
166 /* Pool of sockets. */
167 struct dpif_windows_vport_sock *vport_sock_pool;
168 size_t last_used_pool_idx; /* Index to aid in allocating a
169 socket in the pool to a port. */
173 /* Datapath interface for the openvswitch Linux kernel module. */
174 struct dpif_netlink {
178 /* Upcall messages. */
179 struct fat_rwlock upcall_lock;
180 struct dpif_handler *handlers;
181 uint32_t n_handlers; /* Num of upcall handlers. */
182 int uc_array_size; /* Size of 'handler->channels' and */
183 /* 'handler->epoll_events'. */
185 /* Change notification. */
186 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
187 bool refresh_channels;
190 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
191 uint32_t ch_idx, uint32_t handler_id);
193 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
195 /* Generic Netlink family numbers for OVS.
197 * Initialized by dpif_netlink_init(). */
198 static int ovs_datapath_family;
199 static int ovs_vport_family;
200 static int ovs_flow_family;
201 static int ovs_packet_family;
203 /* Generic Netlink multicast groups for OVS.
205 * Initialized by dpif_netlink_init(). */
206 static unsigned int ovs_vport_mcgroup;
208 static int dpif_netlink_init(void);
209 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
210 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
211 odp_port_t port_no, uint32_t hash);
212 static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
213 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
214 uint32_t n_handlers);
215 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
217 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
218 const struct ofpbuf *);
220 static struct dpif_netlink *
221 dpif_netlink_cast(const struct dpif *dpif)
223 dpif_assert_class(dpif, &dpif_netlink_class);
224 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
228 dpif_netlink_enumerate(struct sset *all_dps,
229 const struct dpif_class *dpif_class OVS_UNUSED)
232 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
233 struct ofpbuf msg, buf;
236 error = dpif_netlink_init();
241 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
242 dpif_netlink_dp_dump_start(&dump);
243 while (nl_dump_next(&dump, &msg, &buf)) {
244 struct dpif_netlink_dp dp;
246 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
247 sset_add(all_dps, dp.name);
251 return nl_dump_done(&dump);
255 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
256 bool create, struct dpif **dpifp)
258 struct dpif_netlink_dp dp_request, dp;
263 error = dpif_netlink_init();
268 /* Create or look up datapath. */
269 dpif_netlink_dp_init(&dp_request);
271 dp_request.cmd = OVS_DP_CMD_NEW;
273 dp_request.upcall_pid = &upcall_pid;
275 /* Use OVS_DP_CMD_SET to report user features */
276 dp_request.cmd = OVS_DP_CMD_SET;
278 dp_request.name = name;
279 dp_request.user_features |= OVS_DP_F_UNALIGNED;
280 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
281 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
286 error = open_dpif(&dp, dpifp);
292 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
294 struct dpif_netlink *dpif;
296 dpif = xzalloc(sizeof *dpif);
297 dpif->port_notifier = NULL;
298 fat_rwlock_init(&dpif->upcall_lock);
300 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
301 dp->dp_ifindex, dp->dp_ifindex);
303 dpif->dp_ifindex = dp->dp_ifindex;
304 *dpifp = &dpif->dpif;
309 /* Destroys the netlink sockets pointed by the elements in 'socksp'
310 * and frees the 'socksp'. */
312 vport_del_socksp__(struct nl_sock **socksp, uint32_t n_socks)
316 for (i = 0; i < n_socks; i++) {
317 nl_sock_destroy(socksp[i]);
323 /* Creates an array of netlink sockets. Returns an array of the
324 * corresponding pointers. Records the error in 'error'. */
325 static struct nl_sock **
326 vport_create_socksp__(uint32_t n_socks, int *error)
328 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
331 for (i = 0; i < n_socks; i++) {
332 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
341 vport_del_socksp__(socksp, n_socks);
348 vport_delete_sock_pool(struct dpif_handler *handler)
349 OVS_REQ_WRLOCK(dpif->upcall_lock)
351 if (handler->vport_sock_pool) {
353 struct dpif_windows_vport_sock *sock_pool =
354 handler->vport_sock_pool;
356 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
357 if (sock_pool[i].nl_sock) {
358 nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
359 nl_sock_destroy(sock_pool[i].nl_sock);
360 sock_pool[i].nl_sock = NULL;
364 free(handler->vport_sock_pool);
365 handler->vport_sock_pool = NULL;
370 vport_create_sock_pool(struct dpif_handler *handler)
371 OVS_REQ_WRLOCK(dpif->upcall_lock)
373 struct dpif_windows_vport_sock *sock_pool;
377 sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
378 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
379 error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
384 /* Enable the netlink socket to receive packets. This is equivalent to
385 * calling nl_sock_join_mcgroup() to receive events. */
386 error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
392 handler->vport_sock_pool = sock_pool;
393 handler->last_used_pool_idx = 0;
397 vport_delete_sock_pool(handler);
401 /* Returns an array pointers to netlink sockets. The sockets are picked from a
402 * pool. Records the error in 'error'. */
403 static struct nl_sock **
404 vport_create_socksp_windows(struct dpif_netlink *dpif, int *error)
405 OVS_REQ_WRLOCK(dpif->upcall_lock)
407 uint32_t n_socks = dpif->n_handlers;
408 struct nl_sock **socksp;
411 ovs_assert(n_socks <= 1);
412 socksp = xzalloc(n_socks * sizeof *socksp);
414 /* Pick netlink sockets to use in a round-robin fashion from each
415 * handler's pool of sockets. */
416 for (i = 0; i < n_socks; i++) {
417 struct dpif_handler *handler = &dpif->handlers[i];
418 struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
419 size_t index = handler->last_used_pool_idx;
421 /* A pool of sockets is allocated when the handler is initialized. */
422 if (sock_pool == NULL) {
428 ovs_assert(index < VPORT_SOCK_POOL_SIZE);
429 socksp[i] = sock_pool[index].nl_sock;
430 socksp[i] = sock_pool[index].nl_sock;
431 ovs_assert(socksp[i]);
432 index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
433 handler->last_used_pool_idx = index;
440 vport_del_socksp_windows(struct dpif_netlink *dpif, struct nl_sock **socksp)
446 static struct nl_sock **
447 vport_create_socksp(struct dpif_netlink *dpif, int *error)
450 return vport_create_socksp_windows(dpif, error);
452 return vport_create_socksp__(dpif->n_handlers, error);
457 vport_del_socksp(struct dpif_netlink *dpif, struct nl_sock **socksp)
460 vport_del_socksp_windows(dpif, socksp);
462 vport_del_socksp__(socksp, dpif->n_handlers);
466 /* Given the array of pointers to netlink sockets 'socksp', returns
467 * the array of corresponding pids. If the 'socksp' is NULL, returns
468 * a single-element array of value 0. */
470 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
475 pids = xzalloc(sizeof *pids);
479 pids = xzalloc(n_socks * sizeof *pids);
480 for (i = 0; i < n_socks; i++) {
481 pids[i] = nl_sock_pid(socksp[i]);
488 /* Given the port number 'port_idx', extracts the pids of netlink sockets
489 * associated to the port and assigns it to 'upcall_pids'. */
491 vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
492 uint32_t **upcall_pids)
497 /* Since the nl_sock can only be assigned in either all
498 * or none "dpif->handlers" channels, the following check
500 if (!dpif->handlers[0].channels[port_idx].sock) {
503 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
505 pids = xzalloc(dpif->n_handlers * sizeof *pids);
507 for (i = 0; i < dpif->n_handlers; i++) {
508 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
517 vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
518 struct nl_sock **socksp)
520 struct epoll_event event;
521 uint32_t port_idx = odp_to_u32(port_no);
525 if (dpif->handlers == NULL) {
529 /* We assume that the datapath densely chooses port numbers, which can
530 * therefore be used as an index into 'channels' and 'epoll_events' of
531 * 'dpif->handler'. */
532 if (port_idx >= dpif->uc_array_size) {
533 uint32_t new_size = port_idx + 1;
535 if (new_size > MAX_PORTS) {
536 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
537 dpif_name(&dpif->dpif), port_no);
541 for (i = 0; i < dpif->n_handlers; i++) {
542 struct dpif_handler *handler = &dpif->handlers[i];
544 handler->channels = xrealloc(handler->channels,
545 new_size * sizeof *handler->channels);
547 for (j = dpif->uc_array_size; j < new_size; j++) {
548 handler->channels[j].sock = NULL;
551 handler->epoll_events = xrealloc(handler->epoll_events,
552 new_size * sizeof *handler->epoll_events);
555 dpif->uc_array_size = new_size;
558 memset(&event, 0, sizeof event);
559 event.events = EPOLLIN;
560 event.data.u32 = port_idx;
562 for (i = 0; i < dpif->n_handlers; i++) {
563 struct dpif_handler *handler = &dpif->handlers[i];
566 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
572 dpif->handlers[i].channels[port_idx].sock = socksp[i];
573 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
579 for (j = 0; j < i; j++) {
581 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
582 nl_sock_fd(socksp[j]), NULL);
584 dpif->handlers[j].channels[port_idx].sock = NULL;
591 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
593 uint32_t port_idx = odp_to_u32(port_no);
596 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
600 /* Since the sock can only be assigned in either all or none
601 * of "dpif->handlers" channels, the following check would
603 if (!dpif->handlers[0].channels[port_idx].sock) {
607 for (i = 0; i < dpif->n_handlers; i++) {
608 struct dpif_handler *handler = &dpif->handlers[i];
610 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
611 nl_sock_fd(handler->channels[port_idx].sock), NULL);
612 nl_sock_destroy(handler->channels[port_idx].sock);
614 handler->channels[port_idx].sock = NULL;
615 handler->event_offset = handler->n_events = 0;
620 destroy_all_channels(struct dpif_netlink *dpif)
621 OVS_REQ_WRLOCK(dpif->upcall_lock)
625 if (!dpif->handlers) {
629 for (i = 0; i < dpif->uc_array_size; i++ ) {
630 struct dpif_netlink_vport vport_request;
631 uint32_t upcall_pids = 0;
633 /* Since the sock can only be assigned in either all or none
634 * of "dpif->handlers" channels, the following check would
636 if (!dpif->handlers[0].channels[i].sock) {
640 /* Turn off upcalls. */
641 dpif_netlink_vport_init(&vport_request);
642 vport_request.cmd = OVS_VPORT_CMD_SET;
643 vport_request.dp_ifindex = dpif->dp_ifindex;
644 vport_request.port_no = u32_to_odp(i);
645 vport_request.upcall_pids = &upcall_pids;
646 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
648 vport_del_channels(dpif, u32_to_odp(i));
651 for (i = 0; i < dpif->n_handlers; i++) {
652 struct dpif_handler *handler = &dpif->handlers[i];
654 dpif_netlink_handler_uninit(handler);
655 free(handler->epoll_events);
656 free(handler->channels);
659 free(dpif->handlers);
660 dpif->handlers = NULL;
661 dpif->n_handlers = 0;
662 dpif->uc_array_size = 0;
666 dpif_netlink_close(struct dpif *dpif_)
668 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
670 nl_sock_destroy(dpif->port_notifier);
672 fat_rwlock_wrlock(&dpif->upcall_lock);
673 destroy_all_channels(dpif);
674 fat_rwlock_unlock(&dpif->upcall_lock);
676 fat_rwlock_destroy(&dpif->upcall_lock);
681 dpif_netlink_destroy(struct dpif *dpif_)
683 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
684 struct dpif_netlink_dp dp;
686 dpif_netlink_dp_init(&dp);
687 dp.cmd = OVS_DP_CMD_DEL;
688 dp.dp_ifindex = dpif->dp_ifindex;
689 return dpif_netlink_dp_transact(&dp, NULL, NULL);
693 dpif_netlink_run(struct dpif *dpif_)
695 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
697 if (dpif->refresh_channels) {
698 dpif->refresh_channels = false;
699 fat_rwlock_wrlock(&dpif->upcall_lock);
700 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
701 fat_rwlock_unlock(&dpif->upcall_lock);
706 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
708 struct dpif_netlink_dp dp;
712 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
714 memset(stats, 0, sizeof *stats);
717 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
718 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
719 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
720 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
723 if (dp.megaflow_stats) {
724 stats->n_masks = dp.megaflow_stats->n_masks;
725 stats->n_mask_hit = get_32aligned_u64(
726 &dp.megaflow_stats->n_mask_hit);
728 stats->n_masks = UINT32_MAX;
729 stats->n_mask_hit = UINT64_MAX;
737 get_vport_type(const struct dpif_netlink_vport *vport)
739 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
741 switch (vport->type) {
742 case OVS_VPORT_TYPE_NETDEV: {
743 const char *type = netdev_get_type_from_name(vport->name);
745 return type ? type : "system";
748 case OVS_VPORT_TYPE_INTERNAL:
751 case OVS_VPORT_TYPE_GENEVE:
754 case OVS_VPORT_TYPE_GRE:
757 case OVS_VPORT_TYPE_GRE64:
760 case OVS_VPORT_TYPE_VXLAN:
763 case OVS_VPORT_TYPE_LISP:
766 case OVS_VPORT_TYPE_UNSPEC:
767 case __OVS_VPORT_TYPE_MAX:
771 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
772 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
776 static enum ovs_vport_type
777 netdev_to_ovs_vport_type(const struct netdev *netdev)
779 const char *type = netdev_get_type(netdev);
781 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
782 return OVS_VPORT_TYPE_NETDEV;
783 } else if (!strcmp(type, "internal")) {
784 return OVS_VPORT_TYPE_INTERNAL;
785 } else if (!strcmp(type, "geneve")) {
786 return OVS_VPORT_TYPE_GENEVE;
787 } else if (strstr(type, "gre64")) {
788 return OVS_VPORT_TYPE_GRE64;
789 } else if (strstr(type, "gre")) {
790 return OVS_VPORT_TYPE_GRE;
791 } else if (!strcmp(type, "vxlan")) {
792 return OVS_VPORT_TYPE_VXLAN;
793 } else if (!strcmp(type, "lisp")) {
794 return OVS_VPORT_TYPE_LISP;
796 return OVS_VPORT_TYPE_UNSPEC;
801 dpif_netlink_port_add__(struct dpif_netlink *dpif, struct netdev *netdev,
802 odp_port_t *port_nop)
803 OVS_REQ_WRLOCK(dpif->upcall_lock)
805 const struct netdev_tunnel_config *tnl_cfg;
806 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
807 const char *name = netdev_vport_get_dpif_port(netdev,
808 namebuf, sizeof namebuf);
809 const char *type = netdev_get_type(netdev);
810 struct dpif_netlink_vport request, reply;
812 uint64_t options_stub[64 / 8];
813 struct ofpbuf options;
814 struct nl_sock **socksp = NULL;
815 uint32_t *upcall_pids;
818 if (dpif->handlers) {
819 socksp = vport_create_socksp(dpif, &error);
825 dpif_netlink_vport_init(&request);
826 request.cmd = OVS_VPORT_CMD_NEW;
827 request.dp_ifindex = dpif->dp_ifindex;
828 request.type = netdev_to_ovs_vport_type(netdev);
829 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
830 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
831 "unsupported type `%s'",
832 dpif_name(&dpif->dpif), name, type);
833 vport_del_socksp(dpif, socksp);
838 if (request.type == OVS_VPORT_TYPE_NETDEV) {
840 /* XXX : Map appropiate Windows handle */
842 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
846 tnl_cfg = netdev_get_tunnel_config(netdev);
847 if (tnl_cfg && tnl_cfg->dst_port != 0) {
848 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
849 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
850 ntohs(tnl_cfg->dst_port));
851 request.options = ofpbuf_data(&options);
852 request.options_len = ofpbuf_size(&options);
855 request.port_no = *port_nop;
856 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
857 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
858 request.upcall_pids = upcall_pids;
860 error = dpif_netlink_vport_transact(&request, &reply, &buf);
862 *port_nop = reply.port_no;
864 if (error == EBUSY && *port_nop != ODPP_NONE) {
865 VLOG_INFO("%s: requested port %"PRIu32" is in use",
866 dpif_name(&dpif->dpif), *port_nop);
869 vport_del_socksp(dpif, socksp);
874 error = vport_add_channels(dpif, *port_nop, socksp);
876 VLOG_INFO("%s: could not add channel for port %s",
877 dpif_name(&dpif->dpif), name);
879 /* Delete the port. */
880 dpif_netlink_vport_init(&request);
881 request.cmd = OVS_VPORT_CMD_DEL;
882 request.dp_ifindex = dpif->dp_ifindex;
883 request.port_no = *port_nop;
884 dpif_netlink_vport_transact(&request, NULL, NULL);
885 vport_del_socksp(dpif, socksp);
899 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
900 odp_port_t *port_nop)
902 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
905 fat_rwlock_wrlock(&dpif->upcall_lock);
906 error = dpif_netlink_port_add__(dpif, netdev, port_nop);
907 fat_rwlock_unlock(&dpif->upcall_lock);
913 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
914 OVS_REQ_WRLOCK(dpif->upcall_lock)
916 struct dpif_netlink_vport vport;
919 dpif_netlink_vport_init(&vport);
920 vport.cmd = OVS_VPORT_CMD_DEL;
921 vport.dp_ifindex = dpif->dp_ifindex;
922 vport.port_no = port_no;
923 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
925 vport_del_channels(dpif, port_no);
931 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
933 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
936 fat_rwlock_wrlock(&dpif->upcall_lock);
937 error = dpif_netlink_port_del__(dpif, port_no);
938 fat_rwlock_unlock(&dpif->upcall_lock);
944 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
945 const char *port_name, struct dpif_port *dpif_port)
947 struct dpif_netlink_vport request;
948 struct dpif_netlink_vport reply;
952 dpif_netlink_vport_init(&request);
953 request.cmd = OVS_VPORT_CMD_GET;
954 request.dp_ifindex = dpif->dp_ifindex;
955 request.port_no = port_no;
956 request.name = port_name;
958 error = dpif_netlink_vport_transact(&request, &reply, &buf);
960 if (reply.dp_ifindex != request.dp_ifindex) {
961 /* A query by name reported that 'port_name' is in some datapath
962 * other than 'dpif', but the caller wants to know about 'dpif'. */
964 } else if (dpif_port) {
965 dpif_port->name = xstrdup(reply.name);
966 dpif_port->type = xstrdup(get_vport_type(&reply));
967 dpif_port->port_no = reply.port_no;
975 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
976 struct dpif_port *dpif_port)
978 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
980 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
984 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
985 struct dpif_port *dpif_port)
987 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
989 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
993 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
994 odp_port_t port_no, uint32_t hash)
995 OVS_REQ_RDLOCK(dpif->upcall_lock)
997 uint32_t port_idx = odp_to_u32(port_no);
1000 if (dpif->handlers && dpif->uc_array_size > 0) {
1001 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1002 * channel, since it is not heavily loaded. */
1003 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
1004 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
1006 /* Needs to check in case the socket pointer is changed in between
1007 * the holding of upcall_lock. A known case happens when the main
1008 * thread deletes the vport while the handler thread is handling
1009 * the upcall from that port. */
1010 if (h->channels[idx].sock) {
1011 pid = nl_sock_pid(h->channels[idx].sock);
1019 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
1022 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1025 fat_rwlock_rdlock(&dpif->upcall_lock);
1026 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
1027 fat_rwlock_unlock(&dpif->upcall_lock);
1033 dpif_netlink_flow_flush(struct dpif *dpif_)
1035 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1036 struct dpif_netlink_flow flow;
1038 dpif_netlink_flow_init(&flow);
1039 flow.cmd = OVS_FLOW_CMD_DEL;
1040 flow.dp_ifindex = dpif->dp_ifindex;
1041 return dpif_netlink_flow_transact(&flow, NULL, NULL);
1044 struct dpif_netlink_port_state {
1045 struct nl_dump dump;
1050 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
1051 struct nl_dump *dump)
1053 struct dpif_netlink_vport request;
1056 dpif_netlink_vport_init(&request);
1057 request.cmd = OVS_VPORT_CMD_GET;
1058 request.dp_ifindex = dpif->dp_ifindex;
1060 buf = ofpbuf_new(1024);
1061 dpif_netlink_vport_to_ofpbuf(&request, buf);
1062 nl_dump_start(dump, NETLINK_GENERIC, buf);
1067 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
1069 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1070 struct dpif_netlink_port_state *state;
1072 *statep = state = xmalloc(sizeof *state);
1073 dpif_netlink_port_dump_start__(dpif, &state->dump);
1075 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
1080 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
1081 struct nl_dump *dump,
1082 struct dpif_netlink_vport *vport,
1083 struct ofpbuf *buffer)
1088 if (!nl_dump_next(dump, &buf, buffer)) {
1092 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
1094 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
1095 dpif_name(&dpif->dpif), ovs_strerror(error));
1101 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
1102 struct dpif_port *dpif_port)
1104 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1105 struct dpif_netlink_port_state *state = state_;
1106 struct dpif_netlink_vport vport;
1109 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
1114 dpif_port->name = CONST_CAST(char *, vport.name);
1115 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
1116 dpif_port->port_no = vport.port_no;
1121 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
1123 struct dpif_netlink_port_state *state = state_;
1124 int error = nl_dump_done(&state->dump);
1126 ofpbuf_uninit(&state->buf);
1132 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1134 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1136 /* Lazily create the Netlink socket to listen for notifications. */
1137 if (!dpif->port_notifier) {
1138 struct nl_sock *sock;
1141 error = nl_sock_create(NETLINK_GENERIC, &sock);
1146 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1148 nl_sock_destroy(sock);
1151 dpif->port_notifier = sock;
1153 /* We have no idea of the current state so report that everything
1159 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1160 uint64_t buf_stub[4096 / 8];
1164 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1165 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1167 struct dpif_netlink_vport vport;
1169 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1171 if (vport.dp_ifindex == dpif->dp_ifindex
1172 && (vport.cmd == OVS_VPORT_CMD_NEW
1173 || vport.cmd == OVS_VPORT_CMD_DEL
1174 || vport.cmd == OVS_VPORT_CMD_SET)) {
1175 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1176 dpif->dpif.full_name, vport.name, vport.cmd);
1177 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1178 dpif->refresh_channels = true;
1180 *devnamep = xstrdup(vport.name);
1181 ofpbuf_uninit(&buf);
1185 } else if (error != EAGAIN) {
1186 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1187 ovs_strerror(error));
1188 nl_sock_drain(dpif->port_notifier);
1192 ofpbuf_uninit(&buf);
1200 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1202 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1204 if (dpif->port_notifier) {
1205 nl_sock_wait(dpif->port_notifier, POLLIN);
1207 poll_immediate_wake();
1212 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1213 const struct nlattr *key, size_t key_len,
1214 struct dpif_netlink_flow *request)
1216 dpif_netlink_flow_init(request);
1217 request->cmd = OVS_FLOW_CMD_GET;
1218 request->dp_ifindex = dpif->dp_ifindex;
1220 request->key_len = key_len;
1224 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1225 const struct nlattr *key, size_t key_len,
1226 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1228 struct dpif_netlink_flow request;
1230 dpif_netlink_init_flow_get(dpif, key, key_len, &request);
1231 return dpif_netlink_flow_transact(&request, reply, bufp);
1235 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1236 const struct dpif_flow_put *put,
1237 struct dpif_netlink_flow *request)
1239 static const struct nlattr dummy_action;
1241 dpif_netlink_flow_init(request);
1242 request->cmd = (put->flags & DPIF_FP_CREATE
1243 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1244 request->dp_ifindex = dpif->dp_ifindex;
1245 request->key = put->key;
1246 request->key_len = put->key_len;
1247 request->mask = put->mask;
1248 request->mask_len = put->mask_len;
1249 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1250 request->actions = (put->actions
1252 : CONST_CAST(struct nlattr *, &dummy_action));
1253 request->actions_len = put->actions_len;
1254 if (put->flags & DPIF_FP_ZERO_STATS) {
1255 request->clear = true;
1257 if (put->flags & DPIF_FP_PROBE) {
1258 request->probe = true;
1260 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1264 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1265 const struct dpif_flow_del *del,
1266 struct dpif_netlink_flow *request)
1268 dpif_netlink_flow_init(request);
1269 request->cmd = OVS_FLOW_CMD_DEL;
1270 request->dp_ifindex = dpif->dp_ifindex;
1271 request->key = del->key;
1272 request->key_len = del->key_len;
1275 struct dpif_netlink_flow_dump {
1276 struct dpif_flow_dump up;
1277 struct nl_dump nl_dump;
1281 static struct dpif_netlink_flow_dump *
1282 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1284 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1287 static struct dpif_flow_dump *
1288 dpif_netlink_flow_dump_create(const struct dpif *dpif_)
1290 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1291 struct dpif_netlink_flow_dump *dump;
1292 struct dpif_netlink_flow request;
1295 dump = xmalloc(sizeof *dump);
1296 dpif_flow_dump_init(&dump->up, dpif_);
1298 dpif_netlink_flow_init(&request);
1299 request.cmd = OVS_FLOW_CMD_GET;
1300 request.dp_ifindex = dpif->dp_ifindex;
1302 buf = ofpbuf_new(1024);
1303 dpif_netlink_flow_to_ofpbuf(&request, buf);
1304 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1306 atomic_init(&dump->status, 0);
1312 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1314 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1315 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1318 /* No other thread has access to 'dump' at this point. */
1319 atomic_read_relaxed(&dump->status, &dump_status);
1321 return dump_status ? dump_status : nl_status;
1324 struct dpif_netlink_flow_dump_thread {
1325 struct dpif_flow_dump_thread up;
1326 struct dpif_netlink_flow_dump *dump;
1327 struct dpif_netlink_flow flow;
1328 struct dpif_flow_stats stats;
1329 struct ofpbuf nl_flows; /* Always used to store flows. */
1330 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1333 static struct dpif_netlink_flow_dump_thread *
1334 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1336 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1339 static struct dpif_flow_dump_thread *
1340 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1342 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1343 struct dpif_netlink_flow_dump_thread *thread;
1345 thread = xmalloc(sizeof *thread);
1346 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1347 thread->dump = dump;
1348 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1349 thread->nl_actions = NULL;
1355 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1357 struct dpif_netlink_flow_dump_thread *thread
1358 = dpif_netlink_flow_dump_thread_cast(thread_);
1360 ofpbuf_uninit(&thread->nl_flows);
1361 ofpbuf_delete(thread->nl_actions);
1366 dpif_netlink_flow_to_dpif_flow(struct dpif_flow *dpif_flow,
1367 const struct dpif_netlink_flow *datapath_flow)
1369 dpif_flow->key = datapath_flow->key;
1370 dpif_flow->key_len = datapath_flow->key_len;
1371 dpif_flow->mask = datapath_flow->mask;
1372 dpif_flow->mask_len = datapath_flow->mask_len;
1373 dpif_flow->actions = datapath_flow->actions;
1374 dpif_flow->actions_len = datapath_flow->actions_len;
1375 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1379 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1380 struct dpif_flow *flows, int max_flows)
1382 struct dpif_netlink_flow_dump_thread *thread
1383 = dpif_netlink_flow_dump_thread_cast(thread_);
1384 struct dpif_netlink_flow_dump *dump = thread->dump;
1385 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1388 ofpbuf_delete(thread->nl_actions);
1389 thread->nl_actions = NULL;
1393 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1394 struct dpif_netlink_flow datapath_flow;
1395 struct ofpbuf nl_flow;
1398 /* Try to grab another flow. */
1399 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1403 /* Convert the flow to our output format. */
1404 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1406 atomic_store_relaxed(&dump->status, error);
1410 if (datapath_flow.actions) {
1411 /* Common case: the flow includes actions. */
1412 dpif_netlink_flow_to_dpif_flow(&flows[n_flows++], &datapath_flow);
1414 /* Rare case: the flow does not include actions. Retrieve this
1415 * individual flow again to get the actions. */
1416 error = dpif_netlink_flow_get(dpif, datapath_flow.key,
1417 datapath_flow.key_len,
1418 &datapath_flow, &thread->nl_actions);
1419 if (error == ENOENT) {
1420 VLOG_DBG("dumped flow disappeared on get");
1423 VLOG_WARN("error fetching dumped flow: %s",
1424 ovs_strerror(error));
1425 atomic_store_relaxed(&dump->status, error);
1429 /* Save this flow. Then exit, because we only have one buffer to
1430 * handle this case. */
1431 dpif_netlink_flow_to_dpif_flow(&flows[n_flows++], &datapath_flow);
1439 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1442 struct ovs_header *k_exec;
1445 ofpbuf_prealloc_tailroom(buf, (64
1446 + ofpbuf_size(d_exec->packet)
1447 + ODP_KEY_METADATA_SIZE
1448 + d_exec->actions_len));
1450 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1451 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1453 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1454 k_exec->dp_ifindex = dp_ifindex;
1456 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1457 ofpbuf_data(d_exec->packet),
1458 ofpbuf_size(d_exec->packet));
1460 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1461 odp_key_from_pkt_metadata(buf, &d_exec->md);
1462 nl_msg_end_nested(buf, key_ofs);
1464 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1465 d_exec->actions, d_exec->actions_len);
1466 if (d_exec->probe) {
1467 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
1474 dpif_netlink_operate__(struct dpif_netlink *dpif,
1475 struct dpif_op **ops, size_t n_ops)
1478 struct nl_transaction txn;
1480 struct ofpbuf request;
1481 uint64_t request_stub[1024 / 8];
1483 struct ofpbuf reply;
1484 uint64_t reply_stub[1024 / 8];
1487 struct nl_transaction *txnsp[MAX_OPS];
1490 ovs_assert(n_ops <= MAX_OPS);
1491 for (i = 0; i < n_ops; i++) {
1492 struct op_auxdata *aux = &auxes[i];
1493 struct dpif_op *op = ops[i];
1494 struct dpif_flow_put *put;
1495 struct dpif_flow_del *del;
1496 struct dpif_execute *execute;
1497 struct dpif_flow_get *get;
1498 struct dpif_netlink_flow flow;
1500 ofpbuf_use_stub(&aux->request,
1501 aux->request_stub, sizeof aux->request_stub);
1502 aux->txn.request = &aux->request;
1504 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1505 aux->txn.reply = NULL;
1508 case DPIF_OP_FLOW_PUT:
1509 put = &op->u.flow_put;
1510 dpif_netlink_init_flow_put(dpif, put, &flow);
1512 flow.nlmsg_flags |= NLM_F_ECHO;
1513 aux->txn.reply = &aux->reply;
1515 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1518 case DPIF_OP_FLOW_DEL:
1519 del = &op->u.flow_del;
1520 dpif_netlink_init_flow_del(dpif, del, &flow);
1522 flow.nlmsg_flags |= NLM_F_ECHO;
1523 aux->txn.reply = &aux->reply;
1525 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1528 case DPIF_OP_EXECUTE:
1529 execute = &op->u.execute;
1530 dpif_netlink_encode_execute(dpif->dp_ifindex, execute,
1534 case DPIF_OP_FLOW_GET:
1535 get = &op->u.flow_get;
1536 dpif_netlink_init_flow_get(dpif, get->key, get->key_len, &flow);
1537 aux->txn.reply = get->buffer;
1538 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1546 for (i = 0; i < n_ops; i++) {
1547 txnsp[i] = &auxes[i].txn;
1549 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1551 for (i = 0; i < n_ops; i++) {
1552 struct op_auxdata *aux = &auxes[i];
1553 struct nl_transaction *txn = &auxes[i].txn;
1554 struct dpif_op *op = ops[i];
1555 struct dpif_flow_put *put;
1556 struct dpif_flow_del *del;
1557 struct dpif_flow_get *get;
1559 op->error = txn->error;
1562 case DPIF_OP_FLOW_PUT:
1563 put = &op->u.flow_put;
1566 struct dpif_netlink_flow reply;
1568 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1571 dpif_netlink_flow_get_stats(&reply, put->stats);
1577 case DPIF_OP_FLOW_DEL:
1578 del = &op->u.flow_del;
1581 struct dpif_netlink_flow reply;
1583 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1586 dpif_netlink_flow_get_stats(&reply, del->stats);
1592 case DPIF_OP_EXECUTE:
1595 case DPIF_OP_FLOW_GET:
1596 get = &op->u.flow_get;
1598 struct dpif_netlink_flow reply;
1600 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
1602 dpif_netlink_flow_to_dpif_flow(get->flow, &reply);
1611 ofpbuf_uninit(&aux->request);
1612 ofpbuf_uninit(&aux->reply);
1617 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1619 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1622 size_t chunk = MIN(n_ops, MAX_OPS);
1623 dpif_netlink_operate__(dpif, ops, chunk);
1631 dpif_netlink_handler_uninit(struct dpif_handler *handler)
1633 vport_delete_sock_pool(handler);
1637 dpif_netlink_handler_init(struct dpif_handler *handler)
1639 return vport_create_sock_pool(handler);
1644 dpif_netlink_handler_init(struct dpif_handler *handler)
1646 handler->epoll_fd = epoll_create(10);
1647 return handler->epoll_fd < 0 ? errno : 0;
1651 dpif_netlink_handler_uninit(struct dpif_handler *handler)
1653 close(handler->epoll_fd);
1657 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1658 * currently in 'dpif' in the kernel, by adding a new set of channels for
1659 * any kernel vport that lacks one and deleting any channels that have no
1660 * backing kernel vports. */
1662 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
1663 OVS_REQ_WRLOCK(dpif->upcall_lock)
1665 unsigned long int *keep_channels;
1666 struct dpif_netlink_vport vport;
1667 size_t keep_channels_nbits;
1668 struct nl_dump dump;
1669 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1674 ovs_assert(!WINDOWS || n_handlers <= 1);
1675 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
1677 if (dpif->n_handlers != n_handlers) {
1678 destroy_all_channels(dpif);
1679 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1680 for (i = 0; i < n_handlers; i++) {
1682 struct dpif_handler *handler = &dpif->handlers[i];
1684 error = dpif_netlink_handler_init(handler);
1687 struct dpif_handler *tmp = &dpif->handlers[i];
1690 for (j = 0; j < i; j++) {
1691 dpif_netlink_handler_uninit(tmp);
1693 free(dpif->handlers);
1694 dpif->handlers = NULL;
1699 dpif->n_handlers = n_handlers;
1702 for (i = 0; i < n_handlers; i++) {
1703 struct dpif_handler *handler = &dpif->handlers[i];
1705 handler->event_offset = handler->n_events = 0;
1708 keep_channels_nbits = dpif->uc_array_size;
1709 keep_channels = bitmap_allocate(keep_channels_nbits);
1711 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1712 dpif_netlink_port_dump_start__(dpif, &dump);
1713 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
1714 uint32_t port_no = odp_to_u32(vport.port_no);
1715 uint32_t *upcall_pids = NULL;
1718 if (port_no >= dpif->uc_array_size
1719 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1720 struct nl_sock **socksp = vport_create_socksp(dpif, &error);
1726 error = vport_add_channels(dpif, vport.port_no, socksp);
1728 VLOG_INFO("%s: could not add channels for port %s",
1729 dpif_name(&dpif->dpif), vport.name);
1730 vport_del_socksp(dpif, socksp);
1734 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1738 /* Configure the vport to deliver misses to 'sock'. */
1739 if (vport.upcall_pids[0] == 0
1740 || vport.n_upcall_pids != dpif->n_handlers
1741 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1743 struct dpif_netlink_vport vport_request;
1745 dpif_netlink_vport_init(&vport_request);
1746 vport_request.cmd = OVS_VPORT_CMD_SET;
1747 vport_request.dp_ifindex = dpif->dp_ifindex;
1748 vport_request.port_no = vport.port_no;
1749 vport_request.n_upcall_pids = dpif->n_handlers;
1750 vport_request.upcall_pids = upcall_pids;
1751 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
1753 VLOG_WARN_RL(&error_rl,
1754 "%s: failed to set upcall pid on port: %s",
1755 dpif_name(&dpif->dpif), ovs_strerror(error));
1757 if (error != ENODEV && error != ENOENT) {
1760 /* The vport isn't really there, even though the dump says
1761 * it is. Probably we just hit a race after a port
1768 if (port_no < keep_channels_nbits) {
1769 bitmap_set1(keep_channels, port_no);
1776 vport_del_channels(dpif, vport.port_no);
1778 nl_dump_done(&dump);
1779 ofpbuf_uninit(&buf);
1781 /* Discard any saved channels that we didn't reuse. */
1782 for (i = 0; i < keep_channels_nbits; i++) {
1783 if (!bitmap_is_set(keep_channels, i)) {
1784 vport_del_channels(dpif, u32_to_odp(i));
1787 free(keep_channels);
1793 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
1794 OVS_REQ_WRLOCK(dpif->upcall_lock)
1796 if ((dpif->handlers != NULL) == enable) {
1798 } else if (!enable) {
1799 destroy_all_channels(dpif);
1802 return dpif_netlink_refresh_channels(dpif, 1);
1807 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
1809 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1812 fat_rwlock_wrlock(&dpif->upcall_lock);
1813 error = dpif_netlink_recv_set__(dpif, enable);
1814 fat_rwlock_unlock(&dpif->upcall_lock);
1820 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1822 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1826 /* Multiple upcall handlers will be supported once kernel datapath supports
1828 if (n_handlers > 1) {
1833 fat_rwlock_wrlock(&dpif->upcall_lock);
1834 if (dpif->handlers) {
1835 error = dpif_netlink_refresh_channels(dpif, n_handlers);
1837 fat_rwlock_unlock(&dpif->upcall_lock);
1843 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1844 uint32_t queue_id, uint32_t *priority)
1846 if (queue_id < 0xf000) {
1847 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1855 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1858 static const struct nl_policy ovs_packet_policy[] = {
1859 /* Always present. */
1860 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1861 .min_len = ETH_HEADER_LEN },
1862 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1864 /* OVS_PACKET_CMD_ACTION only. */
1865 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1866 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
1869 struct ovs_header *ovs_header;
1870 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1871 struct nlmsghdr *nlmsg;
1872 struct genlmsghdr *genl;
1876 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1878 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1879 genl = ofpbuf_try_pull(&b, sizeof *genl);
1880 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1881 if (!nlmsg || !genl || !ovs_header
1882 || nlmsg->nlmsg_type != ovs_packet_family
1883 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1884 ARRAY_SIZE(ovs_packet_policy))) {
1888 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1889 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1895 /* (Re)set ALL fields of '*upcall' on successful return. */
1896 upcall->type = type;
1897 upcall->key = CONST_CAST(struct nlattr *,
1898 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1899 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1900 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1901 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
1903 /* Allow overwriting the netlink attribute header without reallocating. */
1904 ofpbuf_use_stub(&upcall->packet,
1905 CONST_CAST(struct nlattr *,
1906 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1907 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1908 sizeof(struct nlattr));
1909 ofpbuf_set_data(&upcall->packet,
1910 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1911 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1913 *dp_ifindex = ovs_header->dp_ifindex;
1919 #define PACKET_RECV_BATCH_SIZE 50
1921 dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id,
1922 struct dpif_upcall *upcall, struct ofpbuf *buf)
1923 OVS_REQ_RDLOCK(dpif->upcall_lock)
1925 struct dpif_handler *handler;
1927 struct dpif_windows_vport_sock *sock_pool;
1930 if (!dpif->handlers) {
1934 /* Only one handler is supported currently. */
1935 if (handler_id >= 1) {
1939 if (handler_id >= dpif->n_handlers) {
1943 handler = &dpif->handlers[handler_id];
1944 sock_pool = handler->vport_sock_pool;
1946 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
1951 if (++read_tries > PACKET_RECV_BATCH_SIZE) {
1955 error = nl_sock_recv(sock_pool[i].nl_sock, buf, false);
1956 if (error == ENOBUFS) {
1957 /* ENOBUFS typically means that we've received so many
1958 * packets that the buffer overflowed. Try again
1959 * immediately because there's almost certainly a packet
1960 * waiting for us. */
1961 /* XXX: report_loss(dpif, ch, idx, handler_id); */
1965 /* XXX: ch->last_poll = time_msec(); */
1967 if (error == EAGAIN) {
1973 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1974 if (!error && dp_ifindex == dpif->dp_ifindex) {
1986 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
1987 struct dpif_upcall *upcall, struct ofpbuf *buf)
1988 OVS_REQ_RDLOCK(dpif->upcall_lock)
1990 struct dpif_handler *handler;
1993 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1997 handler = &dpif->handlers[handler_id];
1998 if (handler->event_offset >= handler->n_events) {
2001 handler->event_offset = handler->n_events = 0;
2004 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
2005 dpif->uc_array_size, 0);
2006 } while (retval < 0 && errno == EINTR);
2009 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2010 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
2011 } else if (retval > 0) {
2012 handler->n_events = retval;
2016 while (handler->event_offset < handler->n_events) {
2017 int idx = handler->epoll_events[handler->event_offset].data.u32;
2018 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
2020 handler->event_offset++;
2026 if (++read_tries > 50) {
2030 error = nl_sock_recv(ch->sock, buf, false);
2031 if (error == ENOBUFS) {
2032 /* ENOBUFS typically means that we've received so many
2033 * packets that the buffer overflowed. Try again
2034 * immediately because there's almost certainly a packet
2035 * waiting for us. */
2036 report_loss(dpif, ch, idx, handler_id);
2040 ch->last_poll = time_msec();
2042 if (error == EAGAIN) {
2048 error = parse_odp_packet(buf, upcall, &dp_ifindex);
2049 if (!error && dp_ifindex == dpif->dp_ifindex) {
2062 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
2063 struct dpif_upcall *upcall, struct ofpbuf *buf)
2065 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2068 fat_rwlock_rdlock(&dpif->upcall_lock);
2070 error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf);
2072 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
2074 fat_rwlock_unlock(&dpif->upcall_lock);
2080 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
2081 OVS_REQ_RDLOCK(dpif->upcall_lock)
2085 struct dpif_windows_vport_sock *sock_pool =
2086 dpif->handlers[handler_id].vport_sock_pool;
2088 /* Only one handler is supported currently. */
2089 if (handler_id >= 1) {
2093 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2094 nl_sock_wait(sock_pool[i].nl_sock, POLLIN);
2097 if (dpif->handlers && handler_id < dpif->n_handlers) {
2098 struct dpif_handler *handler = &dpif->handlers[handler_id];
2100 poll_fd_wait(handler->epoll_fd, POLLIN);
2106 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
2108 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2110 fat_rwlock_rdlock(&dpif->upcall_lock);
2111 dpif_netlink_recv_wait__(dpif, handler_id);
2112 fat_rwlock_unlock(&dpif->upcall_lock);
2116 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
2117 OVS_REQ_WRLOCK(dpif->upcall_lock)
2119 if (dpif->handlers) {
2122 for (i = 0; i < dpif->uc_array_size; i++ ) {
2123 if (!dpif->handlers[0].channels[i].sock) {
2127 for (j = 0; j < dpif->n_handlers; j++) {
2128 nl_sock_drain(dpif->handlers[j].channels[i].sock);
2135 dpif_netlink_recv_purge(struct dpif *dpif_)
2137 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2139 fat_rwlock_wrlock(&dpif->upcall_lock);
2140 dpif_netlink_recv_purge__(dpif);
2141 fat_rwlock_unlock(&dpif->upcall_lock);
2145 dpif_netlink_get_datapath_version(void)
2147 char *version_str = NULL;
2151 #define MAX_VERSION_STR_SIZE 80
2152 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2155 f = fopen(LINUX_DATAPATH_VERSION_FILE, "r");
2158 char version[MAX_VERSION_STR_SIZE];
2160 if (fgets(version, MAX_VERSION_STR_SIZE, f)) {
2161 newline = strchr(version, '\n');
2165 version_str = xstrdup(version);
2174 const struct dpif_class dpif_netlink_class = {
2176 dpif_netlink_enumerate,
2180 dpif_netlink_destroy,
2183 dpif_netlink_get_stats,
2184 dpif_netlink_port_add,
2185 dpif_netlink_port_del,
2186 dpif_netlink_port_query_by_number,
2187 dpif_netlink_port_query_by_name,
2188 dpif_netlink_port_get_pid,
2189 dpif_netlink_port_dump_start,
2190 dpif_netlink_port_dump_next,
2191 dpif_netlink_port_dump_done,
2192 dpif_netlink_port_poll,
2193 dpif_netlink_port_poll_wait,
2194 dpif_netlink_flow_flush,
2195 dpif_netlink_flow_dump_create,
2196 dpif_netlink_flow_dump_destroy,
2197 dpif_netlink_flow_dump_thread_create,
2198 dpif_netlink_flow_dump_thread_destroy,
2199 dpif_netlink_flow_dump_next,
2200 dpif_netlink_operate,
2201 dpif_netlink_recv_set,
2202 dpif_netlink_handlers_set,
2203 NULL, /* poll_thread_set */
2204 dpif_netlink_queue_to_priority,
2206 dpif_netlink_recv_wait,
2207 dpif_netlink_recv_purge,
2208 NULL, /* register_upcall_cb */
2209 NULL, /* enable_upcall */
2210 NULL, /* disable_upcall */
2211 dpif_netlink_get_datapath_version, /* get_datapath_version */
2215 dpif_netlink_init(void)
2217 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2220 if (ovsthread_once_start(&once)) {
2221 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
2222 &ovs_datapath_family);
2224 VLOG_ERR("Generic Netlink family '%s' does not exist. "
2225 "The Open vSwitch kernel module is probably not loaded.",
2226 OVS_DATAPATH_FAMILY);
2229 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
2232 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
2235 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
2236 &ovs_packet_family);
2239 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
2240 &ovs_vport_mcgroup);
2243 ovsthread_once_done(&once);
2250 dpif_netlink_is_internal_device(const char *name)
2252 struct dpif_netlink_vport reply;
2256 error = dpif_netlink_vport_get(name, &reply, &buf);
2259 } else if (error != ENODEV && error != ENOENT) {
2260 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
2261 name, ovs_strerror(error));
2264 return reply.type == OVS_VPORT_TYPE_INTERNAL;
2267 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2268 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
2269 * positive errno value.
2271 * 'vport' will contain pointers into 'buf', so the caller should not free
2272 * 'buf' while 'vport' is still in use. */
2274 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
2275 const struct ofpbuf *buf)
2277 static const struct nl_policy ovs_vport_policy[] = {
2278 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2279 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2280 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2281 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2282 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2284 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2287 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2288 struct ovs_header *ovs_header;
2289 struct nlmsghdr *nlmsg;
2290 struct genlmsghdr *genl;
2293 dpif_netlink_vport_init(vport);
2295 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2296 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2297 genl = ofpbuf_try_pull(&b, sizeof *genl);
2298 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2299 if (!nlmsg || !genl || !ovs_header
2300 || nlmsg->nlmsg_type != ovs_vport_family
2301 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2302 ARRAY_SIZE(ovs_vport_policy))) {
2306 vport->cmd = genl->cmd;
2307 vport->dp_ifindex = ovs_header->dp_ifindex;
2308 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2309 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2310 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2311 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2312 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2313 / (sizeof *vport->upcall_pids);
2314 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2317 if (a[OVS_VPORT_ATTR_STATS]) {
2318 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2320 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2321 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2322 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2327 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2328 * followed by Netlink attributes corresponding to 'vport'. */
2330 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
2333 struct ovs_header *ovs_header;
2335 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2336 vport->cmd, OVS_VPORT_VERSION);
2338 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2339 ovs_header->dp_ifindex = vport->dp_ifindex;
2341 if (vport->port_no != ODPP_NONE) {
2342 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2345 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2346 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2350 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2353 if (vport->upcall_pids) {
2354 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2356 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2360 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2361 vport->stats, sizeof *vport->stats);
2364 if (vport->options) {
2365 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2366 vport->options, vport->options_len);
2370 /* Clears 'vport' to "empty" values. */
2372 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
2374 memset(vport, 0, sizeof *vport);
2375 vport->port_no = ODPP_NONE;
2378 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2379 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2380 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2381 * result of the command is expected to be an ovs_vport also, which is decoded
2382 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2383 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2385 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
2386 struct dpif_netlink_vport *reply,
2387 struct ofpbuf **bufp)
2389 struct ofpbuf *request_buf;
2392 ovs_assert((reply != NULL) == (bufp != NULL));
2394 error = dpif_netlink_init();
2398 dpif_netlink_vport_init(reply);
2403 request_buf = ofpbuf_new(1024);
2404 dpif_netlink_vport_to_ofpbuf(request, request_buf);
2405 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2406 ofpbuf_delete(request_buf);
2410 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
2413 dpif_netlink_vport_init(reply);
2414 ofpbuf_delete(*bufp);
2421 /* Obtains information about the kernel vport named 'name' and stores it into
2422 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2423 * longer needed ('reply' will contain pointers into '*bufp'). */
2425 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
2426 struct ofpbuf **bufp)
2428 struct dpif_netlink_vport request;
2430 dpif_netlink_vport_init(&request);
2431 request.cmd = OVS_VPORT_CMD_GET;
2432 request.name = name;
2434 return dpif_netlink_vport_transact(&request, reply, bufp);
2437 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2438 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2439 * positive errno value.
2441 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2442 * while 'dp' is still in use. */
2444 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
2446 static const struct nl_policy ovs_datapath_policy[] = {
2447 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2448 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2450 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2451 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2455 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2456 struct ovs_header *ovs_header;
2457 struct nlmsghdr *nlmsg;
2458 struct genlmsghdr *genl;
2461 dpif_netlink_dp_init(dp);
2463 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2464 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2465 genl = ofpbuf_try_pull(&b, sizeof *genl);
2466 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2467 if (!nlmsg || !genl || !ovs_header
2468 || nlmsg->nlmsg_type != ovs_datapath_family
2469 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2470 ARRAY_SIZE(ovs_datapath_policy))) {
2474 dp->cmd = genl->cmd;
2475 dp->dp_ifindex = ovs_header->dp_ifindex;
2476 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2477 if (a[OVS_DP_ATTR_STATS]) {
2478 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
2481 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2482 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
2488 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2490 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
2492 struct ovs_header *ovs_header;
2494 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2495 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2496 OVS_DATAPATH_VERSION);
2498 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2499 ovs_header->dp_ifindex = dp->dp_ifindex;
2502 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2505 if (dp->upcall_pid) {
2506 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2509 if (dp->user_features) {
2510 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2513 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2516 /* Clears 'dp' to "empty" values. */
2518 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
2520 memset(dp, 0, sizeof *dp);
2524 dpif_netlink_dp_dump_start(struct nl_dump *dump)
2526 struct dpif_netlink_dp request;
2529 dpif_netlink_dp_init(&request);
2530 request.cmd = OVS_DP_CMD_GET;
2532 buf = ofpbuf_new(1024);
2533 dpif_netlink_dp_to_ofpbuf(&request, buf);
2534 nl_dump_start(dump, NETLINK_GENERIC, buf);
2538 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2539 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2540 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2541 * result of the command is expected to be of the same form, which is decoded
2542 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2543 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2545 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
2546 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
2548 struct ofpbuf *request_buf;
2551 ovs_assert((reply != NULL) == (bufp != NULL));
2553 request_buf = ofpbuf_new(1024);
2554 dpif_netlink_dp_to_ofpbuf(request, request_buf);
2555 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2556 ofpbuf_delete(request_buf);
2559 dpif_netlink_dp_init(reply);
2561 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
2564 ofpbuf_delete(*bufp);
2571 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2572 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2573 * will contain pointers into '*bufp'). */
2575 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
2576 struct ofpbuf **bufp)
2578 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2579 struct dpif_netlink_dp request;
2581 dpif_netlink_dp_init(&request);
2582 request.cmd = OVS_DP_CMD_GET;
2583 request.dp_ifindex = dpif->dp_ifindex;
2585 return dpif_netlink_dp_transact(&request, reply, bufp);
2588 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2589 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2590 * positive errno value.
2592 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2593 * while 'flow' is still in use. */
2595 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
2596 const struct ofpbuf *buf)
2598 static const struct nl_policy ovs_flow_policy[] = {
2599 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2600 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2601 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2602 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2604 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2605 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2606 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2607 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
2610 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2611 struct ovs_header *ovs_header;
2612 struct nlmsghdr *nlmsg;
2613 struct genlmsghdr *genl;
2616 dpif_netlink_flow_init(flow);
2618 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2619 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2620 genl = ofpbuf_try_pull(&b, sizeof *genl);
2621 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2622 if (!nlmsg || !genl || !ovs_header
2623 || nlmsg->nlmsg_type != ovs_flow_family
2624 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2625 ARRAY_SIZE(ovs_flow_policy))) {
2629 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2630 flow->dp_ifindex = ovs_header->dp_ifindex;
2631 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2632 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2634 if (a[OVS_FLOW_ATTR_MASK]) {
2635 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2636 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2638 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2639 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2640 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2642 if (a[OVS_FLOW_ATTR_STATS]) {
2643 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2645 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2646 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2648 if (a[OVS_FLOW_ATTR_USED]) {
2649 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2654 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2655 * followed by Netlink attributes corresponding to 'flow'. */
2657 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
2660 struct ovs_header *ovs_header;
2662 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2663 NLM_F_REQUEST | flow->nlmsg_flags,
2664 flow->cmd, OVS_FLOW_VERSION);
2666 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2667 ovs_header->dp_ifindex = flow->dp_ifindex;
2669 if (flow->key_len) {
2670 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2673 if (flow->mask_len) {
2674 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2677 if (flow->actions || flow->actions_len) {
2678 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2679 flow->actions, flow->actions_len);
2682 /* We never need to send these to the kernel. */
2683 ovs_assert(!flow->stats);
2684 ovs_assert(!flow->tcp_flags);
2685 ovs_assert(!flow->used);
2688 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2691 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
2695 /* Clears 'flow' to "empty" values. */
2697 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
2699 memset(flow, 0, sizeof *flow);
2702 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2703 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2704 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2705 * result of the command is expected to be a flow also, which is decoded and
2706 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2707 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2709 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
2710 struct dpif_netlink_flow *reply,
2711 struct ofpbuf **bufp)
2713 struct ofpbuf *request_buf;
2716 ovs_assert((reply != NULL) == (bufp != NULL));
2719 request->nlmsg_flags |= NLM_F_ECHO;
2722 request_buf = ofpbuf_new(1024);
2723 dpif_netlink_flow_to_ofpbuf(request, request_buf);
2724 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2725 ofpbuf_delete(request_buf);
2729 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
2732 dpif_netlink_flow_init(reply);
2733 ofpbuf_delete(*bufp);
2741 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
2742 struct dpif_flow_stats *stats)
2745 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
2746 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
2748 stats->n_packets = 0;
2751 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2752 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2755 /* Logs information about a packet that was recently lost in 'ch' (in
2758 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2759 uint32_t handler_id)
2761 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2764 if (VLOG_DROP_WARN(&rl)) {
2769 if (ch->last_poll != LLONG_MIN) {
2770 ds_put_format(&s, " (last polled %lld ms ago)",
2771 time_msec() - ch->last_poll);
2774 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2775 dpif_name(&dpif->dpif), ch_idx, handler_id);