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"
54 #include "unaligned.h"
58 VLOG_DEFINE_THIS_MODULE(dpif_linux);
60 enum { LRU_MAX_PORTS = 1024 };
61 enum { LRU_MASK = LRU_MAX_PORTS - 1};
62 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
64 enum { N_UPCALL_SOCKS = 16 };
65 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
67 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
68 * missing if we have old headers. */
69 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
71 struct dpif_linux_dp {
72 /* Generic Netlink header. */
75 /* struct ovs_header. */
79 const char *name; /* OVS_DP_ATTR_NAME. */
80 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
81 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
82 enum ovs_datapath_frag ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
85 static void dpif_linux_dp_init(struct dpif_linux_dp *);
86 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
87 const struct ofpbuf *);
88 static void dpif_linux_dp_dump_start(struct nl_dump *);
89 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
90 struct dpif_linux_dp *reply,
91 struct ofpbuf **bufp);
92 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
93 struct ofpbuf **bufp);
95 struct dpif_linux_flow {
96 /* Generic Netlink header. */
99 /* struct ovs_header. */
100 unsigned int nlmsg_flags;
105 * The 'stats' member points to 64-bit data that might only be aligned on
106 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
109 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
110 * the Netlink version of the command, even if actions_len is zero. */
111 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
113 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
115 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
116 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
117 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
118 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
121 static void dpif_linux_flow_init(struct dpif_linux_flow *);
122 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
123 const struct ofpbuf *);
124 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
126 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
127 struct dpif_linux_flow *reply,
128 struct ofpbuf **bufp);
129 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
130 struct dpif_flow_stats *);
132 /* Datapath interface for the openvswitch Linux kernel module. */
137 /* Upcall messages. */
138 struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
139 int last_read_upcall;
140 unsigned int listen_mask;
142 /* Change notification. */
143 struct sset changed_ports; /* Ports that have changed. */
144 struct nln_notifier *port_notifier;
147 /* Queue of unused ports. */
148 unsigned long *lru_bitmap;
149 uint16_t lru_ports[LRU_MAX_PORTS];
154 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
156 /* Generic Netlink family numbers for OVS. */
157 static int ovs_datapath_family;
158 static int ovs_vport_family;
159 static int ovs_flow_family;
160 static int ovs_packet_family;
162 /* Generic Netlink socket. */
163 static struct nl_sock *genl_sock;
164 static struct nln *nln = NULL;
166 static int dpif_linux_init(void);
167 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
168 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
169 static void dpif_linux_port_changed(const void *vport, void *dpif);
170 static uint32_t dpif_linux_port_get_pid__(const struct dpif *,
172 enum dpif_upcall_type);
174 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
176 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
177 const struct ofpbuf *);
179 static struct dpif_linux *
180 dpif_linux_cast(const struct dpif *dpif)
182 dpif_assert_class(dpif, &dpif_linux_class);
183 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
187 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
189 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
190 bitmap_set1(dp->lru_bitmap, port);
191 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
196 dpif_linux_pop_port(struct dpif_linux *dp)
200 if (dp->lru_head == dp->lru_tail) {
204 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
205 bitmap_set0(dp->lru_bitmap, port);
210 dpif_linux_enumerate(struct sset *all_dps)
216 error = dpif_linux_init();
221 dpif_linux_dp_dump_start(&dump);
222 while (nl_dump_next(&dump, &msg)) {
223 struct dpif_linux_dp dp;
225 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
226 sset_add(all_dps, dp.name);
229 return nl_dump_done(&dump);
233 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
234 bool create, struct dpif **dpifp)
236 struct dpif_linux_dp dp_request, dp;
241 error = dpif_linux_init();
246 /* Create or look up datapath. */
247 dpif_linux_dp_init(&dp_request);
249 dp_request.cmd = OVS_DP_CMD_NEW;
251 dp_request.upcall_pid = &upcall_pid;
253 dp_request.cmd = OVS_DP_CMD_GET;
255 dp_request.name = name;
256 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
261 open_dpif(&dp, dpifp);
267 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
269 struct dpif_linux *dpif;
272 dpif = xzalloc(sizeof *dpif);
273 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
276 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
277 dp->dp_ifindex, dp->dp_ifindex);
279 dpif->dp_ifindex = dp->dp_ifindex;
280 sset_init(&dpif->changed_ports);
281 *dpifp = &dpif->dpif;
283 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
284 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
285 for (i = 1; i < LRU_MAX_PORTS; i++) {
286 dpif_linux_push_port(dpif, i);
291 destroy_upcall_socks(struct dpif_linux *dpif)
295 for (i = 0; i < N_UPCALL_SOCKS; i++) {
296 nl_sock_destroy(dpif->upcall_socks[i]);
297 dpif->upcall_socks[i] = NULL;
302 dpif_linux_close(struct dpif *dpif_)
304 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
306 nln_notifier_destroy(dpif->port_notifier);
307 destroy_upcall_socks(dpif);
308 sset_destroy(&dpif->changed_ports);
309 free(dpif->lru_bitmap);
314 dpif_linux_destroy(struct dpif *dpif_)
316 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
317 struct dpif_linux_dp dp;
319 dpif_linux_dp_init(&dp);
320 dp.cmd = OVS_DP_CMD_DEL;
321 dp.dp_ifindex = dpif->dp_ifindex;
322 return dpif_linux_dp_transact(&dp, NULL, NULL);
326 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
334 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
342 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
344 struct dpif_linux_dp dp;
348 error = dpif_linux_dp_get(dpif_, &dp, &buf);
350 stats->n_frags = dp.stats.n_frags;
351 stats->n_hit = dp.stats.n_hit;
352 stats->n_missed = dp.stats.n_missed;
353 stats->n_lost = dp.stats.n_lost;
354 stats->n_flows = dp.stats.n_flows;
361 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
363 struct dpif_linux_dp dp;
367 error = dpif_linux_dp_get(dpif_, &dp, &buf);
369 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
376 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
378 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
379 struct dpif_linux_dp dp;
381 dpif_linux_dp_init(&dp);
382 dp.cmd = OVS_DP_CMD_SET;
383 dp.dp_ifindex = dpif->dp_ifindex;
384 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
385 return dpif_linux_dp_transact(&dp, NULL, NULL);
389 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
392 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
393 const char *name = netdev_get_name(netdev);
394 const char *type = netdev_get_type(netdev);
395 struct dpif_linux_vport request, reply;
396 const struct ofpbuf *options;
400 dpif_linux_vport_init(&request);
401 request.cmd = OVS_VPORT_CMD_NEW;
402 request.dp_ifindex = dpif->dp_ifindex;
403 request.type = netdev_vport_get_vport_type(netdev);
404 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
405 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
406 "unsupported type `%s'",
407 dpif_name(dpif_), name, type);
412 options = netdev_vport_get_options(netdev);
413 if (options && options->size) {
414 request.options = options->data;
415 request.options_len = options->size;
418 if (request.type == OVS_VPORT_TYPE_NETDEV) {
419 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
422 /* Loop until we find a port that isn't used. */
426 request.port_no = dpif_linux_pop_port(dpif);
427 upcall_pid = dpif_linux_port_get_pid__(dpif_, request.port_no,
429 request.upcall_pid = &upcall_pid;
430 error = dpif_linux_vport_transact(&request, &reply, &buf);
433 *port_nop = reply.port_no;
434 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
435 dpif_name(dpif_), request.port_no, upcall_pid);
438 } while (request.port_no != UINT32_MAX
439 && (error == EBUSY || error == EFBIG));
445 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
447 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
448 struct dpif_linux_vport vport;
451 dpif_linux_vport_init(&vport);
452 vport.cmd = OVS_VPORT_CMD_DEL;
453 vport.dp_ifindex = dpif->dp_ifindex;
454 vport.port_no = port_no;
455 error = dpif_linux_vport_transact(&vport, NULL, NULL);
458 dpif_linux_push_port(dpif, port_no);
464 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
465 const char *port_name, struct dpif_port *dpif_port)
467 struct dpif_linux_vport request;
468 struct dpif_linux_vport reply;
472 dpif_linux_vport_init(&request);
473 request.cmd = OVS_VPORT_CMD_GET;
474 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
475 request.port_no = port_no;
476 request.name = port_name;
478 error = dpif_linux_vport_transact(&request, &reply, &buf);
480 dpif_port->name = xstrdup(reply.name);
481 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
482 dpif_port->port_no = reply.port_no;
489 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
490 struct dpif_port *dpif_port)
492 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
496 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
497 struct dpif_port *dpif_port)
499 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
503 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
505 /* If the datapath increases its range of supported ports, then it should
506 * start reporting that. */
511 dpif_linux_port_get_pid__(const struct dpif *dpif_, uint16_t port_no,
512 enum dpif_upcall_type upcall_type)
514 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
516 if (!(dpif->listen_mask & (1u << upcall_type))) {
519 int idx = port_no & (N_UPCALL_SOCKS - 1);
520 return nl_sock_pid(dpif->upcall_socks[idx]);
525 dpif_linux_port_get_pid(const struct dpif *dpif, uint16_t port_no)
527 return dpif_linux_port_get_pid__(dpif, port_no, DPIF_UC_ACTION);
531 dpif_linux_flow_flush(struct dpif *dpif_)
533 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
534 struct dpif_linux_flow flow;
536 dpif_linux_flow_init(&flow);
537 flow.cmd = OVS_FLOW_CMD_DEL;
538 flow.dp_ifindex = dpif->dp_ifindex;
539 return dpif_linux_flow_transact(&flow, NULL, NULL);
542 struct dpif_linux_port_state {
544 unsigned long *port_bitmap; /* Ports in the datapath. */
545 bool complete; /* Dump completed without error. */
549 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
551 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
552 struct dpif_linux_port_state *state;
553 struct dpif_linux_vport request;
556 *statep = state = xmalloc(sizeof *state);
557 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
558 state->complete = false;
560 dpif_linux_vport_init(&request);
561 request.cmd = OVS_DP_CMD_GET;
562 request.dp_ifindex = dpif->dp_ifindex;
564 buf = ofpbuf_new(1024);
565 dpif_linux_vport_to_ofpbuf(&request, buf);
566 nl_dump_start(&state->dump, genl_sock, buf);
573 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
574 struct dpif_port *dpif_port)
576 struct dpif_linux_port_state *state = state_;
577 struct dpif_linux_vport vport;
581 if (!nl_dump_next(&state->dump, &buf)) {
582 state->complete = true;
586 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
591 if (vport.port_no < LRU_MAX_PORTS) {
592 bitmap_set1(state->port_bitmap, vport.port_no);
595 dpif_port->name = (char *) vport.name;
596 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
597 dpif_port->port_no = vport.port_no;
602 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
604 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
605 struct dpif_linux_port_state *state = state_;
606 int error = nl_dump_done(&state->dump);
608 if (state->complete) {
611 for (i = 0; i < LRU_MAX_PORTS; i++) {
612 if (!bitmap_is_set(state->port_bitmap, i)) {
613 dpif_linux_push_port(dpif, i);
618 free(state->port_bitmap);
624 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
626 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
628 if (dpif->change_error) {
629 dpif->change_error = false;
630 sset_clear(&dpif->changed_ports);
632 } else if (!sset_is_empty(&dpif->changed_ports)) {
633 *devnamep = sset_pop(&dpif->changed_ports);
641 dpif_linux_port_poll_wait(const struct dpif *dpif_)
643 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
644 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
645 poll_immediate_wake();
650 dpif_linux_flow_get__(const struct dpif *dpif_,
651 const struct nlattr *key, size_t key_len,
652 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
654 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
655 struct dpif_linux_flow request;
657 dpif_linux_flow_init(&request);
658 request.cmd = OVS_FLOW_CMD_GET;
659 request.dp_ifindex = dpif->dp_ifindex;
661 request.key_len = key_len;
662 return dpif_linux_flow_transact(&request, reply, bufp);
666 dpif_linux_flow_get(const struct dpif *dpif_,
667 const struct nlattr *key, size_t key_len,
668 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
670 struct dpif_linux_flow reply;
674 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
677 dpif_linux_flow_get_stats(&reply, stats);
680 buf->data = (void *) reply.actions;
681 buf->size = reply.actions_len;
691 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
692 const struct nlattr *key, size_t key_len,
693 const struct nlattr *actions, size_t actions_len,
694 struct dpif_flow_stats *stats)
696 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
697 struct dpif_linux_flow request, reply;
698 struct nlattr dummy_action;
702 dpif_linux_flow_init(&request);
703 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
704 request.dp_ifindex = dpif->dp_ifindex;
706 request.key_len = key_len;
707 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
708 request.actions = actions ? actions : &dummy_action;
709 request.actions_len = actions_len;
710 if (flags & DPIF_FP_ZERO_STATS) {
711 request.clear = true;
713 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
714 error = dpif_linux_flow_transact(&request,
715 stats ? &reply : NULL,
716 stats ? &buf : NULL);
717 if (!error && stats) {
718 dpif_linux_flow_get_stats(&reply, stats);
725 dpif_linux_flow_del(struct dpif *dpif_,
726 const struct nlattr *key, size_t key_len,
727 struct dpif_flow_stats *stats)
729 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
730 struct dpif_linux_flow request, reply;
734 dpif_linux_flow_init(&request);
735 request.cmd = OVS_FLOW_CMD_DEL;
736 request.dp_ifindex = dpif->dp_ifindex;
738 request.key_len = key_len;
739 error = dpif_linux_flow_transact(&request,
740 stats ? &reply : NULL,
741 stats ? &buf : NULL);
742 if (!error && stats) {
743 dpif_linux_flow_get_stats(&reply, stats);
749 struct dpif_linux_flow_state {
751 struct dpif_linux_flow flow;
752 struct dpif_flow_stats stats;
757 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
759 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
760 struct dpif_linux_flow_state *state;
761 struct dpif_linux_flow request;
764 *statep = state = xmalloc(sizeof *state);
766 dpif_linux_flow_init(&request);
767 request.cmd = OVS_DP_CMD_GET;
768 request.dp_ifindex = dpif->dp_ifindex;
770 buf = ofpbuf_new(1024);
771 dpif_linux_flow_to_ofpbuf(&request, buf);
772 nl_dump_start(&state->dump, genl_sock, buf);
781 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
782 const struct nlattr **key, size_t *key_len,
783 const struct nlattr **actions, size_t *actions_len,
784 const struct dpif_flow_stats **stats)
786 struct dpif_linux_flow_state *state = state_;
791 ofpbuf_delete(state->buf);
794 if (!nl_dump_next(&state->dump, &buf)) {
798 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
803 if (actions && !state->flow.actions) {
804 error = dpif_linux_flow_get__(dpif_, state->flow.key,
806 &state->flow, &state->buf);
807 if (error == ENOENT) {
808 VLOG_DBG("dumped flow disappeared on get");
810 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
816 *actions = state->flow.actions;
817 *actions_len = state->flow.actions_len;
820 *key = state->flow.key;
821 *key_len = state->flow.key_len;
824 dpif_linux_flow_get_stats(&state->flow, &state->stats);
825 *stats = &state->stats;
831 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
833 struct dpif_linux_flow_state *state = state_;
834 int error = nl_dump_done(&state->dump);
835 ofpbuf_delete(state->buf);
841 dpif_linux_execute__(int dp_ifindex, const struct nlattr *key, size_t key_len,
842 const struct nlattr *actions, size_t actions_len,
843 const struct ofpbuf *packet)
845 struct ovs_header *execute;
849 buf = ofpbuf_new(128 + actions_len + packet->size);
851 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
852 OVS_PACKET_CMD_EXECUTE, 1);
854 execute = ofpbuf_put_uninit(buf, sizeof *execute);
855 execute->dp_ifindex = dp_ifindex;
857 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
858 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
859 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
861 error = nl_sock_transact(genl_sock, buf, NULL);
867 dpif_linux_execute(struct dpif *dpif_,
868 const struct nlattr *key, size_t key_len,
869 const struct nlattr *actions, size_t actions_len,
870 const struct ofpbuf *packet)
872 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
874 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
875 actions, actions_len, packet);
879 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
881 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
882 *listen_mask = dpif->listen_mask;
887 set_upcall_pids(struct dpif *dpif_)
889 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
890 struct dpif_port_dump port_dump;
891 struct dpif_port port;
894 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
895 uint32_t upcall_pid = dpif_linux_port_get_pid__(dpif_, port.port_no,
897 struct dpif_linux_vport vport_request;
899 dpif_linux_vport_init(&vport_request);
900 vport_request.cmd = OVS_VPORT_CMD_SET;
901 vport_request.dp_ifindex = dpif->dp_ifindex;
902 vport_request.port_no = port.port_no;
903 vport_request.upcall_pid = &upcall_pid;
904 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
906 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
907 dpif_name(&dpif->dpif), vport_request.port_no,
910 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
911 dpif_name(&dpif->dpif), strerror(error));
917 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
919 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
921 if (listen_mask == dpif->listen_mask) {
926 destroy_upcall_socks(dpif);
927 } else if (!dpif->listen_mask) {
931 for (i = 0; i < N_UPCALL_SOCKS; i++) {
932 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
934 destroy_upcall_socks(dpif);
940 dpif->listen_mask = listen_mask;
941 set_upcall_pids(dpif_);
947 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
948 uint32_t queue_id, uint32_t *priority)
950 if (queue_id < 0xf000) {
951 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
959 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
962 static const struct nl_policy ovs_packet_policy[] = {
963 /* Always present. */
964 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
965 .min_len = ETH_HEADER_LEN },
966 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
968 /* OVS_PACKET_CMD_ACTION only. */
969 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
972 struct ovs_header *ovs_header;
973 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
974 struct nlmsghdr *nlmsg;
975 struct genlmsghdr *genl;
979 ofpbuf_use_const(&b, buf->data, buf->size);
981 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
982 genl = ofpbuf_try_pull(&b, sizeof *genl);
983 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
984 if (!nlmsg || !genl || !ovs_header
985 || nlmsg->nlmsg_type != ovs_packet_family
986 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
987 ARRAY_SIZE(ovs_packet_policy))) {
991 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
992 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
998 memset(upcall, 0, sizeof *upcall);
1000 upcall->packet = buf;
1001 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1002 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1003 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1004 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1005 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1006 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1008 *dp_ifindex = ovs_header->dp_ifindex;
1014 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1016 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1020 if (!dpif->listen_mask) {
1024 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1025 struct nl_sock *upcall_sock;
1026 dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
1027 (N_UPCALL_SOCKS - 1);
1028 upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
1030 if (nl_sock_woke(upcall_sock)) {
1037 if (++read_tries > 50) {
1041 error = nl_sock_recv(upcall_sock, &buf, false);
1042 if (error == EAGAIN) {
1048 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1050 && dp_ifindex == dpif->dp_ifindex
1051 && dpif->listen_mask & (1u << upcall->type)) {
1067 dpif_linux_recv_wait(struct dpif *dpif_)
1069 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1072 if (!dpif->listen_mask) {
1076 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1077 nl_sock_wait(dpif->upcall_socks[i], POLLIN);
1082 dpif_linux_recv_purge(struct dpif *dpif_)
1084 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1087 if (!dpif->listen_mask) {
1091 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1092 nl_sock_drain(dpif->upcall_socks[i]);
1096 const struct dpif_class dpif_linux_class = {
1098 dpif_linux_enumerate,
1104 dpif_linux_get_stats,
1105 dpif_linux_get_drop_frags,
1106 dpif_linux_set_drop_frags,
1107 dpif_linux_port_add,
1108 dpif_linux_port_del,
1109 dpif_linux_port_query_by_number,
1110 dpif_linux_port_query_by_name,
1111 dpif_linux_get_max_ports,
1112 dpif_linux_port_get_pid,
1113 dpif_linux_port_dump_start,
1114 dpif_linux_port_dump_next,
1115 dpif_linux_port_dump_done,
1116 dpif_linux_port_poll,
1117 dpif_linux_port_poll_wait,
1118 dpif_linux_flow_get,
1119 dpif_linux_flow_put,
1120 dpif_linux_flow_del,
1121 dpif_linux_flow_flush,
1122 dpif_linux_flow_dump_start,
1123 dpif_linux_flow_dump_next,
1124 dpif_linux_flow_dump_done,
1126 dpif_linux_recv_get_mask,
1127 dpif_linux_recv_set_mask,
1128 dpif_linux_queue_to_priority,
1130 dpif_linux_recv_wait,
1131 dpif_linux_recv_purge,
1135 dpif_linux_init(void)
1137 static int error = -1;
1140 unsigned int ovs_vport_mcgroup;
1142 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1143 &ovs_datapath_family);
1145 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1146 "The Open vSwitch kernel module is probably not loaded.",
1147 OVS_DATAPATH_FAMILY);
1150 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1153 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1156 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1157 &ovs_packet_family);
1160 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1163 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1165 OVS_VPORT_MCGROUP_FALLBACK_ID);
1168 static struct dpif_linux_vport vport;
1169 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1170 dpif_linux_nln_parse, &vport);
1178 dpif_linux_is_internal_device(const char *name)
1180 struct dpif_linux_vport reply;
1184 error = dpif_linux_vport_get(name, &reply, &buf);
1187 } else if (error != ENODEV && error != ENOENT) {
1188 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1189 name, strerror(error));
1192 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1196 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1197 const void *data, size_t size)
1199 struct ofpbuf actions, key, packet;
1200 struct odputil_keybuf keybuf;
1204 ofpbuf_use_const(&packet, data, size);
1205 flow_extract(&packet, htonll(0), 0, &flow);
1207 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1208 odp_flow_key_from_flow(&key, &flow);
1210 ofpbuf_use_stack(&actions, &action, sizeof action);
1211 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1213 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1214 actions.data, actions.size, &packet);
1218 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1220 struct dpif_linux_vport *vport = vport_;
1221 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1225 dpif_linux_port_changed(const void *vport_, void *dpif_)
1227 const struct dpif_linux_vport *vport = vport_;
1228 struct dpif_linux *dpif = dpif_;
1231 if (vport->dp_ifindex == dpif->dp_ifindex
1232 && (vport->cmd == OVS_VPORT_CMD_NEW
1233 || vport->cmd == OVS_VPORT_CMD_DEL
1234 || vport->cmd == OVS_VPORT_CMD_SET)) {
1235 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1236 dpif->dpif.full_name, vport->name, vport->cmd);
1237 sset_add(&dpif->changed_ports, vport->name);
1240 dpif->change_error = true;
1244 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1245 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1246 * positive errno value.
1248 * 'vport' will contain pointers into 'buf', so the caller should not free
1249 * 'buf' while 'vport' is still in use. */
1251 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1252 const struct ofpbuf *buf)
1254 static const struct nl_policy ovs_vport_policy[] = {
1255 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1256 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1257 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1258 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1259 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1260 .min_len = sizeof(struct ovs_vport_stats),
1261 .max_len = sizeof(struct ovs_vport_stats),
1263 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1264 .min_len = ETH_ADDR_LEN,
1265 .max_len = ETH_ADDR_LEN,
1267 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1270 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1271 struct ovs_header *ovs_header;
1272 struct nlmsghdr *nlmsg;
1273 struct genlmsghdr *genl;
1276 dpif_linux_vport_init(vport);
1278 ofpbuf_use_const(&b, buf->data, buf->size);
1279 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1280 genl = ofpbuf_try_pull(&b, sizeof *genl);
1281 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1282 if (!nlmsg || !genl || !ovs_header
1283 || nlmsg->nlmsg_type != ovs_vport_family
1284 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1285 ARRAY_SIZE(ovs_vport_policy))) {
1289 vport->cmd = genl->cmd;
1290 vport->dp_ifindex = ovs_header->dp_ifindex;
1291 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1292 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1293 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1294 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1295 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1297 if (a[OVS_VPORT_ATTR_STATS]) {
1298 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1300 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1301 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1303 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1304 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1305 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1310 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1311 * followed by Netlink attributes corresponding to 'vport'. */
1313 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1316 struct ovs_header *ovs_header;
1318 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1321 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1322 ovs_header->dp_ifindex = vport->dp_ifindex;
1324 if (vport->port_no != UINT32_MAX) {
1325 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1328 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1329 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1333 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1336 if (vport->upcall_pid) {
1337 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1341 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1342 vport->stats, sizeof *vport->stats);
1345 if (vport->address) {
1346 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1347 vport->address, ETH_ADDR_LEN);
1350 if (vport->options) {
1351 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1352 vport->options, vport->options_len);
1356 /* Clears 'vport' to "empty" values. */
1358 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1360 memset(vport, 0, sizeof *vport);
1361 vport->port_no = UINT32_MAX;
1364 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1365 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1366 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1367 * result of the command is expected to be an ovs_vport also, which is decoded
1368 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1369 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1371 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1372 struct dpif_linux_vport *reply,
1373 struct ofpbuf **bufp)
1375 struct ofpbuf *request_buf;
1378 assert((reply != NULL) == (bufp != NULL));
1380 error = dpif_linux_init();
1384 dpif_linux_vport_init(reply);
1389 request_buf = ofpbuf_new(1024);
1390 dpif_linux_vport_to_ofpbuf(request, request_buf);
1391 error = nl_sock_transact(genl_sock, request_buf, bufp);
1392 ofpbuf_delete(request_buf);
1396 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1399 dpif_linux_vport_init(reply);
1400 ofpbuf_delete(*bufp);
1407 /* Obtains information about the kernel vport named 'name' and stores it into
1408 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1409 * longer needed ('reply' will contain pointers into '*bufp'). */
1411 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1412 struct ofpbuf **bufp)
1414 struct dpif_linux_vport request;
1416 dpif_linux_vport_init(&request);
1417 request.cmd = OVS_VPORT_CMD_GET;
1418 request.name = name;
1420 return dpif_linux_vport_transact(&request, reply, bufp);
1423 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1424 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1425 * positive errno value.
1427 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1428 * while 'dp' is still in use. */
1430 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1432 static const struct nl_policy ovs_datapath_policy[] = {
1433 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1434 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1435 .min_len = sizeof(struct ovs_dp_stats),
1436 .max_len = sizeof(struct ovs_dp_stats),
1438 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1441 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1442 struct ovs_header *ovs_header;
1443 struct nlmsghdr *nlmsg;
1444 struct genlmsghdr *genl;
1447 dpif_linux_dp_init(dp);
1449 ofpbuf_use_const(&b, buf->data, buf->size);
1450 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1451 genl = ofpbuf_try_pull(&b, sizeof *genl);
1452 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1453 if (!nlmsg || !genl || !ovs_header
1454 || nlmsg->nlmsg_type != ovs_datapath_family
1455 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1456 ARRAY_SIZE(ovs_datapath_policy))) {
1460 dp->cmd = genl->cmd;
1461 dp->dp_ifindex = ovs_header->dp_ifindex;
1462 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1463 if (a[OVS_DP_ATTR_STATS]) {
1464 /* Can't use structure assignment because Netlink doesn't ensure
1465 * sufficient alignment for 64-bit members. */
1466 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1469 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1470 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1476 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1478 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1480 struct ovs_header *ovs_header;
1482 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1483 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1485 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1486 ovs_header->dp_ifindex = dp->dp_ifindex;
1489 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1492 if (dp->upcall_pid) {
1493 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1496 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1498 if (dp->ipv4_frags) {
1499 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1503 /* Clears 'dp' to "empty" values. */
1505 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1507 memset(dp, 0, sizeof *dp);
1511 dpif_linux_dp_dump_start(struct nl_dump *dump)
1513 struct dpif_linux_dp request;
1516 dpif_linux_dp_init(&request);
1517 request.cmd = OVS_DP_CMD_GET;
1519 buf = ofpbuf_new(1024);
1520 dpif_linux_dp_to_ofpbuf(&request, buf);
1521 nl_dump_start(dump, genl_sock, buf);
1525 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1526 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1527 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1528 * result of the command is expected to be of the same form, which is decoded
1529 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1530 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1532 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1533 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1535 struct ofpbuf *request_buf;
1538 assert((reply != NULL) == (bufp != NULL));
1540 request_buf = ofpbuf_new(1024);
1541 dpif_linux_dp_to_ofpbuf(request, request_buf);
1542 error = nl_sock_transact(genl_sock, request_buf, bufp);
1543 ofpbuf_delete(request_buf);
1547 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1550 dpif_linux_dp_init(reply);
1551 ofpbuf_delete(*bufp);
1558 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1559 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1560 * will contain pointers into '*bufp'). */
1562 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1563 struct ofpbuf **bufp)
1565 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1566 struct dpif_linux_dp request;
1568 dpif_linux_dp_init(&request);
1569 request.cmd = OVS_DP_CMD_GET;
1570 request.dp_ifindex = dpif->dp_ifindex;
1572 return dpif_linux_dp_transact(&request, reply, bufp);
1575 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1576 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1577 * positive errno value.
1579 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1580 * while 'flow' is still in use. */
1582 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1583 const struct ofpbuf *buf)
1585 static const struct nl_policy ovs_flow_policy[] = {
1586 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1587 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1588 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1589 .min_len = sizeof(struct ovs_flow_stats),
1590 .max_len = sizeof(struct ovs_flow_stats),
1592 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1593 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1594 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1597 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1598 struct ovs_header *ovs_header;
1599 struct nlmsghdr *nlmsg;
1600 struct genlmsghdr *genl;
1603 dpif_linux_flow_init(flow);
1605 ofpbuf_use_const(&b, buf->data, buf->size);
1606 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1607 genl = ofpbuf_try_pull(&b, sizeof *genl);
1608 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1609 if (!nlmsg || !genl || !ovs_header
1610 || nlmsg->nlmsg_type != ovs_flow_family
1611 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1612 ARRAY_SIZE(ovs_flow_policy))) {
1616 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1617 flow->dp_ifindex = ovs_header->dp_ifindex;
1618 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1619 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1620 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1621 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1622 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1624 if (a[OVS_FLOW_ATTR_STATS]) {
1625 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1627 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1628 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1630 if (a[OVS_FLOW_ATTR_USED]) {
1631 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1636 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1637 * followed by Netlink attributes corresponding to 'flow'. */
1639 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1642 struct ovs_header *ovs_header;
1644 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1645 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1648 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1649 ovs_header->dp_ifindex = flow->dp_ifindex;
1651 if (flow->key_len) {
1652 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1655 if (flow->actions || flow->actions_len) {
1656 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1657 flow->actions, flow->actions_len);
1660 /* We never need to send these to the kernel. */
1661 assert(!flow->stats);
1662 assert(!flow->tcp_flags);
1663 assert(!flow->used);
1666 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1670 /* Clears 'flow' to "empty" values. */
1672 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1674 memset(flow, 0, sizeof *flow);
1677 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1678 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1679 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1680 * result of the command is expected to be a flow also, which is decoded and
1681 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1682 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1684 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1685 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1687 struct ofpbuf *request_buf;
1690 assert((reply != NULL) == (bufp != NULL));
1692 request_buf = ofpbuf_new(1024);
1693 dpif_linux_flow_to_ofpbuf(request, request_buf);
1694 error = nl_sock_transact(genl_sock, request_buf, bufp);
1695 ofpbuf_delete(request_buf);
1699 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1702 dpif_linux_flow_init(reply);
1703 ofpbuf_delete(*bufp);
1711 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1712 struct dpif_flow_stats *stats)
1715 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1716 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1718 stats->n_packets = 0;
1721 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1722 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;