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;
472 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
473 struct dpif_port *dpif_port)
475 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
479 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
480 struct dpif_port *dpif_port)
482 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
486 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
488 /* If the datapath increases its range of supported ports, then it should
489 * start reporting that. */
494 dpif_linux_flow_flush(struct dpif *dpif_)
496 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
497 struct dpif_linux_flow flow;
499 dpif_linux_flow_init(&flow);
500 flow.cmd = OVS_FLOW_CMD_DEL;
501 flow.dp_ifindex = dpif->dp_ifindex;
502 return dpif_linux_flow_transact(&flow, NULL, NULL);
505 struct dpif_linux_port_state {
507 unsigned long *port_bitmap; /* Ports in the datapath. */
508 bool complete; /* Dump completed without error. */
512 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
514 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
515 struct dpif_linux_port_state *state;
516 struct dpif_linux_vport request;
519 *statep = state = xmalloc(sizeof *state);
520 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
521 state->complete = false;
523 dpif_linux_vport_init(&request);
524 request.cmd = OVS_DP_CMD_GET;
525 request.dp_ifindex = dpif->dp_ifindex;
527 buf = ofpbuf_new(1024);
528 dpif_linux_vport_to_ofpbuf(&request, buf);
529 nl_dump_start(&state->dump, genl_sock, buf);
536 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
537 struct dpif_port *dpif_port)
539 struct dpif_linux_port_state *state = state_;
540 struct dpif_linux_vport vport;
544 if (!nl_dump_next(&state->dump, &buf)) {
545 state->complete = true;
549 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
554 if (vport.port_no < LRU_MAX_PORTS) {
555 bitmap_set1(state->port_bitmap, vport.port_no);
558 dpif_port->name = (char *) vport.name;
559 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
560 dpif_port->port_no = vport.port_no;
565 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
567 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
568 struct dpif_linux_port_state *state = state_;
569 int error = nl_dump_done(&state->dump);
571 if (state->complete) {
574 for (i = 0; i < LRU_MAX_PORTS; i++) {
575 if (!bitmap_is_set(state->port_bitmap, i)) {
576 dpif_linux_push_port(dpif, i);
581 free(state->port_bitmap);
587 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
589 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
591 if (dpif->change_error) {
592 dpif->change_error = false;
593 sset_clear(&dpif->changed_ports);
595 } else if (!sset_is_empty(&dpif->changed_ports)) {
596 *devnamep = sset_pop(&dpif->changed_ports);
604 dpif_linux_port_poll_wait(const struct dpif *dpif_)
606 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
607 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
608 poll_immediate_wake();
613 dpif_linux_flow_get__(const struct dpif *dpif_,
614 const struct nlattr *key, size_t key_len,
615 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
617 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
618 struct dpif_linux_flow request;
620 dpif_linux_flow_init(&request);
621 request.cmd = OVS_FLOW_CMD_GET;
622 request.dp_ifindex = dpif->dp_ifindex;
624 request.key_len = key_len;
625 return dpif_linux_flow_transact(&request, reply, bufp);
629 dpif_linux_flow_get(const struct dpif *dpif_,
630 const struct nlattr *key, size_t key_len,
631 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
633 struct dpif_linux_flow reply;
637 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
640 dpif_linux_flow_get_stats(&reply, stats);
643 buf->data = (void *) reply.actions;
644 buf->size = reply.actions_len;
654 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
655 const struct nlattr *key, size_t key_len,
656 const struct nlattr *actions, size_t actions_len,
657 struct dpif_flow_stats *stats)
659 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
660 struct dpif_linux_flow request, reply;
661 struct nlattr dummy_action;
665 dpif_linux_flow_init(&request);
666 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
667 request.dp_ifindex = dpif->dp_ifindex;
669 request.key_len = key_len;
670 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
671 request.actions = actions ? actions : &dummy_action;
672 request.actions_len = actions_len;
673 if (flags & DPIF_FP_ZERO_STATS) {
674 request.clear = true;
676 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
677 error = dpif_linux_flow_transact(&request,
678 stats ? &reply : NULL,
679 stats ? &buf : NULL);
680 if (!error && stats) {
681 dpif_linux_flow_get_stats(&reply, stats);
688 dpif_linux_flow_del(struct dpif *dpif_,
689 const struct nlattr *key, size_t key_len,
690 struct dpif_flow_stats *stats)
692 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
693 struct dpif_linux_flow request, reply;
697 dpif_linux_flow_init(&request);
698 request.cmd = OVS_FLOW_CMD_DEL;
699 request.dp_ifindex = dpif->dp_ifindex;
701 request.key_len = key_len;
702 error = dpif_linux_flow_transact(&request,
703 stats ? &reply : NULL,
704 stats ? &buf : NULL);
705 if (!error && stats) {
706 dpif_linux_flow_get_stats(&reply, stats);
712 struct dpif_linux_flow_state {
714 struct dpif_linux_flow flow;
715 struct dpif_flow_stats stats;
720 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
722 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
723 struct dpif_linux_flow_state *state;
724 struct dpif_linux_flow request;
727 *statep = state = xmalloc(sizeof *state);
729 dpif_linux_flow_init(&request);
730 request.cmd = OVS_DP_CMD_GET;
731 request.dp_ifindex = dpif->dp_ifindex;
733 buf = ofpbuf_new(1024);
734 dpif_linux_flow_to_ofpbuf(&request, buf);
735 nl_dump_start(&state->dump, genl_sock, buf);
744 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
745 const struct nlattr **key, size_t *key_len,
746 const struct nlattr **actions, size_t *actions_len,
747 const struct dpif_flow_stats **stats)
749 struct dpif_linux_flow_state *state = state_;
754 ofpbuf_delete(state->buf);
757 if (!nl_dump_next(&state->dump, &buf)) {
761 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
766 if (actions && !state->flow.actions) {
767 error = dpif_linux_flow_get__(dpif_, state->flow.key,
769 &state->flow, &state->buf);
770 if (error == ENOENT) {
771 VLOG_DBG("dumped flow disappeared on get");
773 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
779 *actions = state->flow.actions;
780 *actions_len = state->flow.actions_len;
783 *key = state->flow.key;
784 *key_len = state->flow.key_len;
787 dpif_linux_flow_get_stats(&state->flow, &state->stats);
788 *stats = &state->stats;
794 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
796 struct dpif_linux_flow_state *state = state_;
797 int error = nl_dump_done(&state->dump);
798 ofpbuf_delete(state->buf);
804 dpif_linux_execute__(int dp_ifindex,
805 const struct nlattr *key, size_t key_len,
806 const struct nlattr *actions, size_t actions_len,
807 const struct ofpbuf *packet)
809 struct ovs_header *execute;
813 buf = ofpbuf_new(128 + actions_len + packet->size);
815 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
816 OVS_PACKET_CMD_EXECUTE, 1);
818 execute = ofpbuf_put_uninit(buf, sizeof *execute);
819 execute->dp_ifindex = dp_ifindex;
821 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
822 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
823 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
825 error = nl_sock_transact(genl_sock, buf, NULL);
831 dpif_linux_execute(struct dpif *dpif_,
832 const struct nlattr *key, size_t key_len,
833 const struct nlattr *actions, size_t actions_len,
834 const struct ofpbuf *packet)
836 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
838 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
839 actions, actions_len, packet);
843 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
845 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
846 *listen_mask = dpif->listen_mask;
851 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
853 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
857 if (listen_mask == dpif->listen_mask) {
859 } else if (!listen_mask) {
860 nl_sock_destroy(dpif->mc_sock);
861 dpif->mc_sock = NULL;
862 dpif->listen_mask = 0;
864 } else if (!dpif->mc_sock) {
865 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
871 /* Unsubscribe from old groups. */
872 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
873 if (dpif->listen_mask & (1u << i)) {
874 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
878 /* Update listen_mask. */
879 dpif->listen_mask = listen_mask;
881 /* Subscribe to new groups. */
883 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
884 if (dpif->listen_mask & (1u << i)) {
887 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
897 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
898 uint32_t *probability)
900 struct dpif_linux_dp dp;
904 error = dpif_linux_dp_get(dpif_, &dp, &buf);
906 *probability = dp.sampling ? *dp.sampling : 0;
913 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
915 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
916 struct dpif_linux_dp dp;
918 dpif_linux_dp_init(&dp);
919 dp.cmd = OVS_DP_CMD_SET;
920 dp.dp_ifindex = dpif->dp_ifindex;
921 dp.sampling = &probability;
922 return dpif_linux_dp_transact(&dp, NULL, NULL);
926 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
927 uint32_t queue_id, uint32_t *priority)
929 if (queue_id < 0xf000) {
930 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
938 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
941 static const struct nl_policy ovs_packet_policy[] = {
942 /* Always present. */
943 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
944 .min_len = ETH_HEADER_LEN },
945 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
947 /* OVS_PACKET_CMD_ACTION only. */
948 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
950 /* OVS_PACKET_CMD_SAMPLE only. */
951 [OVS_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
952 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
955 struct ovs_header *ovs_header;
956 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
957 struct nlmsghdr *nlmsg;
958 struct genlmsghdr *genl;
962 ofpbuf_use_const(&b, buf->data, buf->size);
964 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
965 genl = ofpbuf_try_pull(&b, sizeof *genl);
966 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
967 if (!nlmsg || !genl || !ovs_header
968 || nlmsg->nlmsg_type != ovs_packet_family
969 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
970 ARRAY_SIZE(ovs_packet_policy))) {
974 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
975 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
976 : genl->cmd == OVS_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
982 memset(upcall, 0, sizeof *upcall);
984 upcall->packet = buf;
985 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
986 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
987 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
988 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
989 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
990 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
992 upcall->sample_pool = (a[OVS_PACKET_ATTR_SAMPLE_POOL]
993 ? nl_attr_get_u32(a[OVS_PACKET_ATTR_SAMPLE_POOL])
995 if (a[OVS_PACKET_ATTR_ACTIONS]) {
996 upcall->actions = (void *) nl_attr_get(a[OVS_PACKET_ATTR_ACTIONS]);
997 upcall->actions_len = nl_attr_get_size(a[OVS_PACKET_ATTR_ACTIONS]);
1000 *dp_ifindex = ovs_header->dp_ifindex;
1006 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1008 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1013 if (!dpif->mc_sock) {
1017 for (i = 0; i < 50; i++) {
1020 error = nl_sock_recv(dpif->mc_sock, &buf, false);
1025 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1027 && dp_ifindex == dpif->dp_ifindex
1028 && dpif->listen_mask & (1u << upcall->type)) {
1042 dpif_linux_recv_wait(struct dpif *dpif_)
1044 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1045 if (dpif->mc_sock) {
1046 nl_sock_wait(dpif->mc_sock, POLLIN);
1051 dpif_linux_recv_purge(struct dpif *dpif_)
1053 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1055 if (dpif->mc_sock) {
1056 nl_sock_drain(dpif->mc_sock);
1060 const struct dpif_class dpif_linux_class = {
1062 dpif_linux_enumerate,
1068 dpif_linux_get_stats,
1069 dpif_linux_get_drop_frags,
1070 dpif_linux_set_drop_frags,
1071 dpif_linux_port_add,
1072 dpif_linux_port_del,
1073 dpif_linux_port_query_by_number,
1074 dpif_linux_port_query_by_name,
1075 dpif_linux_get_max_ports,
1076 dpif_linux_port_dump_start,
1077 dpif_linux_port_dump_next,
1078 dpif_linux_port_dump_done,
1079 dpif_linux_port_poll,
1080 dpif_linux_port_poll_wait,
1081 dpif_linux_flow_get,
1082 dpif_linux_flow_put,
1083 dpif_linux_flow_del,
1084 dpif_linux_flow_flush,
1085 dpif_linux_flow_dump_start,
1086 dpif_linux_flow_dump_next,
1087 dpif_linux_flow_dump_done,
1089 dpif_linux_recv_get_mask,
1090 dpif_linux_recv_set_mask,
1091 dpif_linux_get_sflow_probability,
1092 dpif_linux_set_sflow_probability,
1093 dpif_linux_queue_to_priority,
1095 dpif_linux_recv_wait,
1096 dpif_linux_recv_purge,
1100 dpif_linux_init(void)
1102 static int error = -1;
1105 unsigned int ovs_vport_mcgroup;
1107 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1108 &ovs_datapath_family);
1110 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1111 "The Open vSwitch kernel module is probably not loaded.",
1112 OVS_DATAPATH_FAMILY);
1115 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1118 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1121 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1122 &ovs_packet_family);
1125 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1128 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1129 &ovs_vport_mcgroup);
1132 static struct dpif_linux_vport vport;
1133 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1134 dpif_linux_nln_parse, &vport);
1142 dpif_linux_is_internal_device(const char *name)
1144 struct dpif_linux_vport reply;
1148 error = dpif_linux_vport_get(name, &reply, &buf);
1151 } else if (error != ENODEV && error != ENOENT) {
1152 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1153 name, strerror(error));
1156 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1160 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1161 const void *data, size_t size)
1163 struct ofpbuf actions, key, packet;
1164 struct odputil_keybuf keybuf;
1168 ofpbuf_use_const(&packet, data, size);
1169 flow_extract(&packet, htonll(0), 0, &flow);
1171 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1172 odp_flow_key_from_flow(&key, &flow);
1174 ofpbuf_use_stack(&actions, &action, sizeof action);
1175 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1177 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1178 actions.data, actions.size, &packet);
1182 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1184 struct dpif_linux_vport *vport = vport_;
1185 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1189 dpif_linux_port_changed(const void *vport_, void *dpif_)
1191 const struct dpif_linux_vport *vport = vport_;
1192 struct dpif_linux *dpif = dpif_;
1195 if (vport->dp_ifindex == dpif->dp_ifindex
1196 && (vport->cmd == OVS_VPORT_CMD_NEW
1197 || vport->cmd == OVS_VPORT_CMD_DEL
1198 || vport->cmd == OVS_VPORT_CMD_SET)) {
1199 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1200 dpif->dpif.full_name, vport->name, vport->cmd);
1201 sset_add(&dpif->changed_ports, vport->name);
1204 dpif->change_error = true;
1208 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1209 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1210 * positive errno value.
1212 * 'vport' will contain pointers into 'buf', so the caller should not free
1213 * 'buf' while 'vport' is still in use. */
1215 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1216 const struct ofpbuf *buf)
1218 static const struct nl_policy ovs_vport_policy[] = {
1219 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1220 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1221 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1222 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1223 .min_len = sizeof(struct ovs_vport_stats),
1224 .max_len = sizeof(struct ovs_vport_stats),
1226 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1227 .min_len = ETH_ADDR_LEN,
1228 .max_len = ETH_ADDR_LEN,
1230 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1231 [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1234 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1235 struct ovs_header *ovs_header;
1236 struct nlmsghdr *nlmsg;
1237 struct genlmsghdr *genl;
1240 dpif_linux_vport_init(vport);
1242 ofpbuf_use_const(&b, buf->data, buf->size);
1243 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1244 genl = ofpbuf_try_pull(&b, sizeof *genl);
1245 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1246 if (!nlmsg || !genl || !ovs_header
1247 || nlmsg->nlmsg_type != ovs_vport_family
1248 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1249 ARRAY_SIZE(ovs_vport_policy))) {
1253 vport->cmd = genl->cmd;
1254 vport->dp_ifindex = ovs_header->dp_ifindex;
1255 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1256 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1257 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1258 if (a[OVS_VPORT_ATTR_STATS]) {
1259 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1261 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1262 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1264 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1265 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1266 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1268 if (a[OVS_VPORT_ATTR_IFINDEX]) {
1269 vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1274 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1275 * followed by Netlink attributes corresponding to 'vport'. */
1277 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1280 struct ovs_header *ovs_header;
1282 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1285 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1286 ovs_header->dp_ifindex = vport->dp_ifindex;
1288 if (vport->port_no != UINT32_MAX) {
1289 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1292 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1293 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1297 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1301 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1302 vport->stats, sizeof *vport->stats);
1305 if (vport->address) {
1306 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1307 vport->address, ETH_ADDR_LEN);
1310 if (vport->options) {
1311 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1312 vport->options, vport->options_len);
1315 if (vport->ifindex) {
1316 nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1320 /* Clears 'vport' to "empty" values. */
1322 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1324 memset(vport, 0, sizeof *vport);
1325 vport->port_no = UINT32_MAX;
1328 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1329 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1330 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1331 * result of the command is expected to be an ovs_vport also, which is decoded
1332 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1333 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1335 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1336 struct dpif_linux_vport *reply,
1337 struct ofpbuf **bufp)
1339 struct ofpbuf *request_buf;
1342 assert((reply != NULL) == (bufp != NULL));
1344 error = dpif_linux_init();
1348 dpif_linux_vport_init(reply);
1353 request_buf = ofpbuf_new(1024);
1354 dpif_linux_vport_to_ofpbuf(request, request_buf);
1355 error = nl_sock_transact(genl_sock, request_buf, bufp);
1356 ofpbuf_delete(request_buf);
1360 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1363 dpif_linux_vport_init(reply);
1364 ofpbuf_delete(*bufp);
1371 /* Obtains information about the kernel vport named 'name' and stores it into
1372 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1373 * longer needed ('reply' will contain pointers into '*bufp'). */
1375 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1376 struct ofpbuf **bufp)
1378 struct dpif_linux_vport request;
1380 dpif_linux_vport_init(&request);
1381 request.cmd = OVS_VPORT_CMD_GET;
1382 request.name = name;
1384 return dpif_linux_vport_transact(&request, reply, bufp);
1387 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1388 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1389 * positive errno value.
1391 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1392 * while 'dp' is still in use. */
1394 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1396 static const struct nl_policy ovs_datapath_policy[] = {
1397 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1398 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1399 .min_len = sizeof(struct ovs_dp_stats),
1400 .max_len = sizeof(struct ovs_dp_stats),
1402 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1403 [OVS_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1404 [OVS_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1407 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1408 struct ovs_header *ovs_header;
1409 struct nlmsghdr *nlmsg;
1410 struct genlmsghdr *genl;
1413 dpif_linux_dp_init(dp);
1415 ofpbuf_use_const(&b, buf->data, buf->size);
1416 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1417 genl = ofpbuf_try_pull(&b, sizeof *genl);
1418 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1419 if (!nlmsg || !genl || !ovs_header
1420 || nlmsg->nlmsg_type != ovs_datapath_family
1421 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1422 ARRAY_SIZE(ovs_datapath_policy))) {
1426 dp->cmd = genl->cmd;
1427 dp->dp_ifindex = ovs_header->dp_ifindex;
1428 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1429 if (a[OVS_DP_ATTR_STATS]) {
1430 /* Can't use structure assignment because Netlink doesn't ensure
1431 * sufficient alignment for 64-bit members. */
1432 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1435 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1436 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1438 if (a[OVS_DP_ATTR_SAMPLING]) {
1439 dp->sampling = nl_attr_get(a[OVS_DP_ATTR_SAMPLING]);
1442 if (a[OVS_DP_ATTR_MCGROUPS]) {
1443 static const struct nl_policy ovs_mcgroup_policy[] = {
1444 [OVS_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1445 [OVS_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1446 [OVS_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1449 struct nlattr *mcgroups[ARRAY_SIZE(ovs_mcgroup_policy)];
1451 if (!nl_parse_nested(a[OVS_DP_ATTR_MCGROUPS], ovs_mcgroup_policy,
1452 mcgroups, ARRAY_SIZE(ovs_mcgroup_policy))) {
1456 if (mcgroups[OVS_PACKET_CMD_MISS]) {
1457 dp->mcgroups[DPIF_UC_MISS]
1458 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_MISS]);
1460 if (mcgroups[OVS_PACKET_CMD_ACTION]) {
1461 dp->mcgroups[DPIF_UC_ACTION]
1462 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_ACTION]);
1464 if (mcgroups[OVS_PACKET_CMD_SAMPLE]) {
1465 dp->mcgroups[DPIF_UC_SAMPLE]
1466 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_SAMPLE]);
1473 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1475 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1477 struct ovs_header *ovs_header;
1479 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1480 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1482 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1483 ovs_header->dp_ifindex = dp->dp_ifindex;
1486 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1489 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1491 if (dp->ipv4_frags) {
1492 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1496 nl_msg_put_u32(buf, OVS_DP_ATTR_SAMPLING, *dp->sampling);
1500 /* Clears 'dp' to "empty" values. */
1502 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1504 memset(dp, 0, sizeof *dp);
1508 dpif_linux_dp_dump_start(struct nl_dump *dump)
1510 struct dpif_linux_dp request;
1513 dpif_linux_dp_init(&request);
1514 request.cmd = OVS_DP_CMD_GET;
1516 buf = ofpbuf_new(1024);
1517 dpif_linux_dp_to_ofpbuf(&request, buf);
1518 nl_dump_start(dump, genl_sock, buf);
1522 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1523 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1524 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1525 * result of the command is expected to be of the same form, which is decoded
1526 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1527 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1529 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1530 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1532 struct ofpbuf *request_buf;
1535 assert((reply != NULL) == (bufp != NULL));
1537 request_buf = ofpbuf_new(1024);
1538 dpif_linux_dp_to_ofpbuf(request, request_buf);
1539 error = nl_sock_transact(genl_sock, request_buf, bufp);
1540 ofpbuf_delete(request_buf);
1544 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1547 dpif_linux_dp_init(reply);
1548 ofpbuf_delete(*bufp);
1555 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1556 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1557 * will contain pointers into '*bufp'). */
1559 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1560 struct ofpbuf **bufp)
1562 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1563 struct dpif_linux_dp request;
1565 dpif_linux_dp_init(&request);
1566 request.cmd = OVS_DP_CMD_GET;
1567 request.dp_ifindex = dpif->dp_ifindex;
1569 return dpif_linux_dp_transact(&request, reply, bufp);
1572 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1573 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1574 * positive errno value.
1576 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1577 * while 'flow' is still in use. */
1579 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1580 const struct ofpbuf *buf)
1582 static const struct nl_policy ovs_flow_policy[] = {
1583 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1584 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1585 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1586 .min_len = sizeof(struct ovs_flow_stats),
1587 .max_len = sizeof(struct ovs_flow_stats),
1589 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1590 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1591 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1594 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1595 struct ovs_header *ovs_header;
1596 struct nlmsghdr *nlmsg;
1597 struct genlmsghdr *genl;
1600 dpif_linux_flow_init(flow);
1602 ofpbuf_use_const(&b, buf->data, buf->size);
1603 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1604 genl = ofpbuf_try_pull(&b, sizeof *genl);
1605 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1606 if (!nlmsg || !genl || !ovs_header
1607 || nlmsg->nlmsg_type != ovs_flow_family
1608 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1609 ARRAY_SIZE(ovs_flow_policy))) {
1613 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1614 flow->dp_ifindex = ovs_header->dp_ifindex;
1615 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1616 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1617 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1618 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1619 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1621 if (a[OVS_FLOW_ATTR_STATS]) {
1622 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1624 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1625 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1627 if (a[OVS_FLOW_ATTR_USED]) {
1628 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1633 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1634 * followed by Netlink attributes corresponding to 'flow'. */
1636 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1639 struct ovs_header *ovs_header;
1641 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1642 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1645 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1646 ovs_header->dp_ifindex = flow->dp_ifindex;
1648 if (flow->key_len) {
1649 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1652 if (flow->actions || flow->actions_len) {
1653 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1654 flow->actions, flow->actions_len);
1657 /* We never need to send these to the kernel. */
1658 assert(!flow->stats);
1659 assert(!flow->tcp_flags);
1660 assert(!flow->used);
1663 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1667 /* Clears 'flow' to "empty" values. */
1669 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1671 memset(flow, 0, sizeof *flow);
1674 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1675 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1676 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1677 * result of the command is expected to be a flow also, which is decoded and
1678 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1679 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1681 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1682 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1684 struct ofpbuf *request_buf;
1687 assert((reply != NULL) == (bufp != NULL));
1689 request_buf = ofpbuf_new(1024);
1690 dpif_linux_flow_to_ofpbuf(request, request_buf);
1691 error = nl_sock_transact(genl_sock, request_buf, bufp);
1692 ofpbuf_delete(request_buf);
1696 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1699 dpif_linux_flow_init(reply);
1700 ofpbuf_delete(*bufp);
1708 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1709 struct dpif_flow_stats *stats)
1712 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1713 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1715 stats->n_packets = 0;
1718 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1719 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;