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;
258 dpif = xmalloc(sizeof *dpif);
259 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
261 if (!dpif->port_notifier) {
265 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
266 dp->dp_ifindex, dp->dp_ifindex);
268 dpif->mc_sock = NULL;
269 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
270 dpif->mcgroups[i] = dp->mcgroups[i];
272 dpif->listen_mask = 0;
273 dpif->dp_ifindex = dp->dp_ifindex;
274 sset_init(&dpif->changed_ports);
275 dpif->change_error = false;
276 *dpifp = &dpif->dpif;
278 dpif->lru_head = dpif->lru_tail = 0;
279 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
280 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
281 for (i = 1; i < LRU_MAX_PORTS; i++) {
282 dpif_linux_push_port(dpif, i);
292 dpif_linux_close(struct dpif *dpif_)
294 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
296 nln_notifier_destroy(dpif->port_notifier);
297 nl_sock_destroy(dpif->mc_sock);
298 sset_destroy(&dpif->changed_ports);
299 free(dpif->lru_bitmap);
304 dpif_linux_destroy(struct dpif *dpif_)
306 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
307 struct dpif_linux_dp dp;
309 dpif_linux_dp_init(&dp);
310 dp.cmd = OVS_DP_CMD_DEL;
311 dp.dp_ifindex = dpif->dp_ifindex;
312 return dpif_linux_dp_transact(&dp, NULL, NULL);
316 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
324 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
332 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
334 struct dpif_linux_dp dp;
338 error = dpif_linux_dp_get(dpif_, &dp, &buf);
347 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
349 struct dpif_linux_dp dp;
353 error = dpif_linux_dp_get(dpif_, &dp, &buf);
355 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
362 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
364 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
365 struct dpif_linux_dp dp;
367 dpif_linux_dp_init(&dp);
368 dp.cmd = OVS_DP_CMD_SET;
369 dp.dp_ifindex = dpif->dp_ifindex;
370 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
371 return dpif_linux_dp_transact(&dp, NULL, NULL);
375 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
378 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
379 const char *name = netdev_get_name(netdev);
380 const char *type = netdev_get_type(netdev);
381 struct dpif_linux_vport request, reply;
382 const struct ofpbuf *options;
386 dpif_linux_vport_init(&request);
387 request.cmd = OVS_VPORT_CMD_NEW;
388 request.dp_ifindex = dpif->dp_ifindex;
389 request.type = netdev_vport_get_vport_type(netdev);
390 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
391 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
392 "unsupported type `%s'",
393 dpif_name(dpif_), name, type);
398 options = netdev_vport_get_options(netdev);
399 if (options && options->size) {
400 request.options = options->data;
401 request.options_len = options->size;
404 if (request.type == OVS_VPORT_TYPE_NETDEV) {
405 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
408 /* Loop until we find a port that isn't used. */
410 request.port_no = dpif_linux_pop_port(dpif);
411 error = dpif_linux_vport_transact(&request, &reply, &buf);
414 *port_nop = reply.port_no;
417 } while (request.port_no != UINT32_MAX
418 && (error == EBUSY || error == EFBIG));
424 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
426 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
427 struct dpif_linux_vport vport;
430 dpif_linux_vport_init(&vport);
431 vport.cmd = OVS_VPORT_CMD_DEL;
432 vport.dp_ifindex = dpif->dp_ifindex;
433 vport.port_no = port_no;
434 error = dpif_linux_vport_transact(&vport, NULL, NULL);
437 dpif_linux_push_port(dpif, port_no);
443 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
444 const char *port_name, struct dpif_port *dpif_port)
446 struct dpif_linux_vport request;
447 struct dpif_linux_vport reply;
451 dpif_linux_vport_init(&request);
452 request.cmd = OVS_VPORT_CMD_GET;
453 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
454 request.port_no = port_no;
455 request.name = port_name;
457 error = dpif_linux_vport_transact(&request, &reply, &buf);
459 dpif_port->name = xstrdup(reply.name);
460 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
461 dpif_port->port_no = reply.port_no;
468 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
469 struct dpif_port *dpif_port)
471 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
475 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
476 struct dpif_port *dpif_port)
478 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
482 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
484 /* If the datapath increases its range of supported ports, then it should
485 * start reporting that. */
490 dpif_linux_flow_flush(struct dpif *dpif_)
492 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
493 struct dpif_linux_flow flow;
495 dpif_linux_flow_init(&flow);
496 flow.cmd = OVS_FLOW_CMD_DEL;
497 flow.dp_ifindex = dpif->dp_ifindex;
498 return dpif_linux_flow_transact(&flow, NULL, NULL);
501 struct dpif_linux_port_state {
503 unsigned long *port_bitmap; /* Ports in the datapath. */
504 bool complete; /* Dump completed without error. */
508 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
510 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
511 struct dpif_linux_port_state *state;
512 struct dpif_linux_vport request;
515 *statep = state = xmalloc(sizeof *state);
516 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
517 state->complete = false;
519 dpif_linux_vport_init(&request);
520 request.cmd = OVS_DP_CMD_GET;
521 request.dp_ifindex = dpif->dp_ifindex;
523 buf = ofpbuf_new(1024);
524 dpif_linux_vport_to_ofpbuf(&request, buf);
525 nl_dump_start(&state->dump, genl_sock, buf);
532 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
533 struct dpif_port *dpif_port)
535 struct dpif_linux_port_state *state = state_;
536 struct dpif_linux_vport vport;
540 if (!nl_dump_next(&state->dump, &buf)) {
541 state->complete = true;
545 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
550 if (vport.port_no < LRU_MAX_PORTS) {
551 bitmap_set1(state->port_bitmap, vport.port_no);
554 dpif_port->name = (char *) vport.name;
555 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
556 dpif_port->port_no = vport.port_no;
561 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
563 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
564 struct dpif_linux_port_state *state = state_;
565 int error = nl_dump_done(&state->dump);
567 if (state->complete) {
570 for (i = 0; i < LRU_MAX_PORTS; i++) {
571 if (!bitmap_is_set(state->port_bitmap, i)) {
572 dpif_linux_push_port(dpif, i);
577 free(state->port_bitmap);
583 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
585 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
587 if (dpif->change_error) {
588 dpif->change_error = false;
589 sset_clear(&dpif->changed_ports);
591 } else if (!sset_is_empty(&dpif->changed_ports)) {
592 *devnamep = sset_pop(&dpif->changed_ports);
600 dpif_linux_port_poll_wait(const struct dpif *dpif_)
602 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
603 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
604 poll_immediate_wake();
609 dpif_linux_flow_get__(const struct dpif *dpif_,
610 const struct nlattr *key, size_t key_len,
611 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
613 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
614 struct dpif_linux_flow request;
616 dpif_linux_flow_init(&request);
617 request.cmd = OVS_FLOW_CMD_GET;
618 request.dp_ifindex = dpif->dp_ifindex;
620 request.key_len = key_len;
621 return dpif_linux_flow_transact(&request, reply, bufp);
625 dpif_linux_flow_get(const struct dpif *dpif_,
626 const struct nlattr *key, size_t key_len,
627 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
629 struct dpif_linux_flow reply;
633 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
636 dpif_linux_flow_get_stats(&reply, stats);
639 buf->data = (void *) reply.actions;
640 buf->size = reply.actions_len;
650 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
651 const struct nlattr *key, size_t key_len,
652 const struct nlattr *actions, size_t actions_len,
653 struct dpif_flow_stats *stats)
655 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
656 struct dpif_linux_flow request, reply;
657 struct nlattr dummy_action;
661 dpif_linux_flow_init(&request);
662 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
663 request.dp_ifindex = dpif->dp_ifindex;
665 request.key_len = key_len;
666 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
667 request.actions = actions ? actions : &dummy_action;
668 request.actions_len = actions_len;
669 if (flags & DPIF_FP_ZERO_STATS) {
670 request.clear = true;
672 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
673 error = dpif_linux_flow_transact(&request,
674 stats ? &reply : NULL,
675 stats ? &buf : NULL);
676 if (!error && stats) {
677 dpif_linux_flow_get_stats(&reply, stats);
684 dpif_linux_flow_del(struct dpif *dpif_,
685 const struct nlattr *key, size_t key_len,
686 struct dpif_flow_stats *stats)
688 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
689 struct dpif_linux_flow request, reply;
693 dpif_linux_flow_init(&request);
694 request.cmd = OVS_FLOW_CMD_DEL;
695 request.dp_ifindex = dpif->dp_ifindex;
697 request.key_len = key_len;
698 error = dpif_linux_flow_transact(&request,
699 stats ? &reply : NULL,
700 stats ? &buf : NULL);
701 if (!error && stats) {
702 dpif_linux_flow_get_stats(&reply, stats);
708 struct dpif_linux_flow_state {
710 struct dpif_linux_flow flow;
711 struct dpif_flow_stats stats;
716 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
718 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
719 struct dpif_linux_flow_state *state;
720 struct dpif_linux_flow request;
723 *statep = state = xmalloc(sizeof *state);
725 dpif_linux_flow_init(&request);
726 request.cmd = OVS_DP_CMD_GET;
727 request.dp_ifindex = dpif->dp_ifindex;
729 buf = ofpbuf_new(1024);
730 dpif_linux_flow_to_ofpbuf(&request, buf);
731 nl_dump_start(&state->dump, genl_sock, buf);
740 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
741 const struct nlattr **key, size_t *key_len,
742 const struct nlattr **actions, size_t *actions_len,
743 const struct dpif_flow_stats **stats)
745 struct dpif_linux_flow_state *state = state_;
750 ofpbuf_delete(state->buf);
753 if (!nl_dump_next(&state->dump, &buf)) {
757 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
762 if (actions && !state->flow.actions) {
763 error = dpif_linux_flow_get__(dpif_, state->flow.key,
765 &state->flow, &state->buf);
766 if (error == ENOENT) {
767 VLOG_DBG("dumped flow disappeared on get");
769 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
775 *actions = state->flow.actions;
776 *actions_len = state->flow.actions_len;
779 *key = state->flow.key;
780 *key_len = state->flow.key_len;
783 dpif_linux_flow_get_stats(&state->flow, &state->stats);
784 *stats = &state->stats;
790 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
792 struct dpif_linux_flow_state *state = state_;
793 int error = nl_dump_done(&state->dump);
794 ofpbuf_delete(state->buf);
800 dpif_linux_execute__(int dp_ifindex,
801 const struct nlattr *key, size_t key_len,
802 const struct nlattr *actions, size_t actions_len,
803 const struct ofpbuf *packet)
805 struct ovs_header *execute;
809 buf = ofpbuf_new(128 + actions_len + packet->size);
811 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
812 OVS_PACKET_CMD_EXECUTE, 1);
814 execute = ofpbuf_put_uninit(buf, sizeof *execute);
815 execute->dp_ifindex = dp_ifindex;
817 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
818 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
819 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
821 error = nl_sock_transact(genl_sock, buf, NULL);
827 dpif_linux_execute(struct dpif *dpif_,
828 const struct nlattr *key, size_t key_len,
829 const struct nlattr *actions, size_t actions_len,
830 const struct ofpbuf *packet)
832 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
834 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
835 actions, actions_len, packet);
839 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
841 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
842 *listen_mask = dpif->listen_mask;
847 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
849 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
853 if (listen_mask == dpif->listen_mask) {
855 } else if (!listen_mask) {
856 nl_sock_destroy(dpif->mc_sock);
857 dpif->mc_sock = NULL;
858 dpif->listen_mask = 0;
860 } else if (!dpif->mc_sock) {
861 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
867 /* Unsubscribe from old groups. */
868 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
869 if (dpif->listen_mask & (1u << i)) {
870 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
874 /* Update listen_mask. */
875 dpif->listen_mask = listen_mask;
877 /* Subscribe to new groups. */
879 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
880 if (dpif->listen_mask & (1u << i)) {
883 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
893 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
894 uint32_t *probability)
896 struct dpif_linux_dp dp;
900 error = dpif_linux_dp_get(dpif_, &dp, &buf);
902 *probability = dp.sampling ? *dp.sampling : 0;
909 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
911 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
912 struct dpif_linux_dp dp;
914 dpif_linux_dp_init(&dp);
915 dp.cmd = OVS_DP_CMD_SET;
916 dp.dp_ifindex = dpif->dp_ifindex;
917 dp.sampling = &probability;
918 return dpif_linux_dp_transact(&dp, NULL, NULL);
922 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
923 uint32_t queue_id, uint32_t *priority)
925 if (queue_id < 0xf000) {
926 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
934 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
937 static const struct nl_policy ovs_packet_policy[] = {
938 /* Always present. */
939 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
940 .min_len = ETH_HEADER_LEN },
941 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
943 /* OVS_PACKET_CMD_ACTION only. */
944 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
946 /* OVS_PACKET_CMD_SAMPLE only. */
947 [OVS_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
948 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
951 struct ovs_header *ovs_header;
952 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
953 struct nlmsghdr *nlmsg;
954 struct genlmsghdr *genl;
958 ofpbuf_use_const(&b, buf->data, buf->size);
960 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
961 genl = ofpbuf_try_pull(&b, sizeof *genl);
962 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
963 if (!nlmsg || !genl || !ovs_header
964 || nlmsg->nlmsg_type != ovs_packet_family
965 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
966 ARRAY_SIZE(ovs_packet_policy))) {
970 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
971 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
972 : genl->cmd == OVS_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
978 memset(upcall, 0, sizeof *upcall);
980 upcall->packet = buf;
981 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
982 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
983 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
984 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
985 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
986 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
988 upcall->sample_pool = (a[OVS_PACKET_ATTR_SAMPLE_POOL]
989 ? nl_attr_get_u32(a[OVS_PACKET_ATTR_SAMPLE_POOL])
991 if (a[OVS_PACKET_ATTR_ACTIONS]) {
992 upcall->actions = (void *) nl_attr_get(a[OVS_PACKET_ATTR_ACTIONS]);
993 upcall->actions_len = nl_attr_get_size(a[OVS_PACKET_ATTR_ACTIONS]);
996 *dp_ifindex = ovs_header->dp_ifindex;
1002 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1004 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1009 if (!dpif->mc_sock) {
1013 for (i = 0; i < 50; i++) {
1016 error = nl_sock_recv(dpif->mc_sock, &buf, false);
1021 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1023 && dp_ifindex == dpif->dp_ifindex
1024 && dpif->listen_mask & (1u << upcall->type)) {
1038 dpif_linux_recv_wait(struct dpif *dpif_)
1040 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1041 if (dpif->mc_sock) {
1042 nl_sock_wait(dpif->mc_sock, POLLIN);
1047 dpif_linux_recv_purge(struct dpif *dpif_)
1049 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1051 if (dpif->mc_sock) {
1052 nl_sock_drain(dpif->mc_sock);
1056 const struct dpif_class dpif_linux_class = {
1058 dpif_linux_enumerate,
1064 dpif_linux_get_stats,
1065 dpif_linux_get_drop_frags,
1066 dpif_linux_set_drop_frags,
1067 dpif_linux_port_add,
1068 dpif_linux_port_del,
1069 dpif_linux_port_query_by_number,
1070 dpif_linux_port_query_by_name,
1071 dpif_linux_get_max_ports,
1072 dpif_linux_port_dump_start,
1073 dpif_linux_port_dump_next,
1074 dpif_linux_port_dump_done,
1075 dpif_linux_port_poll,
1076 dpif_linux_port_poll_wait,
1077 dpif_linux_flow_get,
1078 dpif_linux_flow_put,
1079 dpif_linux_flow_del,
1080 dpif_linux_flow_flush,
1081 dpif_linux_flow_dump_start,
1082 dpif_linux_flow_dump_next,
1083 dpif_linux_flow_dump_done,
1085 dpif_linux_recv_get_mask,
1086 dpif_linux_recv_set_mask,
1087 dpif_linux_get_sflow_probability,
1088 dpif_linux_set_sflow_probability,
1089 dpif_linux_queue_to_priority,
1091 dpif_linux_recv_wait,
1092 dpif_linux_recv_purge,
1096 dpif_linux_init(void)
1098 static int error = -1;
1101 unsigned int ovs_vport_mcgroup;
1103 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1104 &ovs_datapath_family);
1106 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1107 "The Open vSwitch kernel module is probably not loaded.",
1108 OVS_DATAPATH_FAMILY);
1111 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1114 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1117 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1118 &ovs_packet_family);
1121 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1124 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1125 &ovs_vport_mcgroup);
1128 static struct dpif_linux_vport vport;
1129 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1130 dpif_linux_nln_parse, &vport);
1138 dpif_linux_is_internal_device(const char *name)
1140 struct dpif_linux_vport reply;
1144 error = dpif_linux_vport_get(name, &reply, &buf);
1147 } else if (error != ENODEV && error != ENOENT) {
1148 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1149 name, strerror(error));
1152 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1156 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1157 const void *data, size_t size)
1159 struct ofpbuf actions, key, packet;
1160 struct odputil_keybuf keybuf;
1164 ofpbuf_use_const(&packet, data, size);
1165 flow_extract(&packet, htonll(0), 0, &flow);
1167 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1168 odp_flow_key_from_flow(&key, &flow);
1170 ofpbuf_use_stack(&actions, &action, sizeof action);
1171 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1173 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1174 actions.data, actions.size, &packet);
1178 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1180 struct dpif_linux_vport *vport = vport_;
1181 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1185 dpif_linux_port_changed(const void *vport_, void *dpif_)
1187 const struct dpif_linux_vport *vport = vport_;
1188 struct dpif_linux *dpif = dpif_;
1191 if (vport->dp_ifindex == dpif->dp_ifindex
1192 && (vport->cmd == OVS_VPORT_CMD_NEW
1193 || vport->cmd == OVS_VPORT_CMD_DEL
1194 || vport->cmd == OVS_VPORT_CMD_SET)) {
1195 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1196 dpif->dpif.full_name, vport->name, vport->cmd);
1197 sset_add(&dpif->changed_ports, vport->name);
1200 dpif->change_error = true;
1204 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1205 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1206 * positive errno value.
1208 * 'vport' will contain pointers into 'buf', so the caller should not free
1209 * 'buf' while 'vport' is still in use. */
1211 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1212 const struct ofpbuf *buf)
1214 static const struct nl_policy ovs_vport_policy[] = {
1215 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1216 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1217 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1218 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1219 .min_len = sizeof(struct ovs_vport_stats),
1220 .max_len = sizeof(struct ovs_vport_stats),
1222 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1223 .min_len = ETH_ADDR_LEN,
1224 .max_len = ETH_ADDR_LEN,
1226 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1227 [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1230 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1231 struct ovs_header *ovs_header;
1232 struct nlmsghdr *nlmsg;
1233 struct genlmsghdr *genl;
1236 dpif_linux_vport_init(vport);
1238 ofpbuf_use_const(&b, buf->data, buf->size);
1239 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1240 genl = ofpbuf_try_pull(&b, sizeof *genl);
1241 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1242 if (!nlmsg || !genl || !ovs_header
1243 || nlmsg->nlmsg_type != ovs_vport_family
1244 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1245 ARRAY_SIZE(ovs_vport_policy))) {
1249 vport->cmd = genl->cmd;
1250 vport->dp_ifindex = ovs_header->dp_ifindex;
1251 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1252 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1253 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1254 if (a[OVS_VPORT_ATTR_STATS]) {
1255 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1257 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1258 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1260 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1261 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1262 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1264 if (a[OVS_VPORT_ATTR_IFINDEX]) {
1265 vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1270 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1271 * followed by Netlink attributes corresponding to 'vport'. */
1273 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1276 struct ovs_header *ovs_header;
1278 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1281 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1282 ovs_header->dp_ifindex = vport->dp_ifindex;
1284 if (vport->port_no != UINT32_MAX) {
1285 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1288 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1289 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1293 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1297 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1298 vport->stats, sizeof *vport->stats);
1301 if (vport->address) {
1302 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1303 vport->address, ETH_ADDR_LEN);
1306 if (vport->options) {
1307 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1308 vport->options, vport->options_len);
1311 if (vport->ifindex) {
1312 nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1316 /* Clears 'vport' to "empty" values. */
1318 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1320 memset(vport, 0, sizeof *vport);
1321 vport->port_no = UINT32_MAX;
1324 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1325 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1326 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1327 * result of the command is expected to be an ovs_vport also, which is decoded
1328 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1329 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1331 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1332 struct dpif_linux_vport *reply,
1333 struct ofpbuf **bufp)
1335 struct ofpbuf *request_buf;
1338 assert((reply != NULL) == (bufp != NULL));
1340 error = dpif_linux_init();
1344 dpif_linux_vport_init(reply);
1349 request_buf = ofpbuf_new(1024);
1350 dpif_linux_vport_to_ofpbuf(request, request_buf);
1351 error = nl_sock_transact(genl_sock, request_buf, bufp);
1352 ofpbuf_delete(request_buf);
1356 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1359 dpif_linux_vport_init(reply);
1360 ofpbuf_delete(*bufp);
1367 /* Obtains information about the kernel vport named 'name' and stores it into
1368 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1369 * longer needed ('reply' will contain pointers into '*bufp'). */
1371 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1372 struct ofpbuf **bufp)
1374 struct dpif_linux_vport request;
1376 dpif_linux_vport_init(&request);
1377 request.cmd = OVS_VPORT_CMD_GET;
1378 request.name = name;
1380 return dpif_linux_vport_transact(&request, reply, bufp);
1383 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1384 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1385 * positive errno value.
1387 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1388 * while 'dp' is still in use. */
1390 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1392 static const struct nl_policy ovs_datapath_policy[] = {
1393 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1394 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1395 .min_len = sizeof(struct ovs_dp_stats),
1396 .max_len = sizeof(struct ovs_dp_stats),
1398 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1399 [OVS_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1400 [OVS_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1403 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1404 struct ovs_header *ovs_header;
1405 struct nlmsghdr *nlmsg;
1406 struct genlmsghdr *genl;
1409 dpif_linux_dp_init(dp);
1411 ofpbuf_use_const(&b, buf->data, buf->size);
1412 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1413 genl = ofpbuf_try_pull(&b, sizeof *genl);
1414 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1415 if (!nlmsg || !genl || !ovs_header
1416 || nlmsg->nlmsg_type != ovs_datapath_family
1417 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1418 ARRAY_SIZE(ovs_datapath_policy))) {
1422 dp->cmd = genl->cmd;
1423 dp->dp_ifindex = ovs_header->dp_ifindex;
1424 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1425 if (a[OVS_DP_ATTR_STATS]) {
1426 /* Can't use structure assignment because Netlink doesn't ensure
1427 * sufficient alignment for 64-bit members. */
1428 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1431 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1432 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1434 if (a[OVS_DP_ATTR_SAMPLING]) {
1435 dp->sampling = nl_attr_get(a[OVS_DP_ATTR_SAMPLING]);
1438 if (a[OVS_DP_ATTR_MCGROUPS]) {
1439 static const struct nl_policy ovs_mcgroup_policy[] = {
1440 [OVS_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1441 [OVS_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1442 [OVS_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1445 struct nlattr *mcgroups[ARRAY_SIZE(ovs_mcgroup_policy)];
1447 if (!nl_parse_nested(a[OVS_DP_ATTR_MCGROUPS], ovs_mcgroup_policy,
1448 mcgroups, ARRAY_SIZE(ovs_mcgroup_policy))) {
1452 if (mcgroups[OVS_PACKET_CMD_MISS]) {
1453 dp->mcgroups[DPIF_UC_MISS]
1454 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_MISS]);
1456 if (mcgroups[OVS_PACKET_CMD_ACTION]) {
1457 dp->mcgroups[DPIF_UC_ACTION]
1458 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_ACTION]);
1460 if (mcgroups[OVS_PACKET_CMD_SAMPLE]) {
1461 dp->mcgroups[DPIF_UC_SAMPLE]
1462 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_SAMPLE]);
1469 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1471 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1473 struct ovs_header *ovs_header;
1475 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1476 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1478 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1479 ovs_header->dp_ifindex = dp->dp_ifindex;
1482 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1485 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1487 if (dp->ipv4_frags) {
1488 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1492 nl_msg_put_u32(buf, OVS_DP_ATTR_SAMPLING, *dp->sampling);
1496 /* Clears 'dp' to "empty" values. */
1498 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1500 memset(dp, 0, sizeof *dp);
1504 dpif_linux_dp_dump_start(struct nl_dump *dump)
1506 struct dpif_linux_dp request;
1509 dpif_linux_dp_init(&request);
1510 request.cmd = OVS_DP_CMD_GET;
1512 buf = ofpbuf_new(1024);
1513 dpif_linux_dp_to_ofpbuf(&request, buf);
1514 nl_dump_start(dump, genl_sock, buf);
1518 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1519 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1520 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1521 * result of the command is expected to be of the same form, which is decoded
1522 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1523 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1525 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1526 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1528 struct ofpbuf *request_buf;
1531 assert((reply != NULL) == (bufp != NULL));
1533 request_buf = ofpbuf_new(1024);
1534 dpif_linux_dp_to_ofpbuf(request, request_buf);
1535 error = nl_sock_transact(genl_sock, request_buf, bufp);
1536 ofpbuf_delete(request_buf);
1540 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1543 dpif_linux_dp_init(reply);
1544 ofpbuf_delete(*bufp);
1551 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1552 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1553 * will contain pointers into '*bufp'). */
1555 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1556 struct ofpbuf **bufp)
1558 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1559 struct dpif_linux_dp request;
1561 dpif_linux_dp_init(&request);
1562 request.cmd = OVS_DP_CMD_GET;
1563 request.dp_ifindex = dpif->dp_ifindex;
1565 return dpif_linux_dp_transact(&request, reply, bufp);
1568 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1569 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1570 * positive errno value.
1572 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1573 * while 'flow' is still in use. */
1575 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1576 const struct ofpbuf *buf)
1578 static const struct nl_policy ovs_flow_policy[] = {
1579 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1580 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1581 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1582 .min_len = sizeof(struct ovs_flow_stats),
1583 .max_len = sizeof(struct ovs_flow_stats),
1585 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1586 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1587 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1590 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1591 struct ovs_header *ovs_header;
1592 struct nlmsghdr *nlmsg;
1593 struct genlmsghdr *genl;
1596 dpif_linux_flow_init(flow);
1598 ofpbuf_use_const(&b, buf->data, buf->size);
1599 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1600 genl = ofpbuf_try_pull(&b, sizeof *genl);
1601 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1602 if (!nlmsg || !genl || !ovs_header
1603 || nlmsg->nlmsg_type != ovs_flow_family
1604 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1605 ARRAY_SIZE(ovs_flow_policy))) {
1609 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1610 flow->dp_ifindex = ovs_header->dp_ifindex;
1611 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1612 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1613 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1614 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1615 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1617 if (a[OVS_FLOW_ATTR_STATS]) {
1618 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1620 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1621 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1623 if (a[OVS_FLOW_ATTR_USED]) {
1624 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1629 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1630 * followed by Netlink attributes corresponding to 'flow'. */
1632 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1635 struct ovs_header *ovs_header;
1637 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1638 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1641 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1642 ovs_header->dp_ifindex = flow->dp_ifindex;
1644 if (flow->key_len) {
1645 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1648 if (flow->actions || flow->actions_len) {
1649 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1650 flow->actions, flow->actions_len);
1653 /* We never need to send these to the kernel. */
1654 assert(!flow->stats);
1655 assert(!flow->tcp_flags);
1656 assert(!flow->used);
1659 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1663 /* Clears 'flow' to "empty" values. */
1665 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1667 memset(flow, 0, sizeof *flow);
1670 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1671 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1672 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1673 * result of the command is expected to be a flow also, which is decoded and
1674 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1675 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1677 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1678 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1680 struct ofpbuf *request_buf;
1683 assert((reply != NULL) == (bufp != NULL));
1685 request_buf = ofpbuf_new(1024);
1686 dpif_linux_flow_to_ofpbuf(request, request_buf);
1687 error = nl_sock_transact(genl_sock, request_buf, bufp);
1688 ofpbuf_delete(request_buf);
1692 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1695 dpif_linux_flow_init(reply);
1696 ofpbuf_delete(*bufp);
1704 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1705 struct dpif_flow_stats *stats)
1708 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1709 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1711 stats->n_packets = 0;
1714 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1715 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;