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"
56 #include "openvswitch/vlog.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 ovs_u128 ufid; /* OVS_FLOW_ATTR_FLOW_ID. */
120 bool ufid_present; /* Is there a UFID? */
121 bool ufid_terse; /* Skip serializing key/mask/acts? */
122 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
123 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
124 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
125 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
126 bool probe; /* OVS_FLOW_ATTR_PROBE. */
129 static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
130 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
131 const struct ofpbuf *);
132 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
134 static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
135 struct dpif_netlink_flow *reply,
136 struct ofpbuf **bufp);
137 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
138 struct dpif_flow_stats *);
139 static void dpif_netlink_flow_to_dpif_flow(struct dpif *, struct dpif_flow *,
140 const struct dpif_netlink_flow *);
142 /* One of the dpif channels between the kernel and userspace. */
143 struct dpif_channel {
144 struct nl_sock *sock; /* Netlink socket. */
145 long long int last_poll; /* Last time this channel was polled. */
149 #define VPORT_SOCK_POOL_SIZE 1
150 /* On Windows, there is no native support for epoll. There are equivalent
151 * interfaces though, that are not used currently. For simpicity, a pool of
152 * netlink sockets is used. Each socket is represented by 'struct
153 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
154 * sharing the same socket. In the future, we can add a reference count and
156 struct dpif_windows_vport_sock {
157 struct nl_sock *nl_sock; /* netlink socket. */
161 struct dpif_handler {
162 struct dpif_channel *channels;/* Array of channels for each handler. */
163 struct epoll_event *epoll_events;
164 int epoll_fd; /* epoll fd that includes channel socks. */
165 int n_events; /* Num events returned by epoll_wait(). */
166 int event_offset; /* Offset into 'epoll_events'. */
169 /* Pool of sockets. */
170 struct dpif_windows_vport_sock *vport_sock_pool;
171 size_t last_used_pool_idx; /* Index to aid in allocating a
172 socket in the pool to a port. */
176 /* Datapath interface for the openvswitch Linux kernel module. */
177 struct dpif_netlink {
181 /* Upcall messages. */
182 struct fat_rwlock upcall_lock;
183 struct dpif_handler *handlers;
184 uint32_t n_handlers; /* Num of upcall handlers. */
185 int uc_array_size; /* Size of 'handler->channels' and */
186 /* 'handler->epoll_events'. */
188 /* Change notification. */
189 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
190 bool refresh_channels;
193 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
194 uint32_t ch_idx, uint32_t handler_id);
196 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
198 /* Generic Netlink family numbers for OVS.
200 * Initialized by dpif_netlink_init(). */
201 static int ovs_datapath_family;
202 static int ovs_vport_family;
203 static int ovs_flow_family;
204 static int ovs_packet_family;
206 /* Generic Netlink multicast groups for OVS.
208 * Initialized by dpif_netlink_init(). */
209 static unsigned int ovs_vport_mcgroup;
211 static int dpif_netlink_init(void);
212 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
213 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
214 odp_port_t port_no, uint32_t hash);
215 static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
216 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
217 uint32_t n_handlers);
218 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
220 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
221 const struct ofpbuf *);
223 static struct dpif_netlink *
224 dpif_netlink_cast(const struct dpif *dpif)
226 dpif_assert_class(dpif, &dpif_netlink_class);
227 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
231 dpif_netlink_enumerate(struct sset *all_dps,
232 const struct dpif_class *dpif_class OVS_UNUSED)
235 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
236 struct ofpbuf msg, buf;
239 error = dpif_netlink_init();
244 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
245 dpif_netlink_dp_dump_start(&dump);
246 while (nl_dump_next(&dump, &msg, &buf)) {
247 struct dpif_netlink_dp dp;
249 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
250 sset_add(all_dps, dp.name);
254 return nl_dump_done(&dump);
258 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
259 bool create, struct dpif **dpifp)
261 struct dpif_netlink_dp dp_request, dp;
266 error = dpif_netlink_init();
271 /* Create or look up datapath. */
272 dpif_netlink_dp_init(&dp_request);
274 dp_request.cmd = OVS_DP_CMD_NEW;
276 dp_request.upcall_pid = &upcall_pid;
278 /* Use OVS_DP_CMD_SET to report user features */
279 dp_request.cmd = OVS_DP_CMD_SET;
281 dp_request.name = name;
282 dp_request.user_features |= OVS_DP_F_UNALIGNED;
283 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
284 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
289 error = open_dpif(&dp, dpifp);
295 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
297 struct dpif_netlink *dpif;
299 dpif = xzalloc(sizeof *dpif);
300 dpif->port_notifier = NULL;
301 fat_rwlock_init(&dpif->upcall_lock);
303 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
304 dp->dp_ifindex, dp->dp_ifindex);
306 dpif->dp_ifindex = dp->dp_ifindex;
307 *dpifp = &dpif->dpif;
312 /* Destroys the netlink sockets pointed by the elements in 'socksp'
313 * and frees the 'socksp'. */
315 vport_del_socksp__(struct nl_sock **socksp, uint32_t n_socks)
319 for (i = 0; i < n_socks; i++) {
320 nl_sock_destroy(socksp[i]);
326 /* Creates an array of netlink sockets. Returns an array of the
327 * corresponding pointers. Records the error in 'error'. */
328 static struct nl_sock **
329 vport_create_socksp__(uint32_t n_socks, int *error)
331 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
334 for (i = 0; i < n_socks; i++) {
335 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
344 vport_del_socksp__(socksp, n_socks);
351 vport_delete_sock_pool(struct dpif_handler *handler)
352 OVS_REQ_WRLOCK(dpif->upcall_lock)
354 if (handler->vport_sock_pool) {
356 struct dpif_windows_vport_sock *sock_pool =
357 handler->vport_sock_pool;
359 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
360 if (sock_pool[i].nl_sock) {
361 nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
362 nl_sock_destroy(sock_pool[i].nl_sock);
363 sock_pool[i].nl_sock = NULL;
367 free(handler->vport_sock_pool);
368 handler->vport_sock_pool = NULL;
373 vport_create_sock_pool(struct dpif_handler *handler)
374 OVS_REQ_WRLOCK(dpif->upcall_lock)
376 struct dpif_windows_vport_sock *sock_pool;
380 sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
381 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
382 error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
387 /* Enable the netlink socket to receive packets. This is equivalent to
388 * calling nl_sock_join_mcgroup() to receive events. */
389 error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
395 handler->vport_sock_pool = sock_pool;
396 handler->last_used_pool_idx = 0;
400 vport_delete_sock_pool(handler);
404 /* Returns an array pointers to netlink sockets. The sockets are picked from a
405 * pool. Records the error in 'error'. */
406 static struct nl_sock **
407 vport_create_socksp_windows(struct dpif_netlink *dpif, int *error)
408 OVS_REQ_WRLOCK(dpif->upcall_lock)
410 uint32_t n_socks = dpif->n_handlers;
411 struct nl_sock **socksp;
414 ovs_assert(n_socks <= 1);
415 socksp = xzalloc(n_socks * sizeof *socksp);
417 /* Pick netlink sockets to use in a round-robin fashion from each
418 * handler's pool of sockets. */
419 for (i = 0; i < n_socks; i++) {
420 struct dpif_handler *handler = &dpif->handlers[i];
421 struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
422 size_t index = handler->last_used_pool_idx;
424 /* A pool of sockets is allocated when the handler is initialized. */
425 if (sock_pool == NULL) {
431 ovs_assert(index < VPORT_SOCK_POOL_SIZE);
432 socksp[i] = sock_pool[index].nl_sock;
433 socksp[i] = sock_pool[index].nl_sock;
434 ovs_assert(socksp[i]);
435 index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
436 handler->last_used_pool_idx = index;
443 vport_del_socksp_windows(struct dpif_netlink *dpif, struct nl_sock **socksp)
449 static struct nl_sock **
450 vport_create_socksp(struct dpif_netlink *dpif, int *error)
453 return vport_create_socksp_windows(dpif, error);
455 return vport_create_socksp__(dpif->n_handlers, error);
460 vport_del_socksp(struct dpif_netlink *dpif, struct nl_sock **socksp)
463 vport_del_socksp_windows(dpif, socksp);
465 vport_del_socksp__(socksp, dpif->n_handlers);
469 /* Given the array of pointers to netlink sockets 'socksp', returns
470 * the array of corresponding pids. If the 'socksp' is NULL, returns
471 * a single-element array of value 0. */
473 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
478 pids = xzalloc(sizeof *pids);
482 pids = xzalloc(n_socks * sizeof *pids);
483 for (i = 0; i < n_socks; i++) {
484 pids[i] = nl_sock_pid(socksp[i]);
491 /* Given the port number 'port_idx', extracts the pids of netlink sockets
492 * associated to the port and assigns it to 'upcall_pids'. */
494 vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
495 uint32_t **upcall_pids)
500 /* Since the nl_sock can only be assigned in either all
501 * or none "dpif->handlers" channels, the following check
503 if (!dpif->handlers[0].channels[port_idx].sock) {
506 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
508 pids = xzalloc(dpif->n_handlers * sizeof *pids);
510 for (i = 0; i < dpif->n_handlers; i++) {
511 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
520 vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
521 struct nl_sock **socksp)
523 struct epoll_event event;
524 uint32_t port_idx = odp_to_u32(port_no);
528 if (dpif->handlers == NULL) {
532 /* We assume that the datapath densely chooses port numbers, which can
533 * therefore be used as an index into 'channels' and 'epoll_events' of
534 * 'dpif->handler'. */
535 if (port_idx >= dpif->uc_array_size) {
536 uint32_t new_size = port_idx + 1;
538 if (new_size > MAX_PORTS) {
539 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
540 dpif_name(&dpif->dpif), port_no);
544 for (i = 0; i < dpif->n_handlers; i++) {
545 struct dpif_handler *handler = &dpif->handlers[i];
547 handler->channels = xrealloc(handler->channels,
548 new_size * sizeof *handler->channels);
550 for (j = dpif->uc_array_size; j < new_size; j++) {
551 handler->channels[j].sock = NULL;
554 handler->epoll_events = xrealloc(handler->epoll_events,
555 new_size * sizeof *handler->epoll_events);
558 dpif->uc_array_size = new_size;
561 memset(&event, 0, sizeof event);
562 event.events = EPOLLIN;
563 event.data.u32 = port_idx;
565 for (i = 0; i < dpif->n_handlers; i++) {
566 struct dpif_handler *handler = &dpif->handlers[i];
569 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
575 dpif->handlers[i].channels[port_idx].sock = socksp[i];
576 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
582 for (j = 0; j < i; j++) {
584 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
585 nl_sock_fd(socksp[j]), NULL);
587 dpif->handlers[j].channels[port_idx].sock = NULL;
594 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
596 uint32_t port_idx = odp_to_u32(port_no);
599 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
603 /* Since the sock can only be assigned in either all or none
604 * of "dpif->handlers" channels, the following check would
606 if (!dpif->handlers[0].channels[port_idx].sock) {
610 for (i = 0; i < dpif->n_handlers; i++) {
611 struct dpif_handler *handler = &dpif->handlers[i];
613 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
614 nl_sock_fd(handler->channels[port_idx].sock), NULL);
615 nl_sock_destroy(handler->channels[port_idx].sock);
617 handler->channels[port_idx].sock = NULL;
618 handler->event_offset = handler->n_events = 0;
623 destroy_all_channels(struct dpif_netlink *dpif)
624 OVS_REQ_WRLOCK(dpif->upcall_lock)
628 if (!dpif->handlers) {
632 for (i = 0; i < dpif->uc_array_size; i++ ) {
633 struct dpif_netlink_vport vport_request;
634 uint32_t upcall_pids = 0;
636 /* Since the sock can only be assigned in either all or none
637 * of "dpif->handlers" channels, the following check would
639 if (!dpif->handlers[0].channels[i].sock) {
643 /* Turn off upcalls. */
644 dpif_netlink_vport_init(&vport_request);
645 vport_request.cmd = OVS_VPORT_CMD_SET;
646 vport_request.dp_ifindex = dpif->dp_ifindex;
647 vport_request.port_no = u32_to_odp(i);
648 vport_request.upcall_pids = &upcall_pids;
649 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
651 vport_del_channels(dpif, u32_to_odp(i));
654 for (i = 0; i < dpif->n_handlers; i++) {
655 struct dpif_handler *handler = &dpif->handlers[i];
657 dpif_netlink_handler_uninit(handler);
658 free(handler->epoll_events);
659 free(handler->channels);
662 free(dpif->handlers);
663 dpif->handlers = NULL;
664 dpif->n_handlers = 0;
665 dpif->uc_array_size = 0;
669 dpif_netlink_close(struct dpif *dpif_)
671 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
673 nl_sock_destroy(dpif->port_notifier);
675 fat_rwlock_wrlock(&dpif->upcall_lock);
676 destroy_all_channels(dpif);
677 fat_rwlock_unlock(&dpif->upcall_lock);
679 fat_rwlock_destroy(&dpif->upcall_lock);
684 dpif_netlink_destroy(struct dpif *dpif_)
686 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
687 struct dpif_netlink_dp dp;
689 dpif_netlink_dp_init(&dp);
690 dp.cmd = OVS_DP_CMD_DEL;
691 dp.dp_ifindex = dpif->dp_ifindex;
692 return dpif_netlink_dp_transact(&dp, NULL, NULL);
696 dpif_netlink_run(struct dpif *dpif_)
698 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
700 if (dpif->refresh_channels) {
701 dpif->refresh_channels = false;
702 fat_rwlock_wrlock(&dpif->upcall_lock);
703 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
704 fat_rwlock_unlock(&dpif->upcall_lock);
710 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
712 struct dpif_netlink_dp dp;
716 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
718 memset(stats, 0, sizeof *stats);
721 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
722 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
723 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
724 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
727 if (dp.megaflow_stats) {
728 stats->n_masks = dp.megaflow_stats->n_masks;
729 stats->n_mask_hit = get_32aligned_u64(
730 &dp.megaflow_stats->n_mask_hit);
732 stats->n_masks = UINT32_MAX;
733 stats->n_mask_hit = UINT64_MAX;
741 get_vport_type(const struct dpif_netlink_vport *vport)
743 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
745 switch (vport->type) {
746 case OVS_VPORT_TYPE_NETDEV: {
747 const char *type = netdev_get_type_from_name(vport->name);
749 return type ? type : "system";
752 case OVS_VPORT_TYPE_INTERNAL:
755 case OVS_VPORT_TYPE_GENEVE:
758 case OVS_VPORT_TYPE_GRE:
761 case OVS_VPORT_TYPE_GRE64:
764 case OVS_VPORT_TYPE_VXLAN:
767 case OVS_VPORT_TYPE_LISP:
770 case OVS_VPORT_TYPE_UNSPEC:
771 case __OVS_VPORT_TYPE_MAX:
775 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
776 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
780 static enum ovs_vport_type
781 netdev_to_ovs_vport_type(const struct netdev *netdev)
783 const char *type = netdev_get_type(netdev);
785 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
786 return OVS_VPORT_TYPE_NETDEV;
787 } else if (!strcmp(type, "internal")) {
788 return OVS_VPORT_TYPE_INTERNAL;
789 } else if (!strcmp(type, "geneve")) {
790 return OVS_VPORT_TYPE_GENEVE;
791 } else if (strstr(type, "gre64")) {
792 return OVS_VPORT_TYPE_GRE64;
793 } else if (strstr(type, "gre")) {
794 return OVS_VPORT_TYPE_GRE;
795 } else if (!strcmp(type, "vxlan")) {
796 return OVS_VPORT_TYPE_VXLAN;
797 } else if (!strcmp(type, "lisp")) {
798 return OVS_VPORT_TYPE_LISP;
800 return OVS_VPORT_TYPE_UNSPEC;
805 dpif_netlink_port_add__(struct dpif_netlink *dpif, struct netdev *netdev,
806 odp_port_t *port_nop)
807 OVS_REQ_WRLOCK(dpif->upcall_lock)
809 const struct netdev_tunnel_config *tnl_cfg;
810 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
811 const char *name = netdev_vport_get_dpif_port(netdev,
812 namebuf, sizeof namebuf);
813 const char *type = netdev_get_type(netdev);
814 struct dpif_netlink_vport request, reply;
816 uint64_t options_stub[64 / 8];
817 struct ofpbuf options;
818 struct nl_sock **socksp = NULL;
819 uint32_t *upcall_pids;
822 if (dpif->handlers) {
823 socksp = vport_create_socksp(dpif, &error);
829 dpif_netlink_vport_init(&request);
830 request.cmd = OVS_VPORT_CMD_NEW;
831 request.dp_ifindex = dpif->dp_ifindex;
832 request.type = netdev_to_ovs_vport_type(netdev);
833 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
834 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
835 "unsupported type `%s'",
836 dpif_name(&dpif->dpif), name, type);
837 vport_del_socksp(dpif, socksp);
842 if (request.type == OVS_VPORT_TYPE_NETDEV) {
844 /* XXX : Map appropiate Windows handle */
846 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
850 tnl_cfg = netdev_get_tunnel_config(netdev);
851 if (tnl_cfg && tnl_cfg->dst_port != 0) {
852 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
853 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
854 ntohs(tnl_cfg->dst_port));
855 request.options = ofpbuf_data(&options);
856 request.options_len = ofpbuf_size(&options);
859 request.port_no = *port_nop;
860 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
861 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
862 request.upcall_pids = upcall_pids;
864 error = dpif_netlink_vport_transact(&request, &reply, &buf);
866 *port_nop = reply.port_no;
868 if (error == EBUSY && *port_nop != ODPP_NONE) {
869 VLOG_INFO("%s: requested port %"PRIu32" is in use",
870 dpif_name(&dpif->dpif), *port_nop);
873 vport_del_socksp(dpif, socksp);
878 error = vport_add_channels(dpif, *port_nop, socksp);
880 VLOG_INFO("%s: could not add channel for port %s",
881 dpif_name(&dpif->dpif), name);
883 /* Delete the port. */
884 dpif_netlink_vport_init(&request);
885 request.cmd = OVS_VPORT_CMD_DEL;
886 request.dp_ifindex = dpif->dp_ifindex;
887 request.port_no = *port_nop;
888 dpif_netlink_vport_transact(&request, NULL, NULL);
889 vport_del_socksp(dpif, socksp);
903 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
904 odp_port_t *port_nop)
906 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
909 fat_rwlock_wrlock(&dpif->upcall_lock);
910 error = dpif_netlink_port_add__(dpif, netdev, port_nop);
911 fat_rwlock_unlock(&dpif->upcall_lock);
917 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
918 OVS_REQ_WRLOCK(dpif->upcall_lock)
920 struct dpif_netlink_vport vport;
923 dpif_netlink_vport_init(&vport);
924 vport.cmd = OVS_VPORT_CMD_DEL;
925 vport.dp_ifindex = dpif->dp_ifindex;
926 vport.port_no = port_no;
927 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
929 vport_del_channels(dpif, port_no);
935 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
937 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
940 fat_rwlock_wrlock(&dpif->upcall_lock);
941 error = dpif_netlink_port_del__(dpif, port_no);
942 fat_rwlock_unlock(&dpif->upcall_lock);
948 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
949 const char *port_name, struct dpif_port *dpif_port)
951 struct dpif_netlink_vport request;
952 struct dpif_netlink_vport reply;
956 dpif_netlink_vport_init(&request);
957 request.cmd = OVS_VPORT_CMD_GET;
958 request.dp_ifindex = dpif->dp_ifindex;
959 request.port_no = port_no;
960 request.name = port_name;
962 error = dpif_netlink_vport_transact(&request, &reply, &buf);
964 if (reply.dp_ifindex != request.dp_ifindex) {
965 /* A query by name reported that 'port_name' is in some datapath
966 * other than 'dpif', but the caller wants to know about 'dpif'. */
968 } else if (dpif_port) {
969 dpif_port->name = xstrdup(reply.name);
970 dpif_port->type = xstrdup(get_vport_type(&reply));
971 dpif_port->port_no = reply.port_no;
979 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
980 struct dpif_port *dpif_port)
982 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
984 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
988 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
989 struct dpif_port *dpif_port)
991 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
993 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
997 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
998 odp_port_t port_no, uint32_t hash)
999 OVS_REQ_RDLOCK(dpif->upcall_lock)
1001 uint32_t port_idx = odp_to_u32(port_no);
1004 if (dpif->handlers && dpif->uc_array_size > 0) {
1005 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1006 * channel, since it is not heavily loaded. */
1007 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
1008 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
1010 /* Needs to check in case the socket pointer is changed in between
1011 * the holding of upcall_lock. A known case happens when the main
1012 * thread deletes the vport while the handler thread is handling
1013 * the upcall from that port. */
1014 if (h->channels[idx].sock) {
1015 pid = nl_sock_pid(h->channels[idx].sock);
1023 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
1026 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1029 fat_rwlock_rdlock(&dpif->upcall_lock);
1030 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
1031 fat_rwlock_unlock(&dpif->upcall_lock);
1037 dpif_netlink_flow_flush(struct dpif *dpif_)
1039 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1040 struct dpif_netlink_flow flow;
1042 dpif_netlink_flow_init(&flow);
1043 flow.cmd = OVS_FLOW_CMD_DEL;
1044 flow.dp_ifindex = dpif->dp_ifindex;
1045 return dpif_netlink_flow_transact(&flow, NULL, NULL);
1048 struct dpif_netlink_port_state {
1049 struct nl_dump dump;
1054 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
1055 struct nl_dump *dump)
1057 struct dpif_netlink_vport request;
1060 dpif_netlink_vport_init(&request);
1061 request.cmd = OVS_VPORT_CMD_GET;
1062 request.dp_ifindex = dpif->dp_ifindex;
1064 buf = ofpbuf_new(1024);
1065 dpif_netlink_vport_to_ofpbuf(&request, buf);
1066 nl_dump_start(dump, NETLINK_GENERIC, buf);
1071 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
1073 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1074 struct dpif_netlink_port_state *state;
1076 *statep = state = xmalloc(sizeof *state);
1077 dpif_netlink_port_dump_start__(dpif, &state->dump);
1079 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
1084 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
1085 struct nl_dump *dump,
1086 struct dpif_netlink_vport *vport,
1087 struct ofpbuf *buffer)
1092 if (!nl_dump_next(dump, &buf, buffer)) {
1096 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
1098 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
1099 dpif_name(&dpif->dpif), ovs_strerror(error));
1105 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
1106 struct dpif_port *dpif_port)
1108 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1109 struct dpif_netlink_port_state *state = state_;
1110 struct dpif_netlink_vport vport;
1113 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
1118 dpif_port->name = CONST_CAST(char *, vport.name);
1119 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
1120 dpif_port->port_no = vport.port_no;
1125 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
1127 struct dpif_netlink_port_state *state = state_;
1128 int error = nl_dump_done(&state->dump);
1130 ofpbuf_uninit(&state->buf);
1136 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1138 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1140 /* Lazily create the Netlink socket to listen for notifications. */
1141 if (!dpif->port_notifier) {
1142 struct nl_sock *sock;
1145 error = nl_sock_create(NETLINK_GENERIC, &sock);
1150 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1152 nl_sock_destroy(sock);
1155 dpif->port_notifier = sock;
1157 /* We have no idea of the current state so report that everything
1163 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1164 uint64_t buf_stub[4096 / 8];
1168 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1169 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1171 struct dpif_netlink_vport vport;
1173 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1175 if (vport.dp_ifindex == dpif->dp_ifindex
1176 && (vport.cmd == OVS_VPORT_CMD_NEW
1177 || vport.cmd == OVS_VPORT_CMD_DEL
1178 || vport.cmd == OVS_VPORT_CMD_SET)) {
1179 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1180 dpif->dpif.full_name, vport.name, vport.cmd);
1181 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1182 dpif->refresh_channels = true;
1184 *devnamep = xstrdup(vport.name);
1185 ofpbuf_uninit(&buf);
1189 } else if (error != EAGAIN) {
1190 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1191 ovs_strerror(error));
1192 nl_sock_drain(dpif->port_notifier);
1196 ofpbuf_uninit(&buf);
1204 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1206 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1208 if (dpif->port_notifier) {
1209 nl_sock_wait(dpif->port_notifier, POLLIN);
1211 poll_immediate_wake();
1216 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow *request,
1217 const ovs_u128 *ufid, bool terse)
1220 request->ufid = *ufid;
1221 request->ufid_present = true;
1223 request->ufid_present = false;
1225 request->ufid_terse = terse;
1229 dpif_netlink_init_flow_get__(const struct dpif_netlink *dpif,
1230 const struct nlattr *key, size_t key_len,
1231 const ovs_u128 *ufid, bool terse,
1232 struct dpif_netlink_flow *request)
1234 dpif_netlink_flow_init(request);
1235 request->cmd = OVS_FLOW_CMD_GET;
1236 request->dp_ifindex = dpif->dp_ifindex;
1238 request->key_len = key_len;
1239 dpif_netlink_flow_init_ufid(request, ufid, terse);
1243 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1244 const struct dpif_flow_get *get,
1245 struct dpif_netlink_flow *request)
1247 dpif_netlink_init_flow_get__(dpif, get->key, get->key_len, get->ufid,
1252 dpif_netlink_flow_get__(const struct dpif_netlink *dpif,
1253 const struct nlattr *key, size_t key_len,
1254 const ovs_u128 *ufid, bool terse,
1255 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1257 struct dpif_netlink_flow request;
1259 dpif_netlink_init_flow_get__(dpif, key, key_len, ufid, terse, &request);
1260 return dpif_netlink_flow_transact(&request, reply, bufp);
1264 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1265 const struct dpif_netlink_flow *flow,
1266 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1268 return dpif_netlink_flow_get__(dpif, flow->key, flow->key_len,
1269 flow->ufid_present ? &flow->ufid : NULL,
1270 false, reply, bufp);
1274 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1275 const struct dpif_flow_put *put,
1276 struct dpif_netlink_flow *request)
1278 static const struct nlattr dummy_action;
1280 dpif_netlink_flow_init(request);
1281 request->cmd = (put->flags & DPIF_FP_CREATE
1282 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1283 request->dp_ifindex = dpif->dp_ifindex;
1284 request->key = put->key;
1285 request->key_len = put->key_len;
1286 request->mask = put->mask;
1287 request->mask_len = put->mask_len;
1288 dpif_netlink_flow_init_ufid(request, put->ufid, false);
1290 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1291 request->actions = (put->actions
1293 : CONST_CAST(struct nlattr *, &dummy_action));
1294 request->actions_len = put->actions_len;
1295 if (put->flags & DPIF_FP_ZERO_STATS) {
1296 request->clear = true;
1298 if (put->flags & DPIF_FP_PROBE) {
1299 request->probe = true;
1301 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1305 dpif_netlink_init_flow_del__(struct dpif_netlink *dpif,
1306 const struct nlattr *key, size_t key_len,
1307 const ovs_u128 *ufid, bool terse,
1308 struct dpif_netlink_flow *request)
1310 dpif_netlink_flow_init(request);
1311 request->cmd = OVS_FLOW_CMD_DEL;
1312 request->dp_ifindex = dpif->dp_ifindex;
1314 request->key_len = key_len;
1315 dpif_netlink_flow_init_ufid(request, ufid, terse);
1319 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1320 const struct dpif_flow_del *del,
1321 struct dpif_netlink_flow *request)
1323 return dpif_netlink_init_flow_del__(dpif, del->key, del->key_len,
1324 del->ufid, del->terse, request);
1327 struct dpif_netlink_flow_dump {
1328 struct dpif_flow_dump up;
1329 struct nl_dump nl_dump;
1333 static struct dpif_netlink_flow_dump *
1334 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1336 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1339 static struct dpif_flow_dump *
1340 dpif_netlink_flow_dump_create(const struct dpif *dpif_, bool terse)
1342 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1343 struct dpif_netlink_flow_dump *dump;
1344 struct dpif_netlink_flow request;
1347 dump = xmalloc(sizeof *dump);
1348 dpif_flow_dump_init(&dump->up, dpif_);
1350 dpif_netlink_flow_init(&request);
1351 request.cmd = OVS_FLOW_CMD_GET;
1352 request.dp_ifindex = dpif->dp_ifindex;
1353 request.ufid_present = false;
1354 request.ufid_terse = terse;
1356 buf = ofpbuf_new(1024);
1357 dpif_netlink_flow_to_ofpbuf(&request, buf);
1358 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1360 atomic_init(&dump->status, 0);
1361 dump->up.terse = terse;
1367 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1369 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1370 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1373 /* No other thread has access to 'dump' at this point. */
1374 atomic_read_relaxed(&dump->status, &dump_status);
1376 return dump_status ? dump_status : nl_status;
1379 struct dpif_netlink_flow_dump_thread {
1380 struct dpif_flow_dump_thread up;
1381 struct dpif_netlink_flow_dump *dump;
1382 struct dpif_netlink_flow flow;
1383 struct dpif_flow_stats stats;
1384 struct ofpbuf nl_flows; /* Always used to store flows. */
1385 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1388 static struct dpif_netlink_flow_dump_thread *
1389 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1391 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1394 static struct dpif_flow_dump_thread *
1395 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1397 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1398 struct dpif_netlink_flow_dump_thread *thread;
1400 thread = xmalloc(sizeof *thread);
1401 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1402 thread->dump = dump;
1403 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1404 thread->nl_actions = NULL;
1410 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1412 struct dpif_netlink_flow_dump_thread *thread
1413 = dpif_netlink_flow_dump_thread_cast(thread_);
1415 ofpbuf_uninit(&thread->nl_flows);
1416 ofpbuf_delete(thread->nl_actions);
1421 dpif_netlink_flow_to_dpif_flow(struct dpif *dpif, struct dpif_flow *dpif_flow,
1422 const struct dpif_netlink_flow *datapath_flow)
1424 dpif_flow->key = datapath_flow->key;
1425 dpif_flow->key_len = datapath_flow->key_len;
1426 dpif_flow->mask = datapath_flow->mask;
1427 dpif_flow->mask_len = datapath_flow->mask_len;
1428 dpif_flow->actions = datapath_flow->actions;
1429 dpif_flow->actions_len = datapath_flow->actions_len;
1430 dpif_flow->ufid_present = datapath_flow->ufid_present;
1431 if (datapath_flow->ufid_present) {
1432 dpif_flow->ufid = datapath_flow->ufid;
1433 dpif_flow->pmd_id = PMD_ID_NULL;
1435 ovs_assert(datapath_flow->key && datapath_flow->key_len);
1436 dpif_flow_hash(dpif, datapath_flow->key, datapath_flow->key_len,
1439 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1443 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1444 struct dpif_flow *flows, int max_flows)
1446 struct dpif_netlink_flow_dump_thread *thread
1447 = dpif_netlink_flow_dump_thread_cast(thread_);
1448 struct dpif_netlink_flow_dump *dump = thread->dump;
1449 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1452 ofpbuf_delete(thread->nl_actions);
1453 thread->nl_actions = NULL;
1457 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1458 struct dpif_netlink_flow datapath_flow;
1459 struct ofpbuf nl_flow;
1462 /* Try to grab another flow. */
1463 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1467 /* Convert the flow to our output format. */
1468 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1470 atomic_store_relaxed(&dump->status, error);
1474 if (dump->up.terse || datapath_flow.actions) {
1475 /* Common case: we don't want actions, or the flow includes
1477 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1480 /* Rare case: the flow does not include actions. Retrieve this
1481 * individual flow again to get the actions. */
1482 error = dpif_netlink_flow_get(dpif, &datapath_flow,
1483 &datapath_flow, &thread->nl_actions);
1484 if (error == ENOENT) {
1485 VLOG_DBG("dumped flow disappeared on get");
1488 VLOG_WARN("error fetching dumped flow: %s",
1489 ovs_strerror(error));
1490 atomic_store_relaxed(&dump->status, error);
1494 /* Save this flow. Then exit, because we only have one buffer to
1495 * handle this case. */
1496 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1505 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1508 struct ovs_header *k_exec;
1511 ofpbuf_prealloc_tailroom(buf, (64
1512 + ofpbuf_size(d_exec->packet)
1513 + ODP_KEY_METADATA_SIZE
1514 + d_exec->actions_len));
1516 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1517 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1519 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1520 k_exec->dp_ifindex = dp_ifindex;
1522 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1523 ofpbuf_data(d_exec->packet),
1524 ofpbuf_size(d_exec->packet));
1526 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1527 odp_key_from_pkt_metadata(buf, &d_exec->md);
1528 nl_msg_end_nested(buf, key_ofs);
1530 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1531 d_exec->actions, d_exec->actions_len);
1532 if (d_exec->probe) {
1533 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
1540 dpif_netlink_operate__(struct dpif_netlink *dpif,
1541 struct dpif_op **ops, size_t n_ops)
1544 struct nl_transaction txn;
1546 struct ofpbuf request;
1547 uint64_t request_stub[1024 / 8];
1549 struct ofpbuf reply;
1550 uint64_t reply_stub[1024 / 8];
1553 struct nl_transaction *txnsp[MAX_OPS];
1556 ovs_assert(n_ops <= MAX_OPS);
1557 for (i = 0; i < n_ops; i++) {
1558 struct op_auxdata *aux = &auxes[i];
1559 struct dpif_op *op = ops[i];
1560 struct dpif_flow_put *put;
1561 struct dpif_flow_del *del;
1562 struct dpif_execute *execute;
1563 struct dpif_flow_get *get;
1564 struct dpif_netlink_flow flow;
1566 ofpbuf_use_stub(&aux->request,
1567 aux->request_stub, sizeof aux->request_stub);
1568 aux->txn.request = &aux->request;
1570 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1571 aux->txn.reply = NULL;
1574 case DPIF_OP_FLOW_PUT:
1575 put = &op->u.flow_put;
1576 dpif_netlink_init_flow_put(dpif, put, &flow);
1578 flow.nlmsg_flags |= NLM_F_ECHO;
1579 aux->txn.reply = &aux->reply;
1581 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1584 case DPIF_OP_FLOW_DEL:
1585 del = &op->u.flow_del;
1586 dpif_netlink_init_flow_del(dpif, del, &flow);
1588 flow.nlmsg_flags |= NLM_F_ECHO;
1589 aux->txn.reply = &aux->reply;
1591 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1594 case DPIF_OP_EXECUTE:
1595 execute = &op->u.execute;
1596 dpif_netlink_encode_execute(dpif->dp_ifindex, execute,
1600 case DPIF_OP_FLOW_GET:
1601 get = &op->u.flow_get;
1602 dpif_netlink_init_flow_get(dpif, get, &flow);
1603 aux->txn.reply = get->buffer;
1604 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1612 for (i = 0; i < n_ops; i++) {
1613 txnsp[i] = &auxes[i].txn;
1615 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1617 for (i = 0; i < n_ops; i++) {
1618 struct op_auxdata *aux = &auxes[i];
1619 struct nl_transaction *txn = &auxes[i].txn;
1620 struct dpif_op *op = ops[i];
1621 struct dpif_flow_put *put;
1622 struct dpif_flow_del *del;
1623 struct dpif_flow_get *get;
1625 op->error = txn->error;
1628 case DPIF_OP_FLOW_PUT:
1629 put = &op->u.flow_put;
1632 struct dpif_netlink_flow reply;
1634 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1637 dpif_netlink_flow_get_stats(&reply, put->stats);
1643 case DPIF_OP_FLOW_DEL:
1644 del = &op->u.flow_del;
1647 struct dpif_netlink_flow reply;
1649 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1652 dpif_netlink_flow_get_stats(&reply, del->stats);
1658 case DPIF_OP_EXECUTE:
1661 case DPIF_OP_FLOW_GET:
1662 get = &op->u.flow_get;
1664 struct dpif_netlink_flow reply;
1666 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
1668 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, get->flow,
1678 ofpbuf_uninit(&aux->request);
1679 ofpbuf_uninit(&aux->reply);
1684 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1686 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1689 size_t chunk = MIN(n_ops, MAX_OPS);
1690 dpif_netlink_operate__(dpif, ops, chunk);
1698 dpif_netlink_handler_uninit(struct dpif_handler *handler)
1700 vport_delete_sock_pool(handler);
1704 dpif_netlink_handler_init(struct dpif_handler *handler)
1706 return vport_create_sock_pool(handler);
1711 dpif_netlink_handler_init(struct dpif_handler *handler)
1713 handler->epoll_fd = epoll_create(10);
1714 return handler->epoll_fd < 0 ? errno : 0;
1718 dpif_netlink_handler_uninit(struct dpif_handler *handler)
1720 close(handler->epoll_fd);
1724 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1725 * currently in 'dpif' in the kernel, by adding a new set of channels for
1726 * any kernel vport that lacks one and deleting any channels that have no
1727 * backing kernel vports. */
1729 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
1730 OVS_REQ_WRLOCK(dpif->upcall_lock)
1732 unsigned long int *keep_channels;
1733 struct dpif_netlink_vport vport;
1734 size_t keep_channels_nbits;
1735 struct nl_dump dump;
1736 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1741 ovs_assert(!WINDOWS || n_handlers <= 1);
1742 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
1744 if (dpif->n_handlers != n_handlers) {
1745 destroy_all_channels(dpif);
1746 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1747 for (i = 0; i < n_handlers; i++) {
1749 struct dpif_handler *handler = &dpif->handlers[i];
1751 error = dpif_netlink_handler_init(handler);
1754 struct dpif_handler *tmp = &dpif->handlers[i];
1757 for (j = 0; j < i; j++) {
1758 dpif_netlink_handler_uninit(tmp);
1760 free(dpif->handlers);
1761 dpif->handlers = NULL;
1766 dpif->n_handlers = n_handlers;
1769 for (i = 0; i < n_handlers; i++) {
1770 struct dpif_handler *handler = &dpif->handlers[i];
1772 handler->event_offset = handler->n_events = 0;
1775 keep_channels_nbits = dpif->uc_array_size;
1776 keep_channels = bitmap_allocate(keep_channels_nbits);
1778 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1779 dpif_netlink_port_dump_start__(dpif, &dump);
1780 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
1781 uint32_t port_no = odp_to_u32(vport.port_no);
1782 uint32_t *upcall_pids = NULL;
1785 if (port_no >= dpif->uc_array_size
1786 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1787 struct nl_sock **socksp = vport_create_socksp(dpif, &error);
1793 error = vport_add_channels(dpif, vport.port_no, socksp);
1795 VLOG_INFO("%s: could not add channels for port %s",
1796 dpif_name(&dpif->dpif), vport.name);
1797 vport_del_socksp(dpif, socksp);
1801 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1805 /* Configure the vport to deliver misses to 'sock'. */
1806 if (vport.upcall_pids[0] == 0
1807 || vport.n_upcall_pids != dpif->n_handlers
1808 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1810 struct dpif_netlink_vport vport_request;
1812 dpif_netlink_vport_init(&vport_request);
1813 vport_request.cmd = OVS_VPORT_CMD_SET;
1814 vport_request.dp_ifindex = dpif->dp_ifindex;
1815 vport_request.port_no = vport.port_no;
1816 vport_request.n_upcall_pids = dpif->n_handlers;
1817 vport_request.upcall_pids = upcall_pids;
1818 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
1820 VLOG_WARN_RL(&error_rl,
1821 "%s: failed to set upcall pid on port: %s",
1822 dpif_name(&dpif->dpif), ovs_strerror(error));
1824 if (error != ENODEV && error != ENOENT) {
1827 /* The vport isn't really there, even though the dump says
1828 * it is. Probably we just hit a race after a port
1835 if (port_no < keep_channels_nbits) {
1836 bitmap_set1(keep_channels, port_no);
1843 vport_del_channels(dpif, vport.port_no);
1845 nl_dump_done(&dump);
1846 ofpbuf_uninit(&buf);
1848 /* Discard any saved channels that we didn't reuse. */
1849 for (i = 0; i < keep_channels_nbits; i++) {
1850 if (!bitmap_is_set(keep_channels, i)) {
1851 vport_del_channels(dpif, u32_to_odp(i));
1854 free(keep_channels);
1860 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
1861 OVS_REQ_WRLOCK(dpif->upcall_lock)
1863 if ((dpif->handlers != NULL) == enable) {
1865 } else if (!enable) {
1866 destroy_all_channels(dpif);
1869 return dpif_netlink_refresh_channels(dpif, 1);
1874 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
1876 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1879 fat_rwlock_wrlock(&dpif->upcall_lock);
1880 error = dpif_netlink_recv_set__(dpif, enable);
1881 fat_rwlock_unlock(&dpif->upcall_lock);
1887 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1889 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1893 /* Multiple upcall handlers will be supported once kernel datapath supports
1895 if (n_handlers > 1) {
1900 fat_rwlock_wrlock(&dpif->upcall_lock);
1901 if (dpif->handlers) {
1902 error = dpif_netlink_refresh_channels(dpif, n_handlers);
1904 fat_rwlock_unlock(&dpif->upcall_lock);
1910 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1911 uint32_t queue_id, uint32_t *priority)
1913 if (queue_id < 0xf000) {
1914 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1922 parse_odp_packet(const struct dpif_netlink *dpif, struct ofpbuf *buf,
1923 struct dpif_upcall *upcall, int *dp_ifindex)
1925 static const struct nl_policy ovs_packet_policy[] = {
1926 /* Always present. */
1927 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1928 .min_len = ETH_HEADER_LEN },
1929 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1931 /* OVS_PACKET_CMD_ACTION only. */
1932 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1933 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
1936 struct ovs_header *ovs_header;
1937 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1938 struct nlmsghdr *nlmsg;
1939 struct genlmsghdr *genl;
1943 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1945 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1946 genl = ofpbuf_try_pull(&b, sizeof *genl);
1947 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1948 if (!nlmsg || !genl || !ovs_header
1949 || nlmsg->nlmsg_type != ovs_packet_family
1950 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1951 ARRAY_SIZE(ovs_packet_policy))) {
1955 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1956 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1962 /* (Re)set ALL fields of '*upcall' on successful return. */
1963 upcall->type = type;
1964 upcall->key = CONST_CAST(struct nlattr *,
1965 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1966 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1967 dpif_flow_hash(&dpif->dpif, upcall->key, upcall->key_len, &upcall->ufid);
1968 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1969 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
1971 /* Allow overwriting the netlink attribute header without reallocating. */
1972 ofpbuf_use_stub(&upcall->packet,
1973 CONST_CAST(struct nlattr *,
1974 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1975 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1976 sizeof(struct nlattr));
1977 ofpbuf_set_data(&upcall->packet,
1978 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1979 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1981 *dp_ifindex = ovs_header->dp_ifindex;
1987 #define PACKET_RECV_BATCH_SIZE 50
1989 dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id,
1990 struct dpif_upcall *upcall, struct ofpbuf *buf)
1991 OVS_REQ_RDLOCK(dpif->upcall_lock)
1993 struct dpif_handler *handler;
1995 struct dpif_windows_vport_sock *sock_pool;
1998 if (!dpif->handlers) {
2002 /* Only one handler is supported currently. */
2003 if (handler_id >= 1) {
2007 if (handler_id >= dpif->n_handlers) {
2011 handler = &dpif->handlers[handler_id];
2012 sock_pool = handler->vport_sock_pool;
2014 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2019 if (++read_tries > PACKET_RECV_BATCH_SIZE) {
2023 error = nl_sock_recv(sock_pool[i].nl_sock, buf, false);
2024 if (error == ENOBUFS) {
2025 /* ENOBUFS typically means that we've received so many
2026 * packets that the buffer overflowed. Try again
2027 * immediately because there's almost certainly a packet
2028 * waiting for us. */
2029 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2033 /* XXX: ch->last_poll = time_msec(); */
2035 if (error == EAGAIN) {
2041 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2042 if (!error && dp_ifindex == dpif->dp_ifindex) {
2054 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
2055 struct dpif_upcall *upcall, struct ofpbuf *buf)
2056 OVS_REQ_RDLOCK(dpif->upcall_lock)
2058 struct dpif_handler *handler;
2061 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
2065 handler = &dpif->handlers[handler_id];
2066 if (handler->event_offset >= handler->n_events) {
2069 handler->event_offset = handler->n_events = 0;
2072 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
2073 dpif->uc_array_size, 0);
2074 } while (retval < 0 && errno == EINTR);
2077 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2078 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
2079 } else if (retval > 0) {
2080 handler->n_events = retval;
2084 while (handler->event_offset < handler->n_events) {
2085 int idx = handler->epoll_events[handler->event_offset].data.u32;
2086 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
2088 handler->event_offset++;
2094 if (++read_tries > 50) {
2098 error = nl_sock_recv(ch->sock, buf, false);
2099 if (error == ENOBUFS) {
2100 /* ENOBUFS typically means that we've received so many
2101 * packets that the buffer overflowed. Try again
2102 * immediately because there's almost certainly a packet
2103 * waiting for us. */
2104 report_loss(dpif, ch, idx, handler_id);
2108 ch->last_poll = time_msec();
2110 if (error == EAGAIN) {
2116 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2117 if (!error && dp_ifindex == dpif->dp_ifindex) {
2130 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
2131 struct dpif_upcall *upcall, struct ofpbuf *buf)
2133 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2136 fat_rwlock_rdlock(&dpif->upcall_lock);
2138 error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf);
2140 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
2142 fat_rwlock_unlock(&dpif->upcall_lock);
2148 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
2149 OVS_REQ_RDLOCK(dpif->upcall_lock)
2153 struct dpif_windows_vport_sock *sock_pool =
2154 dpif->handlers[handler_id].vport_sock_pool;
2156 /* Only one handler is supported currently. */
2157 if (handler_id >= 1) {
2161 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2162 nl_sock_wait(sock_pool[i].nl_sock, POLLIN);
2165 if (dpif->handlers && handler_id < dpif->n_handlers) {
2166 struct dpif_handler *handler = &dpif->handlers[handler_id];
2168 poll_fd_wait(handler->epoll_fd, POLLIN);
2174 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
2176 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2178 fat_rwlock_rdlock(&dpif->upcall_lock);
2179 dpif_netlink_recv_wait__(dpif, handler_id);
2180 fat_rwlock_unlock(&dpif->upcall_lock);
2184 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
2185 OVS_REQ_WRLOCK(dpif->upcall_lock)
2187 if (dpif->handlers) {
2190 for (i = 0; i < dpif->uc_array_size; i++ ) {
2191 if (!dpif->handlers[0].channels[i].sock) {
2195 for (j = 0; j < dpif->n_handlers; j++) {
2196 nl_sock_drain(dpif->handlers[j].channels[i].sock);
2203 dpif_netlink_recv_purge(struct dpif *dpif_)
2205 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2207 fat_rwlock_wrlock(&dpif->upcall_lock);
2208 dpif_netlink_recv_purge__(dpif);
2209 fat_rwlock_unlock(&dpif->upcall_lock);
2213 dpif_netlink_get_datapath_version(void)
2215 char *version_str = NULL;
2219 #define MAX_VERSION_STR_SIZE 80
2220 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2223 f = fopen(LINUX_DATAPATH_VERSION_FILE, "r");
2226 char version[MAX_VERSION_STR_SIZE];
2228 if (fgets(version, MAX_VERSION_STR_SIZE, f)) {
2229 newline = strchr(version, '\n');
2233 version_str = xstrdup(version);
2242 const struct dpif_class dpif_netlink_class = {
2244 dpif_netlink_enumerate,
2248 dpif_netlink_destroy,
2251 dpif_netlink_get_stats,
2252 dpif_netlink_port_add,
2253 dpif_netlink_port_del,
2254 dpif_netlink_port_query_by_number,
2255 dpif_netlink_port_query_by_name,
2256 dpif_netlink_port_get_pid,
2257 dpif_netlink_port_dump_start,
2258 dpif_netlink_port_dump_next,
2259 dpif_netlink_port_dump_done,
2260 dpif_netlink_port_poll,
2261 dpif_netlink_port_poll_wait,
2262 dpif_netlink_flow_flush,
2263 dpif_netlink_flow_dump_create,
2264 dpif_netlink_flow_dump_destroy,
2265 dpif_netlink_flow_dump_thread_create,
2266 dpif_netlink_flow_dump_thread_destroy,
2267 dpif_netlink_flow_dump_next,
2268 dpif_netlink_operate,
2269 dpif_netlink_recv_set,
2270 dpif_netlink_handlers_set,
2271 NULL, /* poll_thread_set */
2272 dpif_netlink_queue_to_priority,
2274 dpif_netlink_recv_wait,
2275 dpif_netlink_recv_purge,
2276 NULL, /* register_upcall_cb */
2277 NULL, /* enable_upcall */
2278 NULL, /* disable_upcall */
2279 dpif_netlink_get_datapath_version, /* get_datapath_version */
2283 dpif_netlink_init(void)
2285 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2288 if (ovsthread_once_start(&once)) {
2289 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
2290 &ovs_datapath_family);
2292 VLOG_ERR("Generic Netlink family '%s' does not exist. "
2293 "The Open vSwitch kernel module is probably not loaded.",
2294 OVS_DATAPATH_FAMILY);
2297 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
2300 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
2303 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
2304 &ovs_packet_family);
2307 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
2308 &ovs_vport_mcgroup);
2311 ovsthread_once_done(&once);
2318 dpif_netlink_is_internal_device(const char *name)
2320 struct dpif_netlink_vport reply;
2324 error = dpif_netlink_vport_get(name, &reply, &buf);
2327 } else if (error != ENODEV && error != ENOENT) {
2328 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
2329 name, ovs_strerror(error));
2332 return reply.type == OVS_VPORT_TYPE_INTERNAL;
2335 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2336 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
2337 * positive errno value.
2339 * 'vport' will contain pointers into 'buf', so the caller should not free
2340 * 'buf' while 'vport' is still in use. */
2342 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
2343 const struct ofpbuf *buf)
2345 static const struct nl_policy ovs_vport_policy[] = {
2346 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2347 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2348 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2349 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2350 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2352 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2355 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2356 struct ovs_header *ovs_header;
2357 struct nlmsghdr *nlmsg;
2358 struct genlmsghdr *genl;
2361 dpif_netlink_vport_init(vport);
2363 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2364 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2365 genl = ofpbuf_try_pull(&b, sizeof *genl);
2366 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2367 if (!nlmsg || !genl || !ovs_header
2368 || nlmsg->nlmsg_type != ovs_vport_family
2369 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2370 ARRAY_SIZE(ovs_vport_policy))) {
2374 vport->cmd = genl->cmd;
2375 vport->dp_ifindex = ovs_header->dp_ifindex;
2376 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2377 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2378 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2379 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2380 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2381 / (sizeof *vport->upcall_pids);
2382 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2385 if (a[OVS_VPORT_ATTR_STATS]) {
2386 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2388 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2389 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2390 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2395 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2396 * followed by Netlink attributes corresponding to 'vport'. */
2398 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
2401 struct ovs_header *ovs_header;
2403 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2404 vport->cmd, OVS_VPORT_VERSION);
2406 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2407 ovs_header->dp_ifindex = vport->dp_ifindex;
2409 if (vport->port_no != ODPP_NONE) {
2410 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2413 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2414 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2418 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2421 if (vport->upcall_pids) {
2422 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2424 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2428 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2429 vport->stats, sizeof *vport->stats);
2432 if (vport->options) {
2433 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2434 vport->options, vport->options_len);
2438 /* Clears 'vport' to "empty" values. */
2440 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
2442 memset(vport, 0, sizeof *vport);
2443 vport->port_no = ODPP_NONE;
2446 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2447 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2448 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2449 * result of the command is expected to be an ovs_vport also, which is decoded
2450 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2451 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2453 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
2454 struct dpif_netlink_vport *reply,
2455 struct ofpbuf **bufp)
2457 struct ofpbuf *request_buf;
2460 ovs_assert((reply != NULL) == (bufp != NULL));
2462 error = dpif_netlink_init();
2466 dpif_netlink_vport_init(reply);
2471 request_buf = ofpbuf_new(1024);
2472 dpif_netlink_vport_to_ofpbuf(request, request_buf);
2473 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2474 ofpbuf_delete(request_buf);
2478 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
2481 dpif_netlink_vport_init(reply);
2482 ofpbuf_delete(*bufp);
2489 /* Obtains information about the kernel vport named 'name' and stores it into
2490 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2491 * longer needed ('reply' will contain pointers into '*bufp'). */
2493 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
2494 struct ofpbuf **bufp)
2496 struct dpif_netlink_vport request;
2498 dpif_netlink_vport_init(&request);
2499 request.cmd = OVS_VPORT_CMD_GET;
2500 request.name = name;
2502 return dpif_netlink_vport_transact(&request, reply, bufp);
2505 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2506 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2507 * positive errno value.
2509 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2510 * while 'dp' is still in use. */
2512 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
2514 static const struct nl_policy ovs_datapath_policy[] = {
2515 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2516 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2518 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2519 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2523 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2524 struct ovs_header *ovs_header;
2525 struct nlmsghdr *nlmsg;
2526 struct genlmsghdr *genl;
2529 dpif_netlink_dp_init(dp);
2531 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2532 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2533 genl = ofpbuf_try_pull(&b, sizeof *genl);
2534 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2535 if (!nlmsg || !genl || !ovs_header
2536 || nlmsg->nlmsg_type != ovs_datapath_family
2537 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2538 ARRAY_SIZE(ovs_datapath_policy))) {
2542 dp->cmd = genl->cmd;
2543 dp->dp_ifindex = ovs_header->dp_ifindex;
2544 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2545 if (a[OVS_DP_ATTR_STATS]) {
2546 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
2549 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2550 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
2556 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2558 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
2560 struct ovs_header *ovs_header;
2562 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2563 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2564 OVS_DATAPATH_VERSION);
2566 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2567 ovs_header->dp_ifindex = dp->dp_ifindex;
2570 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2573 if (dp->upcall_pid) {
2574 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2577 if (dp->user_features) {
2578 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2581 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2584 /* Clears 'dp' to "empty" values. */
2586 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
2588 memset(dp, 0, sizeof *dp);
2592 dpif_netlink_dp_dump_start(struct nl_dump *dump)
2594 struct dpif_netlink_dp request;
2597 dpif_netlink_dp_init(&request);
2598 request.cmd = OVS_DP_CMD_GET;
2600 buf = ofpbuf_new(1024);
2601 dpif_netlink_dp_to_ofpbuf(&request, buf);
2602 nl_dump_start(dump, NETLINK_GENERIC, buf);
2606 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2607 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2608 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2609 * result of the command is expected to be of the same form, which is decoded
2610 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2611 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2613 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
2614 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
2616 struct ofpbuf *request_buf;
2619 ovs_assert((reply != NULL) == (bufp != NULL));
2621 request_buf = ofpbuf_new(1024);
2622 dpif_netlink_dp_to_ofpbuf(request, request_buf);
2623 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2624 ofpbuf_delete(request_buf);
2627 dpif_netlink_dp_init(reply);
2629 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
2632 ofpbuf_delete(*bufp);
2639 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2640 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2641 * will contain pointers into '*bufp'). */
2643 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
2644 struct ofpbuf **bufp)
2646 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2647 struct dpif_netlink_dp request;
2649 dpif_netlink_dp_init(&request);
2650 request.cmd = OVS_DP_CMD_GET;
2651 request.dp_ifindex = dpif->dp_ifindex;
2653 return dpif_netlink_dp_transact(&request, reply, bufp);
2656 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2657 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2658 * positive errno value.
2660 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2661 * while 'flow' is still in use. */
2663 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
2664 const struct ofpbuf *buf)
2666 static const struct nl_policy ovs_flow_policy[__OVS_FLOW_ATTR_MAX] = {
2667 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED, .optional = true },
2668 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2669 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2670 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2672 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2673 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2674 [OVS_FLOW_ATTR_UFID] = { .type = NL_A_UNSPEC, .optional = true,
2675 .min_len = sizeof(ovs_u128) },
2676 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2677 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
2678 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
2681 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2682 struct ovs_header *ovs_header;
2683 struct nlmsghdr *nlmsg;
2684 struct genlmsghdr *genl;
2687 dpif_netlink_flow_init(flow);
2689 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2690 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2691 genl = ofpbuf_try_pull(&b, sizeof *genl);
2692 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2693 if (!nlmsg || !genl || !ovs_header
2694 || nlmsg->nlmsg_type != ovs_flow_family
2695 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2696 ARRAY_SIZE(ovs_flow_policy))) {
2699 if (!a[OVS_FLOW_ATTR_KEY] && !a[OVS_FLOW_ATTR_UFID]) {
2703 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2704 flow->dp_ifindex = ovs_header->dp_ifindex;
2705 if (a[OVS_FLOW_ATTR_KEY]) {
2706 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2707 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2710 if (a[OVS_FLOW_ATTR_UFID]) {
2711 const ovs_u128 *ufid;
2713 ufid = nl_attr_get_unspec(a[OVS_FLOW_ATTR_UFID],
2714 nl_attr_get_size(a[OVS_FLOW_ATTR_UFID]));
2716 flow->ufid_present = true;
2718 if (a[OVS_FLOW_ATTR_MASK]) {
2719 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2720 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2722 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2723 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2724 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2726 if (a[OVS_FLOW_ATTR_STATS]) {
2727 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2729 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2730 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2732 if (a[OVS_FLOW_ATTR_USED]) {
2733 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2738 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2739 * followed by Netlink attributes corresponding to 'flow'. */
2741 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
2744 struct ovs_header *ovs_header;
2746 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2747 NLM_F_REQUEST | flow->nlmsg_flags,
2748 flow->cmd, OVS_FLOW_VERSION);
2750 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2751 ovs_header->dp_ifindex = flow->dp_ifindex;
2753 if (flow->ufid_present) {
2754 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_UFID, &flow->ufid,
2757 if (flow->ufid_terse) {
2758 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UFID_FLAGS,
2759 OVS_UFID_F_OMIT_KEY | OVS_UFID_F_OMIT_MASK
2760 | OVS_UFID_F_OMIT_ACTIONS);
2762 if (!flow->ufid_terse || !flow->ufid_present) {
2763 if (flow->key_len) {
2764 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY,
2765 flow->key, flow->key_len);
2768 if (flow->mask_len) {
2769 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK,
2770 flow->mask, flow->mask_len);
2772 if (flow->actions || flow->actions_len) {
2773 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2774 flow->actions, flow->actions_len);
2778 /* We never need to send these to the kernel. */
2779 ovs_assert(!flow->stats);
2780 ovs_assert(!flow->tcp_flags);
2781 ovs_assert(!flow->used);
2784 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2787 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
2791 /* Clears 'flow' to "empty" values. */
2793 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
2795 memset(flow, 0, sizeof *flow);
2798 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2799 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2800 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2801 * result of the command is expected to be a flow also, which is decoded and
2802 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2803 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2805 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
2806 struct dpif_netlink_flow *reply,
2807 struct ofpbuf **bufp)
2809 struct ofpbuf *request_buf;
2812 ovs_assert((reply != NULL) == (bufp != NULL));
2815 request->nlmsg_flags |= NLM_F_ECHO;
2818 request_buf = ofpbuf_new(1024);
2819 dpif_netlink_flow_to_ofpbuf(request, request_buf);
2820 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2821 ofpbuf_delete(request_buf);
2825 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
2828 dpif_netlink_flow_init(reply);
2829 ofpbuf_delete(*bufp);
2837 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
2838 struct dpif_flow_stats *stats)
2841 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
2842 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
2844 stats->n_packets = 0;
2847 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2848 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2851 /* Logs information about a packet that was recently lost in 'ch' (in
2854 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2855 uint32_t handler_id)
2857 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2860 if (VLOG_DROP_WARN(&rl)) {
2865 if (ch->last_poll != LLONG_MIN) {
2866 ds_put_format(&s, " (last polled %lld ms ago)",
2867 time_msec() - ch->last_poll);
2870 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2871 dpif_name(&dpif->dpif), ch_idx, handler_id);