2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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-linux.h"
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-notifier.h"
43 #include "netlink-socket.h"
47 #include "openvswitch/tunnel.h"
49 #include "poll-loop.h"
52 #include "unaligned.h"
56 VLOG_DEFINE_THIS_MODULE(dpif_linux);
58 enum { LRU_MAX_PORTS = 1024 };
59 enum { LRU_MASK = LRU_MAX_PORTS - 1};
60 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
62 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
63 * missing if we have old headers. */
64 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
66 struct dpif_linux_dp {
67 /* Generic Netlink header. */
70 /* struct ovs_header. */
74 const char *name; /* OVS_DP_ATTR_NAME. */
75 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
76 enum ovs_frag_handling ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
77 const uint32_t *sampling; /* OVS_DP_ATTR_SAMPLING. */
78 uint32_t mcgroups[DPIF_N_UC_TYPES]; /* OVS_DP_ATTR_MCGROUPS. */
81 static void dpif_linux_dp_init(struct dpif_linux_dp *);
82 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
83 const struct ofpbuf *);
84 static void dpif_linux_dp_dump_start(struct nl_dump *);
85 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
86 struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
88 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
89 struct ofpbuf **bufp);
91 struct dpif_linux_flow {
92 /* Generic Netlink header. */
95 /* struct ovs_header. */
96 unsigned int nlmsg_flags;
101 * The 'stats' and 'used' members point to 64-bit data that might only be
102 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
103 * access their values.
105 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
106 * the Netlink version of the command, even if actions_len is zero. */
107 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
109 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
111 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
112 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
113 const uint64_t *used; /* OVS_FLOW_ATTR_USED. */
114 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
117 static void dpif_linux_flow_init(struct dpif_linux_flow *);
118 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
119 const struct ofpbuf *);
120 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
122 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
123 struct dpif_linux_flow *reply,
124 struct ofpbuf **bufp);
125 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
126 struct dpif_flow_stats *);
128 /* Datapath interface for the openvswitch Linux kernel module. */
133 /* Multicast group messages. */
134 struct nl_sock *mc_sock;
135 uint32_t mcgroups[DPIF_N_UC_TYPES];
136 unsigned int listen_mask;
138 /* Change notification. */
139 struct sset changed_ports; /* Ports that have changed. */
140 struct nln_notifier port_notifier;
143 /* Queue of unused ports. */
144 unsigned long *lru_bitmap;
145 uint16_t lru_ports[LRU_MAX_PORTS];
150 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
152 /* Generic Netlink family numbers for OVS. */
153 static int ovs_datapath_family;
154 static int ovs_vport_family;
155 static int ovs_flow_family;
156 static int ovs_packet_family;
158 /* Generic Netlink socket. */
159 static struct nl_sock *genl_sock;
160 static struct nln *nln = NULL;
162 static int dpif_linux_init(void);
163 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
164 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
165 static void dpif_linux_port_changed(const void *vport, void *dpif);
167 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
169 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
170 const struct ofpbuf *);
172 static struct dpif_linux *
173 dpif_linux_cast(const struct dpif *dpif)
175 dpif_assert_class(dpif, &dpif_linux_class);
176 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
180 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
182 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
183 bitmap_set1(dp->lru_bitmap, port);
184 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
189 dpif_linux_pop_port(struct dpif_linux *dp)
193 if (dp->lru_head == dp->lru_tail) {
197 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
198 bitmap_set0(dp->lru_bitmap, port);
203 dpif_linux_enumerate(struct sset *all_dps)
209 error = dpif_linux_init();
214 dpif_linux_dp_dump_start(&dump);
215 while (nl_dump_next(&dump, &msg)) {
216 struct dpif_linux_dp dp;
218 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
219 sset_add(all_dps, dp.name);
222 return nl_dump_done(&dump);
226 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
227 bool create, struct dpif **dpifp)
229 struct dpif_linux_dp dp_request, dp;
233 error = dpif_linux_init();
238 /* Create or look up datapath. */
239 dpif_linux_dp_init(&dp_request);
240 dp_request.cmd = create ? OVS_DP_CMD_NEW : OVS_DP_CMD_GET;
241 dp_request.name = name;
242 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
246 error = open_dpif(&dp, dpifp);
253 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
255 struct dpif_linux *dpif;
259 dpif = xmalloc(sizeof *dpif);
260 error = nln_notifier_register(nln, &dpif->port_notifier,
261 dpif_linux_port_changed, dpif);
266 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
267 dp->dp_ifindex, dp->dp_ifindex);
269 dpif->mc_sock = NULL;
270 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
271 dpif->mcgroups[i] = dp->mcgroups[i];
273 dpif->listen_mask = 0;
274 dpif->dp_ifindex = dp->dp_ifindex;
275 sset_init(&dpif->changed_ports);
276 dpif->change_error = false;
277 *dpifp = &dpif->dpif;
279 dpif->lru_head = dpif->lru_tail = 0;
280 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
281 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
282 for (i = 1; i < LRU_MAX_PORTS; i++) {
283 dpif_linux_push_port(dpif, i);
293 dpif_linux_close(struct dpif *dpif_)
295 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
298 nln_notifier_unregister(nln, &dpif->port_notifier);
301 nl_sock_destroy(dpif->mc_sock);
302 sset_destroy(&dpif->changed_ports);
303 free(dpif->lru_bitmap);
308 dpif_linux_destroy(struct dpif *dpif_)
310 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
311 struct dpif_linux_dp dp;
313 dpif_linux_dp_init(&dp);
314 dp.cmd = OVS_DP_CMD_DEL;
315 dp.dp_ifindex = dpif->dp_ifindex;
316 return dpif_linux_dp_transact(&dp, NULL, NULL);
320 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
323 nln_notifier_run(nln);
328 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
331 nln_notifier_wait(nln);
336 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
338 struct dpif_linux_dp dp;
342 error = dpif_linux_dp_get(dpif_, &dp, &buf);
351 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
353 struct dpif_linux_dp dp;
357 error = dpif_linux_dp_get(dpif_, &dp, &buf);
359 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
366 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
368 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
369 struct dpif_linux_dp dp;
371 dpif_linux_dp_init(&dp);
372 dp.cmd = OVS_DP_CMD_SET;
373 dp.dp_ifindex = dpif->dp_ifindex;
374 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
375 return dpif_linux_dp_transact(&dp, NULL, NULL);
379 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
382 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
383 const char *name = netdev_get_name(netdev);
384 const char *type = netdev_get_type(netdev);
385 struct dpif_linux_vport request, reply;
386 const struct ofpbuf *options;
390 dpif_linux_vport_init(&request);
391 request.cmd = OVS_VPORT_CMD_NEW;
392 request.dp_ifindex = dpif->dp_ifindex;
393 request.type = netdev_vport_get_vport_type(netdev);
394 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
395 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
396 "unsupported type `%s'",
397 dpif_name(dpif_), name, type);
402 options = netdev_vport_get_options(netdev);
403 if (options && options->size) {
404 request.options = options->data;
405 request.options_len = options->size;
408 if (request.type == OVS_VPORT_TYPE_NETDEV) {
409 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
412 /* Loop until we find a port that isn't used. */
414 request.port_no = dpif_linux_pop_port(dpif);
415 error = dpif_linux_vport_transact(&request, &reply, &buf);
418 *port_nop = reply.port_no;
421 } while (request.port_no != UINT32_MAX
422 && (error == EBUSY || error == EFBIG));
428 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
430 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
431 struct dpif_linux_vport vport;
434 dpif_linux_vport_init(&vport);
435 vport.cmd = OVS_VPORT_CMD_DEL;
436 vport.dp_ifindex = dpif->dp_ifindex;
437 vport.port_no = port_no;
438 error = dpif_linux_vport_transact(&vport, NULL, NULL);
441 dpif_linux_push_port(dpif, port_no);
447 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
448 const char *port_name, struct dpif_port *dpif_port)
450 struct dpif_linux_vport request;
451 struct dpif_linux_vport reply;
455 dpif_linux_vport_init(&request);
456 request.cmd = OVS_VPORT_CMD_GET;
457 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
458 request.port_no = port_no;
459 request.name = port_name;
461 error = dpif_linux_vport_transact(&request, &reply, &buf);
463 dpif_port->name = xstrdup(reply.name);
464 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
465 dpif_port->port_no = reply.port_no;
467 netdev_stats_from_rtnl_link_stats64(&dpif_port->stats,
470 memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
478 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
479 struct dpif_port *dpif_port)
481 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
485 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
486 struct dpif_port *dpif_port)
488 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
492 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
494 /* If the datapath increases its range of supported ports, then it should
495 * start reporting that. */
500 dpif_linux_flow_flush(struct dpif *dpif_)
502 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
503 struct dpif_linux_flow flow;
505 dpif_linux_flow_init(&flow);
506 flow.cmd = OVS_FLOW_CMD_DEL;
507 flow.dp_ifindex = dpif->dp_ifindex;
508 return dpif_linux_flow_transact(&flow, NULL, NULL);
511 struct dpif_linux_port_state {
513 unsigned long *port_bitmap; /* Ports in the datapath. */
514 bool complete; /* Dump completed without error. */
518 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
520 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
521 struct dpif_linux_port_state *state;
522 struct dpif_linux_vport request;
525 *statep = state = xmalloc(sizeof *state);
526 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
527 state->complete = false;
529 dpif_linux_vport_init(&request);
530 request.cmd = OVS_DP_CMD_GET;
531 request.dp_ifindex = dpif->dp_ifindex;
533 buf = ofpbuf_new(1024);
534 dpif_linux_vport_to_ofpbuf(&request, buf);
535 nl_dump_start(&state->dump, genl_sock, buf);
542 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
543 struct dpif_port *dpif_port)
545 struct dpif_linux_port_state *state = state_;
546 struct dpif_linux_vport vport;
550 if (!nl_dump_next(&state->dump, &buf)) {
551 state->complete = true;
555 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
560 if (vport.port_no < LRU_MAX_PORTS) {
561 bitmap_set1(state->port_bitmap, vport.port_no);
564 dpif_port->name = (char *) vport.name;
565 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
566 dpif_port->port_no = vport.port_no;
568 netdev_stats_from_rtnl_link_stats64(&dpif_port->stats, vport.stats);
570 memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
576 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
578 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
579 struct dpif_linux_port_state *state = state_;
580 int error = nl_dump_done(&state->dump);
582 if (state->complete) {
585 for (i = 0; i < LRU_MAX_PORTS; i++) {
586 if (!bitmap_is_set(state->port_bitmap, i)) {
587 dpif_linux_push_port(dpif, i);
592 free(state->port_bitmap);
598 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
600 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
602 if (dpif->change_error) {
603 dpif->change_error = false;
604 sset_clear(&dpif->changed_ports);
606 } else if (!sset_is_empty(&dpif->changed_ports)) {
607 *devnamep = sset_pop(&dpif->changed_ports);
615 dpif_linux_port_poll_wait(const struct dpif *dpif_)
617 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
618 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
619 poll_immediate_wake();
624 dpif_linux_flow_get__(const struct dpif *dpif_,
625 const struct nlattr *key, size_t key_len,
626 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
628 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
629 struct dpif_linux_flow request;
631 dpif_linux_flow_init(&request);
632 request.cmd = OVS_FLOW_CMD_GET;
633 request.dp_ifindex = dpif->dp_ifindex;
635 request.key_len = key_len;
636 return dpif_linux_flow_transact(&request, reply, bufp);
640 dpif_linux_flow_get(const struct dpif *dpif_,
641 const struct nlattr *key, size_t key_len,
642 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
644 struct dpif_linux_flow reply;
648 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
651 dpif_linux_flow_get_stats(&reply, stats);
654 buf->data = (void *) reply.actions;
655 buf->size = reply.actions_len;
665 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
666 const struct nlattr *key, size_t key_len,
667 const struct nlattr *actions, size_t actions_len,
668 struct dpif_flow_stats *stats)
670 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
671 struct dpif_linux_flow request, reply;
672 struct nlattr dummy_action;
676 dpif_linux_flow_init(&request);
677 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
678 request.dp_ifindex = dpif->dp_ifindex;
680 request.key_len = key_len;
681 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
682 request.actions = actions ? actions : &dummy_action;
683 request.actions_len = actions_len;
684 if (flags & DPIF_FP_ZERO_STATS) {
685 request.clear = true;
687 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
688 error = dpif_linux_flow_transact(&request,
689 stats ? &reply : NULL,
690 stats ? &buf : NULL);
691 if (!error && stats) {
692 dpif_linux_flow_get_stats(&reply, stats);
699 dpif_linux_flow_del(struct dpif *dpif_,
700 const struct nlattr *key, size_t key_len,
701 struct dpif_flow_stats *stats)
703 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
704 struct dpif_linux_flow request, reply;
708 dpif_linux_flow_init(&request);
709 request.cmd = OVS_FLOW_CMD_DEL;
710 request.dp_ifindex = dpif->dp_ifindex;
712 request.key_len = key_len;
713 error = dpif_linux_flow_transact(&request,
714 stats ? &reply : NULL,
715 stats ? &buf : NULL);
716 if (!error && stats) {
717 dpif_linux_flow_get_stats(&reply, stats);
723 struct dpif_linux_flow_state {
725 struct dpif_linux_flow flow;
726 struct dpif_flow_stats stats;
731 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
733 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
734 struct dpif_linux_flow_state *state;
735 struct dpif_linux_flow request;
738 *statep = state = xmalloc(sizeof *state);
740 dpif_linux_flow_init(&request);
741 request.cmd = OVS_DP_CMD_GET;
742 request.dp_ifindex = dpif->dp_ifindex;
744 buf = ofpbuf_new(1024);
745 dpif_linux_flow_to_ofpbuf(&request, buf);
746 nl_dump_start(&state->dump, genl_sock, buf);
755 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
756 const struct nlattr **key, size_t *key_len,
757 const struct nlattr **actions, size_t *actions_len,
758 const struct dpif_flow_stats **stats)
760 struct dpif_linux_flow_state *state = state_;
765 ofpbuf_delete(state->buf);
768 if (!nl_dump_next(&state->dump, &buf)) {
772 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
777 if (actions && !state->flow.actions) {
778 error = dpif_linux_flow_get__(dpif_, state->flow.key,
780 &state->flow, &state->buf);
781 if (error == ENOENT) {
782 VLOG_DBG("dumped flow disappeared on get");
784 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
790 *actions = state->flow.actions;
791 *actions_len = state->flow.actions_len;
794 *key = state->flow.key;
795 *key_len = state->flow.key_len;
798 dpif_linux_flow_get_stats(&state->flow, &state->stats);
799 *stats = &state->stats;
805 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
807 struct dpif_linux_flow_state *state = state_;
808 int error = nl_dump_done(&state->dump);
809 ofpbuf_delete(state->buf);
815 dpif_linux_execute__(int dp_ifindex,
816 const struct nlattr *key, size_t key_len,
817 const struct nlattr *actions, size_t actions_len,
818 const struct ofpbuf *packet)
820 struct ovs_header *execute;
824 buf = ofpbuf_new(128 + actions_len + packet->size);
826 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
827 OVS_PACKET_CMD_EXECUTE, 1);
829 execute = ofpbuf_put_uninit(buf, sizeof *execute);
830 execute->dp_ifindex = dp_ifindex;
832 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
833 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
834 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
836 error = nl_sock_transact(genl_sock, buf, NULL);
842 dpif_linux_execute(struct dpif *dpif_,
843 const struct nlattr *key, size_t key_len,
844 const struct nlattr *actions, size_t actions_len,
845 const struct ofpbuf *packet)
847 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
849 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
850 actions, actions_len, packet);
854 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
856 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
857 *listen_mask = dpif->listen_mask;
862 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
864 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
868 if (listen_mask == dpif->listen_mask) {
870 } else if (!listen_mask) {
871 nl_sock_destroy(dpif->mc_sock);
872 dpif->mc_sock = NULL;
873 dpif->listen_mask = 0;
875 } else if (!dpif->mc_sock) {
876 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
882 /* Unsubscribe from old groups. */
883 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
884 if (dpif->listen_mask & (1u << i)) {
885 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
889 /* Update listen_mask. */
890 dpif->listen_mask = listen_mask;
892 /* Subscribe to new groups. */
894 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
895 if (dpif->listen_mask & (1u << i)) {
898 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
908 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
909 uint32_t *probability)
911 struct dpif_linux_dp dp;
915 error = dpif_linux_dp_get(dpif_, &dp, &buf);
917 *probability = dp.sampling ? *dp.sampling : 0;
924 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
926 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
927 struct dpif_linux_dp dp;
929 dpif_linux_dp_init(&dp);
930 dp.cmd = OVS_DP_CMD_SET;
931 dp.dp_ifindex = dpif->dp_ifindex;
932 dp.sampling = &probability;
933 return dpif_linux_dp_transact(&dp, NULL, NULL);
937 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
938 uint32_t queue_id, uint32_t *priority)
940 if (queue_id < 0xf000) {
941 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
949 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
952 static const struct nl_policy ovs_packet_policy[] = {
953 /* Always present. */
954 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
955 .min_len = ETH_HEADER_LEN },
956 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
958 /* OVS_PACKET_CMD_ACTION only. */
959 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
961 /* OVS_PACKET_CMD_SAMPLE only. */
962 [OVS_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
963 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
966 struct ovs_header *ovs_header;
967 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
968 struct nlmsghdr *nlmsg;
969 struct genlmsghdr *genl;
973 ofpbuf_use_const(&b, buf->data, buf->size);
975 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
976 genl = ofpbuf_try_pull(&b, sizeof *genl);
977 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
978 if (!nlmsg || !genl || !ovs_header
979 || nlmsg->nlmsg_type != ovs_packet_family
980 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
981 ARRAY_SIZE(ovs_packet_policy))) {
985 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
986 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
987 : genl->cmd == OVS_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
993 memset(upcall, 0, sizeof *upcall);
995 upcall->packet = buf;
996 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
997 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
998 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
999 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1000 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1001 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1003 upcall->sample_pool = (a[OVS_PACKET_ATTR_SAMPLE_POOL]
1004 ? nl_attr_get_u32(a[OVS_PACKET_ATTR_SAMPLE_POOL])
1006 if (a[OVS_PACKET_ATTR_ACTIONS]) {
1007 upcall->actions = (void *) nl_attr_get(a[OVS_PACKET_ATTR_ACTIONS]);
1008 upcall->actions_len = nl_attr_get_size(a[OVS_PACKET_ATTR_ACTIONS]);
1011 *dp_ifindex = ovs_header->dp_ifindex;
1017 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1019 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1024 if (!dpif->mc_sock) {
1028 for (i = 0; i < 50; i++) {
1031 error = nl_sock_recv(dpif->mc_sock, &buf, false);
1036 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1038 && dp_ifindex == dpif->dp_ifindex
1039 && dpif->listen_mask & (1u << upcall->type)) {
1053 dpif_linux_recv_wait(struct dpif *dpif_)
1055 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1056 if (dpif->mc_sock) {
1057 nl_sock_wait(dpif->mc_sock, POLLIN);
1062 dpif_linux_recv_purge(struct dpif *dpif_)
1064 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1066 if (dpif->mc_sock) {
1067 nl_sock_drain(dpif->mc_sock);
1071 const struct dpif_class dpif_linux_class = {
1073 dpif_linux_enumerate,
1079 dpif_linux_get_stats,
1080 dpif_linux_get_drop_frags,
1081 dpif_linux_set_drop_frags,
1082 dpif_linux_port_add,
1083 dpif_linux_port_del,
1084 dpif_linux_port_query_by_number,
1085 dpif_linux_port_query_by_name,
1086 dpif_linux_get_max_ports,
1087 dpif_linux_port_dump_start,
1088 dpif_linux_port_dump_next,
1089 dpif_linux_port_dump_done,
1090 dpif_linux_port_poll,
1091 dpif_linux_port_poll_wait,
1092 dpif_linux_flow_get,
1093 dpif_linux_flow_put,
1094 dpif_linux_flow_del,
1095 dpif_linux_flow_flush,
1096 dpif_linux_flow_dump_start,
1097 dpif_linux_flow_dump_next,
1098 dpif_linux_flow_dump_done,
1100 dpif_linux_recv_get_mask,
1101 dpif_linux_recv_set_mask,
1102 dpif_linux_get_sflow_probability,
1103 dpif_linux_set_sflow_probability,
1104 dpif_linux_queue_to_priority,
1106 dpif_linux_recv_wait,
1107 dpif_linux_recv_purge,
1111 dpif_linux_init(void)
1113 static int error = -1;
1116 unsigned int ovs_vport_mcgroup;
1118 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1119 &ovs_datapath_family);
1121 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1122 "The Open vSwitch kernel module is probably not loaded.",
1123 OVS_DATAPATH_FAMILY);
1126 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1129 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1132 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1133 &ovs_packet_family);
1136 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1139 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1140 &ovs_vport_mcgroup);
1143 static struct dpif_linux_vport vport;
1144 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1145 dpif_linux_nln_parse, &vport);
1153 dpif_linux_is_internal_device(const char *name)
1155 struct dpif_linux_vport reply;
1159 error = dpif_linux_vport_get(name, &reply, &buf);
1162 } else if (error != ENODEV && error != ENOENT) {
1163 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1164 name, strerror(error));
1167 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1171 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1172 const void *data, size_t size)
1174 struct ofpbuf actions, key, packet;
1175 struct odputil_keybuf keybuf;
1179 ofpbuf_use_const(&packet, data, size);
1180 flow_extract(&packet, htonll(0), 0, &flow);
1182 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1183 odp_flow_key_from_flow(&key, &flow);
1185 ofpbuf_use_stack(&actions, &action, sizeof action);
1186 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1188 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1189 actions.data, actions.size, &packet);
1193 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1195 struct dpif_linux_vport *vport = vport_;
1196 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1200 dpif_linux_port_changed(const void *vport_, void *dpif_)
1202 const struct dpif_linux_vport *vport = vport_;
1203 struct dpif_linux *dpif = dpif_;
1206 if (vport->dp_ifindex == dpif->dp_ifindex
1207 && (vport->cmd == OVS_VPORT_CMD_NEW
1208 || vport->cmd == OVS_VPORT_CMD_DEL
1209 || vport->cmd == OVS_VPORT_CMD_SET)) {
1210 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1211 dpif->dpif.full_name, vport->name, vport->cmd);
1212 sset_add(&dpif->changed_ports, vport->name);
1215 dpif->change_error = true;
1219 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1220 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1221 * positive errno value.
1223 * 'vport' will contain pointers into 'buf', so the caller should not free
1224 * 'buf' while 'vport' is still in use. */
1226 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1227 const struct ofpbuf *buf)
1229 static const struct nl_policy ovs_vport_policy[] = {
1230 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1231 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1232 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1233 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1234 .min_len = sizeof(struct rtnl_link_stats64),
1235 .max_len = sizeof(struct rtnl_link_stats64),
1237 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1238 .min_len = ETH_ADDR_LEN,
1239 .max_len = ETH_ADDR_LEN,
1241 [OVS_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1242 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1243 [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1246 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1247 struct ovs_header *ovs_header;
1248 struct nlmsghdr *nlmsg;
1249 struct genlmsghdr *genl;
1252 dpif_linux_vport_init(vport);
1254 ofpbuf_use_const(&b, buf->data, buf->size);
1255 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1256 genl = ofpbuf_try_pull(&b, sizeof *genl);
1257 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1258 if (!nlmsg || !genl || !ovs_header
1259 || nlmsg->nlmsg_type != ovs_vport_family
1260 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1261 ARRAY_SIZE(ovs_vport_policy))) {
1265 vport->cmd = genl->cmd;
1266 vport->dp_ifindex = ovs_header->dp_ifindex;
1267 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1268 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1269 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1270 if (a[OVS_VPORT_ATTR_STATS]) {
1271 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1273 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1274 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1276 if (a[OVS_VPORT_ATTR_MTU]) {
1277 vport->mtu = nl_attr_get_u32(a[OVS_VPORT_ATTR_MTU]);
1279 vport->mtu = INT_MAX;
1281 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1282 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1283 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1285 if (a[OVS_VPORT_ATTR_IFINDEX]) {
1286 vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1291 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1292 * followed by Netlink attributes corresponding to 'vport'. */
1294 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1297 struct ovs_header *ovs_header;
1299 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1302 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1303 ovs_header->dp_ifindex = vport->dp_ifindex;
1305 if (vport->port_no != UINT32_MAX) {
1306 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1309 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1310 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1314 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1318 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1319 vport->stats, sizeof *vport->stats);
1322 if (vport->address) {
1323 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1324 vport->address, ETH_ADDR_LEN);
1327 if (vport->mtu && vport->mtu != INT_MAX) {
1328 nl_msg_put_u32(buf, OVS_VPORT_ATTR_MTU, vport->mtu);
1331 if (vport->options) {
1332 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1333 vport->options, vport->options_len);
1336 if (vport->ifindex) {
1337 nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1341 /* Clears 'vport' to "empty" values. */
1343 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1345 memset(vport, 0, sizeof *vport);
1346 vport->port_no = UINT32_MAX;
1349 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1350 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1351 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1352 * result of the command is expected to be an ovs_vport also, which is decoded
1353 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1354 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1356 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1357 struct dpif_linux_vport *reply,
1358 struct ofpbuf **bufp)
1360 struct ofpbuf *request_buf;
1363 assert((reply != NULL) == (bufp != NULL));
1365 error = dpif_linux_init();
1369 dpif_linux_vport_init(reply);
1374 request_buf = ofpbuf_new(1024);
1375 dpif_linux_vport_to_ofpbuf(request, request_buf);
1376 error = nl_sock_transact(genl_sock, request_buf, bufp);
1377 ofpbuf_delete(request_buf);
1381 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1384 dpif_linux_vport_init(reply);
1385 ofpbuf_delete(*bufp);
1392 /* Obtains information about the kernel vport named 'name' and stores it into
1393 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1394 * longer needed ('reply' will contain pointers into '*bufp'). */
1396 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1397 struct ofpbuf **bufp)
1399 struct dpif_linux_vport request;
1401 dpif_linux_vport_init(&request);
1402 request.cmd = OVS_VPORT_CMD_GET;
1403 request.name = name;
1405 return dpif_linux_vport_transact(&request, reply, bufp);
1408 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1409 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1410 * positive errno value.
1412 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1413 * while 'dp' is still in use. */
1415 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1417 static const struct nl_policy ovs_datapath_policy[] = {
1418 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1419 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1420 .min_len = sizeof(struct ovs_dp_stats),
1421 .max_len = sizeof(struct ovs_dp_stats),
1423 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1424 [OVS_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1425 [OVS_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1428 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1429 struct ovs_header *ovs_header;
1430 struct nlmsghdr *nlmsg;
1431 struct genlmsghdr *genl;
1434 dpif_linux_dp_init(dp);
1436 ofpbuf_use_const(&b, buf->data, buf->size);
1437 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1438 genl = ofpbuf_try_pull(&b, sizeof *genl);
1439 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1440 if (!nlmsg || !genl || !ovs_header
1441 || nlmsg->nlmsg_type != ovs_datapath_family
1442 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1443 ARRAY_SIZE(ovs_datapath_policy))) {
1447 dp->cmd = genl->cmd;
1448 dp->dp_ifindex = ovs_header->dp_ifindex;
1449 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1450 if (a[OVS_DP_ATTR_STATS]) {
1451 /* Can't use structure assignment because Netlink doesn't ensure
1452 * sufficient alignment for 64-bit members. */
1453 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1456 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1457 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1459 if (a[OVS_DP_ATTR_SAMPLING]) {
1460 dp->sampling = nl_attr_get(a[OVS_DP_ATTR_SAMPLING]);
1463 if (a[OVS_DP_ATTR_MCGROUPS]) {
1464 static const struct nl_policy ovs_mcgroup_policy[] = {
1465 [OVS_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1466 [OVS_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1467 [OVS_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1470 struct nlattr *mcgroups[ARRAY_SIZE(ovs_mcgroup_policy)];
1472 if (!nl_parse_nested(a[OVS_DP_ATTR_MCGROUPS], ovs_mcgroup_policy,
1473 mcgroups, ARRAY_SIZE(ovs_mcgroup_policy))) {
1477 if (mcgroups[OVS_PACKET_CMD_MISS]) {
1478 dp->mcgroups[DPIF_UC_MISS]
1479 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_MISS]);
1481 if (mcgroups[OVS_PACKET_CMD_ACTION]) {
1482 dp->mcgroups[DPIF_UC_ACTION]
1483 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_ACTION]);
1485 if (mcgroups[OVS_PACKET_CMD_SAMPLE]) {
1486 dp->mcgroups[DPIF_UC_SAMPLE]
1487 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_SAMPLE]);
1494 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1496 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1498 struct ovs_header *ovs_header;
1500 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1501 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1503 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1504 ovs_header->dp_ifindex = dp->dp_ifindex;
1507 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1510 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1512 if (dp->ipv4_frags) {
1513 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1517 nl_msg_put_u32(buf, OVS_DP_ATTR_SAMPLING, *dp->sampling);
1521 /* Clears 'dp' to "empty" values. */
1523 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1525 memset(dp, 0, sizeof *dp);
1529 dpif_linux_dp_dump_start(struct nl_dump *dump)
1531 struct dpif_linux_dp request;
1534 dpif_linux_dp_init(&request);
1535 request.cmd = OVS_DP_CMD_GET;
1537 buf = ofpbuf_new(1024);
1538 dpif_linux_dp_to_ofpbuf(&request, buf);
1539 nl_dump_start(dump, genl_sock, buf);
1543 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1544 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1545 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1546 * result of the command is expected to be of the same form, which is decoded
1547 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1548 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1550 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1551 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1553 struct ofpbuf *request_buf;
1556 assert((reply != NULL) == (bufp != NULL));
1558 request_buf = ofpbuf_new(1024);
1559 dpif_linux_dp_to_ofpbuf(request, request_buf);
1560 error = nl_sock_transact(genl_sock, request_buf, bufp);
1561 ofpbuf_delete(request_buf);
1565 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1568 dpif_linux_dp_init(reply);
1569 ofpbuf_delete(*bufp);
1576 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1577 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1578 * will contain pointers into '*bufp'). */
1580 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1581 struct ofpbuf **bufp)
1583 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1584 struct dpif_linux_dp request;
1586 dpif_linux_dp_init(&request);
1587 request.cmd = OVS_DP_CMD_GET;
1588 request.dp_ifindex = dpif->dp_ifindex;
1590 return dpif_linux_dp_transact(&request, reply, bufp);
1593 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1594 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1595 * positive errno value.
1597 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1598 * while 'flow' is still in use. */
1600 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1601 const struct ofpbuf *buf)
1603 static const struct nl_policy ovs_flow_policy[] = {
1604 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1605 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1606 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1607 .min_len = sizeof(struct ovs_flow_stats),
1608 .max_len = sizeof(struct ovs_flow_stats),
1610 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1611 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1612 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1615 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1616 struct ovs_header *ovs_header;
1617 struct nlmsghdr *nlmsg;
1618 struct genlmsghdr *genl;
1621 dpif_linux_flow_init(flow);
1623 ofpbuf_use_const(&b, buf->data, buf->size);
1624 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1625 genl = ofpbuf_try_pull(&b, sizeof *genl);
1626 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1627 if (!nlmsg || !genl || !ovs_header
1628 || nlmsg->nlmsg_type != ovs_flow_family
1629 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1630 ARRAY_SIZE(ovs_flow_policy))) {
1634 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1635 flow->dp_ifindex = ovs_header->dp_ifindex;
1636 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1637 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1638 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1639 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1640 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1642 if (a[OVS_FLOW_ATTR_STATS]) {
1643 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1645 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1646 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1648 if (a[OVS_FLOW_ATTR_USED]) {
1649 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1654 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1655 * followed by Netlink attributes corresponding to 'flow'. */
1657 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1660 struct ovs_header *ovs_header;
1662 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1663 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1666 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1667 ovs_header->dp_ifindex = flow->dp_ifindex;
1669 if (flow->key_len) {
1670 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1673 if (flow->actions || flow->actions_len) {
1674 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1675 flow->actions, flow->actions_len);
1678 /* We never need to send these to the kernel. */
1679 assert(!flow->stats);
1680 assert(!flow->tcp_flags);
1681 assert(!flow->used);
1684 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1688 /* Clears 'flow' to "empty" values. */
1690 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1692 memset(flow, 0, sizeof *flow);
1695 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1696 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1697 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1698 * result of the command is expected to be a flow also, which is decoded and
1699 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1700 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1702 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1703 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1705 struct ofpbuf *request_buf;
1708 assert((reply != NULL) == (bufp != NULL));
1710 request_buf = ofpbuf_new(1024);
1711 dpif_linux_flow_to_ofpbuf(request, request_buf);
1712 error = nl_sock_transact(genl_sock, request_buf, bufp);
1713 ofpbuf_delete(request_buf);
1717 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1720 dpif_linux_flow_init(reply);
1721 ofpbuf_delete(*bufp);
1729 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1730 struct dpif_flow_stats *stats)
1733 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1734 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1736 stats->n_packets = 0;
1739 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1740 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;