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. */
120 static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
121 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
122 const struct ofpbuf *);
123 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
125 static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
126 struct dpif_netlink_flow *reply,
127 struct ofpbuf **bufp);
128 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
129 struct dpif_flow_stats *);
130 static void dpif_netlink_flow_to_dpif_flow(struct dpif_flow *,
131 const struct dpif_netlink_flow *);
133 /* One of the dpif channels between the kernel and userspace. */
134 struct dpif_channel {
135 struct nl_sock *sock; /* Netlink socket. */
136 long long int last_poll; /* Last time this channel was polled. */
139 struct dpif_handler {
140 struct dpif_channel *channels;/* Array of channels for each handler. */
141 struct epoll_event *epoll_events;
142 int epoll_fd; /* epoll fd that includes channel socks. */
143 int n_events; /* Num events returned by epoll_wait(). */
144 int event_offset; /* Offset into 'epoll_events'. */
147 /* Datapath interface for the openvswitch Linux kernel module. */
148 struct dpif_netlink {
152 /* Upcall messages. */
153 struct fat_rwlock upcall_lock;
154 struct dpif_handler *handlers;
155 uint32_t n_handlers; /* Num of upcall handlers. */
156 int uc_array_size; /* Size of 'handler->channels' and */
157 /* 'handler->epoll_events'. */
159 /* Change notification. */
160 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
161 bool refresh_channels;
164 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
165 uint32_t ch_idx, uint32_t handler_id);
167 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
169 /* Generic Netlink family numbers for OVS.
171 * Initialized by dpif_netlink_init(). */
172 static int ovs_datapath_family;
173 static int ovs_vport_family;
174 static int ovs_flow_family;
175 static int ovs_packet_family;
177 /* Generic Netlink multicast groups for OVS.
179 * Initialized by dpif_netlink_init(). */
180 static unsigned int ovs_vport_mcgroup;
182 static int dpif_netlink_init(void);
183 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
184 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
185 odp_port_t port_no, uint32_t hash);
186 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
187 uint32_t n_handlers);
188 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
190 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
191 const struct ofpbuf *);
193 static struct dpif_netlink *
194 dpif_netlink_cast(const struct dpif *dpif)
196 dpif_assert_class(dpif, &dpif_netlink_class);
197 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
201 dpif_netlink_enumerate(struct sset *all_dps,
202 const struct dpif_class *dpif_class OVS_UNUSED)
205 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
206 struct ofpbuf msg, buf;
209 error = dpif_netlink_init();
214 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
215 dpif_netlink_dp_dump_start(&dump);
216 while (nl_dump_next(&dump, &msg, &buf)) {
217 struct dpif_netlink_dp dp;
219 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
220 sset_add(all_dps, dp.name);
224 return nl_dump_done(&dump);
228 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
229 bool create, struct dpif **dpifp)
231 struct dpif_netlink_dp dp_request, dp;
236 error = dpif_netlink_init();
241 /* Create or look up datapath. */
242 dpif_netlink_dp_init(&dp_request);
244 dp_request.cmd = OVS_DP_CMD_NEW;
246 dp_request.upcall_pid = &upcall_pid;
248 /* Use OVS_DP_CMD_SET to report user features */
249 dp_request.cmd = OVS_DP_CMD_SET;
251 dp_request.name = name;
252 dp_request.user_features |= OVS_DP_F_UNALIGNED;
253 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
254 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
259 error = open_dpif(&dp, dpifp);
265 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
267 struct dpif_netlink *dpif;
269 dpif = xzalloc(sizeof *dpif);
270 dpif->port_notifier = NULL;
271 fat_rwlock_init(&dpif->upcall_lock);
273 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
274 dp->dp_ifindex, dp->dp_ifindex);
276 dpif->dp_ifindex = dp->dp_ifindex;
277 *dpifp = &dpif->dpif;
282 /* Destroys the netlink sockets pointed by the elements in 'socksp'
283 * and frees the 'socksp'. */
285 vport_del_socksp(struct nl_sock **socksp, uint32_t n_socks)
289 for (i = 0; i < n_socks; i++) {
290 nl_sock_destroy(socksp[i]);
296 /* Creates an array of netlink sockets. Returns an array of the
297 * corresponding pointers. Records the error in 'error'. */
298 static struct nl_sock **
299 vport_create_socksp(uint32_t n_socks, int *error)
301 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
304 for (i = 0; i < n_socks; i++) {
305 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
314 vport_del_socksp(socksp, n_socks);
319 /* Given the array of pointers to netlink sockets 'socksp', returns
320 * the array of corresponding pids. If the 'socksp' is NULL, returns
321 * a single-element array of value 0. */
323 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
328 pids = xzalloc(sizeof *pids);
332 pids = xzalloc(n_socks * sizeof *pids);
333 for (i = 0; i < n_socks; i++) {
334 pids[i] = nl_sock_pid(socksp[i]);
341 /* Given the port number 'port_idx', extracts the pids of netlink sockets
342 * associated to the port and assigns it to 'upcall_pids'. */
344 vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
345 uint32_t **upcall_pids)
350 /* Since the nl_sock can only be assigned in either all
351 * or none "dpif->handlers" channels, the following check
353 if (!dpif->handlers[0].channels[port_idx].sock) {
357 pids = xzalloc(dpif->n_handlers * sizeof *pids);
359 for (i = 0; i < dpif->n_handlers; i++) {
360 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
369 vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
370 struct nl_sock **socksp)
372 struct epoll_event event;
373 uint32_t port_idx = odp_to_u32(port_no);
377 if (dpif->handlers == NULL) {
381 /* We assume that the datapath densely chooses port numbers, which can
382 * therefore be used as an index into 'channels' and 'epoll_events' of
383 * 'dpif->handler'. */
384 if (port_idx >= dpif->uc_array_size) {
385 uint32_t new_size = port_idx + 1;
387 if (new_size > MAX_PORTS) {
388 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
389 dpif_name(&dpif->dpif), port_no);
393 for (i = 0; i < dpif->n_handlers; i++) {
394 struct dpif_handler *handler = &dpif->handlers[i];
396 handler->channels = xrealloc(handler->channels,
397 new_size * sizeof *handler->channels);
399 for (j = dpif->uc_array_size; j < new_size; j++) {
400 handler->channels[j].sock = NULL;
403 handler->epoll_events = xrealloc(handler->epoll_events,
404 new_size * sizeof *handler->epoll_events);
407 dpif->uc_array_size = new_size;
410 memset(&event, 0, sizeof event);
411 event.events = EPOLLIN;
412 event.data.u32 = port_idx;
414 for (i = 0; i < dpif->n_handlers; i++) {
415 struct dpif_handler *handler = &dpif->handlers[i];
419 * XXX : Map appropiate Windows handle
422 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
428 dpif->handlers[i].channels[port_idx].sock = socksp[i];
429 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
435 for (j = 0; j < i; j++) {
438 * XXX : Map appropiate Windows handle
441 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
442 nl_sock_fd(socksp[j]), NULL);
444 dpif->handlers[j].channels[port_idx].sock = NULL;
451 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
453 uint32_t port_idx = odp_to_u32(port_no);
456 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
460 /* Since the sock can only be assigned in either all or none
461 * of "dpif->handlers" channels, the following check would
463 if (!dpif->handlers[0].channels[port_idx].sock) {
467 for (i = 0; i < dpif->n_handlers; i++) {
468 struct dpif_handler *handler = &dpif->handlers[i];
472 * XXX : Map appropiate Windows handle
475 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
476 nl_sock_fd(handler->channels[port_idx].sock), NULL);
478 nl_sock_destroy(handler->channels[port_idx].sock);
479 handler->channels[port_idx].sock = NULL;
480 handler->event_offset = handler->n_events = 0;
485 destroy_all_channels(struct dpif_netlink *dpif)
486 OVS_REQ_WRLOCK(dpif->upcall_lock)
490 if (!dpif->handlers) {
494 for (i = 0; i < dpif->uc_array_size; i++ ) {
495 struct dpif_netlink_vport vport_request;
496 uint32_t upcall_pids = 0;
498 /* Since the sock can only be assigned in either all or none
499 * of "dpif->handlers" channels, the following check would
501 if (!dpif->handlers[0].channels[i].sock) {
505 /* Turn off upcalls. */
506 dpif_netlink_vport_init(&vport_request);
507 vport_request.cmd = OVS_VPORT_CMD_SET;
508 vport_request.dp_ifindex = dpif->dp_ifindex;
509 vport_request.port_no = u32_to_odp(i);
510 vport_request.upcall_pids = &upcall_pids;
511 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
513 vport_del_channels(dpif, u32_to_odp(i));
516 for (i = 0; i < dpif->n_handlers; i++) {
517 struct dpif_handler *handler = &dpif->handlers[i];
521 * XXX : Map appropiate Windows handle
524 close(handler->epoll_fd);
526 free(handler->epoll_events);
527 free(handler->channels);
530 free(dpif->handlers);
531 dpif->handlers = NULL;
532 dpif->n_handlers = 0;
533 dpif->uc_array_size = 0;
537 dpif_netlink_close(struct dpif *dpif_)
539 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
541 nl_sock_destroy(dpif->port_notifier);
543 fat_rwlock_wrlock(&dpif->upcall_lock);
544 destroy_all_channels(dpif);
545 fat_rwlock_unlock(&dpif->upcall_lock);
547 fat_rwlock_destroy(&dpif->upcall_lock);
552 dpif_netlink_destroy(struct dpif *dpif_)
554 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
555 struct dpif_netlink_dp dp;
557 dpif_netlink_dp_init(&dp);
558 dp.cmd = OVS_DP_CMD_DEL;
559 dp.dp_ifindex = dpif->dp_ifindex;
560 return dpif_netlink_dp_transact(&dp, NULL, NULL);
564 dpif_netlink_run(struct dpif *dpif_)
566 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
568 if (dpif->refresh_channels) {
569 dpif->refresh_channels = false;
570 fat_rwlock_wrlock(&dpif->upcall_lock);
571 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
572 fat_rwlock_unlock(&dpif->upcall_lock);
577 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
579 struct dpif_netlink_dp dp;
583 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
585 memset(stats, 0, sizeof *stats);
588 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
589 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
590 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
591 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
594 if (dp.megaflow_stats) {
595 stats->n_masks = dp.megaflow_stats->n_masks;
596 stats->n_mask_hit = get_32aligned_u64(
597 &dp.megaflow_stats->n_mask_hit);
599 stats->n_masks = UINT32_MAX;
600 stats->n_mask_hit = UINT64_MAX;
608 get_vport_type(const struct dpif_netlink_vport *vport)
610 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
612 switch (vport->type) {
613 case OVS_VPORT_TYPE_NETDEV: {
614 const char *type = netdev_get_type_from_name(vport->name);
616 return type ? type : "system";
619 case OVS_VPORT_TYPE_INTERNAL:
622 case OVS_VPORT_TYPE_GENEVE:
625 case OVS_VPORT_TYPE_GRE:
628 case OVS_VPORT_TYPE_GRE64:
631 case OVS_VPORT_TYPE_VXLAN:
634 case OVS_VPORT_TYPE_LISP:
637 case OVS_VPORT_TYPE_UNSPEC:
638 case __OVS_VPORT_TYPE_MAX:
642 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
643 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
647 static enum ovs_vport_type
648 netdev_to_ovs_vport_type(const struct netdev *netdev)
650 const char *type = netdev_get_type(netdev);
652 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
653 return OVS_VPORT_TYPE_NETDEV;
654 } else if (!strcmp(type, "internal")) {
655 return OVS_VPORT_TYPE_INTERNAL;
656 } else if (!strcmp(type, "geneve")) {
657 return OVS_VPORT_TYPE_GENEVE;
658 } else if (strstr(type, "gre64")) {
659 return OVS_VPORT_TYPE_GRE64;
660 } else if (strstr(type, "gre")) {
661 return OVS_VPORT_TYPE_GRE;
662 } else if (!strcmp(type, "vxlan")) {
663 return OVS_VPORT_TYPE_VXLAN;
664 } else if (!strcmp(type, "lisp")) {
665 return OVS_VPORT_TYPE_LISP;
667 return OVS_VPORT_TYPE_UNSPEC;
672 dpif_netlink_port_add__(struct dpif_netlink *dpif, struct netdev *netdev,
673 odp_port_t *port_nop)
674 OVS_REQ_WRLOCK(dpif->upcall_lock)
676 const struct netdev_tunnel_config *tnl_cfg;
677 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
678 const char *name = netdev_vport_get_dpif_port(netdev,
679 namebuf, sizeof namebuf);
680 const char *type = netdev_get_type(netdev);
681 struct dpif_netlink_vport request, reply;
683 uint64_t options_stub[64 / 8];
684 struct ofpbuf options;
685 struct nl_sock **socksp = NULL;
686 uint32_t *upcall_pids;
689 if (dpif->handlers) {
690 socksp = vport_create_socksp(dpif->n_handlers, &error);
696 dpif_netlink_vport_init(&request);
697 request.cmd = OVS_VPORT_CMD_NEW;
698 request.dp_ifindex = dpif->dp_ifindex;
699 request.type = netdev_to_ovs_vport_type(netdev);
700 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
701 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
702 "unsupported type `%s'",
703 dpif_name(&dpif->dpif), name, type);
704 vport_del_socksp(socksp, dpif->n_handlers);
709 if (request.type == OVS_VPORT_TYPE_NETDEV) {
712 * XXX : Map appropiate Windows handle
715 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
719 tnl_cfg = netdev_get_tunnel_config(netdev);
720 if (tnl_cfg && tnl_cfg->dst_port != 0) {
721 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
722 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
723 ntohs(tnl_cfg->dst_port));
724 request.options = ofpbuf_data(&options);
725 request.options_len = ofpbuf_size(&options);
728 request.port_no = *port_nop;
729 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
730 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
731 request.upcall_pids = upcall_pids;
733 error = dpif_netlink_vport_transact(&request, &reply, &buf);
735 *port_nop = reply.port_no;
737 if (error == EBUSY && *port_nop != ODPP_NONE) {
738 VLOG_INFO("%s: requested port %"PRIu32" is in use",
739 dpif_name(&dpif->dpif), *port_nop);
742 vport_del_socksp(socksp, dpif->n_handlers);
747 error = vport_add_channels(dpif, *port_nop, socksp);
749 VLOG_INFO("%s: could not add channel for port %s",
750 dpif_name(&dpif->dpif), name);
752 /* Delete the port. */
753 dpif_netlink_vport_init(&request);
754 request.cmd = OVS_VPORT_CMD_DEL;
755 request.dp_ifindex = dpif->dp_ifindex;
756 request.port_no = *port_nop;
757 dpif_netlink_vport_transact(&request, NULL, NULL);
758 vport_del_socksp(socksp, dpif->n_handlers);
772 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
773 odp_port_t *port_nop)
775 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
778 fat_rwlock_wrlock(&dpif->upcall_lock);
779 error = dpif_netlink_port_add__(dpif, netdev, port_nop);
780 fat_rwlock_unlock(&dpif->upcall_lock);
786 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
787 OVS_REQ_WRLOCK(dpif->upcall_lock)
789 struct dpif_netlink_vport vport;
792 dpif_netlink_vport_init(&vport);
793 vport.cmd = OVS_VPORT_CMD_DEL;
794 vport.dp_ifindex = dpif->dp_ifindex;
795 vport.port_no = port_no;
796 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
798 vport_del_channels(dpif, port_no);
804 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
806 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
809 fat_rwlock_wrlock(&dpif->upcall_lock);
810 error = dpif_netlink_port_del__(dpif, port_no);
811 fat_rwlock_unlock(&dpif->upcall_lock);
817 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
818 const char *port_name, struct dpif_port *dpif_port)
820 struct dpif_netlink_vport request;
821 struct dpif_netlink_vport reply;
825 dpif_netlink_vport_init(&request);
826 request.cmd = OVS_VPORT_CMD_GET;
827 request.dp_ifindex = dpif->dp_ifindex;
828 request.port_no = port_no;
829 request.name = port_name;
831 error = dpif_netlink_vport_transact(&request, &reply, &buf);
833 if (reply.dp_ifindex != request.dp_ifindex) {
834 /* A query by name reported that 'port_name' is in some datapath
835 * other than 'dpif', but the caller wants to know about 'dpif'. */
837 } else if (dpif_port) {
838 dpif_port->name = xstrdup(reply.name);
839 dpif_port->type = xstrdup(get_vport_type(&reply));
840 dpif_port->port_no = reply.port_no;
848 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
849 struct dpif_port *dpif_port)
851 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
853 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
857 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
858 struct dpif_port *dpif_port)
860 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
862 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
866 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
867 odp_port_t port_no, uint32_t hash)
868 OVS_REQ_RDLOCK(dpif->upcall_lock)
870 uint32_t port_idx = odp_to_u32(port_no);
873 if (dpif->handlers && dpif->uc_array_size > 0) {
874 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
875 * channel, since it is not heavily loaded. */
876 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
877 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
879 /* Needs to check in case the socket pointer is changed in between
880 * the holding of upcall_lock. A known case happens when the main
881 * thread deletes the vport while the handler thread is handling
882 * the upcall from that port. */
883 if (h->channels[idx].sock) {
884 pid = nl_sock_pid(h->channels[idx].sock);
892 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
895 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
898 fat_rwlock_rdlock(&dpif->upcall_lock);
899 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
900 fat_rwlock_unlock(&dpif->upcall_lock);
906 dpif_netlink_flow_flush(struct dpif *dpif_)
908 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
909 struct dpif_netlink_flow flow;
911 dpif_netlink_flow_init(&flow);
912 flow.cmd = OVS_FLOW_CMD_DEL;
913 flow.dp_ifindex = dpif->dp_ifindex;
914 return dpif_netlink_flow_transact(&flow, NULL, NULL);
917 struct dpif_netlink_port_state {
923 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
924 struct nl_dump *dump)
926 struct dpif_netlink_vport request;
929 dpif_netlink_vport_init(&request);
930 request.cmd = OVS_VPORT_CMD_GET;
931 request.dp_ifindex = dpif->dp_ifindex;
933 buf = ofpbuf_new(1024);
934 dpif_netlink_vport_to_ofpbuf(&request, buf);
935 nl_dump_start(dump, NETLINK_GENERIC, buf);
940 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
942 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
943 struct dpif_netlink_port_state *state;
945 *statep = state = xmalloc(sizeof *state);
946 dpif_netlink_port_dump_start__(dpif, &state->dump);
948 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
953 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
954 struct nl_dump *dump,
955 struct dpif_netlink_vport *vport,
956 struct ofpbuf *buffer)
961 if (!nl_dump_next(dump, &buf, buffer)) {
965 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
967 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
968 dpif_name(&dpif->dpif), ovs_strerror(error));
974 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
975 struct dpif_port *dpif_port)
977 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
978 struct dpif_netlink_port_state *state = state_;
979 struct dpif_netlink_vport vport;
982 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
987 dpif_port->name = CONST_CAST(char *, vport.name);
988 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
989 dpif_port->port_no = vport.port_no;
994 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
996 struct dpif_netlink_port_state *state = state_;
997 int error = nl_dump_done(&state->dump);
999 ofpbuf_uninit(&state->buf);
1005 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1007 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1009 /* Lazily create the Netlink socket to listen for notifications. */
1010 if (!dpif->port_notifier) {
1011 struct nl_sock *sock;
1014 error = nl_sock_create(NETLINK_GENERIC, &sock);
1019 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1021 nl_sock_destroy(sock);
1024 dpif->port_notifier = sock;
1026 /* We have no idea of the current state so report that everything
1032 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1033 uint64_t buf_stub[4096 / 8];
1037 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1038 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1040 struct dpif_netlink_vport vport;
1042 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1044 if (vport.dp_ifindex == dpif->dp_ifindex
1045 && (vport.cmd == OVS_VPORT_CMD_NEW
1046 || vport.cmd == OVS_VPORT_CMD_DEL
1047 || vport.cmd == OVS_VPORT_CMD_SET)) {
1048 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1049 dpif->dpif.full_name, vport.name, vport.cmd);
1050 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1051 dpif->refresh_channels = true;
1053 *devnamep = xstrdup(vport.name);
1054 ofpbuf_uninit(&buf);
1058 } else if (error != EAGAIN) {
1059 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1060 ovs_strerror(error));
1061 nl_sock_drain(dpif->port_notifier);
1065 ofpbuf_uninit(&buf);
1073 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1075 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1077 if (dpif->port_notifier) {
1078 nl_sock_wait(dpif->port_notifier, POLLIN);
1080 poll_immediate_wake();
1085 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1086 const struct nlattr *key, size_t key_len,
1087 struct dpif_netlink_flow *request)
1089 dpif_netlink_flow_init(request);
1090 request->cmd = OVS_FLOW_CMD_GET;
1091 request->dp_ifindex = dpif->dp_ifindex;
1093 request->key_len = key_len;
1097 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1098 const struct nlattr *key, size_t key_len,
1099 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1101 struct dpif_netlink_flow request;
1103 dpif_netlink_init_flow_get(dpif, key, key_len, &request);
1104 return dpif_netlink_flow_transact(&request, reply, bufp);
1108 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1109 const struct dpif_flow_put *put,
1110 struct dpif_netlink_flow *request)
1112 static const struct nlattr dummy_action;
1114 dpif_netlink_flow_init(request);
1115 request->cmd = (put->flags & DPIF_FP_CREATE
1116 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1117 request->dp_ifindex = dpif->dp_ifindex;
1118 request->key = put->key;
1119 request->key_len = put->key_len;
1120 request->mask = put->mask;
1121 request->mask_len = put->mask_len;
1122 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1123 request->actions = (put->actions
1125 : CONST_CAST(struct nlattr *, &dummy_action));
1126 request->actions_len = put->actions_len;
1127 if (put->flags & DPIF_FP_ZERO_STATS) {
1128 request->clear = true;
1130 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1134 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1135 const struct dpif_flow_del *del,
1136 struct dpif_netlink_flow *request)
1138 dpif_netlink_flow_init(request);
1139 request->cmd = OVS_FLOW_CMD_DEL;
1140 request->dp_ifindex = dpif->dp_ifindex;
1141 request->key = del->key;
1142 request->key_len = del->key_len;
1145 struct dpif_netlink_flow_dump {
1146 struct dpif_flow_dump up;
1147 struct nl_dump nl_dump;
1151 static struct dpif_netlink_flow_dump *
1152 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1154 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1157 static struct dpif_flow_dump *
1158 dpif_netlink_flow_dump_create(const struct dpif *dpif_)
1160 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1161 struct dpif_netlink_flow_dump *dump;
1162 struct dpif_netlink_flow request;
1165 dump = xmalloc(sizeof *dump);
1166 dpif_flow_dump_init(&dump->up, dpif_);
1168 dpif_netlink_flow_init(&request);
1169 request.cmd = OVS_FLOW_CMD_GET;
1170 request.dp_ifindex = dpif->dp_ifindex;
1172 buf = ofpbuf_new(1024);
1173 dpif_netlink_flow_to_ofpbuf(&request, buf);
1174 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1176 atomic_init(&dump->status, 0);
1182 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1184 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1185 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1188 /* No other thread has access to 'dump' at this point. */
1189 atomic_read_relaxed(&dump->status, &dump_status);
1191 return dump_status ? dump_status : nl_status;
1194 struct dpif_netlink_flow_dump_thread {
1195 struct dpif_flow_dump_thread up;
1196 struct dpif_netlink_flow_dump *dump;
1197 struct dpif_netlink_flow flow;
1198 struct dpif_flow_stats stats;
1199 struct ofpbuf nl_flows; /* Always used to store flows. */
1200 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1203 static struct dpif_netlink_flow_dump_thread *
1204 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1206 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1209 static struct dpif_flow_dump_thread *
1210 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1212 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1213 struct dpif_netlink_flow_dump_thread *thread;
1215 thread = xmalloc(sizeof *thread);
1216 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1217 thread->dump = dump;
1218 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1219 thread->nl_actions = NULL;
1225 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1227 struct dpif_netlink_flow_dump_thread *thread
1228 = dpif_netlink_flow_dump_thread_cast(thread_);
1230 ofpbuf_uninit(&thread->nl_flows);
1231 ofpbuf_delete(thread->nl_actions);
1236 dpif_netlink_flow_to_dpif_flow(struct dpif_flow *dpif_flow,
1237 const struct dpif_netlink_flow *datapath_flow)
1239 dpif_flow->key = datapath_flow->key;
1240 dpif_flow->key_len = datapath_flow->key_len;
1241 dpif_flow->mask = datapath_flow->mask;
1242 dpif_flow->mask_len = datapath_flow->mask_len;
1243 dpif_flow->actions = datapath_flow->actions;
1244 dpif_flow->actions_len = datapath_flow->actions_len;
1245 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1249 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1250 struct dpif_flow *flows, int max_flows)
1252 struct dpif_netlink_flow_dump_thread *thread
1253 = dpif_netlink_flow_dump_thread_cast(thread_);
1254 struct dpif_netlink_flow_dump *dump = thread->dump;
1255 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1258 ofpbuf_delete(thread->nl_actions);
1259 thread->nl_actions = NULL;
1263 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1264 struct dpif_netlink_flow datapath_flow;
1265 struct ofpbuf nl_flow;
1268 /* Try to grab another flow. */
1269 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1273 /* Convert the flow to our output format. */
1274 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1276 atomic_store_relaxed(&dump->status, error);
1280 if (datapath_flow.actions) {
1281 /* Common case: the flow includes actions. */
1282 dpif_netlink_flow_to_dpif_flow(&flows[n_flows++], &datapath_flow);
1284 /* Rare case: the flow does not include actions. Retrieve this
1285 * individual flow again to get the actions. */
1286 error = dpif_netlink_flow_get(dpif, datapath_flow.key,
1287 datapath_flow.key_len,
1288 &datapath_flow, &thread->nl_actions);
1289 if (error == ENOENT) {
1290 VLOG_DBG("dumped flow disappeared on get");
1293 VLOG_WARN("error fetching dumped flow: %s",
1294 ovs_strerror(error));
1295 atomic_store_relaxed(&dump->status, error);
1299 /* Save this flow. Then exit, because we only have one buffer to
1300 * handle this case. */
1301 dpif_netlink_flow_to_dpif_flow(&flows[n_flows++], &datapath_flow);
1309 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1312 struct ovs_header *k_exec;
1315 ofpbuf_prealloc_tailroom(buf, (64
1316 + ofpbuf_size(d_exec->packet)
1317 + ODP_KEY_METADATA_SIZE
1318 + d_exec->actions_len));
1320 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1321 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1323 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1324 k_exec->dp_ifindex = dp_ifindex;
1326 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1327 ofpbuf_data(d_exec->packet),
1328 ofpbuf_size(d_exec->packet));
1330 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1331 odp_key_from_pkt_metadata(buf, &d_exec->md);
1332 nl_msg_end_nested(buf, key_ofs);
1334 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1335 d_exec->actions, d_exec->actions_len);
1341 dpif_netlink_operate__(struct dpif_netlink *dpif,
1342 struct dpif_op **ops, size_t n_ops)
1345 struct nl_transaction txn;
1347 struct ofpbuf request;
1348 uint64_t request_stub[1024 / 8];
1350 struct ofpbuf reply;
1351 uint64_t reply_stub[1024 / 8];
1354 struct nl_transaction *txnsp[MAX_OPS];
1357 ovs_assert(n_ops <= MAX_OPS);
1358 for (i = 0; i < n_ops; i++) {
1359 struct op_auxdata *aux = &auxes[i];
1360 struct dpif_op *op = ops[i];
1361 struct dpif_flow_put *put;
1362 struct dpif_flow_del *del;
1363 struct dpif_execute *execute;
1364 struct dpif_flow_get *get;
1365 struct dpif_netlink_flow flow;
1367 ofpbuf_use_stub(&aux->request,
1368 aux->request_stub, sizeof aux->request_stub);
1369 aux->txn.request = &aux->request;
1371 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1372 aux->txn.reply = NULL;
1375 case DPIF_OP_FLOW_PUT:
1376 put = &op->u.flow_put;
1377 dpif_netlink_init_flow_put(dpif, put, &flow);
1379 flow.nlmsg_flags |= NLM_F_ECHO;
1380 aux->txn.reply = &aux->reply;
1382 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1385 case DPIF_OP_FLOW_DEL:
1386 del = &op->u.flow_del;
1387 dpif_netlink_init_flow_del(dpif, del, &flow);
1389 flow.nlmsg_flags |= NLM_F_ECHO;
1390 aux->txn.reply = &aux->reply;
1392 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1395 case DPIF_OP_EXECUTE:
1396 execute = &op->u.execute;
1397 dpif_netlink_encode_execute(dpif->dp_ifindex, execute,
1401 case DPIF_OP_FLOW_GET:
1402 get = &op->u.flow_get;
1403 dpif_netlink_init_flow_get(dpif, get->key, get->key_len, &flow);
1404 aux->txn.reply = get->buffer;
1405 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1413 for (i = 0; i < n_ops; i++) {
1414 txnsp[i] = &auxes[i].txn;
1416 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1418 for (i = 0; i < n_ops; i++) {
1419 struct op_auxdata *aux = &auxes[i];
1420 struct nl_transaction *txn = &auxes[i].txn;
1421 struct dpif_op *op = ops[i];
1422 struct dpif_flow_put *put;
1423 struct dpif_flow_del *del;
1424 struct dpif_flow_get *get;
1426 op->error = txn->error;
1429 case DPIF_OP_FLOW_PUT:
1430 put = &op->u.flow_put;
1433 struct dpif_netlink_flow reply;
1435 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1438 dpif_netlink_flow_get_stats(&reply, put->stats);
1444 case DPIF_OP_FLOW_DEL:
1445 del = &op->u.flow_del;
1448 struct dpif_netlink_flow reply;
1450 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1453 dpif_netlink_flow_get_stats(&reply, del->stats);
1459 case DPIF_OP_EXECUTE:
1462 case DPIF_OP_FLOW_GET:
1463 get = &op->u.flow_get;
1465 struct dpif_netlink_flow reply;
1467 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
1469 dpif_netlink_flow_to_dpif_flow(get->flow, &reply);
1478 ofpbuf_uninit(&aux->request);
1479 ofpbuf_uninit(&aux->reply);
1484 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1486 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1489 size_t chunk = MIN(n_ops, MAX_OPS);
1490 dpif_netlink_operate__(dpif, ops, chunk);
1496 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1497 * currently in 'dpif' in the kernel, by adding a new set of channels for
1498 * any kernel vport that lacks one and deleting any channels that have no
1499 * backing kernel vports. */
1501 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
1502 OVS_REQ_WRLOCK(dpif->upcall_lock)
1504 unsigned long int *keep_channels;
1505 struct dpif_netlink_vport vport;
1506 size_t keep_channels_nbits;
1507 struct nl_dump dump;
1508 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1513 if (dpif->n_handlers != n_handlers) {
1514 destroy_all_channels(dpif);
1515 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1516 for (i = 0; i < n_handlers; i++) {
1517 struct dpif_handler *handler = &dpif->handlers[i];
1521 * XXX : Map appropiate Windows handle
1524 handler->epoll_fd = epoll_create(10);
1525 if (handler->epoll_fd < 0) {
1528 for (j = 0; j < i; j++) {
1529 close(dpif->handlers[j].epoll_fd);
1531 free(dpif->handlers);
1532 dpif->handlers = NULL;
1538 dpif->n_handlers = n_handlers;
1541 for (i = 0; i < n_handlers; i++) {
1542 struct dpif_handler *handler = &dpif->handlers[i];
1544 handler->event_offset = handler->n_events = 0;
1547 keep_channels_nbits = dpif->uc_array_size;
1548 keep_channels = bitmap_allocate(keep_channels_nbits);
1550 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1551 dpif_netlink_port_dump_start__(dpif, &dump);
1552 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
1553 uint32_t port_no = odp_to_u32(vport.port_no);
1554 uint32_t *upcall_pids = NULL;
1557 if (port_no >= dpif->uc_array_size
1558 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1559 struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
1566 error = vport_add_channels(dpif, vport.port_no, socksp);
1568 VLOG_INFO("%s: could not add channels for port %s",
1569 dpif_name(&dpif->dpif), vport.name);
1570 vport_del_socksp(socksp, dpif->n_handlers);
1574 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1578 /* Configure the vport to deliver misses to 'sock'. */
1579 if (vport.upcall_pids[0] == 0
1580 || vport.n_upcall_pids != dpif->n_handlers
1581 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1583 struct dpif_netlink_vport vport_request;
1585 dpif_netlink_vport_init(&vport_request);
1586 vport_request.cmd = OVS_VPORT_CMD_SET;
1587 vport_request.dp_ifindex = dpif->dp_ifindex;
1588 vport_request.port_no = vport.port_no;
1589 vport_request.n_upcall_pids = dpif->n_handlers;
1590 vport_request.upcall_pids = upcall_pids;
1591 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
1593 VLOG_WARN_RL(&error_rl,
1594 "%s: failed to set upcall pid on port: %s",
1595 dpif_name(&dpif->dpif), ovs_strerror(error));
1597 if (error != ENODEV && error != ENOENT) {
1600 /* The vport isn't really there, even though the dump says
1601 * it is. Probably we just hit a race after a port
1608 if (port_no < keep_channels_nbits) {
1609 bitmap_set1(keep_channels, port_no);
1616 vport_del_channels(dpif, vport.port_no);
1618 nl_dump_done(&dump);
1619 ofpbuf_uninit(&buf);
1621 /* Discard any saved channels that we didn't reuse. */
1622 for (i = 0; i < keep_channels_nbits; i++) {
1623 if (!bitmap_is_set(keep_channels, i)) {
1624 vport_del_channels(dpif, u32_to_odp(i));
1627 free(keep_channels);
1633 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
1634 OVS_REQ_WRLOCK(dpif->upcall_lock)
1636 if ((dpif->handlers != NULL) == enable) {
1638 } else if (!enable) {
1639 destroy_all_channels(dpif);
1642 return dpif_netlink_refresh_channels(dpif, 1);
1647 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
1649 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1652 fat_rwlock_wrlock(&dpif->upcall_lock);
1653 error = dpif_netlink_recv_set__(dpif, enable);
1654 fat_rwlock_unlock(&dpif->upcall_lock);
1660 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1662 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1665 fat_rwlock_wrlock(&dpif->upcall_lock);
1666 if (dpif->handlers) {
1667 error = dpif_netlink_refresh_channels(dpif, n_handlers);
1669 fat_rwlock_unlock(&dpif->upcall_lock);
1675 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1676 uint32_t queue_id, uint32_t *priority)
1678 if (queue_id < 0xf000) {
1679 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1687 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1690 static const struct nl_policy ovs_packet_policy[] = {
1691 /* Always present. */
1692 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1693 .min_len = ETH_HEADER_LEN },
1694 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1696 /* OVS_PACKET_CMD_ACTION only. */
1697 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1698 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
1701 struct ovs_header *ovs_header;
1702 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1703 struct nlmsghdr *nlmsg;
1704 struct genlmsghdr *genl;
1708 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1710 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1711 genl = ofpbuf_try_pull(&b, sizeof *genl);
1712 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1713 if (!nlmsg || !genl || !ovs_header
1714 || nlmsg->nlmsg_type != ovs_packet_family
1715 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1716 ARRAY_SIZE(ovs_packet_policy))) {
1720 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1721 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1727 /* (Re)set ALL fields of '*upcall' on successful return. */
1728 upcall->type = type;
1729 upcall->key = CONST_CAST(struct nlattr *,
1730 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1731 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1732 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1733 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
1735 /* Allow overwriting the netlink attribute header without reallocating. */
1736 ofpbuf_use_stub(&upcall->packet,
1737 CONST_CAST(struct nlattr *,
1738 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1739 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1740 sizeof(struct nlattr));
1741 ofpbuf_set_data(&upcall->packet,
1742 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1743 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1745 *dp_ifindex = ovs_header->dp_ifindex;
1751 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
1752 struct dpif_upcall *upcall, struct ofpbuf *buf)
1753 OVS_REQ_RDLOCK(dpif->upcall_lock)
1755 struct dpif_handler *handler;
1758 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1762 handler = &dpif->handlers[handler_id];
1763 if (handler->event_offset >= handler->n_events) {
1766 handler->event_offset = handler->n_events = 0;
1769 retval = dpif->uc_array_size;
1770 handler->event_offset = 0;
1773 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
1774 dpif->uc_array_size, 0);
1775 } while (retval < 0 && errno == EINTR);
1778 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1779 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1780 } else if (retval > 0) {
1781 handler->n_events = retval;
1785 while (handler->event_offset < handler->n_events) {
1786 int idx = handler->epoll_events[handler->event_offset].data.u32;
1787 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
1789 handler->event_offset++;
1795 if (++read_tries > 50) {
1799 error = nl_sock_recv(ch->sock, buf, false);
1800 if (error == ENOBUFS) {
1801 /* ENOBUFS typically means that we've received so many
1802 * packets that the buffer overflowed. Try again
1803 * immediately because there's almost certainly a packet
1804 * waiting for us. */
1805 report_loss(dpif, ch, idx, handler_id);
1809 ch->last_poll = time_msec();
1811 if (error == EAGAIN) {
1817 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1818 if (!error && dp_ifindex == dpif->dp_ifindex) {
1830 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
1831 struct dpif_upcall *upcall, struct ofpbuf *buf)
1833 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1836 fat_rwlock_rdlock(&dpif->upcall_lock);
1837 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
1838 fat_rwlock_unlock(&dpif->upcall_lock);
1844 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
1845 OVS_REQ_RDLOCK(dpif->upcall_lock)
1849 * XXX : Map appropiate Windows handle
1852 if (dpif->handlers && handler_id < dpif->n_handlers) {
1853 struct dpif_handler *handler = &dpif->handlers[handler_id];
1855 poll_fd_wait(handler->epoll_fd, POLLIN);
1861 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1863 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1865 fat_rwlock_rdlock(&dpif->upcall_lock);
1866 dpif_netlink_recv_wait__(dpif, handler_id);
1867 fat_rwlock_unlock(&dpif->upcall_lock);
1871 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
1872 OVS_REQ_WRLOCK(dpif->upcall_lock)
1874 if (dpif->handlers) {
1877 for (i = 0; i < dpif->uc_array_size; i++ ) {
1878 if (!dpif->handlers[0].channels[i].sock) {
1882 for (j = 0; j < dpif->n_handlers; j++) {
1883 nl_sock_drain(dpif->handlers[j].channels[i].sock);
1890 dpif_netlink_recv_purge(struct dpif *dpif_)
1892 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1894 fat_rwlock_wrlock(&dpif->upcall_lock);
1895 dpif_netlink_recv_purge__(dpif);
1896 fat_rwlock_unlock(&dpif->upcall_lock);
1899 const struct dpif_class dpif_netlink_class = {
1901 dpif_netlink_enumerate,
1905 dpif_netlink_destroy,
1908 dpif_netlink_get_stats,
1909 dpif_netlink_port_add,
1910 dpif_netlink_port_del,
1911 dpif_netlink_port_query_by_number,
1912 dpif_netlink_port_query_by_name,
1913 dpif_netlink_port_get_pid,
1914 dpif_netlink_port_dump_start,
1915 dpif_netlink_port_dump_next,
1916 dpif_netlink_port_dump_done,
1917 dpif_netlink_port_poll,
1918 dpif_netlink_port_poll_wait,
1919 dpif_netlink_flow_flush,
1920 dpif_netlink_flow_dump_create,
1921 dpif_netlink_flow_dump_destroy,
1922 dpif_netlink_flow_dump_thread_create,
1923 dpif_netlink_flow_dump_thread_destroy,
1924 dpif_netlink_flow_dump_next,
1925 dpif_netlink_operate,
1926 dpif_netlink_recv_set,
1927 dpif_netlink_handlers_set,
1928 NULL, /* poll_thread_set */
1929 dpif_netlink_queue_to_priority,
1931 dpif_netlink_recv_wait,
1932 dpif_netlink_recv_purge,
1933 NULL, /* register_upcall_cb */
1934 NULL, /* enable_upcall */
1935 NULL, /* disable_upcall */
1939 dpif_netlink_init(void)
1941 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1944 if (ovsthread_once_start(&once)) {
1945 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1946 &ovs_datapath_family);
1948 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1949 "The Open vSwitch kernel module is probably not loaded.",
1950 OVS_DATAPATH_FAMILY);
1953 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1956 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1959 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1960 &ovs_packet_family);
1963 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1964 &ovs_vport_mcgroup);
1967 ovsthread_once_done(&once);
1974 dpif_netlink_is_internal_device(const char *name)
1976 struct dpif_netlink_vport reply;
1980 error = dpif_netlink_vport_get(name, &reply, &buf);
1983 } else if (error != ENODEV && error != ENOENT) {
1984 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1985 name, ovs_strerror(error));
1988 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1991 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1992 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1993 * positive errno value.
1995 * 'vport' will contain pointers into 'buf', so the caller should not free
1996 * 'buf' while 'vport' is still in use. */
1998 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
1999 const struct ofpbuf *buf)
2001 static const struct nl_policy ovs_vport_policy[] = {
2002 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2003 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2004 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2005 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2006 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2008 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2011 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2012 struct ovs_header *ovs_header;
2013 struct nlmsghdr *nlmsg;
2014 struct genlmsghdr *genl;
2017 dpif_netlink_vport_init(vport);
2019 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2020 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2021 genl = ofpbuf_try_pull(&b, sizeof *genl);
2022 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2023 if (!nlmsg || !genl || !ovs_header
2024 || nlmsg->nlmsg_type != ovs_vport_family
2025 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2026 ARRAY_SIZE(ovs_vport_policy))) {
2030 vport->cmd = genl->cmd;
2031 vport->dp_ifindex = ovs_header->dp_ifindex;
2032 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2033 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2034 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2035 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2036 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2037 / (sizeof *vport->upcall_pids);
2038 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2041 if (a[OVS_VPORT_ATTR_STATS]) {
2042 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2044 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2045 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2046 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2051 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2052 * followed by Netlink attributes corresponding to 'vport'. */
2054 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
2057 struct ovs_header *ovs_header;
2059 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2060 vport->cmd, OVS_VPORT_VERSION);
2062 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2063 ovs_header->dp_ifindex = vport->dp_ifindex;
2065 if (vport->port_no != ODPP_NONE) {
2066 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2069 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2070 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2074 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2077 if (vport->upcall_pids) {
2078 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2080 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2084 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2085 vport->stats, sizeof *vport->stats);
2088 if (vport->options) {
2089 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2090 vport->options, vport->options_len);
2094 /* Clears 'vport' to "empty" values. */
2096 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
2098 memset(vport, 0, sizeof *vport);
2099 vport->port_no = ODPP_NONE;
2102 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2103 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2104 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2105 * result of the command is expected to be an ovs_vport also, which is decoded
2106 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2107 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2109 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
2110 struct dpif_netlink_vport *reply,
2111 struct ofpbuf **bufp)
2113 struct ofpbuf *request_buf;
2116 ovs_assert((reply != NULL) == (bufp != NULL));
2118 error = dpif_netlink_init();
2122 dpif_netlink_vport_init(reply);
2127 request_buf = ofpbuf_new(1024);
2128 dpif_netlink_vport_to_ofpbuf(request, request_buf);
2129 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2130 ofpbuf_delete(request_buf);
2134 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
2137 dpif_netlink_vport_init(reply);
2138 ofpbuf_delete(*bufp);
2145 /* Obtains information about the kernel vport named 'name' and stores it into
2146 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2147 * longer needed ('reply' will contain pointers into '*bufp'). */
2149 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
2150 struct ofpbuf **bufp)
2152 struct dpif_netlink_vport request;
2154 dpif_netlink_vport_init(&request);
2155 request.cmd = OVS_VPORT_CMD_GET;
2156 request.name = name;
2158 return dpif_netlink_vport_transact(&request, reply, bufp);
2161 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2162 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2163 * positive errno value.
2165 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2166 * while 'dp' is still in use. */
2168 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
2170 static const struct nl_policy ovs_datapath_policy[] = {
2171 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2172 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2174 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2175 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2179 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2180 struct ovs_header *ovs_header;
2181 struct nlmsghdr *nlmsg;
2182 struct genlmsghdr *genl;
2185 dpif_netlink_dp_init(dp);
2187 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2188 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2189 genl = ofpbuf_try_pull(&b, sizeof *genl);
2190 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2191 if (!nlmsg || !genl || !ovs_header
2192 || nlmsg->nlmsg_type != ovs_datapath_family
2193 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2194 ARRAY_SIZE(ovs_datapath_policy))) {
2198 dp->cmd = genl->cmd;
2199 dp->dp_ifindex = ovs_header->dp_ifindex;
2200 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2201 if (a[OVS_DP_ATTR_STATS]) {
2202 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
2205 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2206 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
2212 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2214 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
2216 struct ovs_header *ovs_header;
2218 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2219 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2220 OVS_DATAPATH_VERSION);
2222 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2223 ovs_header->dp_ifindex = dp->dp_ifindex;
2226 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2229 if (dp->upcall_pid) {
2230 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2233 if (dp->user_features) {
2234 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2237 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2240 /* Clears 'dp' to "empty" values. */
2242 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
2244 memset(dp, 0, sizeof *dp);
2248 dpif_netlink_dp_dump_start(struct nl_dump *dump)
2250 struct dpif_netlink_dp request;
2253 dpif_netlink_dp_init(&request);
2254 request.cmd = OVS_DP_CMD_GET;
2256 buf = ofpbuf_new(1024);
2257 dpif_netlink_dp_to_ofpbuf(&request, buf);
2258 nl_dump_start(dump, NETLINK_GENERIC, buf);
2262 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2263 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2264 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2265 * result of the command is expected to be of the same form, which is decoded
2266 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2267 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2269 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
2270 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
2272 struct ofpbuf *request_buf;
2275 ovs_assert((reply != NULL) == (bufp != NULL));
2277 request_buf = ofpbuf_new(1024);
2278 dpif_netlink_dp_to_ofpbuf(request, request_buf);
2279 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2280 ofpbuf_delete(request_buf);
2283 dpif_netlink_dp_init(reply);
2285 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
2288 ofpbuf_delete(*bufp);
2295 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2296 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2297 * will contain pointers into '*bufp'). */
2299 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
2300 struct ofpbuf **bufp)
2302 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2303 struct dpif_netlink_dp request;
2305 dpif_netlink_dp_init(&request);
2306 request.cmd = OVS_DP_CMD_GET;
2307 request.dp_ifindex = dpif->dp_ifindex;
2309 return dpif_netlink_dp_transact(&request, reply, bufp);
2312 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2313 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2314 * positive errno value.
2316 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2317 * while 'flow' is still in use. */
2319 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
2320 const struct ofpbuf *buf)
2322 static const struct nl_policy ovs_flow_policy[] = {
2323 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2324 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2325 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2326 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2328 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2329 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2330 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2333 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2334 struct ovs_header *ovs_header;
2335 struct nlmsghdr *nlmsg;
2336 struct genlmsghdr *genl;
2339 dpif_netlink_flow_init(flow);
2341 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2342 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2343 genl = ofpbuf_try_pull(&b, sizeof *genl);
2344 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2345 if (!nlmsg || !genl || !ovs_header
2346 || nlmsg->nlmsg_type != ovs_flow_family
2347 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2348 ARRAY_SIZE(ovs_flow_policy))) {
2352 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2353 flow->dp_ifindex = ovs_header->dp_ifindex;
2354 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2355 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2357 if (a[OVS_FLOW_ATTR_MASK]) {
2358 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2359 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2361 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2362 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2363 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2365 if (a[OVS_FLOW_ATTR_STATS]) {
2366 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2368 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2369 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2371 if (a[OVS_FLOW_ATTR_USED]) {
2372 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2377 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2378 * followed by Netlink attributes corresponding to 'flow'. */
2380 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
2383 struct ovs_header *ovs_header;
2385 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2386 NLM_F_REQUEST | flow->nlmsg_flags,
2387 flow->cmd, OVS_FLOW_VERSION);
2389 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2390 ovs_header->dp_ifindex = flow->dp_ifindex;
2392 if (flow->key_len) {
2393 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2396 if (flow->mask_len) {
2397 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2400 if (flow->actions || flow->actions_len) {
2401 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2402 flow->actions, flow->actions_len);
2405 /* We never need to send these to the kernel. */
2406 ovs_assert(!flow->stats);
2407 ovs_assert(!flow->tcp_flags);
2408 ovs_assert(!flow->used);
2411 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2415 /* Clears 'flow' to "empty" values. */
2417 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
2419 memset(flow, 0, sizeof *flow);
2422 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2423 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2424 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2425 * result of the command is expected to be a flow also, which is decoded and
2426 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2427 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2429 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
2430 struct dpif_netlink_flow *reply,
2431 struct ofpbuf **bufp)
2433 struct ofpbuf *request_buf;
2436 ovs_assert((reply != NULL) == (bufp != NULL));
2439 request->nlmsg_flags |= NLM_F_ECHO;
2442 request_buf = ofpbuf_new(1024);
2443 dpif_netlink_flow_to_ofpbuf(request, request_buf);
2444 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2445 ofpbuf_delete(request_buf);
2449 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
2452 dpif_netlink_flow_init(reply);
2453 ofpbuf_delete(*bufp);
2461 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
2462 struct dpif_flow_stats *stats)
2465 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
2466 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
2468 stats->n_packets = 0;
2471 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2472 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2475 /* Logs information about a packet that was recently lost in 'ch' (in
2478 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2479 uint32_t handler_id)
2481 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2484 if (VLOG_DROP_WARN(&rl)) {
2489 if (ch->last_poll != LLONG_MIN) {
2490 ds_put_format(&s, " (last polled %lld ms ago)",
2491 time_msec() - ch->last_poll);
2494 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2495 dpif_name(&dpif->dpif), ch_idx, handler_id);