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/datapath-compat.h"
48 #include "openvswitch/tunnel.h"
50 #include "poll-loop.h"
53 #include "unaligned.h"
57 VLOG_DEFINE_THIS_MODULE(dpif_linux);
59 enum { LRU_MAX_PORTS = 1024 };
60 enum { LRU_MASK = LRU_MAX_PORTS - 1};
61 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
63 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
64 * missing if we have old headers. */
65 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
67 struct dpif_linux_dp {
68 /* Generic Netlink header. */
71 /* struct ovs_header. */
75 const char *name; /* OVS_DP_ATTR_NAME. */
76 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
77 enum ovs_frag_handling ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
78 const uint32_t *sampling; /* OVS_DP_ATTR_SAMPLING. */
79 uint32_t mcgroups[DPIF_N_UC_TYPES]; /* OVS_DP_ATTR_MCGROUPS. */
82 static void dpif_linux_dp_init(struct dpif_linux_dp *);
83 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
84 const struct ofpbuf *);
85 static void dpif_linux_dp_dump_start(struct nl_dump *);
86 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
87 struct dpif_linux_dp *reply,
88 struct ofpbuf **bufp);
89 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
90 struct ofpbuf **bufp);
92 struct dpif_linux_flow {
93 /* Generic Netlink header. */
96 /* struct ovs_header. */
97 unsigned int nlmsg_flags;
102 * The 'stats' and 'used' members point to 64-bit data that might only be
103 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
104 * access their values.
106 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
107 * the Netlink version of the command, even if actions_len is zero. */
108 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
110 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
112 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
113 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
114 const uint64_t *used; /* OVS_FLOW_ATTR_USED. */
115 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
118 static void dpif_linux_flow_init(struct dpif_linux_flow *);
119 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
120 const struct ofpbuf *);
121 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
123 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
124 struct dpif_linux_flow *reply,
125 struct ofpbuf **bufp);
126 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
127 struct dpif_flow_stats *);
129 /* Datapath interface for the openvswitch Linux kernel module. */
134 /* Multicast group messages. */
135 struct nl_sock *mc_sock;
136 uint32_t mcgroups[DPIF_N_UC_TYPES];
137 unsigned int listen_mask;
139 /* Change notification. */
140 struct sset changed_ports; /* Ports that have changed. */
141 struct nln_notifier *port_notifier;
144 /* Queue of unused ports. */
145 unsigned long *lru_bitmap;
146 uint16_t lru_ports[LRU_MAX_PORTS];
151 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
153 /* Generic Netlink family numbers for OVS. */
154 static int ovs_datapath_family;
155 static int ovs_vport_family;
156 static int ovs_flow_family;
157 static int ovs_packet_family;
159 /* Generic Netlink socket. */
160 static struct nl_sock *genl_sock;
161 static struct nln *nln = NULL;
163 static int dpif_linux_init(void);
164 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
165 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
166 static void dpif_linux_port_changed(const void *vport, void *dpif);
168 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
170 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
171 const struct ofpbuf *);
173 static struct dpif_linux *
174 dpif_linux_cast(const struct dpif *dpif)
176 dpif_assert_class(dpif, &dpif_linux_class);
177 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
181 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
183 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
184 bitmap_set1(dp->lru_bitmap, port);
185 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
190 dpif_linux_pop_port(struct dpif_linux *dp)
194 if (dp->lru_head == dp->lru_tail) {
198 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
199 bitmap_set0(dp->lru_bitmap, port);
204 dpif_linux_enumerate(struct sset *all_dps)
210 error = dpif_linux_init();
215 dpif_linux_dp_dump_start(&dump);
216 while (nl_dump_next(&dump, &msg)) {
217 struct dpif_linux_dp dp;
219 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
220 sset_add(all_dps, dp.name);
223 return nl_dump_done(&dump);
227 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
228 bool create, struct dpif **dpifp)
230 struct dpif_linux_dp dp_request, dp;
234 error = dpif_linux_init();
239 /* Create or look up datapath. */
240 dpif_linux_dp_init(&dp_request);
241 dp_request.cmd = create ? OVS_DP_CMD_NEW : OVS_DP_CMD_GET;
242 dp_request.name = name;
243 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
248 open_dpif(&dp, dpifp);
254 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
256 struct dpif_linux *dpif;
259 dpif = xmalloc(sizeof *dpif);
260 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
263 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
264 dp->dp_ifindex, dp->dp_ifindex);
266 dpif->mc_sock = NULL;
267 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
268 dpif->mcgroups[i] = dp->mcgroups[i];
270 dpif->listen_mask = 0;
271 dpif->dp_ifindex = dp->dp_ifindex;
272 sset_init(&dpif->changed_ports);
273 dpif->change_error = false;
274 *dpifp = &dpif->dpif;
276 dpif->lru_head = dpif->lru_tail = 0;
277 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
278 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
279 for (i = 1; i < LRU_MAX_PORTS; i++) {
280 dpif_linux_push_port(dpif, i);
285 dpif_linux_close(struct dpif *dpif_)
287 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
289 nln_notifier_destroy(dpif->port_notifier);
290 nl_sock_destroy(dpif->mc_sock);
291 sset_destroy(&dpif->changed_ports);
292 free(dpif->lru_bitmap);
297 dpif_linux_destroy(struct dpif *dpif_)
299 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
300 struct dpif_linux_dp dp;
302 dpif_linux_dp_init(&dp);
303 dp.cmd = OVS_DP_CMD_DEL;
304 dp.dp_ifindex = dpif->dp_ifindex;
305 return dpif_linux_dp_transact(&dp, NULL, NULL);
309 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
317 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
325 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
327 struct dpif_linux_dp dp;
331 error = dpif_linux_dp_get(dpif_, &dp, &buf);
340 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
342 struct dpif_linux_dp dp;
346 error = dpif_linux_dp_get(dpif_, &dp, &buf);
348 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
355 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
357 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
358 struct dpif_linux_dp dp;
360 dpif_linux_dp_init(&dp);
361 dp.cmd = OVS_DP_CMD_SET;
362 dp.dp_ifindex = dpif->dp_ifindex;
363 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
364 return dpif_linux_dp_transact(&dp, NULL, NULL);
368 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
371 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
372 const char *name = netdev_get_name(netdev);
373 const char *type = netdev_get_type(netdev);
374 struct dpif_linux_vport request, reply;
375 const struct ofpbuf *options;
379 dpif_linux_vport_init(&request);
380 request.cmd = OVS_VPORT_CMD_NEW;
381 request.dp_ifindex = dpif->dp_ifindex;
382 request.type = netdev_vport_get_vport_type(netdev);
383 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
384 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
385 "unsupported type `%s'",
386 dpif_name(dpif_), name, type);
391 options = netdev_vport_get_options(netdev);
392 if (options && options->size) {
393 request.options = options->data;
394 request.options_len = options->size;
397 if (request.type == OVS_VPORT_TYPE_NETDEV) {
398 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
401 /* Loop until we find a port that isn't used. */
403 request.port_no = dpif_linux_pop_port(dpif);
404 error = dpif_linux_vport_transact(&request, &reply, &buf);
407 *port_nop = reply.port_no;
410 } while (request.port_no != UINT32_MAX
411 && (error == EBUSY || error == EFBIG));
417 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
419 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
420 struct dpif_linux_vport vport;
423 dpif_linux_vport_init(&vport);
424 vport.cmd = OVS_VPORT_CMD_DEL;
425 vport.dp_ifindex = dpif->dp_ifindex;
426 vport.port_no = port_no;
427 error = dpif_linux_vport_transact(&vport, NULL, NULL);
430 dpif_linux_push_port(dpif, port_no);
436 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
437 const char *port_name, struct dpif_port *dpif_port)
439 struct dpif_linux_vport request;
440 struct dpif_linux_vport reply;
444 dpif_linux_vport_init(&request);
445 request.cmd = OVS_VPORT_CMD_GET;
446 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
447 request.port_no = port_no;
448 request.name = port_name;
450 error = dpif_linux_vport_transact(&request, &reply, &buf);
452 dpif_port->name = xstrdup(reply.name);
453 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
454 dpif_port->port_no = reply.port_no;
461 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
462 struct dpif_port *dpif_port)
464 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
468 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
469 struct dpif_port *dpif_port)
471 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
475 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
477 /* If the datapath increases its range of supported ports, then it should
478 * start reporting that. */
483 dpif_linux_flow_flush(struct dpif *dpif_)
485 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
486 struct dpif_linux_flow flow;
488 dpif_linux_flow_init(&flow);
489 flow.cmd = OVS_FLOW_CMD_DEL;
490 flow.dp_ifindex = dpif->dp_ifindex;
491 return dpif_linux_flow_transact(&flow, NULL, NULL);
494 struct dpif_linux_port_state {
496 unsigned long *port_bitmap; /* Ports in the datapath. */
497 bool complete; /* Dump completed without error. */
501 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
503 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
504 struct dpif_linux_port_state *state;
505 struct dpif_linux_vport request;
508 *statep = state = xmalloc(sizeof *state);
509 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
510 state->complete = false;
512 dpif_linux_vport_init(&request);
513 request.cmd = OVS_DP_CMD_GET;
514 request.dp_ifindex = dpif->dp_ifindex;
516 buf = ofpbuf_new(1024);
517 dpif_linux_vport_to_ofpbuf(&request, buf);
518 nl_dump_start(&state->dump, genl_sock, buf);
525 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
526 struct dpif_port *dpif_port)
528 struct dpif_linux_port_state *state = state_;
529 struct dpif_linux_vport vport;
533 if (!nl_dump_next(&state->dump, &buf)) {
534 state->complete = true;
538 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
543 if (vport.port_no < LRU_MAX_PORTS) {
544 bitmap_set1(state->port_bitmap, vport.port_no);
547 dpif_port->name = (char *) vport.name;
548 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
549 dpif_port->port_no = vport.port_no;
554 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
556 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
557 struct dpif_linux_port_state *state = state_;
558 int error = nl_dump_done(&state->dump);
560 if (state->complete) {
563 for (i = 0; i < LRU_MAX_PORTS; i++) {
564 if (!bitmap_is_set(state->port_bitmap, i)) {
565 dpif_linux_push_port(dpif, i);
570 free(state->port_bitmap);
576 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
578 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
580 if (dpif->change_error) {
581 dpif->change_error = false;
582 sset_clear(&dpif->changed_ports);
584 } else if (!sset_is_empty(&dpif->changed_ports)) {
585 *devnamep = sset_pop(&dpif->changed_ports);
593 dpif_linux_port_poll_wait(const struct dpif *dpif_)
595 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
596 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
597 poll_immediate_wake();
602 dpif_linux_flow_get__(const struct dpif *dpif_,
603 const struct nlattr *key, size_t key_len,
604 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
606 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
607 struct dpif_linux_flow request;
609 dpif_linux_flow_init(&request);
610 request.cmd = OVS_FLOW_CMD_GET;
611 request.dp_ifindex = dpif->dp_ifindex;
613 request.key_len = key_len;
614 return dpif_linux_flow_transact(&request, reply, bufp);
618 dpif_linux_flow_get(const struct dpif *dpif_,
619 const struct nlattr *key, size_t key_len,
620 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
622 struct dpif_linux_flow reply;
626 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
629 dpif_linux_flow_get_stats(&reply, stats);
632 buf->data = (void *) reply.actions;
633 buf->size = reply.actions_len;
643 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
644 const struct nlattr *key, size_t key_len,
645 const struct nlattr *actions, size_t actions_len,
646 struct dpif_flow_stats *stats)
648 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
649 struct dpif_linux_flow request, reply;
650 struct nlattr dummy_action;
654 dpif_linux_flow_init(&request);
655 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
656 request.dp_ifindex = dpif->dp_ifindex;
658 request.key_len = key_len;
659 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
660 request.actions = actions ? actions : &dummy_action;
661 request.actions_len = actions_len;
662 if (flags & DPIF_FP_ZERO_STATS) {
663 request.clear = true;
665 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
666 error = dpif_linux_flow_transact(&request,
667 stats ? &reply : NULL,
668 stats ? &buf : NULL);
669 if (!error && stats) {
670 dpif_linux_flow_get_stats(&reply, stats);
677 dpif_linux_flow_del(struct dpif *dpif_,
678 const struct nlattr *key, size_t key_len,
679 struct dpif_flow_stats *stats)
681 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
682 struct dpif_linux_flow request, reply;
686 dpif_linux_flow_init(&request);
687 request.cmd = OVS_FLOW_CMD_DEL;
688 request.dp_ifindex = dpif->dp_ifindex;
690 request.key_len = key_len;
691 error = dpif_linux_flow_transact(&request,
692 stats ? &reply : NULL,
693 stats ? &buf : NULL);
694 if (!error && stats) {
695 dpif_linux_flow_get_stats(&reply, stats);
701 struct dpif_linux_flow_state {
703 struct dpif_linux_flow flow;
704 struct dpif_flow_stats stats;
709 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
711 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
712 struct dpif_linux_flow_state *state;
713 struct dpif_linux_flow request;
716 *statep = state = xmalloc(sizeof *state);
718 dpif_linux_flow_init(&request);
719 request.cmd = OVS_DP_CMD_GET;
720 request.dp_ifindex = dpif->dp_ifindex;
722 buf = ofpbuf_new(1024);
723 dpif_linux_flow_to_ofpbuf(&request, buf);
724 nl_dump_start(&state->dump, genl_sock, buf);
733 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
734 const struct nlattr **key, size_t *key_len,
735 const struct nlattr **actions, size_t *actions_len,
736 const struct dpif_flow_stats **stats)
738 struct dpif_linux_flow_state *state = state_;
743 ofpbuf_delete(state->buf);
746 if (!nl_dump_next(&state->dump, &buf)) {
750 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
755 if (actions && !state->flow.actions) {
756 error = dpif_linux_flow_get__(dpif_, state->flow.key,
758 &state->flow, &state->buf);
759 if (error == ENOENT) {
760 VLOG_DBG("dumped flow disappeared on get");
762 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
768 *actions = state->flow.actions;
769 *actions_len = state->flow.actions_len;
772 *key = state->flow.key;
773 *key_len = state->flow.key_len;
776 dpif_linux_flow_get_stats(&state->flow, &state->stats);
777 *stats = &state->stats;
783 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
785 struct dpif_linux_flow_state *state = state_;
786 int error = nl_dump_done(&state->dump);
787 ofpbuf_delete(state->buf);
793 dpif_linux_execute__(int dp_ifindex,
794 const struct nlattr *key, size_t key_len,
795 const struct nlattr *actions, size_t actions_len,
796 const struct ofpbuf *packet)
798 struct ovs_header *execute;
802 buf = ofpbuf_new(128 + actions_len + packet->size);
804 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
805 OVS_PACKET_CMD_EXECUTE, 1);
807 execute = ofpbuf_put_uninit(buf, sizeof *execute);
808 execute->dp_ifindex = dp_ifindex;
810 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
811 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
812 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
814 error = nl_sock_transact(genl_sock, buf, NULL);
820 dpif_linux_execute(struct dpif *dpif_,
821 const struct nlattr *key, size_t key_len,
822 const struct nlattr *actions, size_t actions_len,
823 const struct ofpbuf *packet)
825 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
827 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
828 actions, actions_len, packet);
832 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
834 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
835 *listen_mask = dpif->listen_mask;
840 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
842 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
846 if (listen_mask == dpif->listen_mask) {
848 } else if (!listen_mask) {
849 nl_sock_destroy(dpif->mc_sock);
850 dpif->mc_sock = NULL;
851 dpif->listen_mask = 0;
853 } else if (!dpif->mc_sock) {
854 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
860 /* Unsubscribe from old groups. */
861 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
862 if (dpif->listen_mask & (1u << i)) {
863 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
867 /* Update listen_mask. */
868 dpif->listen_mask = listen_mask;
870 /* Subscribe to new groups. */
872 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
873 if (dpif->listen_mask & (1u << i)) {
876 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
886 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
887 uint32_t *probability)
889 struct dpif_linux_dp dp;
893 error = dpif_linux_dp_get(dpif_, &dp, &buf);
895 *probability = dp.sampling ? *dp.sampling : 0;
902 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
904 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
905 struct dpif_linux_dp dp;
907 dpif_linux_dp_init(&dp);
908 dp.cmd = OVS_DP_CMD_SET;
909 dp.dp_ifindex = dpif->dp_ifindex;
910 dp.sampling = &probability;
911 return dpif_linux_dp_transact(&dp, NULL, NULL);
915 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
916 uint32_t queue_id, uint32_t *priority)
918 if (queue_id < 0xf000) {
919 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
927 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
930 static const struct nl_policy ovs_packet_policy[] = {
931 /* Always present. */
932 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
933 .min_len = ETH_HEADER_LEN },
934 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
936 /* OVS_PACKET_CMD_ACTION only. */
937 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
939 /* OVS_PACKET_CMD_SAMPLE only. */
940 [OVS_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
941 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
944 struct ovs_header *ovs_header;
945 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
946 struct nlmsghdr *nlmsg;
947 struct genlmsghdr *genl;
951 ofpbuf_use_const(&b, buf->data, buf->size);
953 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
954 genl = ofpbuf_try_pull(&b, sizeof *genl);
955 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
956 if (!nlmsg || !genl || !ovs_header
957 || nlmsg->nlmsg_type != ovs_packet_family
958 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
959 ARRAY_SIZE(ovs_packet_policy))) {
963 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
964 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
965 : genl->cmd == OVS_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
971 memset(upcall, 0, sizeof *upcall);
973 upcall->packet = buf;
974 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
975 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
976 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
977 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
978 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
979 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
981 upcall->sample_pool = (a[OVS_PACKET_ATTR_SAMPLE_POOL]
982 ? nl_attr_get_u32(a[OVS_PACKET_ATTR_SAMPLE_POOL])
984 if (a[OVS_PACKET_ATTR_ACTIONS]) {
985 upcall->actions = (void *) nl_attr_get(a[OVS_PACKET_ATTR_ACTIONS]);
986 upcall->actions_len = nl_attr_get_size(a[OVS_PACKET_ATTR_ACTIONS]);
989 *dp_ifindex = ovs_header->dp_ifindex;
995 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
997 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1002 if (!dpif->mc_sock) {
1006 for (i = 0; i < 50; i++) {
1009 error = nl_sock_recv(dpif->mc_sock, &buf, false);
1014 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1016 && dp_ifindex == dpif->dp_ifindex
1017 && dpif->listen_mask & (1u << upcall->type)) {
1031 dpif_linux_recv_wait(struct dpif *dpif_)
1033 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1034 if (dpif->mc_sock) {
1035 nl_sock_wait(dpif->mc_sock, POLLIN);
1040 dpif_linux_recv_purge(struct dpif *dpif_)
1042 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1044 if (dpif->mc_sock) {
1045 nl_sock_drain(dpif->mc_sock);
1049 const struct dpif_class dpif_linux_class = {
1051 dpif_linux_enumerate,
1057 dpif_linux_get_stats,
1058 dpif_linux_get_drop_frags,
1059 dpif_linux_set_drop_frags,
1060 dpif_linux_port_add,
1061 dpif_linux_port_del,
1062 dpif_linux_port_query_by_number,
1063 dpif_linux_port_query_by_name,
1064 dpif_linux_get_max_ports,
1065 dpif_linux_port_dump_start,
1066 dpif_linux_port_dump_next,
1067 dpif_linux_port_dump_done,
1068 dpif_linux_port_poll,
1069 dpif_linux_port_poll_wait,
1070 dpif_linux_flow_get,
1071 dpif_linux_flow_put,
1072 dpif_linux_flow_del,
1073 dpif_linux_flow_flush,
1074 dpif_linux_flow_dump_start,
1075 dpif_linux_flow_dump_next,
1076 dpif_linux_flow_dump_done,
1078 dpif_linux_recv_get_mask,
1079 dpif_linux_recv_set_mask,
1080 dpif_linux_get_sflow_probability,
1081 dpif_linux_set_sflow_probability,
1082 dpif_linux_queue_to_priority,
1084 dpif_linux_recv_wait,
1085 dpif_linux_recv_purge,
1089 dpif_linux_init(void)
1091 static int error = -1;
1094 unsigned int ovs_vport_mcgroup;
1096 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1097 &ovs_datapath_family);
1099 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1100 "The Open vSwitch kernel module is probably not loaded.",
1101 OVS_DATAPATH_FAMILY);
1104 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1107 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1110 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1111 &ovs_packet_family);
1114 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1117 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1119 OVS_VPORT_MCGROUP_FALLBACK_ID);
1122 static struct dpif_linux_vport vport;
1123 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1124 dpif_linux_nln_parse, &vport);
1132 dpif_linux_is_internal_device(const char *name)
1134 struct dpif_linux_vport reply;
1138 error = dpif_linux_vport_get(name, &reply, &buf);
1141 } else if (error != ENODEV && error != ENOENT) {
1142 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1143 name, strerror(error));
1146 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1150 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1151 const void *data, size_t size)
1153 struct ofpbuf actions, key, packet;
1154 struct odputil_keybuf keybuf;
1158 ofpbuf_use_const(&packet, data, size);
1159 flow_extract(&packet, htonll(0), 0, &flow);
1161 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1162 odp_flow_key_from_flow(&key, &flow);
1164 ofpbuf_use_stack(&actions, &action, sizeof action);
1165 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1167 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1168 actions.data, actions.size, &packet);
1172 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1174 struct dpif_linux_vport *vport = vport_;
1175 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1179 dpif_linux_port_changed(const void *vport_, void *dpif_)
1181 const struct dpif_linux_vport *vport = vport_;
1182 struct dpif_linux *dpif = dpif_;
1185 if (vport->dp_ifindex == dpif->dp_ifindex
1186 && (vport->cmd == OVS_VPORT_CMD_NEW
1187 || vport->cmd == OVS_VPORT_CMD_DEL
1188 || vport->cmd == OVS_VPORT_CMD_SET)) {
1189 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1190 dpif->dpif.full_name, vport->name, vport->cmd);
1191 sset_add(&dpif->changed_ports, vport->name);
1194 dpif->change_error = true;
1198 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1199 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1200 * positive errno value.
1202 * 'vport' will contain pointers into 'buf', so the caller should not free
1203 * 'buf' while 'vport' is still in use. */
1205 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1206 const struct ofpbuf *buf)
1208 static const struct nl_policy ovs_vport_policy[] = {
1209 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1210 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1211 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1212 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1213 .min_len = sizeof(struct ovs_vport_stats),
1214 .max_len = sizeof(struct ovs_vport_stats),
1216 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1217 .min_len = ETH_ADDR_LEN,
1218 .max_len = ETH_ADDR_LEN,
1220 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1221 [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1224 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1225 struct ovs_header *ovs_header;
1226 struct nlmsghdr *nlmsg;
1227 struct genlmsghdr *genl;
1230 dpif_linux_vport_init(vport);
1232 ofpbuf_use_const(&b, buf->data, buf->size);
1233 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1234 genl = ofpbuf_try_pull(&b, sizeof *genl);
1235 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1236 if (!nlmsg || !genl || !ovs_header
1237 || nlmsg->nlmsg_type != ovs_vport_family
1238 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1239 ARRAY_SIZE(ovs_vport_policy))) {
1243 vport->cmd = genl->cmd;
1244 vport->dp_ifindex = ovs_header->dp_ifindex;
1245 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1246 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1247 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1248 if (a[OVS_VPORT_ATTR_STATS]) {
1249 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1251 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1252 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1254 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1255 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1256 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1258 if (a[OVS_VPORT_ATTR_IFINDEX]) {
1259 vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1264 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1265 * followed by Netlink attributes corresponding to 'vport'. */
1267 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1270 struct ovs_header *ovs_header;
1272 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1275 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1276 ovs_header->dp_ifindex = vport->dp_ifindex;
1278 if (vport->port_no != UINT32_MAX) {
1279 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1282 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1283 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1287 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1291 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1292 vport->stats, sizeof *vport->stats);
1295 if (vport->address) {
1296 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1297 vport->address, ETH_ADDR_LEN);
1300 if (vport->options) {
1301 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1302 vport->options, vport->options_len);
1305 if (vport->ifindex) {
1306 nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1310 /* Clears 'vport' to "empty" values. */
1312 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1314 memset(vport, 0, sizeof *vport);
1315 vport->port_no = UINT32_MAX;
1318 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1319 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1320 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1321 * result of the command is expected to be an ovs_vport also, which is decoded
1322 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1323 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1325 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1326 struct dpif_linux_vport *reply,
1327 struct ofpbuf **bufp)
1329 struct ofpbuf *request_buf;
1332 assert((reply != NULL) == (bufp != NULL));
1334 error = dpif_linux_init();
1338 dpif_linux_vport_init(reply);
1343 request_buf = ofpbuf_new(1024);
1344 dpif_linux_vport_to_ofpbuf(request, request_buf);
1345 error = nl_sock_transact(genl_sock, request_buf, bufp);
1346 ofpbuf_delete(request_buf);
1350 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1353 dpif_linux_vport_init(reply);
1354 ofpbuf_delete(*bufp);
1361 /* Obtains information about the kernel vport named 'name' and stores it into
1362 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1363 * longer needed ('reply' will contain pointers into '*bufp'). */
1365 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1366 struct ofpbuf **bufp)
1368 struct dpif_linux_vport request;
1370 dpif_linux_vport_init(&request);
1371 request.cmd = OVS_VPORT_CMD_GET;
1372 request.name = name;
1374 return dpif_linux_vport_transact(&request, reply, bufp);
1377 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1378 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1379 * positive errno value.
1381 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1382 * while 'dp' is still in use. */
1384 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1386 static const struct nl_policy ovs_datapath_policy[] = {
1387 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1388 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1389 .min_len = sizeof(struct ovs_dp_stats),
1390 .max_len = sizeof(struct ovs_dp_stats),
1392 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1393 [OVS_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1394 [OVS_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1397 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1398 struct ovs_header *ovs_header;
1399 struct nlmsghdr *nlmsg;
1400 struct genlmsghdr *genl;
1403 dpif_linux_dp_init(dp);
1405 ofpbuf_use_const(&b, buf->data, buf->size);
1406 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1407 genl = ofpbuf_try_pull(&b, sizeof *genl);
1408 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1409 if (!nlmsg || !genl || !ovs_header
1410 || nlmsg->nlmsg_type != ovs_datapath_family
1411 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1412 ARRAY_SIZE(ovs_datapath_policy))) {
1416 dp->cmd = genl->cmd;
1417 dp->dp_ifindex = ovs_header->dp_ifindex;
1418 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1419 if (a[OVS_DP_ATTR_STATS]) {
1420 /* Can't use structure assignment because Netlink doesn't ensure
1421 * sufficient alignment for 64-bit members. */
1422 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1425 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1426 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1428 if (a[OVS_DP_ATTR_SAMPLING]) {
1429 dp->sampling = nl_attr_get(a[OVS_DP_ATTR_SAMPLING]);
1432 if (a[OVS_DP_ATTR_MCGROUPS]) {
1433 static const struct nl_policy ovs_mcgroup_policy[] = {
1434 [OVS_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1435 [OVS_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1436 [OVS_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1439 struct nlattr *mcgroups[ARRAY_SIZE(ovs_mcgroup_policy)];
1441 if (!nl_parse_nested(a[OVS_DP_ATTR_MCGROUPS], ovs_mcgroup_policy,
1442 mcgroups, ARRAY_SIZE(ovs_mcgroup_policy))) {
1446 if (mcgroups[OVS_PACKET_CMD_MISS]) {
1447 dp->mcgroups[DPIF_UC_MISS]
1448 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_MISS]);
1450 if (mcgroups[OVS_PACKET_CMD_ACTION]) {
1451 dp->mcgroups[DPIF_UC_ACTION]
1452 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_ACTION]);
1454 if (mcgroups[OVS_PACKET_CMD_SAMPLE]) {
1455 dp->mcgroups[DPIF_UC_SAMPLE]
1456 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_SAMPLE]);
1463 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1465 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1467 struct ovs_header *ovs_header;
1469 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1470 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1472 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1473 ovs_header->dp_ifindex = dp->dp_ifindex;
1476 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1479 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1481 if (dp->ipv4_frags) {
1482 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1486 nl_msg_put_u32(buf, OVS_DP_ATTR_SAMPLING, *dp->sampling);
1490 /* Clears 'dp' to "empty" values. */
1492 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1494 memset(dp, 0, sizeof *dp);
1498 dpif_linux_dp_dump_start(struct nl_dump *dump)
1500 struct dpif_linux_dp request;
1503 dpif_linux_dp_init(&request);
1504 request.cmd = OVS_DP_CMD_GET;
1506 buf = ofpbuf_new(1024);
1507 dpif_linux_dp_to_ofpbuf(&request, buf);
1508 nl_dump_start(dump, genl_sock, buf);
1512 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1513 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1514 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1515 * result of the command is expected to be of the same form, which is decoded
1516 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1517 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1519 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1520 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1522 struct ofpbuf *request_buf;
1525 assert((reply != NULL) == (bufp != NULL));
1527 request_buf = ofpbuf_new(1024);
1528 dpif_linux_dp_to_ofpbuf(request, request_buf);
1529 error = nl_sock_transact(genl_sock, request_buf, bufp);
1530 ofpbuf_delete(request_buf);
1534 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1537 dpif_linux_dp_init(reply);
1538 ofpbuf_delete(*bufp);
1545 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1546 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1547 * will contain pointers into '*bufp'). */
1549 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1550 struct ofpbuf **bufp)
1552 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1553 struct dpif_linux_dp request;
1555 dpif_linux_dp_init(&request);
1556 request.cmd = OVS_DP_CMD_GET;
1557 request.dp_ifindex = dpif->dp_ifindex;
1559 return dpif_linux_dp_transact(&request, reply, bufp);
1562 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1563 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1564 * positive errno value.
1566 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1567 * while 'flow' is still in use. */
1569 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1570 const struct ofpbuf *buf)
1572 static const struct nl_policy ovs_flow_policy[] = {
1573 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1574 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1575 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1576 .min_len = sizeof(struct ovs_flow_stats),
1577 .max_len = sizeof(struct ovs_flow_stats),
1579 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1580 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1581 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1584 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1585 struct ovs_header *ovs_header;
1586 struct nlmsghdr *nlmsg;
1587 struct genlmsghdr *genl;
1590 dpif_linux_flow_init(flow);
1592 ofpbuf_use_const(&b, buf->data, buf->size);
1593 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1594 genl = ofpbuf_try_pull(&b, sizeof *genl);
1595 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1596 if (!nlmsg || !genl || !ovs_header
1597 || nlmsg->nlmsg_type != ovs_flow_family
1598 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1599 ARRAY_SIZE(ovs_flow_policy))) {
1603 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1604 flow->dp_ifindex = ovs_header->dp_ifindex;
1605 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1606 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1607 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1608 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1609 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1611 if (a[OVS_FLOW_ATTR_STATS]) {
1612 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1614 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1615 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1617 if (a[OVS_FLOW_ATTR_USED]) {
1618 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1623 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1624 * followed by Netlink attributes corresponding to 'flow'. */
1626 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1629 struct ovs_header *ovs_header;
1631 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1632 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1635 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1636 ovs_header->dp_ifindex = flow->dp_ifindex;
1638 if (flow->key_len) {
1639 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1642 if (flow->actions || flow->actions_len) {
1643 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1644 flow->actions, flow->actions_len);
1647 /* We never need to send these to the kernel. */
1648 assert(!flow->stats);
1649 assert(!flow->tcp_flags);
1650 assert(!flow->used);
1653 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1657 /* Clears 'flow' to "empty" values. */
1659 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1661 memset(flow, 0, sizeof *flow);
1664 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1665 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1666 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1667 * result of the command is expected to be a flow also, which is decoded and
1668 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1669 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1671 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1672 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1674 struct ofpbuf *request_buf;
1677 assert((reply != NULL) == (bufp != NULL));
1679 request_buf = ofpbuf_new(1024);
1680 dpif_linux_flow_to_ofpbuf(request, request_buf);
1681 error = nl_sock_transact(genl_sock, request_buf, bufp);
1682 ofpbuf_delete(request_buf);
1686 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1689 dpif_linux_flow_init(reply);
1690 ofpbuf_delete(*bufp);
1698 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1699 struct dpif_flow_stats *stats)
1702 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1703 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1705 stats->n_packets = 0;
1708 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1709 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;