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);
59 enum { MAX_PORTS = USHRT_MAX };
61 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
62 * missing if we have old headers. */
63 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
65 struct dpif_netlink_dp {
66 /* Generic Netlink header. */
69 /* struct ovs_header. */
73 const char *name; /* OVS_DP_ATTR_NAME. */
74 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
75 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
76 const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
77 const struct ovs_dp_megaflow_stats *megaflow_stats;
78 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
81 static void dpif_netlink_dp_init(struct dpif_netlink_dp *);
82 static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *,
83 const struct ofpbuf *);
84 static void dpif_netlink_dp_dump_start(struct nl_dump *);
85 static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
86 struct dpif_netlink_dp *reply,
87 struct ofpbuf **bufp);
88 static int dpif_netlink_dp_get(const struct dpif *,
89 struct dpif_netlink_dp *reply,
90 struct ofpbuf **bufp);
92 struct dpif_netlink_flow {
93 /* Generic Netlink header. */
96 /* struct ovs_header. */
97 unsigned int nlmsg_flags;
102 * The 'stats' member points to 64-bit data that might only be aligned on
103 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
106 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
107 * the Netlink version of the command, even if actions_len is zero. */
108 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
110 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
112 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
114 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
115 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
116 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
117 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
118 bool probe; /* OVS_FLOW_ATTR_PROBE. */
121 static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
122 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
123 const struct ofpbuf *);
124 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
126 static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
127 struct dpif_netlink_flow *reply,
128 struct ofpbuf **bufp);
129 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
130 struct dpif_flow_stats *);
131 static void dpif_netlink_flow_to_dpif_flow(struct dpif_flow *,
132 const struct dpif_netlink_flow *);
134 /* One of the dpif channels between the kernel and userspace. */
135 struct dpif_channel {
136 struct nl_sock *sock; /* Netlink socket. */
137 long long int last_poll; /* Last time this channel was polled. */
140 struct dpif_handler {
141 struct dpif_channel *channels;/* Array of channels for each handler. */
142 struct epoll_event *epoll_events;
143 int epoll_fd; /* epoll fd that includes channel socks. */
144 int n_events; /* Num events returned by epoll_wait(). */
145 int event_offset; /* Offset into 'epoll_events'. */
148 /* Datapath interface for the openvswitch Linux kernel module. */
149 struct dpif_netlink {
153 /* Upcall messages. */
154 struct fat_rwlock upcall_lock;
155 struct dpif_handler *handlers;
156 uint32_t n_handlers; /* Num of upcall handlers. */
157 int uc_array_size; /* Size of 'handler->channels' and */
158 /* 'handler->epoll_events'. */
160 /* Change notification. */
161 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
162 bool refresh_channels;
165 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
166 uint32_t ch_idx, uint32_t handler_id);
168 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
170 /* Generic Netlink family numbers for OVS.
172 * Initialized by dpif_netlink_init(). */
173 static int ovs_datapath_family;
174 static int ovs_vport_family;
175 static int ovs_flow_family;
176 static int ovs_packet_family;
178 /* Generic Netlink multicast groups for OVS.
180 * Initialized by dpif_netlink_init(). */
181 static unsigned int ovs_vport_mcgroup;
183 static int dpif_netlink_init(void);
184 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
185 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
186 odp_port_t port_no, uint32_t hash);
187 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
188 uint32_t n_handlers);
189 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
191 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
192 const struct ofpbuf *);
194 static struct dpif_netlink *
195 dpif_netlink_cast(const struct dpif *dpif)
197 dpif_assert_class(dpif, &dpif_netlink_class);
198 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
202 dpif_netlink_enumerate(struct sset *all_dps,
203 const struct dpif_class *dpif_class OVS_UNUSED)
206 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
207 struct ofpbuf msg, buf;
210 error = dpif_netlink_init();
215 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
216 dpif_netlink_dp_dump_start(&dump);
217 while (nl_dump_next(&dump, &msg, &buf)) {
218 struct dpif_netlink_dp dp;
220 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
221 sset_add(all_dps, dp.name);
225 return nl_dump_done(&dump);
229 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
230 bool create, struct dpif **dpifp)
232 struct dpif_netlink_dp dp_request, dp;
237 error = dpif_netlink_init();
242 /* Create or look up datapath. */
243 dpif_netlink_dp_init(&dp_request);
245 dp_request.cmd = OVS_DP_CMD_NEW;
247 dp_request.upcall_pid = &upcall_pid;
249 /* Use OVS_DP_CMD_SET to report user features */
250 dp_request.cmd = OVS_DP_CMD_SET;
252 dp_request.name = name;
253 dp_request.user_features |= OVS_DP_F_UNALIGNED;
254 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
255 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
260 error = open_dpif(&dp, dpifp);
266 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
268 struct dpif_netlink *dpif;
270 dpif = xzalloc(sizeof *dpif);
271 dpif->port_notifier = NULL;
272 fat_rwlock_init(&dpif->upcall_lock);
274 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
275 dp->dp_ifindex, dp->dp_ifindex);
277 dpif->dp_ifindex = dp->dp_ifindex;
278 *dpifp = &dpif->dpif;
283 /* Destroys the netlink sockets pointed by the elements in 'socksp'
284 * and frees the 'socksp'. */
286 vport_del_socksp(struct nl_sock **socksp, uint32_t n_socks)
290 for (i = 0; i < n_socks; i++) {
291 nl_sock_destroy(socksp[i]);
297 /* Creates an array of netlink sockets. Returns an array of the
298 * corresponding pointers. Records the error in 'error'. */
299 static struct nl_sock **
300 vport_create_socksp(uint32_t n_socks, int *error)
302 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
305 for (i = 0; i < n_socks; i++) {
306 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
315 vport_del_socksp(socksp, n_socks);
320 /* Given the array of pointers to netlink sockets 'socksp', returns
321 * the array of corresponding pids. If the 'socksp' is NULL, returns
322 * a single-element array of value 0. */
324 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
329 pids = xzalloc(sizeof *pids);
333 pids = xzalloc(n_socks * sizeof *pids);
334 for (i = 0; i < n_socks; i++) {
335 pids[i] = nl_sock_pid(socksp[i]);
342 /* Given the port number 'port_idx', extracts the pids of netlink sockets
343 * associated to the port and assigns it to 'upcall_pids'. */
345 vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
346 uint32_t **upcall_pids)
351 /* Since the nl_sock can only be assigned in either all
352 * or none "dpif->handlers" channels, the following check
354 if (!dpif->handlers[0].channels[port_idx].sock) {
358 pids = xzalloc(dpif->n_handlers * sizeof *pids);
360 for (i = 0; i < dpif->n_handlers; i++) {
361 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
370 vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
371 struct nl_sock **socksp)
373 struct epoll_event event;
374 uint32_t port_idx = odp_to_u32(port_no);
378 if (dpif->handlers == NULL) {
382 /* We assume that the datapath densely chooses port numbers, which can
383 * therefore be used as an index into 'channels' and 'epoll_events' of
384 * 'dpif->handler'. */
385 if (port_idx >= dpif->uc_array_size) {
386 uint32_t new_size = port_idx + 1;
388 if (new_size > MAX_PORTS) {
389 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
390 dpif_name(&dpif->dpif), port_no);
394 for (i = 0; i < dpif->n_handlers; i++) {
395 struct dpif_handler *handler = &dpif->handlers[i];
397 handler->channels = xrealloc(handler->channels,
398 new_size * sizeof *handler->channels);
400 for (j = dpif->uc_array_size; j < new_size; j++) {
401 handler->channels[j].sock = NULL;
404 handler->epoll_events = xrealloc(handler->epoll_events,
405 new_size * sizeof *handler->epoll_events);
408 dpif->uc_array_size = new_size;
411 memset(&event, 0, sizeof event);
412 event.events = EPOLLIN;
413 event.data.u32 = port_idx;
415 for (i = 0; i < dpif->n_handlers; i++) {
416 struct dpif_handler *handler = &dpif->handlers[i];
420 * XXX : Map appropiate Windows handle
423 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
429 dpif->handlers[i].channels[port_idx].sock = socksp[i];
430 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
436 for (j = 0; j < i; j++) {
439 * XXX : Map appropiate Windows handle
442 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
443 nl_sock_fd(socksp[j]), NULL);
445 dpif->handlers[j].channels[port_idx].sock = NULL;
452 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
454 uint32_t port_idx = odp_to_u32(port_no);
457 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
461 /* Since the sock can only be assigned in either all or none
462 * of "dpif->handlers" channels, the following check would
464 if (!dpif->handlers[0].channels[port_idx].sock) {
468 for (i = 0; i < dpif->n_handlers; i++) {
469 struct dpif_handler *handler = &dpif->handlers[i];
473 * XXX : Map appropiate Windows handle
476 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
477 nl_sock_fd(handler->channels[port_idx].sock), NULL);
479 nl_sock_destroy(handler->channels[port_idx].sock);
480 handler->channels[port_idx].sock = NULL;
481 handler->event_offset = handler->n_events = 0;
486 destroy_all_channels(struct dpif_netlink *dpif)
487 OVS_REQ_WRLOCK(dpif->upcall_lock)
491 if (!dpif->handlers) {
495 for (i = 0; i < dpif->uc_array_size; i++ ) {
496 struct dpif_netlink_vport vport_request;
497 uint32_t upcall_pids = 0;
499 /* Since the sock can only be assigned in either all or none
500 * of "dpif->handlers" channels, the following check would
502 if (!dpif->handlers[0].channels[i].sock) {
506 /* Turn off upcalls. */
507 dpif_netlink_vport_init(&vport_request);
508 vport_request.cmd = OVS_VPORT_CMD_SET;
509 vport_request.dp_ifindex = dpif->dp_ifindex;
510 vport_request.port_no = u32_to_odp(i);
511 vport_request.upcall_pids = &upcall_pids;
512 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
514 vport_del_channels(dpif, u32_to_odp(i));
517 for (i = 0; i < dpif->n_handlers; i++) {
518 struct dpif_handler *handler = &dpif->handlers[i];
522 * XXX : Map appropiate Windows handle
525 close(handler->epoll_fd);
527 free(handler->epoll_events);
528 free(handler->channels);
531 free(dpif->handlers);
532 dpif->handlers = NULL;
533 dpif->n_handlers = 0;
534 dpif->uc_array_size = 0;
538 dpif_netlink_close(struct dpif *dpif_)
540 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
542 nl_sock_destroy(dpif->port_notifier);
544 fat_rwlock_wrlock(&dpif->upcall_lock);
545 destroy_all_channels(dpif);
546 fat_rwlock_unlock(&dpif->upcall_lock);
548 fat_rwlock_destroy(&dpif->upcall_lock);
553 dpif_netlink_destroy(struct dpif *dpif_)
555 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
556 struct dpif_netlink_dp dp;
558 dpif_netlink_dp_init(&dp);
559 dp.cmd = OVS_DP_CMD_DEL;
560 dp.dp_ifindex = dpif->dp_ifindex;
561 return dpif_netlink_dp_transact(&dp, NULL, NULL);
565 dpif_netlink_run(struct dpif *dpif_)
567 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
569 if (dpif->refresh_channels) {
570 dpif->refresh_channels = false;
571 fat_rwlock_wrlock(&dpif->upcall_lock);
572 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
573 fat_rwlock_unlock(&dpif->upcall_lock);
578 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
580 struct dpif_netlink_dp dp;
584 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
586 memset(stats, 0, sizeof *stats);
589 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
590 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
591 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
592 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
595 if (dp.megaflow_stats) {
596 stats->n_masks = dp.megaflow_stats->n_masks;
597 stats->n_mask_hit = get_32aligned_u64(
598 &dp.megaflow_stats->n_mask_hit);
600 stats->n_masks = UINT32_MAX;
601 stats->n_mask_hit = UINT64_MAX;
609 get_vport_type(const struct dpif_netlink_vport *vport)
611 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
613 switch (vport->type) {
614 case OVS_VPORT_TYPE_NETDEV: {
615 const char *type = netdev_get_type_from_name(vport->name);
617 return type ? type : "system";
620 case OVS_VPORT_TYPE_INTERNAL:
623 case OVS_VPORT_TYPE_GENEVE:
626 case OVS_VPORT_TYPE_GRE:
629 case OVS_VPORT_TYPE_GRE64:
632 case OVS_VPORT_TYPE_VXLAN:
635 case OVS_VPORT_TYPE_LISP:
638 case OVS_VPORT_TYPE_UNSPEC:
639 case __OVS_VPORT_TYPE_MAX:
643 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
644 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
648 static enum ovs_vport_type
649 netdev_to_ovs_vport_type(const struct netdev *netdev)
651 const char *type = netdev_get_type(netdev);
653 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
654 return OVS_VPORT_TYPE_NETDEV;
655 } else if (!strcmp(type, "internal")) {
656 return OVS_VPORT_TYPE_INTERNAL;
657 } else if (!strcmp(type, "geneve")) {
658 return OVS_VPORT_TYPE_GENEVE;
659 } else if (strstr(type, "gre64")) {
660 return OVS_VPORT_TYPE_GRE64;
661 } else if (strstr(type, "gre")) {
662 return OVS_VPORT_TYPE_GRE;
663 } else if (!strcmp(type, "vxlan")) {
664 return OVS_VPORT_TYPE_VXLAN;
665 } else if (!strcmp(type, "lisp")) {
666 return OVS_VPORT_TYPE_LISP;
668 return OVS_VPORT_TYPE_UNSPEC;
673 dpif_netlink_port_add__(struct dpif_netlink *dpif, struct netdev *netdev,
674 odp_port_t *port_nop)
675 OVS_REQ_WRLOCK(dpif->upcall_lock)
677 const struct netdev_tunnel_config *tnl_cfg;
678 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
679 const char *name = netdev_vport_get_dpif_port(netdev,
680 namebuf, sizeof namebuf);
681 const char *type = netdev_get_type(netdev);
682 struct dpif_netlink_vport request, reply;
684 uint64_t options_stub[64 / 8];
685 struct ofpbuf options;
686 struct nl_sock **socksp = NULL;
687 uint32_t *upcall_pids;
690 if (dpif->handlers) {
691 socksp = vport_create_socksp(dpif->n_handlers, &error);
697 dpif_netlink_vport_init(&request);
698 request.cmd = OVS_VPORT_CMD_NEW;
699 request.dp_ifindex = dpif->dp_ifindex;
700 request.type = netdev_to_ovs_vport_type(netdev);
701 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
702 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
703 "unsupported type `%s'",
704 dpif_name(&dpif->dpif), name, type);
705 vport_del_socksp(socksp, dpif->n_handlers);
710 if (request.type == OVS_VPORT_TYPE_NETDEV) {
713 * XXX : Map appropiate Windows handle
716 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
720 tnl_cfg = netdev_get_tunnel_config(netdev);
721 if (tnl_cfg && tnl_cfg->dst_port != 0) {
722 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
723 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
724 ntohs(tnl_cfg->dst_port));
725 request.options = ofpbuf_data(&options);
726 request.options_len = ofpbuf_size(&options);
729 request.port_no = *port_nop;
730 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
731 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
732 request.upcall_pids = upcall_pids;
734 error = dpif_netlink_vport_transact(&request, &reply, &buf);
736 *port_nop = reply.port_no;
738 if (error == EBUSY && *port_nop != ODPP_NONE) {
739 VLOG_INFO("%s: requested port %"PRIu32" is in use",
740 dpif_name(&dpif->dpif), *port_nop);
743 vport_del_socksp(socksp, dpif->n_handlers);
748 error = vport_add_channels(dpif, *port_nop, socksp);
750 VLOG_INFO("%s: could not add channel for port %s",
751 dpif_name(&dpif->dpif), name);
753 /* Delete the port. */
754 dpif_netlink_vport_init(&request);
755 request.cmd = OVS_VPORT_CMD_DEL;
756 request.dp_ifindex = dpif->dp_ifindex;
757 request.port_no = *port_nop;
758 dpif_netlink_vport_transact(&request, NULL, NULL);
759 vport_del_socksp(socksp, dpif->n_handlers);
773 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
774 odp_port_t *port_nop)
776 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
779 fat_rwlock_wrlock(&dpif->upcall_lock);
780 error = dpif_netlink_port_add__(dpif, netdev, port_nop);
781 fat_rwlock_unlock(&dpif->upcall_lock);
787 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
788 OVS_REQ_WRLOCK(dpif->upcall_lock)
790 struct dpif_netlink_vport vport;
793 dpif_netlink_vport_init(&vport);
794 vport.cmd = OVS_VPORT_CMD_DEL;
795 vport.dp_ifindex = dpif->dp_ifindex;
796 vport.port_no = port_no;
797 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
799 vport_del_channels(dpif, port_no);
805 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
807 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
810 fat_rwlock_wrlock(&dpif->upcall_lock);
811 error = dpif_netlink_port_del__(dpif, port_no);
812 fat_rwlock_unlock(&dpif->upcall_lock);
818 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
819 const char *port_name, struct dpif_port *dpif_port)
821 struct dpif_netlink_vport request;
822 struct dpif_netlink_vport reply;
826 dpif_netlink_vport_init(&request);
827 request.cmd = OVS_VPORT_CMD_GET;
828 request.dp_ifindex = dpif->dp_ifindex;
829 request.port_no = port_no;
830 request.name = port_name;
832 error = dpif_netlink_vport_transact(&request, &reply, &buf);
834 if (reply.dp_ifindex != request.dp_ifindex) {
835 /* A query by name reported that 'port_name' is in some datapath
836 * other than 'dpif', but the caller wants to know about 'dpif'. */
838 } else if (dpif_port) {
839 dpif_port->name = xstrdup(reply.name);
840 dpif_port->type = xstrdup(get_vport_type(&reply));
841 dpif_port->port_no = reply.port_no;
849 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
850 struct dpif_port *dpif_port)
852 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
854 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
858 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
859 struct dpif_port *dpif_port)
861 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
863 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
867 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
868 odp_port_t port_no, uint32_t hash)
869 OVS_REQ_RDLOCK(dpif->upcall_lock)
871 uint32_t port_idx = odp_to_u32(port_no);
874 if (dpif->handlers && dpif->uc_array_size > 0) {
875 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
876 * channel, since it is not heavily loaded. */
877 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
878 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
880 /* Needs to check in case the socket pointer is changed in between
881 * the holding of upcall_lock. A known case happens when the main
882 * thread deletes the vport while the handler thread is handling
883 * the upcall from that port. */
884 if (h->channels[idx].sock) {
885 pid = nl_sock_pid(h->channels[idx].sock);
893 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
896 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
899 fat_rwlock_rdlock(&dpif->upcall_lock);
900 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
901 fat_rwlock_unlock(&dpif->upcall_lock);
907 dpif_netlink_flow_flush(struct dpif *dpif_)
909 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
910 struct dpif_netlink_flow flow;
912 dpif_netlink_flow_init(&flow);
913 flow.cmd = OVS_FLOW_CMD_DEL;
914 flow.dp_ifindex = dpif->dp_ifindex;
915 return dpif_netlink_flow_transact(&flow, NULL, NULL);
918 struct dpif_netlink_port_state {
924 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
925 struct nl_dump *dump)
927 struct dpif_netlink_vport request;
930 dpif_netlink_vport_init(&request);
931 request.cmd = OVS_VPORT_CMD_GET;
932 request.dp_ifindex = dpif->dp_ifindex;
934 buf = ofpbuf_new(1024);
935 dpif_netlink_vport_to_ofpbuf(&request, buf);
936 nl_dump_start(dump, NETLINK_GENERIC, buf);
941 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
943 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
944 struct dpif_netlink_port_state *state;
946 *statep = state = xmalloc(sizeof *state);
947 dpif_netlink_port_dump_start__(dpif, &state->dump);
949 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
954 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
955 struct nl_dump *dump,
956 struct dpif_netlink_vport *vport,
957 struct ofpbuf *buffer)
962 if (!nl_dump_next(dump, &buf, buffer)) {
966 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
968 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
969 dpif_name(&dpif->dpif), ovs_strerror(error));
975 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
976 struct dpif_port *dpif_port)
978 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
979 struct dpif_netlink_port_state *state = state_;
980 struct dpif_netlink_vport vport;
983 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
988 dpif_port->name = CONST_CAST(char *, vport.name);
989 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
990 dpif_port->port_no = vport.port_no;
995 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
997 struct dpif_netlink_port_state *state = state_;
998 int error = nl_dump_done(&state->dump);
1000 ofpbuf_uninit(&state->buf);
1006 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1008 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1010 /* Lazily create the Netlink socket to listen for notifications. */
1011 if (!dpif->port_notifier) {
1012 struct nl_sock *sock;
1015 error = nl_sock_create(NETLINK_GENERIC, &sock);
1020 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1022 nl_sock_destroy(sock);
1025 dpif->port_notifier = sock;
1027 /* We have no idea of the current state so report that everything
1033 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1034 uint64_t buf_stub[4096 / 8];
1038 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1039 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1041 struct dpif_netlink_vport vport;
1043 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1045 if (vport.dp_ifindex == dpif->dp_ifindex
1046 && (vport.cmd == OVS_VPORT_CMD_NEW
1047 || vport.cmd == OVS_VPORT_CMD_DEL
1048 || vport.cmd == OVS_VPORT_CMD_SET)) {
1049 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1050 dpif->dpif.full_name, vport.name, vport.cmd);
1051 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1052 dpif->refresh_channels = true;
1054 *devnamep = xstrdup(vport.name);
1055 ofpbuf_uninit(&buf);
1059 } else if (error != EAGAIN) {
1060 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1061 ovs_strerror(error));
1062 nl_sock_drain(dpif->port_notifier);
1066 ofpbuf_uninit(&buf);
1074 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1076 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1078 if (dpif->port_notifier) {
1079 nl_sock_wait(dpif->port_notifier, POLLIN);
1081 poll_immediate_wake();
1086 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1087 const struct nlattr *key, size_t key_len,
1088 struct dpif_netlink_flow *request)
1090 dpif_netlink_flow_init(request);
1091 request->cmd = OVS_FLOW_CMD_GET;
1092 request->dp_ifindex = dpif->dp_ifindex;
1094 request->key_len = key_len;
1098 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1099 const struct nlattr *key, size_t key_len,
1100 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1102 struct dpif_netlink_flow request;
1104 dpif_netlink_init_flow_get(dpif, key, key_len, &request);
1105 return dpif_netlink_flow_transact(&request, reply, bufp);
1109 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1110 const struct dpif_flow_put *put,
1111 struct dpif_netlink_flow *request)
1113 static const struct nlattr dummy_action;
1115 dpif_netlink_flow_init(request);
1116 request->cmd = (put->flags & DPIF_FP_CREATE
1117 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1118 request->dp_ifindex = dpif->dp_ifindex;
1119 request->key = put->key;
1120 request->key_len = put->key_len;
1121 request->mask = put->mask;
1122 request->mask_len = put->mask_len;
1123 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1124 request->actions = (put->actions
1126 : CONST_CAST(struct nlattr *, &dummy_action));
1127 request->actions_len = put->actions_len;
1128 if (put->flags & DPIF_FP_ZERO_STATS) {
1129 request->clear = true;
1131 if (put->flags & DPIF_FP_PROBE) {
1132 request->probe = true;
1134 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1138 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1139 const struct dpif_flow_del *del,
1140 struct dpif_netlink_flow *request)
1142 dpif_netlink_flow_init(request);
1143 request->cmd = OVS_FLOW_CMD_DEL;
1144 request->dp_ifindex = dpif->dp_ifindex;
1145 request->key = del->key;
1146 request->key_len = del->key_len;
1149 struct dpif_netlink_flow_dump {
1150 struct dpif_flow_dump up;
1151 struct nl_dump nl_dump;
1155 static struct dpif_netlink_flow_dump *
1156 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1158 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1161 static struct dpif_flow_dump *
1162 dpif_netlink_flow_dump_create(const struct dpif *dpif_)
1164 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1165 struct dpif_netlink_flow_dump *dump;
1166 struct dpif_netlink_flow request;
1169 dump = xmalloc(sizeof *dump);
1170 dpif_flow_dump_init(&dump->up, dpif_);
1172 dpif_netlink_flow_init(&request);
1173 request.cmd = OVS_FLOW_CMD_GET;
1174 request.dp_ifindex = dpif->dp_ifindex;
1176 buf = ofpbuf_new(1024);
1177 dpif_netlink_flow_to_ofpbuf(&request, buf);
1178 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1180 atomic_init(&dump->status, 0);
1186 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1188 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1189 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1192 /* No other thread has access to 'dump' at this point. */
1193 atomic_read_relaxed(&dump->status, &dump_status);
1195 return dump_status ? dump_status : nl_status;
1198 struct dpif_netlink_flow_dump_thread {
1199 struct dpif_flow_dump_thread up;
1200 struct dpif_netlink_flow_dump *dump;
1201 struct dpif_netlink_flow flow;
1202 struct dpif_flow_stats stats;
1203 struct ofpbuf nl_flows; /* Always used to store flows. */
1204 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1207 static struct dpif_netlink_flow_dump_thread *
1208 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1210 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1213 static struct dpif_flow_dump_thread *
1214 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1216 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1217 struct dpif_netlink_flow_dump_thread *thread;
1219 thread = xmalloc(sizeof *thread);
1220 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1221 thread->dump = dump;
1222 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1223 thread->nl_actions = NULL;
1229 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1231 struct dpif_netlink_flow_dump_thread *thread
1232 = dpif_netlink_flow_dump_thread_cast(thread_);
1234 ofpbuf_uninit(&thread->nl_flows);
1235 ofpbuf_delete(thread->nl_actions);
1240 dpif_netlink_flow_to_dpif_flow(struct dpif_flow *dpif_flow,
1241 const struct dpif_netlink_flow *datapath_flow)
1243 dpif_flow->key = datapath_flow->key;
1244 dpif_flow->key_len = datapath_flow->key_len;
1245 dpif_flow->mask = datapath_flow->mask;
1246 dpif_flow->mask_len = datapath_flow->mask_len;
1247 dpif_flow->actions = datapath_flow->actions;
1248 dpif_flow->actions_len = datapath_flow->actions_len;
1249 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1253 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1254 struct dpif_flow *flows, int max_flows)
1256 struct dpif_netlink_flow_dump_thread *thread
1257 = dpif_netlink_flow_dump_thread_cast(thread_);
1258 struct dpif_netlink_flow_dump *dump = thread->dump;
1259 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1262 ofpbuf_delete(thread->nl_actions);
1263 thread->nl_actions = NULL;
1267 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1268 struct dpif_netlink_flow datapath_flow;
1269 struct ofpbuf nl_flow;
1272 /* Try to grab another flow. */
1273 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1277 /* Convert the flow to our output format. */
1278 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1280 atomic_store_relaxed(&dump->status, error);
1284 if (datapath_flow.actions) {
1285 /* Common case: the flow includes actions. */
1286 dpif_netlink_flow_to_dpif_flow(&flows[n_flows++], &datapath_flow);
1288 /* Rare case: the flow does not include actions. Retrieve this
1289 * individual flow again to get the actions. */
1290 error = dpif_netlink_flow_get(dpif, datapath_flow.key,
1291 datapath_flow.key_len,
1292 &datapath_flow, &thread->nl_actions);
1293 if (error == ENOENT) {
1294 VLOG_DBG("dumped flow disappeared on get");
1297 VLOG_WARN("error fetching dumped flow: %s",
1298 ovs_strerror(error));
1299 atomic_store_relaxed(&dump->status, error);
1303 /* Save this flow. Then exit, because we only have one buffer to
1304 * handle this case. */
1305 dpif_netlink_flow_to_dpif_flow(&flows[n_flows++], &datapath_flow);
1313 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1316 struct ovs_header *k_exec;
1319 ofpbuf_prealloc_tailroom(buf, (64
1320 + ofpbuf_size(d_exec->packet)
1321 + ODP_KEY_METADATA_SIZE
1322 + d_exec->actions_len));
1324 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1325 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1327 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1328 k_exec->dp_ifindex = dp_ifindex;
1330 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1331 ofpbuf_data(d_exec->packet),
1332 ofpbuf_size(d_exec->packet));
1334 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1335 odp_key_from_pkt_metadata(buf, &d_exec->md);
1336 nl_msg_end_nested(buf, key_ofs);
1338 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1339 d_exec->actions, d_exec->actions_len);
1340 if (d_exec->probe) {
1341 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
1348 dpif_netlink_operate__(struct dpif_netlink *dpif,
1349 struct dpif_op **ops, size_t n_ops)
1352 struct nl_transaction txn;
1354 struct ofpbuf request;
1355 uint64_t request_stub[1024 / 8];
1357 struct ofpbuf reply;
1358 uint64_t reply_stub[1024 / 8];
1361 struct nl_transaction *txnsp[MAX_OPS];
1364 ovs_assert(n_ops <= MAX_OPS);
1365 for (i = 0; i < n_ops; i++) {
1366 struct op_auxdata *aux = &auxes[i];
1367 struct dpif_op *op = ops[i];
1368 struct dpif_flow_put *put;
1369 struct dpif_flow_del *del;
1370 struct dpif_execute *execute;
1371 struct dpif_flow_get *get;
1372 struct dpif_netlink_flow flow;
1374 ofpbuf_use_stub(&aux->request,
1375 aux->request_stub, sizeof aux->request_stub);
1376 aux->txn.request = &aux->request;
1378 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1379 aux->txn.reply = NULL;
1382 case DPIF_OP_FLOW_PUT:
1383 put = &op->u.flow_put;
1384 dpif_netlink_init_flow_put(dpif, put, &flow);
1386 flow.nlmsg_flags |= NLM_F_ECHO;
1387 aux->txn.reply = &aux->reply;
1389 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1392 case DPIF_OP_FLOW_DEL:
1393 del = &op->u.flow_del;
1394 dpif_netlink_init_flow_del(dpif, del, &flow);
1396 flow.nlmsg_flags |= NLM_F_ECHO;
1397 aux->txn.reply = &aux->reply;
1399 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1402 case DPIF_OP_EXECUTE:
1403 execute = &op->u.execute;
1404 dpif_netlink_encode_execute(dpif->dp_ifindex, execute,
1408 case DPIF_OP_FLOW_GET:
1409 get = &op->u.flow_get;
1410 dpif_netlink_init_flow_get(dpif, get->key, get->key_len, &flow);
1411 aux->txn.reply = get->buffer;
1412 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1420 for (i = 0; i < n_ops; i++) {
1421 txnsp[i] = &auxes[i].txn;
1423 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1425 for (i = 0; i < n_ops; i++) {
1426 struct op_auxdata *aux = &auxes[i];
1427 struct nl_transaction *txn = &auxes[i].txn;
1428 struct dpif_op *op = ops[i];
1429 struct dpif_flow_put *put;
1430 struct dpif_flow_del *del;
1431 struct dpif_flow_get *get;
1433 op->error = txn->error;
1436 case DPIF_OP_FLOW_PUT:
1437 put = &op->u.flow_put;
1440 struct dpif_netlink_flow reply;
1442 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1445 dpif_netlink_flow_get_stats(&reply, put->stats);
1451 case DPIF_OP_FLOW_DEL:
1452 del = &op->u.flow_del;
1455 struct dpif_netlink_flow reply;
1457 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1460 dpif_netlink_flow_get_stats(&reply, del->stats);
1466 case DPIF_OP_EXECUTE:
1469 case DPIF_OP_FLOW_GET:
1470 get = &op->u.flow_get;
1472 struct dpif_netlink_flow reply;
1474 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
1476 dpif_netlink_flow_to_dpif_flow(get->flow, &reply);
1485 ofpbuf_uninit(&aux->request);
1486 ofpbuf_uninit(&aux->reply);
1491 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1493 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1496 size_t chunk = MIN(n_ops, MAX_OPS);
1497 dpif_netlink_operate__(dpif, ops, chunk);
1503 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1504 * currently in 'dpif' in the kernel, by adding a new set of channels for
1505 * any kernel vport that lacks one and deleting any channels that have no
1506 * backing kernel vports. */
1508 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
1509 OVS_REQ_WRLOCK(dpif->upcall_lock)
1511 unsigned long int *keep_channels;
1512 struct dpif_netlink_vport vport;
1513 size_t keep_channels_nbits;
1514 struct nl_dump dump;
1515 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1520 if (dpif->n_handlers != n_handlers) {
1521 destroy_all_channels(dpif);
1522 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1523 for (i = 0; i < n_handlers; i++) {
1524 struct dpif_handler *handler = &dpif->handlers[i];
1528 * XXX : Map appropiate Windows handle
1531 handler->epoll_fd = epoll_create(10);
1532 if (handler->epoll_fd < 0) {
1535 for (j = 0; j < i; j++) {
1536 close(dpif->handlers[j].epoll_fd);
1538 free(dpif->handlers);
1539 dpif->handlers = NULL;
1545 dpif->n_handlers = n_handlers;
1548 for (i = 0; i < n_handlers; i++) {
1549 struct dpif_handler *handler = &dpif->handlers[i];
1551 handler->event_offset = handler->n_events = 0;
1554 keep_channels_nbits = dpif->uc_array_size;
1555 keep_channels = bitmap_allocate(keep_channels_nbits);
1557 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1558 dpif_netlink_port_dump_start__(dpif, &dump);
1559 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
1560 uint32_t port_no = odp_to_u32(vport.port_no);
1561 uint32_t *upcall_pids = NULL;
1564 if (port_no >= dpif->uc_array_size
1565 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1566 struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
1573 error = vport_add_channels(dpif, vport.port_no, socksp);
1575 VLOG_INFO("%s: could not add channels for port %s",
1576 dpif_name(&dpif->dpif), vport.name);
1577 vport_del_socksp(socksp, dpif->n_handlers);
1581 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1585 /* Configure the vport to deliver misses to 'sock'. */
1586 if (vport.upcall_pids[0] == 0
1587 || vport.n_upcall_pids != dpif->n_handlers
1588 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1590 struct dpif_netlink_vport vport_request;
1592 dpif_netlink_vport_init(&vport_request);
1593 vport_request.cmd = OVS_VPORT_CMD_SET;
1594 vport_request.dp_ifindex = dpif->dp_ifindex;
1595 vport_request.port_no = vport.port_no;
1596 vport_request.n_upcall_pids = dpif->n_handlers;
1597 vport_request.upcall_pids = upcall_pids;
1598 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
1600 VLOG_WARN_RL(&error_rl,
1601 "%s: failed to set upcall pid on port: %s",
1602 dpif_name(&dpif->dpif), ovs_strerror(error));
1604 if (error != ENODEV && error != ENOENT) {
1607 /* The vport isn't really there, even though the dump says
1608 * it is. Probably we just hit a race after a port
1615 if (port_no < keep_channels_nbits) {
1616 bitmap_set1(keep_channels, port_no);
1623 vport_del_channels(dpif, vport.port_no);
1625 nl_dump_done(&dump);
1626 ofpbuf_uninit(&buf);
1628 /* Discard any saved channels that we didn't reuse. */
1629 for (i = 0; i < keep_channels_nbits; i++) {
1630 if (!bitmap_is_set(keep_channels, i)) {
1631 vport_del_channels(dpif, u32_to_odp(i));
1634 free(keep_channels);
1640 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
1641 OVS_REQ_WRLOCK(dpif->upcall_lock)
1643 if ((dpif->handlers != NULL) == enable) {
1645 } else if (!enable) {
1646 destroy_all_channels(dpif);
1649 return dpif_netlink_refresh_channels(dpif, 1);
1654 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
1656 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1659 fat_rwlock_wrlock(&dpif->upcall_lock);
1660 error = dpif_netlink_recv_set__(dpif, enable);
1661 fat_rwlock_unlock(&dpif->upcall_lock);
1667 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1669 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1672 fat_rwlock_wrlock(&dpif->upcall_lock);
1673 if (dpif->handlers) {
1674 error = dpif_netlink_refresh_channels(dpif, n_handlers);
1676 fat_rwlock_unlock(&dpif->upcall_lock);
1682 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1683 uint32_t queue_id, uint32_t *priority)
1685 if (queue_id < 0xf000) {
1686 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1694 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1697 static const struct nl_policy ovs_packet_policy[] = {
1698 /* Always present. */
1699 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1700 .min_len = ETH_HEADER_LEN },
1701 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1703 /* OVS_PACKET_CMD_ACTION only. */
1704 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1705 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
1708 struct ovs_header *ovs_header;
1709 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1710 struct nlmsghdr *nlmsg;
1711 struct genlmsghdr *genl;
1715 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1717 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1718 genl = ofpbuf_try_pull(&b, sizeof *genl);
1719 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1720 if (!nlmsg || !genl || !ovs_header
1721 || nlmsg->nlmsg_type != ovs_packet_family
1722 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1723 ARRAY_SIZE(ovs_packet_policy))) {
1727 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1728 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1734 /* (Re)set ALL fields of '*upcall' on successful return. */
1735 upcall->type = type;
1736 upcall->key = CONST_CAST(struct nlattr *,
1737 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1738 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1739 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1740 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
1742 /* Allow overwriting the netlink attribute header without reallocating. */
1743 ofpbuf_use_stub(&upcall->packet,
1744 CONST_CAST(struct nlattr *,
1745 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1746 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1747 sizeof(struct nlattr));
1748 ofpbuf_set_data(&upcall->packet,
1749 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1750 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1752 *dp_ifindex = ovs_header->dp_ifindex;
1758 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
1759 struct dpif_upcall *upcall, struct ofpbuf *buf)
1760 OVS_REQ_RDLOCK(dpif->upcall_lock)
1762 struct dpif_handler *handler;
1765 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1769 handler = &dpif->handlers[handler_id];
1770 if (handler->event_offset >= handler->n_events) {
1773 handler->event_offset = handler->n_events = 0;
1776 retval = dpif->uc_array_size;
1777 handler->event_offset = 0;
1780 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
1781 dpif->uc_array_size, 0);
1782 } while (retval < 0 && errno == EINTR);
1785 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1786 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1787 } else if (retval > 0) {
1788 handler->n_events = retval;
1792 while (handler->event_offset < handler->n_events) {
1793 int idx = handler->epoll_events[handler->event_offset].data.u32;
1794 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
1796 handler->event_offset++;
1802 if (++read_tries > 50) {
1806 error = nl_sock_recv(ch->sock, buf, false);
1807 if (error == ENOBUFS) {
1808 /* ENOBUFS typically means that we've received so many
1809 * packets that the buffer overflowed. Try again
1810 * immediately because there's almost certainly a packet
1811 * waiting for us. */
1812 report_loss(dpif, ch, idx, handler_id);
1816 ch->last_poll = time_msec();
1818 if (error == EAGAIN) {
1824 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1825 if (!error && dp_ifindex == dpif->dp_ifindex) {
1837 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
1838 struct dpif_upcall *upcall, struct ofpbuf *buf)
1840 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1843 fat_rwlock_rdlock(&dpif->upcall_lock);
1844 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
1845 fat_rwlock_unlock(&dpif->upcall_lock);
1851 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
1852 OVS_REQ_RDLOCK(dpif->upcall_lock)
1856 * XXX : Map appropiate Windows handle
1859 if (dpif->handlers && handler_id < dpif->n_handlers) {
1860 struct dpif_handler *handler = &dpif->handlers[handler_id];
1862 poll_fd_wait(handler->epoll_fd, POLLIN);
1868 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1870 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1872 fat_rwlock_rdlock(&dpif->upcall_lock);
1873 dpif_netlink_recv_wait__(dpif, handler_id);
1874 fat_rwlock_unlock(&dpif->upcall_lock);
1878 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
1879 OVS_REQ_WRLOCK(dpif->upcall_lock)
1881 if (dpif->handlers) {
1884 for (i = 0; i < dpif->uc_array_size; i++ ) {
1885 if (!dpif->handlers[0].channels[i].sock) {
1889 for (j = 0; j < dpif->n_handlers; j++) {
1890 nl_sock_drain(dpif->handlers[j].channels[i].sock);
1897 dpif_netlink_recv_purge(struct dpif *dpif_)
1899 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1901 fat_rwlock_wrlock(&dpif->upcall_lock);
1902 dpif_netlink_recv_purge__(dpif);
1903 fat_rwlock_unlock(&dpif->upcall_lock);
1906 const struct dpif_class dpif_netlink_class = {
1908 dpif_netlink_enumerate,
1912 dpif_netlink_destroy,
1915 dpif_netlink_get_stats,
1916 dpif_netlink_port_add,
1917 dpif_netlink_port_del,
1918 dpif_netlink_port_query_by_number,
1919 dpif_netlink_port_query_by_name,
1920 dpif_netlink_port_get_pid,
1921 dpif_netlink_port_dump_start,
1922 dpif_netlink_port_dump_next,
1923 dpif_netlink_port_dump_done,
1924 dpif_netlink_port_poll,
1925 dpif_netlink_port_poll_wait,
1926 dpif_netlink_flow_flush,
1927 dpif_netlink_flow_dump_create,
1928 dpif_netlink_flow_dump_destroy,
1929 dpif_netlink_flow_dump_thread_create,
1930 dpif_netlink_flow_dump_thread_destroy,
1931 dpif_netlink_flow_dump_next,
1932 dpif_netlink_operate,
1933 dpif_netlink_recv_set,
1934 dpif_netlink_handlers_set,
1935 NULL, /* poll_thread_set */
1936 dpif_netlink_queue_to_priority,
1938 dpif_netlink_recv_wait,
1939 dpif_netlink_recv_purge,
1940 NULL, /* register_upcall_cb */
1941 NULL, /* enable_upcall */
1942 NULL, /* disable_upcall */
1946 dpif_netlink_init(void)
1948 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1951 if (ovsthread_once_start(&once)) {
1952 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1953 &ovs_datapath_family);
1955 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1956 "The Open vSwitch kernel module is probably not loaded.",
1957 OVS_DATAPATH_FAMILY);
1960 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1963 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1966 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1967 &ovs_packet_family);
1970 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1971 &ovs_vport_mcgroup);
1974 ovsthread_once_done(&once);
1981 dpif_netlink_is_internal_device(const char *name)
1983 struct dpif_netlink_vport reply;
1987 error = dpif_netlink_vport_get(name, &reply, &buf);
1990 } else if (error != ENODEV && error != ENOENT) {
1991 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1992 name, ovs_strerror(error));
1995 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1998 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1999 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
2000 * positive errno value.
2002 * 'vport' will contain pointers into 'buf', so the caller should not free
2003 * 'buf' while 'vport' is still in use. */
2005 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
2006 const struct ofpbuf *buf)
2008 static const struct nl_policy ovs_vport_policy[] = {
2009 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2010 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2011 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2012 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2013 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2015 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2018 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2019 struct ovs_header *ovs_header;
2020 struct nlmsghdr *nlmsg;
2021 struct genlmsghdr *genl;
2024 dpif_netlink_vport_init(vport);
2026 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2027 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2028 genl = ofpbuf_try_pull(&b, sizeof *genl);
2029 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2030 if (!nlmsg || !genl || !ovs_header
2031 || nlmsg->nlmsg_type != ovs_vport_family
2032 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2033 ARRAY_SIZE(ovs_vport_policy))) {
2037 vport->cmd = genl->cmd;
2038 vport->dp_ifindex = ovs_header->dp_ifindex;
2039 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2040 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2041 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2042 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2043 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2044 / (sizeof *vport->upcall_pids);
2045 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2048 if (a[OVS_VPORT_ATTR_STATS]) {
2049 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2051 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2052 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2053 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2058 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2059 * followed by Netlink attributes corresponding to 'vport'. */
2061 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
2064 struct ovs_header *ovs_header;
2066 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2067 vport->cmd, OVS_VPORT_VERSION);
2069 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2070 ovs_header->dp_ifindex = vport->dp_ifindex;
2072 if (vport->port_no != ODPP_NONE) {
2073 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2076 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2077 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2081 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2084 if (vport->upcall_pids) {
2085 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2087 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2091 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2092 vport->stats, sizeof *vport->stats);
2095 if (vport->options) {
2096 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2097 vport->options, vport->options_len);
2101 /* Clears 'vport' to "empty" values. */
2103 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
2105 memset(vport, 0, sizeof *vport);
2106 vport->port_no = ODPP_NONE;
2109 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2110 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2111 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2112 * result of the command is expected to be an ovs_vport also, which is decoded
2113 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2114 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2116 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
2117 struct dpif_netlink_vport *reply,
2118 struct ofpbuf **bufp)
2120 struct ofpbuf *request_buf;
2123 ovs_assert((reply != NULL) == (bufp != NULL));
2125 error = dpif_netlink_init();
2129 dpif_netlink_vport_init(reply);
2134 request_buf = ofpbuf_new(1024);
2135 dpif_netlink_vport_to_ofpbuf(request, request_buf);
2136 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2137 ofpbuf_delete(request_buf);
2141 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
2144 dpif_netlink_vport_init(reply);
2145 ofpbuf_delete(*bufp);
2152 /* Obtains information about the kernel vport named 'name' and stores it into
2153 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2154 * longer needed ('reply' will contain pointers into '*bufp'). */
2156 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
2157 struct ofpbuf **bufp)
2159 struct dpif_netlink_vport request;
2161 dpif_netlink_vport_init(&request);
2162 request.cmd = OVS_VPORT_CMD_GET;
2163 request.name = name;
2165 return dpif_netlink_vport_transact(&request, reply, bufp);
2168 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2169 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2170 * positive errno value.
2172 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2173 * while 'dp' is still in use. */
2175 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
2177 static const struct nl_policy ovs_datapath_policy[] = {
2178 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2179 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2181 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2182 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2186 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2187 struct ovs_header *ovs_header;
2188 struct nlmsghdr *nlmsg;
2189 struct genlmsghdr *genl;
2192 dpif_netlink_dp_init(dp);
2194 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2195 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2196 genl = ofpbuf_try_pull(&b, sizeof *genl);
2197 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2198 if (!nlmsg || !genl || !ovs_header
2199 || nlmsg->nlmsg_type != ovs_datapath_family
2200 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2201 ARRAY_SIZE(ovs_datapath_policy))) {
2205 dp->cmd = genl->cmd;
2206 dp->dp_ifindex = ovs_header->dp_ifindex;
2207 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2208 if (a[OVS_DP_ATTR_STATS]) {
2209 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
2212 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2213 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
2219 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2221 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
2223 struct ovs_header *ovs_header;
2225 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2226 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2227 OVS_DATAPATH_VERSION);
2229 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2230 ovs_header->dp_ifindex = dp->dp_ifindex;
2233 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2236 if (dp->upcall_pid) {
2237 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2240 if (dp->user_features) {
2241 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2244 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2247 /* Clears 'dp' to "empty" values. */
2249 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
2251 memset(dp, 0, sizeof *dp);
2255 dpif_netlink_dp_dump_start(struct nl_dump *dump)
2257 struct dpif_netlink_dp request;
2260 dpif_netlink_dp_init(&request);
2261 request.cmd = OVS_DP_CMD_GET;
2263 buf = ofpbuf_new(1024);
2264 dpif_netlink_dp_to_ofpbuf(&request, buf);
2265 nl_dump_start(dump, NETLINK_GENERIC, buf);
2269 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2270 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2271 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2272 * result of the command is expected to be of the same form, which is decoded
2273 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2274 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2276 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
2277 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
2279 struct ofpbuf *request_buf;
2282 ovs_assert((reply != NULL) == (bufp != NULL));
2284 request_buf = ofpbuf_new(1024);
2285 dpif_netlink_dp_to_ofpbuf(request, request_buf);
2286 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2287 ofpbuf_delete(request_buf);
2290 dpif_netlink_dp_init(reply);
2292 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
2295 ofpbuf_delete(*bufp);
2302 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2303 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2304 * will contain pointers into '*bufp'). */
2306 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
2307 struct ofpbuf **bufp)
2309 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2310 struct dpif_netlink_dp request;
2312 dpif_netlink_dp_init(&request);
2313 request.cmd = OVS_DP_CMD_GET;
2314 request.dp_ifindex = dpif->dp_ifindex;
2316 return dpif_netlink_dp_transact(&request, reply, bufp);
2319 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2320 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2321 * positive errno value.
2323 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2324 * while 'flow' is still in use. */
2326 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
2327 const struct ofpbuf *buf)
2329 static const struct nl_policy ovs_flow_policy[] = {
2330 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2331 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2332 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2333 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2335 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2336 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2337 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2338 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
2341 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2342 struct ovs_header *ovs_header;
2343 struct nlmsghdr *nlmsg;
2344 struct genlmsghdr *genl;
2347 dpif_netlink_flow_init(flow);
2349 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2350 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2351 genl = ofpbuf_try_pull(&b, sizeof *genl);
2352 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2353 if (!nlmsg || !genl || !ovs_header
2354 || nlmsg->nlmsg_type != ovs_flow_family
2355 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2356 ARRAY_SIZE(ovs_flow_policy))) {
2360 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2361 flow->dp_ifindex = ovs_header->dp_ifindex;
2362 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2363 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2365 if (a[OVS_FLOW_ATTR_MASK]) {
2366 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2367 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2369 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2370 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2371 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2373 if (a[OVS_FLOW_ATTR_STATS]) {
2374 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2376 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2377 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2379 if (a[OVS_FLOW_ATTR_USED]) {
2380 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2385 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2386 * followed by Netlink attributes corresponding to 'flow'. */
2388 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
2391 struct ovs_header *ovs_header;
2393 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2394 NLM_F_REQUEST | flow->nlmsg_flags,
2395 flow->cmd, OVS_FLOW_VERSION);
2397 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2398 ovs_header->dp_ifindex = flow->dp_ifindex;
2400 if (flow->key_len) {
2401 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2404 if (flow->mask_len) {
2405 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2408 if (flow->actions || flow->actions_len) {
2409 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2410 flow->actions, flow->actions_len);
2413 /* We never need to send these to the kernel. */
2414 ovs_assert(!flow->stats);
2415 ovs_assert(!flow->tcp_flags);
2416 ovs_assert(!flow->used);
2419 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2422 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
2426 /* Clears 'flow' to "empty" values. */
2428 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
2430 memset(flow, 0, sizeof *flow);
2433 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2434 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2435 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2436 * result of the command is expected to be a flow also, which is decoded and
2437 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2438 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2440 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
2441 struct dpif_netlink_flow *reply,
2442 struct ofpbuf **bufp)
2444 struct ofpbuf *request_buf;
2447 ovs_assert((reply != NULL) == (bufp != NULL));
2450 request->nlmsg_flags |= NLM_F_ECHO;
2453 request_buf = ofpbuf_new(1024);
2454 dpif_netlink_flow_to_ofpbuf(request, request_buf);
2455 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2456 ofpbuf_delete(request_buf);
2460 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
2463 dpif_netlink_flow_init(reply);
2464 ofpbuf_delete(*bufp);
2472 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
2473 struct dpif_flow_stats *stats)
2476 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
2477 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
2479 stats->n_packets = 0;
2482 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2483 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2486 /* Logs information about a packet that was recently lost in 'ch' (in
2489 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2490 uint32_t handler_id)
2492 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2495 if (VLOG_DROP_WARN(&rl)) {
2500 if (ch->last_poll != LLONG_MIN) {
2501 ds_put_format(&s, " (last polled %lld ms ago)",
2502 time_msec() - ch->last_poll);
2505 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2506 dpif_name(&dpif->dpif), ch_idx, handler_id);