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 void 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);
247 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,
262 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
263 dp->dp_ifindex, dp->dp_ifindex);
265 dpif->mc_sock = NULL;
266 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
267 dpif->mcgroups[i] = dp->mcgroups[i];
269 dpif->listen_mask = 0;
270 dpif->dp_ifindex = dp->dp_ifindex;
271 sset_init(&dpif->changed_ports);
272 dpif->change_error = false;
273 *dpifp = &dpif->dpif;
275 dpif->lru_head = dpif->lru_tail = 0;
276 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
277 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
278 for (i = 1; i < LRU_MAX_PORTS; i++) {
279 dpif_linux_push_port(dpif, i);
284 dpif_linux_close(struct dpif *dpif_)
286 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
288 nln_notifier_destroy(dpif->port_notifier);
289 nl_sock_destroy(dpif->mc_sock);
290 sset_destroy(&dpif->changed_ports);
291 free(dpif->lru_bitmap);
296 dpif_linux_destroy(struct dpif *dpif_)
298 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
299 struct dpif_linux_dp dp;
301 dpif_linux_dp_init(&dp);
302 dp.cmd = OVS_DP_CMD_DEL;
303 dp.dp_ifindex = dpif->dp_ifindex;
304 return dpif_linux_dp_transact(&dp, NULL, NULL);
308 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
316 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
324 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
326 struct dpif_linux_dp dp;
330 error = dpif_linux_dp_get(dpif_, &dp, &buf);
339 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
341 struct dpif_linux_dp dp;
345 error = dpif_linux_dp_get(dpif_, &dp, &buf);
347 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
354 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
356 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
357 struct dpif_linux_dp dp;
359 dpif_linux_dp_init(&dp);
360 dp.cmd = OVS_DP_CMD_SET;
361 dp.dp_ifindex = dpif->dp_ifindex;
362 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
363 return dpif_linux_dp_transact(&dp, NULL, NULL);
367 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
370 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
371 const char *name = netdev_get_name(netdev);
372 const char *type = netdev_get_type(netdev);
373 struct dpif_linux_vport request, reply;
374 const struct ofpbuf *options;
378 dpif_linux_vport_init(&request);
379 request.cmd = OVS_VPORT_CMD_NEW;
380 request.dp_ifindex = dpif->dp_ifindex;
381 request.type = netdev_vport_get_vport_type(netdev);
382 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
383 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
384 "unsupported type `%s'",
385 dpif_name(dpif_), name, type);
390 options = netdev_vport_get_options(netdev);
391 if (options && options->size) {
392 request.options = options->data;
393 request.options_len = options->size;
396 if (request.type == OVS_VPORT_TYPE_NETDEV) {
397 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
400 /* Loop until we find a port that isn't used. */
402 request.port_no = dpif_linux_pop_port(dpif);
403 error = dpif_linux_vport_transact(&request, &reply, &buf);
406 *port_nop = reply.port_no;
409 } while (request.port_no != UINT32_MAX
410 && (error == EBUSY || error == EFBIG));
416 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
418 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
419 struct dpif_linux_vport vport;
422 dpif_linux_vport_init(&vport);
423 vport.cmd = OVS_VPORT_CMD_DEL;
424 vport.dp_ifindex = dpif->dp_ifindex;
425 vport.port_no = port_no;
426 error = dpif_linux_vport_transact(&vport, NULL, NULL);
429 dpif_linux_push_port(dpif, port_no);
435 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
436 const char *port_name, struct dpif_port *dpif_port)
438 struct dpif_linux_vport request;
439 struct dpif_linux_vport reply;
443 dpif_linux_vport_init(&request);
444 request.cmd = OVS_VPORT_CMD_GET;
445 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
446 request.port_no = port_no;
447 request.name = port_name;
449 error = dpif_linux_vport_transact(&request, &reply, &buf);
451 dpif_port->name = xstrdup(reply.name);
452 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
453 dpif_port->port_no = reply.port_no;
460 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
461 struct dpif_port *dpif_port)
463 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
467 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
468 struct dpif_port *dpif_port)
470 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
474 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
476 /* If the datapath increases its range of supported ports, then it should
477 * start reporting that. */
482 dpif_linux_flow_flush(struct dpif *dpif_)
484 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
485 struct dpif_linux_flow flow;
487 dpif_linux_flow_init(&flow);
488 flow.cmd = OVS_FLOW_CMD_DEL;
489 flow.dp_ifindex = dpif->dp_ifindex;
490 return dpif_linux_flow_transact(&flow, NULL, NULL);
493 struct dpif_linux_port_state {
495 unsigned long *port_bitmap; /* Ports in the datapath. */
496 bool complete; /* Dump completed without error. */
500 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
502 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
503 struct dpif_linux_port_state *state;
504 struct dpif_linux_vport request;
507 *statep = state = xmalloc(sizeof *state);
508 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
509 state->complete = false;
511 dpif_linux_vport_init(&request);
512 request.cmd = OVS_DP_CMD_GET;
513 request.dp_ifindex = dpif->dp_ifindex;
515 buf = ofpbuf_new(1024);
516 dpif_linux_vport_to_ofpbuf(&request, buf);
517 nl_dump_start(&state->dump, genl_sock, buf);
524 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
525 struct dpif_port *dpif_port)
527 struct dpif_linux_port_state *state = state_;
528 struct dpif_linux_vport vport;
532 if (!nl_dump_next(&state->dump, &buf)) {
533 state->complete = true;
537 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
542 if (vport.port_no < LRU_MAX_PORTS) {
543 bitmap_set1(state->port_bitmap, vport.port_no);
546 dpif_port->name = (char *) vport.name;
547 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
548 dpif_port->port_no = vport.port_no;
553 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
555 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
556 struct dpif_linux_port_state *state = state_;
557 int error = nl_dump_done(&state->dump);
559 if (state->complete) {
562 for (i = 0; i < LRU_MAX_PORTS; i++) {
563 if (!bitmap_is_set(state->port_bitmap, i)) {
564 dpif_linux_push_port(dpif, i);
569 free(state->port_bitmap);
575 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
577 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
579 if (dpif->change_error) {
580 dpif->change_error = false;
581 sset_clear(&dpif->changed_ports);
583 } else if (!sset_is_empty(&dpif->changed_ports)) {
584 *devnamep = sset_pop(&dpif->changed_ports);
592 dpif_linux_port_poll_wait(const struct dpif *dpif_)
594 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
595 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
596 poll_immediate_wake();
601 dpif_linux_flow_get__(const struct dpif *dpif_,
602 const struct nlattr *key, size_t key_len,
603 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
605 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
606 struct dpif_linux_flow request;
608 dpif_linux_flow_init(&request);
609 request.cmd = OVS_FLOW_CMD_GET;
610 request.dp_ifindex = dpif->dp_ifindex;
612 request.key_len = key_len;
613 return dpif_linux_flow_transact(&request, reply, bufp);
617 dpif_linux_flow_get(const struct dpif *dpif_,
618 const struct nlattr *key, size_t key_len,
619 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
621 struct dpif_linux_flow reply;
625 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
628 dpif_linux_flow_get_stats(&reply, stats);
631 buf->data = (void *) reply.actions;
632 buf->size = reply.actions_len;
642 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
643 const struct nlattr *key, size_t key_len,
644 const struct nlattr *actions, size_t actions_len,
645 struct dpif_flow_stats *stats)
647 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
648 struct dpif_linux_flow request, reply;
649 struct nlattr dummy_action;
653 dpif_linux_flow_init(&request);
654 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
655 request.dp_ifindex = dpif->dp_ifindex;
657 request.key_len = key_len;
658 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
659 request.actions = actions ? actions : &dummy_action;
660 request.actions_len = actions_len;
661 if (flags & DPIF_FP_ZERO_STATS) {
662 request.clear = true;
664 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
665 error = dpif_linux_flow_transact(&request,
666 stats ? &reply : NULL,
667 stats ? &buf : NULL);
668 if (!error && stats) {
669 dpif_linux_flow_get_stats(&reply, stats);
676 dpif_linux_flow_del(struct dpif *dpif_,
677 const struct nlattr *key, size_t key_len,
678 struct dpif_flow_stats *stats)
680 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
681 struct dpif_linux_flow request, reply;
685 dpif_linux_flow_init(&request);
686 request.cmd = OVS_FLOW_CMD_DEL;
687 request.dp_ifindex = dpif->dp_ifindex;
689 request.key_len = key_len;
690 error = dpif_linux_flow_transact(&request,
691 stats ? &reply : NULL,
692 stats ? &buf : NULL);
693 if (!error && stats) {
694 dpif_linux_flow_get_stats(&reply, stats);
700 struct dpif_linux_flow_state {
702 struct dpif_linux_flow flow;
703 struct dpif_flow_stats stats;
708 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
710 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
711 struct dpif_linux_flow_state *state;
712 struct dpif_linux_flow request;
715 *statep = state = xmalloc(sizeof *state);
717 dpif_linux_flow_init(&request);
718 request.cmd = OVS_DP_CMD_GET;
719 request.dp_ifindex = dpif->dp_ifindex;
721 buf = ofpbuf_new(1024);
722 dpif_linux_flow_to_ofpbuf(&request, buf);
723 nl_dump_start(&state->dump, genl_sock, buf);
732 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
733 const struct nlattr **key, size_t *key_len,
734 const struct nlattr **actions, size_t *actions_len,
735 const struct dpif_flow_stats **stats)
737 struct dpif_linux_flow_state *state = state_;
742 ofpbuf_delete(state->buf);
745 if (!nl_dump_next(&state->dump, &buf)) {
749 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
754 if (actions && !state->flow.actions) {
755 error = dpif_linux_flow_get__(dpif_, state->flow.key,
757 &state->flow, &state->buf);
758 if (error == ENOENT) {
759 VLOG_DBG("dumped flow disappeared on get");
761 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
767 *actions = state->flow.actions;
768 *actions_len = state->flow.actions_len;
771 *key = state->flow.key;
772 *key_len = state->flow.key_len;
775 dpif_linux_flow_get_stats(&state->flow, &state->stats);
776 *stats = &state->stats;
782 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
784 struct dpif_linux_flow_state *state = state_;
785 int error = nl_dump_done(&state->dump);
786 ofpbuf_delete(state->buf);
792 dpif_linux_execute__(int dp_ifindex,
793 const struct nlattr *key, size_t key_len,
794 const struct nlattr *actions, size_t actions_len,
795 const struct ofpbuf *packet)
797 struct ovs_header *execute;
801 buf = ofpbuf_new(128 + actions_len + packet->size);
803 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
804 OVS_PACKET_CMD_EXECUTE, 1);
806 execute = ofpbuf_put_uninit(buf, sizeof *execute);
807 execute->dp_ifindex = dp_ifindex;
809 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
810 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
811 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
813 error = nl_sock_transact(genl_sock, buf, NULL);
819 dpif_linux_execute(struct dpif *dpif_,
820 const struct nlattr *key, size_t key_len,
821 const struct nlattr *actions, size_t actions_len,
822 const struct ofpbuf *packet)
824 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
826 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
827 actions, actions_len, packet);
831 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
833 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
834 *listen_mask = dpif->listen_mask;
839 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
841 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
845 if (listen_mask == dpif->listen_mask) {
847 } else if (!listen_mask) {
848 nl_sock_destroy(dpif->mc_sock);
849 dpif->mc_sock = NULL;
850 dpif->listen_mask = 0;
852 } else if (!dpif->mc_sock) {
853 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
859 /* Unsubscribe from old groups. */
860 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
861 if (dpif->listen_mask & (1u << i)) {
862 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
866 /* Update listen_mask. */
867 dpif->listen_mask = listen_mask;
869 /* Subscribe to new groups. */
871 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
872 if (dpif->listen_mask & (1u << i)) {
875 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
885 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
886 uint32_t *probability)
888 struct dpif_linux_dp dp;
892 error = dpif_linux_dp_get(dpif_, &dp, &buf);
894 *probability = dp.sampling ? *dp.sampling : 0;
901 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
903 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
904 struct dpif_linux_dp dp;
906 dpif_linux_dp_init(&dp);
907 dp.cmd = OVS_DP_CMD_SET;
908 dp.dp_ifindex = dpif->dp_ifindex;
909 dp.sampling = &probability;
910 return dpif_linux_dp_transact(&dp, NULL, NULL);
914 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
915 uint32_t queue_id, uint32_t *priority)
917 if (queue_id < 0xf000) {
918 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
926 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
929 static const struct nl_policy ovs_packet_policy[] = {
930 /* Always present. */
931 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
932 .min_len = ETH_HEADER_LEN },
933 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
935 /* OVS_PACKET_CMD_ACTION only. */
936 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
938 /* OVS_PACKET_CMD_SAMPLE only. */
939 [OVS_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
940 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
943 struct ovs_header *ovs_header;
944 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
945 struct nlmsghdr *nlmsg;
946 struct genlmsghdr *genl;
950 ofpbuf_use_const(&b, buf->data, buf->size);
952 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
953 genl = ofpbuf_try_pull(&b, sizeof *genl);
954 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
955 if (!nlmsg || !genl || !ovs_header
956 || nlmsg->nlmsg_type != ovs_packet_family
957 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
958 ARRAY_SIZE(ovs_packet_policy))) {
962 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
963 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
964 : genl->cmd == OVS_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
970 memset(upcall, 0, sizeof *upcall);
972 upcall->packet = buf;
973 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
974 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
975 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
976 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
977 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
978 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
980 upcall->sample_pool = (a[OVS_PACKET_ATTR_SAMPLE_POOL]
981 ? nl_attr_get_u32(a[OVS_PACKET_ATTR_SAMPLE_POOL])
983 if (a[OVS_PACKET_ATTR_ACTIONS]) {
984 upcall->actions = (void *) nl_attr_get(a[OVS_PACKET_ATTR_ACTIONS]);
985 upcall->actions_len = nl_attr_get_size(a[OVS_PACKET_ATTR_ACTIONS]);
988 *dp_ifindex = ovs_header->dp_ifindex;
994 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
996 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1001 if (!dpif->mc_sock) {
1005 for (i = 0; i < 50; i++) {
1008 error = nl_sock_recv(dpif->mc_sock, &buf, false);
1013 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1015 && dp_ifindex == dpif->dp_ifindex
1016 && dpif->listen_mask & (1u << upcall->type)) {
1030 dpif_linux_recv_wait(struct dpif *dpif_)
1032 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1033 if (dpif->mc_sock) {
1034 nl_sock_wait(dpif->mc_sock, POLLIN);
1039 dpif_linux_recv_purge(struct dpif *dpif_)
1041 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1043 if (dpif->mc_sock) {
1044 nl_sock_drain(dpif->mc_sock);
1048 const struct dpif_class dpif_linux_class = {
1050 dpif_linux_enumerate,
1056 dpif_linux_get_stats,
1057 dpif_linux_get_drop_frags,
1058 dpif_linux_set_drop_frags,
1059 dpif_linux_port_add,
1060 dpif_linux_port_del,
1061 dpif_linux_port_query_by_number,
1062 dpif_linux_port_query_by_name,
1063 dpif_linux_get_max_ports,
1064 dpif_linux_port_dump_start,
1065 dpif_linux_port_dump_next,
1066 dpif_linux_port_dump_done,
1067 dpif_linux_port_poll,
1068 dpif_linux_port_poll_wait,
1069 dpif_linux_flow_get,
1070 dpif_linux_flow_put,
1071 dpif_linux_flow_del,
1072 dpif_linux_flow_flush,
1073 dpif_linux_flow_dump_start,
1074 dpif_linux_flow_dump_next,
1075 dpif_linux_flow_dump_done,
1077 dpif_linux_recv_get_mask,
1078 dpif_linux_recv_set_mask,
1079 dpif_linux_get_sflow_probability,
1080 dpif_linux_set_sflow_probability,
1081 dpif_linux_queue_to_priority,
1083 dpif_linux_recv_wait,
1084 dpif_linux_recv_purge,
1088 dpif_linux_init(void)
1090 static int error = -1;
1093 unsigned int ovs_vport_mcgroup;
1095 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1096 &ovs_datapath_family);
1098 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1099 "The Open vSwitch kernel module is probably not loaded.",
1100 OVS_DATAPATH_FAMILY);
1103 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1106 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1109 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1110 &ovs_packet_family);
1113 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1116 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1117 &ovs_vport_mcgroup);
1120 static struct dpif_linux_vport vport;
1121 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1122 dpif_linux_nln_parse, &vport);
1130 dpif_linux_is_internal_device(const char *name)
1132 struct dpif_linux_vport reply;
1136 error = dpif_linux_vport_get(name, &reply, &buf);
1139 } else if (error != ENODEV && error != ENOENT) {
1140 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1141 name, strerror(error));
1144 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1148 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1149 const void *data, size_t size)
1151 struct ofpbuf actions, key, packet;
1152 struct odputil_keybuf keybuf;
1156 ofpbuf_use_const(&packet, data, size);
1157 flow_extract(&packet, htonll(0), 0, &flow);
1159 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1160 odp_flow_key_from_flow(&key, &flow);
1162 ofpbuf_use_stack(&actions, &action, sizeof action);
1163 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1165 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1166 actions.data, actions.size, &packet);
1170 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1172 struct dpif_linux_vport *vport = vport_;
1173 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1177 dpif_linux_port_changed(const void *vport_, void *dpif_)
1179 const struct dpif_linux_vport *vport = vport_;
1180 struct dpif_linux *dpif = dpif_;
1183 if (vport->dp_ifindex == dpif->dp_ifindex
1184 && (vport->cmd == OVS_VPORT_CMD_NEW
1185 || vport->cmd == OVS_VPORT_CMD_DEL
1186 || vport->cmd == OVS_VPORT_CMD_SET)) {
1187 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1188 dpif->dpif.full_name, vport->name, vport->cmd);
1189 sset_add(&dpif->changed_ports, vport->name);
1192 dpif->change_error = true;
1196 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1197 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1198 * positive errno value.
1200 * 'vport' will contain pointers into 'buf', so the caller should not free
1201 * 'buf' while 'vport' is still in use. */
1203 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1204 const struct ofpbuf *buf)
1206 static const struct nl_policy ovs_vport_policy[] = {
1207 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1208 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1209 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1210 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1211 .min_len = sizeof(struct ovs_vport_stats),
1212 .max_len = sizeof(struct ovs_vport_stats),
1214 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1215 .min_len = ETH_ADDR_LEN,
1216 .max_len = ETH_ADDR_LEN,
1218 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1219 [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1222 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1223 struct ovs_header *ovs_header;
1224 struct nlmsghdr *nlmsg;
1225 struct genlmsghdr *genl;
1228 dpif_linux_vport_init(vport);
1230 ofpbuf_use_const(&b, buf->data, buf->size);
1231 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1232 genl = ofpbuf_try_pull(&b, sizeof *genl);
1233 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1234 if (!nlmsg || !genl || !ovs_header
1235 || nlmsg->nlmsg_type != ovs_vport_family
1236 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1237 ARRAY_SIZE(ovs_vport_policy))) {
1241 vport->cmd = genl->cmd;
1242 vport->dp_ifindex = ovs_header->dp_ifindex;
1243 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1244 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1245 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1246 if (a[OVS_VPORT_ATTR_STATS]) {
1247 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1249 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1250 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1252 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1253 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1254 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1256 if (a[OVS_VPORT_ATTR_IFINDEX]) {
1257 vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1262 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1263 * followed by Netlink attributes corresponding to 'vport'. */
1265 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1268 struct ovs_header *ovs_header;
1270 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1273 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1274 ovs_header->dp_ifindex = vport->dp_ifindex;
1276 if (vport->port_no != UINT32_MAX) {
1277 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1280 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1281 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1285 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1289 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1290 vport->stats, sizeof *vport->stats);
1293 if (vport->address) {
1294 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1295 vport->address, ETH_ADDR_LEN);
1298 if (vport->options) {
1299 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1300 vport->options, vport->options_len);
1303 if (vport->ifindex) {
1304 nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1308 /* Clears 'vport' to "empty" values. */
1310 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1312 memset(vport, 0, sizeof *vport);
1313 vport->port_no = UINT32_MAX;
1316 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1317 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1318 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1319 * result of the command is expected to be an ovs_vport also, which is decoded
1320 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1321 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1323 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1324 struct dpif_linux_vport *reply,
1325 struct ofpbuf **bufp)
1327 struct ofpbuf *request_buf;
1330 assert((reply != NULL) == (bufp != NULL));
1332 error = dpif_linux_init();
1336 dpif_linux_vport_init(reply);
1341 request_buf = ofpbuf_new(1024);
1342 dpif_linux_vport_to_ofpbuf(request, request_buf);
1343 error = nl_sock_transact(genl_sock, request_buf, bufp);
1344 ofpbuf_delete(request_buf);
1348 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1351 dpif_linux_vport_init(reply);
1352 ofpbuf_delete(*bufp);
1359 /* Obtains information about the kernel vport named 'name' and stores it into
1360 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1361 * longer needed ('reply' will contain pointers into '*bufp'). */
1363 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1364 struct ofpbuf **bufp)
1366 struct dpif_linux_vport request;
1368 dpif_linux_vport_init(&request);
1369 request.cmd = OVS_VPORT_CMD_GET;
1370 request.name = name;
1372 return dpif_linux_vport_transact(&request, reply, bufp);
1375 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1376 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1377 * positive errno value.
1379 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1380 * while 'dp' is still in use. */
1382 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1384 static const struct nl_policy ovs_datapath_policy[] = {
1385 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1386 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1387 .min_len = sizeof(struct ovs_dp_stats),
1388 .max_len = sizeof(struct ovs_dp_stats),
1390 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1391 [OVS_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1392 [OVS_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1395 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1396 struct ovs_header *ovs_header;
1397 struct nlmsghdr *nlmsg;
1398 struct genlmsghdr *genl;
1401 dpif_linux_dp_init(dp);
1403 ofpbuf_use_const(&b, buf->data, buf->size);
1404 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1405 genl = ofpbuf_try_pull(&b, sizeof *genl);
1406 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1407 if (!nlmsg || !genl || !ovs_header
1408 || nlmsg->nlmsg_type != ovs_datapath_family
1409 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1410 ARRAY_SIZE(ovs_datapath_policy))) {
1414 dp->cmd = genl->cmd;
1415 dp->dp_ifindex = ovs_header->dp_ifindex;
1416 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1417 if (a[OVS_DP_ATTR_STATS]) {
1418 /* Can't use structure assignment because Netlink doesn't ensure
1419 * sufficient alignment for 64-bit members. */
1420 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1423 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1424 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1426 if (a[OVS_DP_ATTR_SAMPLING]) {
1427 dp->sampling = nl_attr_get(a[OVS_DP_ATTR_SAMPLING]);
1430 if (a[OVS_DP_ATTR_MCGROUPS]) {
1431 static const struct nl_policy ovs_mcgroup_policy[] = {
1432 [OVS_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1433 [OVS_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1434 [OVS_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1437 struct nlattr *mcgroups[ARRAY_SIZE(ovs_mcgroup_policy)];
1439 if (!nl_parse_nested(a[OVS_DP_ATTR_MCGROUPS], ovs_mcgroup_policy,
1440 mcgroups, ARRAY_SIZE(ovs_mcgroup_policy))) {
1444 if (mcgroups[OVS_PACKET_CMD_MISS]) {
1445 dp->mcgroups[DPIF_UC_MISS]
1446 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_MISS]);
1448 if (mcgroups[OVS_PACKET_CMD_ACTION]) {
1449 dp->mcgroups[DPIF_UC_ACTION]
1450 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_ACTION]);
1452 if (mcgroups[OVS_PACKET_CMD_SAMPLE]) {
1453 dp->mcgroups[DPIF_UC_SAMPLE]
1454 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_SAMPLE]);
1461 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1463 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1465 struct ovs_header *ovs_header;
1467 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1468 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1470 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1471 ovs_header->dp_ifindex = dp->dp_ifindex;
1474 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1477 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1479 if (dp->ipv4_frags) {
1480 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1484 nl_msg_put_u32(buf, OVS_DP_ATTR_SAMPLING, *dp->sampling);
1488 /* Clears 'dp' to "empty" values. */
1490 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1492 memset(dp, 0, sizeof *dp);
1496 dpif_linux_dp_dump_start(struct nl_dump *dump)
1498 struct dpif_linux_dp request;
1501 dpif_linux_dp_init(&request);
1502 request.cmd = OVS_DP_CMD_GET;
1504 buf = ofpbuf_new(1024);
1505 dpif_linux_dp_to_ofpbuf(&request, buf);
1506 nl_dump_start(dump, genl_sock, buf);
1510 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1511 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1512 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1513 * result of the command is expected to be of the same form, which is decoded
1514 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1515 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1517 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1518 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1520 struct ofpbuf *request_buf;
1523 assert((reply != NULL) == (bufp != NULL));
1525 request_buf = ofpbuf_new(1024);
1526 dpif_linux_dp_to_ofpbuf(request, request_buf);
1527 error = nl_sock_transact(genl_sock, request_buf, bufp);
1528 ofpbuf_delete(request_buf);
1532 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1535 dpif_linux_dp_init(reply);
1536 ofpbuf_delete(*bufp);
1543 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1544 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1545 * will contain pointers into '*bufp'). */
1547 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1548 struct ofpbuf **bufp)
1550 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1551 struct dpif_linux_dp request;
1553 dpif_linux_dp_init(&request);
1554 request.cmd = OVS_DP_CMD_GET;
1555 request.dp_ifindex = dpif->dp_ifindex;
1557 return dpif_linux_dp_transact(&request, reply, bufp);
1560 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1561 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1562 * positive errno value.
1564 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1565 * while 'flow' is still in use. */
1567 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1568 const struct ofpbuf *buf)
1570 static const struct nl_policy ovs_flow_policy[] = {
1571 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1572 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1573 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1574 .min_len = sizeof(struct ovs_flow_stats),
1575 .max_len = sizeof(struct ovs_flow_stats),
1577 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1578 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1579 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1582 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1583 struct ovs_header *ovs_header;
1584 struct nlmsghdr *nlmsg;
1585 struct genlmsghdr *genl;
1588 dpif_linux_flow_init(flow);
1590 ofpbuf_use_const(&b, buf->data, buf->size);
1591 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1592 genl = ofpbuf_try_pull(&b, sizeof *genl);
1593 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1594 if (!nlmsg || !genl || !ovs_header
1595 || nlmsg->nlmsg_type != ovs_flow_family
1596 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1597 ARRAY_SIZE(ovs_flow_policy))) {
1601 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1602 flow->dp_ifindex = ovs_header->dp_ifindex;
1603 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1604 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1605 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1606 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1607 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1609 if (a[OVS_FLOW_ATTR_STATS]) {
1610 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1612 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1613 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1615 if (a[OVS_FLOW_ATTR_USED]) {
1616 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1621 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1622 * followed by Netlink attributes corresponding to 'flow'. */
1624 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1627 struct ovs_header *ovs_header;
1629 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1630 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1633 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1634 ovs_header->dp_ifindex = flow->dp_ifindex;
1636 if (flow->key_len) {
1637 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1640 if (flow->actions || flow->actions_len) {
1641 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1642 flow->actions, flow->actions_len);
1645 /* We never need to send these to the kernel. */
1646 assert(!flow->stats);
1647 assert(!flow->tcp_flags);
1648 assert(!flow->used);
1651 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1655 /* Clears 'flow' to "empty" values. */
1657 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1659 memset(flow, 0, sizeof *flow);
1662 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1663 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1664 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1665 * result of the command is expected to be a flow also, which is decoded and
1666 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1667 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1669 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1670 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1672 struct ofpbuf *request_buf;
1675 assert((reply != NULL) == (bufp != NULL));
1677 request_buf = ofpbuf_new(1024);
1678 dpif_linux_flow_to_ofpbuf(request, request_buf);
1679 error = nl_sock_transact(genl_sock, request_buf, bufp);
1680 ofpbuf_delete(request_buf);
1684 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1687 dpif_linux_flow_init(reply);
1688 ofpbuf_delete(*bufp);
1696 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1697 struct dpif_flow_stats *stats)
1700 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1701 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1703 stats->n_packets = 0;
1706 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1707 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;