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 uint32_t upcall_pid; /* OVS_DP_UPCALL_PID. */
77 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
78 enum ovs_frag_handling ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
79 const uint32_t *sampling; /* OVS_DP_ATTR_SAMPLING. */
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 uint32_t upcall_pid; /* OVS_FLOW_ATTR_UPCALL_PID. */
113 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
114 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
115 const uint64_t *used; /* OVS_FLOW_ATTR_USED. */
116 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
119 static void dpif_linux_flow_init(struct dpif_linux_flow *);
120 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
121 const struct ofpbuf *);
122 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
124 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
125 struct dpif_linux_flow *reply,
126 struct ofpbuf **bufp);
127 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
128 struct dpif_flow_stats *);
130 /* Datapath interface for the openvswitch Linux kernel module. */
135 /* Upcall messages. */
136 struct nl_sock *upcall_sock;
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->upcall_sock = NULL;
267 dpif->listen_mask = 0;
268 dpif->dp_ifindex = dp->dp_ifindex;
269 sset_init(&dpif->changed_ports);
270 dpif->change_error = false;
271 *dpifp = &dpif->dpif;
273 dpif->lru_head = dpif->lru_tail = 0;
274 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
275 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
276 for (i = 1; i < LRU_MAX_PORTS; i++) {
277 dpif_linux_push_port(dpif, i);
282 dpif_linux_close(struct dpif *dpif_)
284 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
286 nln_notifier_destroy(dpif->port_notifier);
287 nl_sock_destroy(dpif->upcall_sock);
288 sset_destroy(&dpif->changed_ports);
289 free(dpif->lru_bitmap);
294 dpif_linux_destroy(struct dpif *dpif_)
296 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
297 struct dpif_linux_dp dp;
299 dpif_linux_dp_init(&dp);
300 dp.cmd = OVS_DP_CMD_DEL;
301 dp.dp_ifindex = dpif->dp_ifindex;
302 return dpif_linux_dp_transact(&dp, NULL, NULL);
306 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
314 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
322 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
324 struct dpif_linux_dp dp;
328 error = dpif_linux_dp_get(dpif_, &dp, &buf);
337 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
339 struct dpif_linux_dp dp;
343 error = dpif_linux_dp_get(dpif_, &dp, &buf);
345 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
352 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
354 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
355 struct dpif_linux_dp dp;
357 dpif_linux_dp_init(&dp);
358 dp.cmd = OVS_DP_CMD_SET;
359 dp.dp_ifindex = dpif->dp_ifindex;
360 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
361 return dpif_linux_dp_transact(&dp, NULL, NULL);
365 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
368 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
369 const char *name = netdev_get_name(netdev);
370 const char *type = netdev_get_type(netdev);
371 struct dpif_linux_vport request, reply;
372 const struct ofpbuf *options;
376 dpif_linux_vport_init(&request);
377 request.cmd = OVS_VPORT_CMD_NEW;
378 request.dp_ifindex = dpif->dp_ifindex;
379 request.type = netdev_vport_get_vport_type(netdev);
380 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
381 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
382 "unsupported type `%s'",
383 dpif_name(dpif_), name, type);
388 options = netdev_vport_get_options(netdev);
389 if (options && options->size) {
390 request.options = options->data;
391 request.options_len = options->size;
394 if (request.type == OVS_VPORT_TYPE_NETDEV) {
395 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
398 /* Loop until we find a port that isn't used. */
400 request.port_no = dpif_linux_pop_port(dpif);
401 if (dpif->upcall_sock) {
402 request.upcall_pid = nl_sock_pid(dpif->upcall_sock);
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 (dpif->upcall_sock) {
663 request.upcall_pid = nl_sock_pid(dpif->upcall_sock);
665 if (flags & DPIF_FP_ZERO_STATS) {
666 request.clear = true;
668 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
669 error = dpif_linux_flow_transact(&request,
670 stats ? &reply : NULL,
671 stats ? &buf : NULL);
672 if (!error && stats) {
673 dpif_linux_flow_get_stats(&reply, stats);
680 dpif_linux_flow_del(struct dpif *dpif_,
681 const struct nlattr *key, size_t key_len,
682 struct dpif_flow_stats *stats)
684 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
685 struct dpif_linux_flow request, reply;
689 dpif_linux_flow_init(&request);
690 request.cmd = OVS_FLOW_CMD_DEL;
691 request.dp_ifindex = dpif->dp_ifindex;
693 request.key_len = key_len;
694 error = dpif_linux_flow_transact(&request,
695 stats ? &reply : NULL,
696 stats ? &buf : NULL);
697 if (!error && stats) {
698 dpif_linux_flow_get_stats(&reply, stats);
704 struct dpif_linux_flow_state {
706 struct dpif_linux_flow flow;
707 struct dpif_flow_stats stats;
712 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
714 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
715 struct dpif_linux_flow_state *state;
716 struct dpif_linux_flow request;
719 *statep = state = xmalloc(sizeof *state);
721 dpif_linux_flow_init(&request);
722 request.cmd = OVS_DP_CMD_GET;
723 request.dp_ifindex = dpif->dp_ifindex;
725 buf = ofpbuf_new(1024);
726 dpif_linux_flow_to_ofpbuf(&request, buf);
727 nl_dump_start(&state->dump, genl_sock, buf);
736 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
737 const struct nlattr **key, size_t *key_len,
738 const struct nlattr **actions, size_t *actions_len,
739 const struct dpif_flow_stats **stats)
741 struct dpif_linux_flow_state *state = state_;
746 ofpbuf_delete(state->buf);
749 if (!nl_dump_next(&state->dump, &buf)) {
753 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
758 if (actions && !state->flow.actions) {
759 error = dpif_linux_flow_get__(dpif_, state->flow.key,
761 &state->flow, &state->buf);
762 if (error == ENOENT) {
763 VLOG_DBG("dumped flow disappeared on get");
765 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
771 *actions = state->flow.actions;
772 *actions_len = state->flow.actions_len;
775 *key = state->flow.key;
776 *key_len = state->flow.key_len;
779 dpif_linux_flow_get_stats(&state->flow, &state->stats);
780 *stats = &state->stats;
786 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
788 struct dpif_linux_flow_state *state = state_;
789 int error = nl_dump_done(&state->dump);
790 ofpbuf_delete(state->buf);
796 dpif_linux_execute__(int dp_ifindex, uint32_t upcall_pid,
797 const struct nlattr *key, size_t key_len,
798 const struct nlattr *actions, size_t actions_len,
799 const struct ofpbuf *packet)
801 struct ovs_header *execute;
805 buf = ofpbuf_new(128 + actions_len + packet->size);
807 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
808 OVS_PACKET_CMD_EXECUTE, 1);
810 execute = ofpbuf_put_uninit(buf, sizeof *execute);
811 execute->dp_ifindex = dp_ifindex;
813 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
814 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
815 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
816 nl_msg_put_u32(buf, OVS_PACKET_ATTR_UPCALL_PID, upcall_pid);
818 error = nl_sock_transact(genl_sock, buf, NULL);
824 dpif_linux_execute(struct dpif *dpif_,
825 const struct nlattr *key, size_t key_len,
826 const struct nlattr *actions, size_t actions_len,
827 const struct ofpbuf *packet)
829 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
830 uint32_t upcall_pid = 0;
832 if (dpif->upcall_sock) {
833 upcall_pid = nl_sock_pid(dpif->upcall_sock);
836 return dpif_linux_execute__(dpif->dp_ifindex, upcall_pid, key, key_len,
837 actions, actions_len, packet);
841 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
843 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
844 *listen_mask = dpif->listen_mask;
849 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
851 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
854 if (listen_mask == dpif->listen_mask) {
856 } else if (!listen_mask) {
857 nl_sock_destroy(dpif->upcall_sock);
858 dpif->upcall_sock = NULL;
859 } else if (!dpif->upcall_sock) {
860 struct dpif_port port;
861 struct dpif_port_dump port_dump;
862 struct dpif_flow_dump flow_dump;
863 const struct nlattr *key;
866 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_sock);
871 DPIF_PORT_FOR_EACH (&port, &port_dump, dpif_) {
872 struct dpif_linux_vport vport_request;
874 dpif_linux_vport_init(&vport_request);
875 vport_request.cmd = OVS_VPORT_CMD_SET;
876 vport_request.dp_ifindex = dpif->dp_ifindex;
877 vport_request.port_no = port.port_no;
878 vport_request.upcall_pid = nl_sock_pid(dpif->upcall_sock);
879 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
881 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on "
882 "port: %s", dpif_name(dpif_), strerror(error));
886 dpif_flow_dump_start(&flow_dump, dpif_);
887 while (dpif_flow_dump_next(&flow_dump, &key, &key_len,
889 struct dpif_linux_flow flow_request;
891 dpif_linux_flow_init(&flow_request);
892 flow_request.cmd = OVS_FLOW_CMD_SET;
893 flow_request.dp_ifindex = dpif->dp_ifindex;
894 flow_request.key = key;
895 flow_request.key_len = key_len;
896 flow_request.upcall_pid = nl_sock_pid(dpif->upcall_sock);
897 error = dpif_linux_flow_transact(&flow_request, NULL, NULL);
899 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on "
900 "flow: %s", dpif_name(dpif_), strerror(error));
903 dpif_flow_dump_done(&flow_dump);
906 dpif->listen_mask = listen_mask;
911 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
912 uint32_t *probability)
914 struct dpif_linux_dp dp;
918 error = dpif_linux_dp_get(dpif_, &dp, &buf);
920 *probability = dp.sampling ? *dp.sampling : 0;
927 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
929 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
930 struct dpif_linux_dp dp;
932 dpif_linux_dp_init(&dp);
933 dp.cmd = OVS_DP_CMD_SET;
934 dp.dp_ifindex = dpif->dp_ifindex;
935 dp.sampling = &probability;
936 return dpif_linux_dp_transact(&dp, NULL, NULL);
940 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
941 uint32_t queue_id, uint32_t *priority)
943 if (queue_id < 0xf000) {
944 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
952 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
955 static const struct nl_policy ovs_packet_policy[] = {
956 /* Always present. */
957 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
958 .min_len = ETH_HEADER_LEN },
959 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
961 /* OVS_PACKET_CMD_ACTION only. */
962 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
964 /* OVS_PACKET_CMD_SAMPLE only. */
965 [OVS_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
966 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
969 struct ovs_header *ovs_header;
970 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
971 struct nlmsghdr *nlmsg;
972 struct genlmsghdr *genl;
976 ofpbuf_use_const(&b, buf->data, buf->size);
978 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
979 genl = ofpbuf_try_pull(&b, sizeof *genl);
980 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
981 if (!nlmsg || !genl || !ovs_header
982 || nlmsg->nlmsg_type != ovs_packet_family
983 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
984 ARRAY_SIZE(ovs_packet_policy))) {
988 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
989 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
990 : genl->cmd == OVS_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
996 memset(upcall, 0, sizeof *upcall);
998 upcall->packet = buf;
999 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1000 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1001 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1002 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1003 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1004 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1006 upcall->sample_pool = (a[OVS_PACKET_ATTR_SAMPLE_POOL]
1007 ? nl_attr_get_u32(a[OVS_PACKET_ATTR_SAMPLE_POOL])
1009 if (a[OVS_PACKET_ATTR_ACTIONS]) {
1010 upcall->actions = (void *) nl_attr_get(a[OVS_PACKET_ATTR_ACTIONS]);
1011 upcall->actions_len = nl_attr_get_size(a[OVS_PACKET_ATTR_ACTIONS]);
1014 *dp_ifindex = ovs_header->dp_ifindex;
1020 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1022 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1027 if (!dpif->upcall_sock) {
1031 for (i = 0; i < 50; i++) {
1034 error = nl_sock_recv(dpif->upcall_sock, &buf, false);
1039 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1041 && dp_ifindex == dpif->dp_ifindex
1042 && dpif->listen_mask & (1u << upcall->type)) {
1056 dpif_linux_recv_wait(struct dpif *dpif_)
1058 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1059 if (dpif->upcall_sock) {
1060 nl_sock_wait(dpif->upcall_sock, POLLIN);
1065 dpif_linux_recv_purge(struct dpif *dpif_)
1067 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1069 if (dpif->upcall_sock) {
1070 nl_sock_drain(dpif->upcall_sock);
1074 const struct dpif_class dpif_linux_class = {
1076 dpif_linux_enumerate,
1082 dpif_linux_get_stats,
1083 dpif_linux_get_drop_frags,
1084 dpif_linux_set_drop_frags,
1085 dpif_linux_port_add,
1086 dpif_linux_port_del,
1087 dpif_linux_port_query_by_number,
1088 dpif_linux_port_query_by_name,
1089 dpif_linux_get_max_ports,
1090 dpif_linux_port_dump_start,
1091 dpif_linux_port_dump_next,
1092 dpif_linux_port_dump_done,
1093 dpif_linux_port_poll,
1094 dpif_linux_port_poll_wait,
1095 dpif_linux_flow_get,
1096 dpif_linux_flow_put,
1097 dpif_linux_flow_del,
1098 dpif_linux_flow_flush,
1099 dpif_linux_flow_dump_start,
1100 dpif_linux_flow_dump_next,
1101 dpif_linux_flow_dump_done,
1103 dpif_linux_recv_get_mask,
1104 dpif_linux_recv_set_mask,
1105 dpif_linux_get_sflow_probability,
1106 dpif_linux_set_sflow_probability,
1107 dpif_linux_queue_to_priority,
1109 dpif_linux_recv_wait,
1110 dpif_linux_recv_purge,
1114 dpif_linux_init(void)
1116 static int error = -1;
1119 unsigned int ovs_vport_mcgroup;
1121 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1122 &ovs_datapath_family);
1124 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1125 "The Open vSwitch kernel module is probably not loaded.",
1126 OVS_DATAPATH_FAMILY);
1129 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1132 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1135 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1136 &ovs_packet_family);
1139 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1142 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1144 OVS_VPORT_MCGROUP_FALLBACK_ID);
1147 static struct dpif_linux_vport vport;
1148 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1149 dpif_linux_nln_parse, &vport);
1157 dpif_linux_is_internal_device(const char *name)
1159 struct dpif_linux_vport reply;
1163 error = dpif_linux_vport_get(name, &reply, &buf);
1166 } else if (error != ENODEV && error != ENOENT) {
1167 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1168 name, strerror(error));
1171 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1175 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1176 const void *data, size_t size)
1178 struct ofpbuf actions, key, packet;
1179 struct odputil_keybuf keybuf;
1183 ofpbuf_use_const(&packet, data, size);
1184 flow_extract(&packet, htonll(0), 0, &flow);
1186 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1187 odp_flow_key_from_flow(&key, &flow);
1189 ofpbuf_use_stack(&actions, &action, sizeof action);
1190 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1192 return dpif_linux_execute__(dp_ifindex, 0, key.data, key.size,
1193 actions.data, actions.size, &packet);
1197 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1199 struct dpif_linux_vport *vport = vport_;
1200 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1204 dpif_linux_port_changed(const void *vport_, void *dpif_)
1206 const struct dpif_linux_vport *vport = vport_;
1207 struct dpif_linux *dpif = dpif_;
1210 if (vport->dp_ifindex == dpif->dp_ifindex
1211 && (vport->cmd == OVS_VPORT_CMD_NEW
1212 || vport->cmd == OVS_VPORT_CMD_DEL
1213 || vport->cmd == OVS_VPORT_CMD_SET)) {
1214 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1215 dpif->dpif.full_name, vport->name, vport->cmd);
1216 sset_add(&dpif->changed_ports, vport->name);
1219 dpif->change_error = true;
1223 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1224 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1225 * positive errno value.
1227 * 'vport' will contain pointers into 'buf', so the caller should not free
1228 * 'buf' while 'vport' is still in use. */
1230 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1231 const struct ofpbuf *buf)
1233 static const struct nl_policy ovs_vport_policy[] = {
1234 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1235 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1236 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1237 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1238 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1239 .min_len = sizeof(struct ovs_vport_stats),
1240 .max_len = sizeof(struct ovs_vport_stats),
1242 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1243 .min_len = ETH_ADDR_LEN,
1244 .max_len = ETH_ADDR_LEN,
1246 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1247 [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1250 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1251 struct ovs_header *ovs_header;
1252 struct nlmsghdr *nlmsg;
1253 struct genlmsghdr *genl;
1256 dpif_linux_vport_init(vport);
1258 ofpbuf_use_const(&b, buf->data, buf->size);
1259 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1260 genl = ofpbuf_try_pull(&b, sizeof *genl);
1261 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1262 if (!nlmsg || !genl || !ovs_header
1263 || nlmsg->nlmsg_type != ovs_vport_family
1264 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1265 ARRAY_SIZE(ovs_vport_policy))) {
1269 vport->cmd = genl->cmd;
1270 vport->dp_ifindex = ovs_header->dp_ifindex;
1271 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1272 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1273 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1274 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1275 vport->upcall_pid = nl_attr_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1277 if (a[OVS_VPORT_ATTR_STATS]) {
1278 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1280 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1281 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1283 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1284 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1285 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1287 if (a[OVS_VPORT_ATTR_IFINDEX]) {
1288 vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1293 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1294 * followed by Netlink attributes corresponding to 'vport'. */
1296 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1299 struct ovs_header *ovs_header;
1301 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1304 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1305 ovs_header->dp_ifindex = vport->dp_ifindex;
1307 if (vport->port_no != UINT32_MAX) {
1308 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1311 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1312 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1316 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1319 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1322 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1323 vport->stats, sizeof *vport->stats);
1326 if (vport->address) {
1327 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1328 vport->address, ETH_ADDR_LEN);
1331 if (vport->options) {
1332 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1333 vport->options, vport->options_len);
1336 if (vport->ifindex) {
1337 nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1341 /* Clears 'vport' to "empty" values. */
1343 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1345 memset(vport, 0, sizeof *vport);
1346 vport->port_no = UINT32_MAX;
1349 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1350 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1351 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1352 * result of the command is expected to be an ovs_vport also, which is decoded
1353 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1354 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1356 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1357 struct dpif_linux_vport *reply,
1358 struct ofpbuf **bufp)
1360 struct ofpbuf *request_buf;
1363 assert((reply != NULL) == (bufp != NULL));
1365 error = dpif_linux_init();
1369 dpif_linux_vport_init(reply);
1374 request_buf = ofpbuf_new(1024);
1375 dpif_linux_vport_to_ofpbuf(request, request_buf);
1376 error = nl_sock_transact(genl_sock, request_buf, bufp);
1377 ofpbuf_delete(request_buf);
1381 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1384 dpif_linux_vport_init(reply);
1385 ofpbuf_delete(*bufp);
1392 /* Obtains information about the kernel vport named 'name' and stores it into
1393 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1394 * longer needed ('reply' will contain pointers into '*bufp'). */
1396 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1397 struct ofpbuf **bufp)
1399 struct dpif_linux_vport request;
1401 dpif_linux_vport_init(&request);
1402 request.cmd = OVS_VPORT_CMD_GET;
1403 request.name = name;
1405 return dpif_linux_vport_transact(&request, reply, bufp);
1408 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1409 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1410 * positive errno value.
1412 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1413 * while 'dp' is still in use. */
1415 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1417 static const struct nl_policy ovs_datapath_policy[] = {
1418 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1419 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1420 .min_len = sizeof(struct ovs_dp_stats),
1421 .max_len = sizeof(struct ovs_dp_stats),
1423 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1424 [OVS_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1427 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1428 struct ovs_header *ovs_header;
1429 struct nlmsghdr *nlmsg;
1430 struct genlmsghdr *genl;
1433 dpif_linux_dp_init(dp);
1435 ofpbuf_use_const(&b, buf->data, buf->size);
1436 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1437 genl = ofpbuf_try_pull(&b, sizeof *genl);
1438 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1439 if (!nlmsg || !genl || !ovs_header
1440 || nlmsg->nlmsg_type != ovs_datapath_family
1441 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1442 ARRAY_SIZE(ovs_datapath_policy))) {
1446 dp->cmd = genl->cmd;
1447 dp->dp_ifindex = ovs_header->dp_ifindex;
1448 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1449 if (a[OVS_DP_ATTR_STATS]) {
1450 /* Can't use structure assignment because Netlink doesn't ensure
1451 * sufficient alignment for 64-bit members. */
1452 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1455 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1456 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1458 if (a[OVS_DP_ATTR_SAMPLING]) {
1459 dp->sampling = nl_attr_get(a[OVS_DP_ATTR_SAMPLING]);
1465 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1467 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1469 struct ovs_header *ovs_header;
1471 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1472 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1474 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1475 ovs_header->dp_ifindex = dp->dp_ifindex;
1478 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1481 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, dp->upcall_pid);
1483 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1485 if (dp->ipv4_frags) {
1486 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1490 nl_msg_put_u32(buf, OVS_DP_ATTR_SAMPLING, *dp->sampling);
1494 /* Clears 'dp' to "empty" values. */
1496 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1498 memset(dp, 0, sizeof *dp);
1502 dpif_linux_dp_dump_start(struct nl_dump *dump)
1504 struct dpif_linux_dp request;
1507 dpif_linux_dp_init(&request);
1508 request.cmd = OVS_DP_CMD_GET;
1510 buf = ofpbuf_new(1024);
1511 dpif_linux_dp_to_ofpbuf(&request, buf);
1512 nl_dump_start(dump, genl_sock, buf);
1516 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1517 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1518 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1519 * result of the command is expected to be of the same form, which is decoded
1520 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1521 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1523 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1524 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1526 struct ofpbuf *request_buf;
1529 assert((reply != NULL) == (bufp != NULL));
1531 request_buf = ofpbuf_new(1024);
1532 dpif_linux_dp_to_ofpbuf(request, request_buf);
1533 error = nl_sock_transact(genl_sock, request_buf, bufp);
1534 ofpbuf_delete(request_buf);
1538 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1541 dpif_linux_dp_init(reply);
1542 ofpbuf_delete(*bufp);
1549 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1550 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1551 * will contain pointers into '*bufp'). */
1553 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1554 struct ofpbuf **bufp)
1556 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1557 struct dpif_linux_dp request;
1559 dpif_linux_dp_init(&request);
1560 request.cmd = OVS_DP_CMD_GET;
1561 request.dp_ifindex = dpif->dp_ifindex;
1563 return dpif_linux_dp_transact(&request, reply, bufp);
1566 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1567 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1568 * positive errno value.
1570 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1571 * while 'flow' is still in use. */
1573 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1574 const struct ofpbuf *buf)
1576 static const struct nl_policy ovs_flow_policy[] = {
1577 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1578 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1579 [OVS_FLOW_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1580 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1581 .min_len = sizeof(struct ovs_flow_stats),
1582 .max_len = sizeof(struct ovs_flow_stats),
1584 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1585 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1586 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1589 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1590 struct ovs_header *ovs_header;
1591 struct nlmsghdr *nlmsg;
1592 struct genlmsghdr *genl;
1595 dpif_linux_flow_init(flow);
1597 ofpbuf_use_const(&b, buf->data, buf->size);
1598 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1599 genl = ofpbuf_try_pull(&b, sizeof *genl);
1600 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1601 if (!nlmsg || !genl || !ovs_header
1602 || nlmsg->nlmsg_type != ovs_flow_family
1603 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1604 ARRAY_SIZE(ovs_flow_policy))) {
1608 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1609 flow->dp_ifindex = ovs_header->dp_ifindex;
1610 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1611 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1612 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1613 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1614 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1616 if (a[OVS_FLOW_ATTR_UPCALL_PID]) {
1617 flow->upcall_pid = nl_attr_get_u32(a[OVS_FLOW_ATTR_UPCALL_PID]);
1619 if (a[OVS_FLOW_ATTR_STATS]) {
1620 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1622 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1623 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1625 if (a[OVS_FLOW_ATTR_USED]) {
1626 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1631 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1632 * followed by Netlink attributes corresponding to 'flow'. */
1634 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1637 struct ovs_header *ovs_header;
1639 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1640 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1643 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1644 ovs_header->dp_ifindex = flow->dp_ifindex;
1646 if (flow->key_len) {
1647 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1650 if (flow->actions || flow->actions_len) {
1651 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1652 flow->actions, flow->actions_len);
1655 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UPCALL_PID, flow->upcall_pid);
1657 /* We never need to send these to the kernel. */
1658 assert(!flow->stats);
1659 assert(!flow->tcp_flags);
1660 assert(!flow->used);
1663 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1667 /* Clears 'flow' to "empty" values. */
1669 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1671 memset(flow, 0, sizeof *flow);
1674 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1675 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1676 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1677 * result of the command is expected to be a flow also, which is decoded and
1678 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1679 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1681 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1682 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1684 struct ofpbuf *request_buf;
1687 assert((reply != NULL) == (bufp != NULL));
1689 request_buf = ofpbuf_new(1024);
1690 dpif_linux_flow_to_ofpbuf(request, request_buf);
1691 error = nl_sock_transact(genl_sock, request_buf, bufp);
1692 ofpbuf_delete(request_buf);
1696 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1699 dpif_linux_flow_init(reply);
1700 ofpbuf_delete(*bufp);
1708 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1709 struct dpif_flow_stats *stats)
1712 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1713 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1715 stats->n_packets = 0;
1718 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1719 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;