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(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_init_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_linux_flow *request)
696 static struct nlattr dummy_action;
698 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
700 dpif_linux_flow_init(request);
701 request->cmd = (flags & DPIF_FP_CREATE
702 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
703 request->dp_ifindex = dpif->dp_ifindex;
705 request->key_len = key_len;
706 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
707 request->actions = actions ? actions : &dummy_action;
708 request->actions_len = actions_len;
709 if (flags & DPIF_FP_ZERO_STATS) {
710 request->clear = true;
712 request->nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
716 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
717 const struct nlattr *key, size_t key_len,
718 const struct nlattr *actions, size_t actions_len,
719 struct dpif_flow_stats *stats)
721 struct dpif_linux_flow request, reply;
725 dpif_linux_init_flow_put(dpif_, flags, key, key_len, actions, actions_len,
727 error = dpif_linux_flow_transact(&request,
728 stats ? &reply : NULL,
729 stats ? &buf : NULL);
730 if (!error && stats) {
731 dpif_linux_flow_get_stats(&reply, stats);
738 dpif_linux_flow_del(struct dpif *dpif_,
739 const struct nlattr *key, size_t key_len,
740 struct dpif_flow_stats *stats)
742 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
743 struct dpif_linux_flow request, reply;
747 dpif_linux_flow_init(&request);
748 request.cmd = OVS_FLOW_CMD_DEL;
749 request.dp_ifindex = dpif->dp_ifindex;
751 request.key_len = key_len;
752 error = dpif_linux_flow_transact(&request,
753 stats ? &reply : NULL,
754 stats ? &buf : NULL);
755 if (!error && stats) {
756 dpif_linux_flow_get_stats(&reply, stats);
762 struct dpif_linux_flow_state {
764 struct dpif_linux_flow flow;
765 struct dpif_flow_stats stats;
770 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
772 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
773 struct dpif_linux_flow_state *state;
774 struct dpif_linux_flow request;
777 *statep = state = xmalloc(sizeof *state);
779 dpif_linux_flow_init(&request);
780 request.cmd = OVS_DP_CMD_GET;
781 request.dp_ifindex = dpif->dp_ifindex;
783 buf = ofpbuf_new(1024);
784 dpif_linux_flow_to_ofpbuf(&request, buf);
785 nl_dump_start(&state->dump, genl_sock, buf);
794 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
795 const struct nlattr **key, size_t *key_len,
796 const struct nlattr **actions, size_t *actions_len,
797 const struct dpif_flow_stats **stats)
799 struct dpif_linux_flow_state *state = state_;
804 ofpbuf_delete(state->buf);
807 if (!nl_dump_next(&state->dump, &buf)) {
811 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
816 if (actions && !state->flow.actions) {
817 error = dpif_linux_flow_get__(dpif_, state->flow.key,
819 &state->flow, &state->buf);
820 if (error == ENOENT) {
821 VLOG_DBG("dumped flow disappeared on get");
823 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
829 *actions = state->flow.actions;
830 *actions_len = state->flow.actions_len;
833 *key = state->flow.key;
834 *key_len = state->flow.key_len;
837 dpif_linux_flow_get_stats(&state->flow, &state->stats);
838 *stats = &state->stats;
844 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
846 struct dpif_linux_flow_state *state = state_;
847 int error = nl_dump_done(&state->dump);
848 ofpbuf_delete(state->buf);
853 static struct ofpbuf *
854 dpif_linux_encode_execute(int dp_ifindex,
855 const struct nlattr *key, size_t key_len,
856 const struct nlattr *actions, size_t actions_len,
857 const struct ofpbuf *packet)
859 struct ovs_header *execute;
862 buf = ofpbuf_new(128 + actions_len + packet->size);
864 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
865 OVS_PACKET_CMD_EXECUTE, 1);
867 execute = ofpbuf_put_uninit(buf, sizeof *execute);
868 execute->dp_ifindex = dp_ifindex;
870 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
871 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
872 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
878 dpif_linux_execute__(int dp_ifindex, const struct nlattr *key, size_t key_len,
879 const struct nlattr *actions, size_t actions_len,
880 const struct ofpbuf *packet)
882 struct ofpbuf *request;
885 request = dpif_linux_encode_execute(dp_ifindex,
886 key, key_len, actions, actions_len,
888 error = nl_sock_transact(genl_sock, request, NULL);
889 ofpbuf_delete(request);
895 dpif_linux_execute(struct dpif *dpif_,
896 const struct nlattr *key, size_t key_len,
897 const struct nlattr *actions, size_t actions_len,
898 const struct ofpbuf *packet)
900 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
902 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
903 actions, actions_len, packet);
907 dpif_linux_operate(struct dpif *dpif_, union dpif_op **ops, size_t n_ops)
909 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
910 struct nl_transaction **txnsp;
911 struct nl_transaction *txns;
914 txns = xmalloc(n_ops * sizeof *txns);
915 for (i = 0; i < n_ops; i++) {
916 struct nl_transaction *txn = &txns[i];
917 union dpif_op *op = ops[i];
919 if (op->type == DPIF_OP_FLOW_PUT) {
920 struct dpif_flow_put *put = &op->flow_put;
921 struct dpif_linux_flow request;
923 dpif_linux_init_flow_put(dpif_, put->flags, put->key, put->key_len,
924 put->actions, put->actions_len,
927 request.nlmsg_flags |= NLM_F_ECHO;
929 txn->request = ofpbuf_new(1024);
930 dpif_linux_flow_to_ofpbuf(&request, txn->request);
931 } else if (op->type == DPIF_OP_EXECUTE) {
932 struct dpif_execute *execute = &op->execute;
934 txn->request = dpif_linux_encode_execute(
935 dpif->dp_ifindex, execute->key, execute->key_len,
936 execute->actions, execute->actions_len, execute->packet);
942 txnsp = xmalloc(n_ops * sizeof *txnsp);
943 for (i = 0; i < n_ops; i++) {
947 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
951 for (i = 0; i < n_ops; i++) {
952 struct nl_transaction *txn = &txns[i];
953 union dpif_op *op = ops[i];
955 if (op->type == DPIF_OP_FLOW_PUT) {
956 struct dpif_flow_put *put = &op->flow_put;
957 int error = txn->error;
959 if (!error && put->stats) {
960 struct dpif_linux_flow reply;
962 error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
964 dpif_linux_flow_get_stats(&reply, put->stats);
968 } else if (op->type == DPIF_OP_EXECUTE) {
969 struct dpif_execute *execute = &op->execute;
971 execute->error = txn->error;
976 ofpbuf_delete(txn->request);
977 ofpbuf_delete(txn->reply);
983 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
985 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
986 *listen_mask = dpif->listen_mask;
991 set_upcall_pids(struct dpif *dpif_)
993 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
994 struct dpif_port_dump port_dump;
995 struct dpif_port port;
998 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
999 uint32_t upcall_pid = dpif_linux_port_get_pid__(dpif_, port.port_no,
1001 struct dpif_linux_vport vport_request;
1003 dpif_linux_vport_init(&vport_request);
1004 vport_request.cmd = OVS_VPORT_CMD_SET;
1005 vport_request.dp_ifindex = dpif->dp_ifindex;
1006 vport_request.port_no = port.port_no;
1007 vport_request.upcall_pid = &upcall_pid;
1008 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1010 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1011 dpif_name(&dpif->dpif), vport_request.port_no,
1014 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
1015 dpif_name(&dpif->dpif), strerror(error));
1021 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
1023 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1025 if (listen_mask == dpif->listen_mask) {
1030 destroy_upcall_socks(dpif);
1031 } else if (!dpif->listen_mask) {
1035 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1036 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
1038 destroy_upcall_socks(dpif);
1044 dpif->listen_mask = listen_mask;
1045 set_upcall_pids(dpif_);
1051 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1052 uint32_t queue_id, uint32_t *priority)
1054 if (queue_id < 0xf000) {
1055 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1063 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1066 static const struct nl_policy ovs_packet_policy[] = {
1067 /* Always present. */
1068 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1069 .min_len = ETH_HEADER_LEN },
1070 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1072 /* OVS_PACKET_CMD_ACTION only. */
1073 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1076 struct ovs_header *ovs_header;
1077 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1078 struct nlmsghdr *nlmsg;
1079 struct genlmsghdr *genl;
1083 ofpbuf_use_const(&b, buf->data, buf->size);
1085 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1086 genl = ofpbuf_try_pull(&b, sizeof *genl);
1087 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1088 if (!nlmsg || !genl || !ovs_header
1089 || nlmsg->nlmsg_type != ovs_packet_family
1090 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1091 ARRAY_SIZE(ovs_packet_policy))) {
1095 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1096 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1102 memset(upcall, 0, sizeof *upcall);
1103 upcall->type = type;
1104 upcall->packet = buf;
1105 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1106 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1107 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1108 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1109 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1110 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1112 *dp_ifindex = ovs_header->dp_ifindex;
1118 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1120 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1124 if (!dpif->listen_mask) {
1128 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1129 struct nl_sock *upcall_sock;
1130 dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
1131 (N_UPCALL_SOCKS - 1);
1132 upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
1134 if (nl_sock_woke(upcall_sock)) {
1141 if (++read_tries > 50) {
1145 error = nl_sock_recv(upcall_sock, &buf, false);
1146 if (error == EAGAIN) {
1152 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1154 && dp_ifindex == dpif->dp_ifindex
1155 && dpif->listen_mask & (1u << upcall->type)) {
1171 dpif_linux_recv_wait(struct dpif *dpif_)
1173 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1176 if (!dpif->listen_mask) {
1180 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1181 nl_sock_wait(dpif->upcall_socks[i], POLLIN);
1186 dpif_linux_recv_purge(struct dpif *dpif_)
1188 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1191 if (!dpif->listen_mask) {
1195 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1196 nl_sock_drain(dpif->upcall_socks[i]);
1200 const struct dpif_class dpif_linux_class = {
1202 dpif_linux_enumerate,
1208 dpif_linux_get_stats,
1209 dpif_linux_get_drop_frags,
1210 dpif_linux_set_drop_frags,
1211 dpif_linux_port_add,
1212 dpif_linux_port_del,
1213 dpif_linux_port_query_by_number,
1214 dpif_linux_port_query_by_name,
1215 dpif_linux_get_max_ports,
1216 dpif_linux_port_get_pid,
1217 dpif_linux_port_dump_start,
1218 dpif_linux_port_dump_next,
1219 dpif_linux_port_dump_done,
1220 dpif_linux_port_poll,
1221 dpif_linux_port_poll_wait,
1222 dpif_linux_flow_get,
1223 dpif_linux_flow_put,
1224 dpif_linux_flow_del,
1225 dpif_linux_flow_flush,
1226 dpif_linux_flow_dump_start,
1227 dpif_linux_flow_dump_next,
1228 dpif_linux_flow_dump_done,
1231 dpif_linux_recv_get_mask,
1232 dpif_linux_recv_set_mask,
1233 dpif_linux_queue_to_priority,
1235 dpif_linux_recv_wait,
1236 dpif_linux_recv_purge,
1240 dpif_linux_init(void)
1242 static int error = -1;
1245 unsigned int ovs_vport_mcgroup;
1247 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1248 &ovs_datapath_family);
1250 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1251 "The Open vSwitch kernel module is probably not loaded.",
1252 OVS_DATAPATH_FAMILY);
1255 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1258 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1261 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1262 &ovs_packet_family);
1265 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1268 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1270 OVS_VPORT_MCGROUP_FALLBACK_ID);
1273 static struct dpif_linux_vport vport;
1274 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1275 dpif_linux_nln_parse, &vport);
1283 dpif_linux_is_internal_device(const char *name)
1285 struct dpif_linux_vport reply;
1289 error = dpif_linux_vport_get(name, &reply, &buf);
1292 } else if (error != ENODEV && error != ENOENT) {
1293 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1294 name, strerror(error));
1297 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1301 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1302 const void *data, size_t size)
1304 struct ofpbuf actions, key, packet;
1305 struct odputil_keybuf keybuf;
1309 ofpbuf_use_const(&packet, data, size);
1310 flow_extract(&packet, htonll(0), 0, &flow);
1312 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1313 odp_flow_key_from_flow(&key, &flow);
1315 ofpbuf_use_stack(&actions, &action, sizeof action);
1316 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1318 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1319 actions.data, actions.size, &packet);
1323 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1325 struct dpif_linux_vport *vport = vport_;
1326 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1330 dpif_linux_port_changed(const void *vport_, void *dpif_)
1332 const struct dpif_linux_vport *vport = vport_;
1333 struct dpif_linux *dpif = dpif_;
1336 if (vport->dp_ifindex == dpif->dp_ifindex
1337 && (vport->cmd == OVS_VPORT_CMD_NEW
1338 || vport->cmd == OVS_VPORT_CMD_DEL
1339 || vport->cmd == OVS_VPORT_CMD_SET)) {
1340 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1341 dpif->dpif.full_name, vport->name, vport->cmd);
1342 sset_add(&dpif->changed_ports, vport->name);
1345 dpif->change_error = true;
1349 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1350 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1351 * positive errno value.
1353 * 'vport' will contain pointers into 'buf', so the caller should not free
1354 * 'buf' while 'vport' is still in use. */
1356 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1357 const struct ofpbuf *buf)
1359 static const struct nl_policy ovs_vport_policy[] = {
1360 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1361 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1362 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1363 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1364 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1365 .min_len = sizeof(struct ovs_vport_stats),
1366 .max_len = sizeof(struct ovs_vport_stats),
1368 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1369 .min_len = ETH_ADDR_LEN,
1370 .max_len = ETH_ADDR_LEN,
1372 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1375 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1376 struct ovs_header *ovs_header;
1377 struct nlmsghdr *nlmsg;
1378 struct genlmsghdr *genl;
1381 dpif_linux_vport_init(vport);
1383 ofpbuf_use_const(&b, buf->data, buf->size);
1384 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1385 genl = ofpbuf_try_pull(&b, sizeof *genl);
1386 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1387 if (!nlmsg || !genl || !ovs_header
1388 || nlmsg->nlmsg_type != ovs_vport_family
1389 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1390 ARRAY_SIZE(ovs_vport_policy))) {
1394 vport->cmd = genl->cmd;
1395 vport->dp_ifindex = ovs_header->dp_ifindex;
1396 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1397 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1398 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1399 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1400 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1402 if (a[OVS_VPORT_ATTR_STATS]) {
1403 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1405 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1406 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1408 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1409 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1410 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1415 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1416 * followed by Netlink attributes corresponding to 'vport'. */
1418 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1421 struct ovs_header *ovs_header;
1423 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1426 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1427 ovs_header->dp_ifindex = vport->dp_ifindex;
1429 if (vport->port_no != UINT32_MAX) {
1430 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1433 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1434 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1438 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1441 if (vport->upcall_pid) {
1442 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1446 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1447 vport->stats, sizeof *vport->stats);
1450 if (vport->address) {
1451 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1452 vport->address, ETH_ADDR_LEN);
1455 if (vport->options) {
1456 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1457 vport->options, vport->options_len);
1461 /* Clears 'vport' to "empty" values. */
1463 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1465 memset(vport, 0, sizeof *vport);
1466 vport->port_no = UINT32_MAX;
1469 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1470 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1471 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1472 * result of the command is expected to be an ovs_vport also, which is decoded
1473 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1474 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1476 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1477 struct dpif_linux_vport *reply,
1478 struct ofpbuf **bufp)
1480 struct ofpbuf *request_buf;
1483 assert((reply != NULL) == (bufp != NULL));
1485 error = dpif_linux_init();
1489 dpif_linux_vport_init(reply);
1494 request_buf = ofpbuf_new(1024);
1495 dpif_linux_vport_to_ofpbuf(request, request_buf);
1496 error = nl_sock_transact(genl_sock, request_buf, bufp);
1497 ofpbuf_delete(request_buf);
1501 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1504 dpif_linux_vport_init(reply);
1505 ofpbuf_delete(*bufp);
1512 /* Obtains information about the kernel vport named 'name' and stores it into
1513 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1514 * longer needed ('reply' will contain pointers into '*bufp'). */
1516 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1517 struct ofpbuf **bufp)
1519 struct dpif_linux_vport request;
1521 dpif_linux_vport_init(&request);
1522 request.cmd = OVS_VPORT_CMD_GET;
1523 request.name = name;
1525 return dpif_linux_vport_transact(&request, reply, bufp);
1528 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1529 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1530 * positive errno value.
1532 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1533 * while 'dp' is still in use. */
1535 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1537 static const struct nl_policy ovs_datapath_policy[] = {
1538 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1539 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1540 .min_len = sizeof(struct ovs_dp_stats),
1541 .max_len = sizeof(struct ovs_dp_stats),
1543 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1546 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1547 struct ovs_header *ovs_header;
1548 struct nlmsghdr *nlmsg;
1549 struct genlmsghdr *genl;
1552 dpif_linux_dp_init(dp);
1554 ofpbuf_use_const(&b, buf->data, buf->size);
1555 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1556 genl = ofpbuf_try_pull(&b, sizeof *genl);
1557 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1558 if (!nlmsg || !genl || !ovs_header
1559 || nlmsg->nlmsg_type != ovs_datapath_family
1560 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1561 ARRAY_SIZE(ovs_datapath_policy))) {
1565 dp->cmd = genl->cmd;
1566 dp->dp_ifindex = ovs_header->dp_ifindex;
1567 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1568 if (a[OVS_DP_ATTR_STATS]) {
1569 /* Can't use structure assignment because Netlink doesn't ensure
1570 * sufficient alignment for 64-bit members. */
1571 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1574 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1575 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1581 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1583 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1585 struct ovs_header *ovs_header;
1587 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1588 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1590 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1591 ovs_header->dp_ifindex = dp->dp_ifindex;
1594 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1597 if (dp->upcall_pid) {
1598 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1601 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1603 if (dp->ipv4_frags) {
1604 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1608 /* Clears 'dp' to "empty" values. */
1610 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1612 memset(dp, 0, sizeof *dp);
1616 dpif_linux_dp_dump_start(struct nl_dump *dump)
1618 struct dpif_linux_dp request;
1621 dpif_linux_dp_init(&request);
1622 request.cmd = OVS_DP_CMD_GET;
1624 buf = ofpbuf_new(1024);
1625 dpif_linux_dp_to_ofpbuf(&request, buf);
1626 nl_dump_start(dump, genl_sock, buf);
1630 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1631 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1632 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1633 * result of the command is expected to be of the same form, which is decoded
1634 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1635 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1637 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1638 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1640 struct ofpbuf *request_buf;
1643 assert((reply != NULL) == (bufp != NULL));
1645 request_buf = ofpbuf_new(1024);
1646 dpif_linux_dp_to_ofpbuf(request, request_buf);
1647 error = nl_sock_transact(genl_sock, request_buf, bufp);
1648 ofpbuf_delete(request_buf);
1652 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1655 dpif_linux_dp_init(reply);
1656 ofpbuf_delete(*bufp);
1663 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1664 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1665 * will contain pointers into '*bufp'). */
1667 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1668 struct ofpbuf **bufp)
1670 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1671 struct dpif_linux_dp request;
1673 dpif_linux_dp_init(&request);
1674 request.cmd = OVS_DP_CMD_GET;
1675 request.dp_ifindex = dpif->dp_ifindex;
1677 return dpif_linux_dp_transact(&request, reply, bufp);
1680 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1681 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1682 * positive errno value.
1684 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1685 * while 'flow' is still in use. */
1687 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1688 const struct ofpbuf *buf)
1690 static const struct nl_policy ovs_flow_policy[] = {
1691 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1692 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1693 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1694 .min_len = sizeof(struct ovs_flow_stats),
1695 .max_len = sizeof(struct ovs_flow_stats),
1697 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1698 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1699 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1702 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1703 struct ovs_header *ovs_header;
1704 struct nlmsghdr *nlmsg;
1705 struct genlmsghdr *genl;
1708 dpif_linux_flow_init(flow);
1710 ofpbuf_use_const(&b, buf->data, buf->size);
1711 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1712 genl = ofpbuf_try_pull(&b, sizeof *genl);
1713 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1714 if (!nlmsg || !genl || !ovs_header
1715 || nlmsg->nlmsg_type != ovs_flow_family
1716 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1717 ARRAY_SIZE(ovs_flow_policy))) {
1721 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1722 flow->dp_ifindex = ovs_header->dp_ifindex;
1723 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1724 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1725 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1726 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1727 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1729 if (a[OVS_FLOW_ATTR_STATS]) {
1730 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1732 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1733 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1735 if (a[OVS_FLOW_ATTR_USED]) {
1736 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1741 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1742 * followed by Netlink attributes corresponding to 'flow'. */
1744 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1747 struct ovs_header *ovs_header;
1749 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1750 NLM_F_REQUEST | flow->nlmsg_flags,
1753 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1754 ovs_header->dp_ifindex = flow->dp_ifindex;
1756 if (flow->key_len) {
1757 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1760 if (flow->actions || flow->actions_len) {
1761 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1762 flow->actions, flow->actions_len);
1765 /* We never need to send these to the kernel. */
1766 assert(!flow->stats);
1767 assert(!flow->tcp_flags);
1768 assert(!flow->used);
1771 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1775 /* Clears 'flow' to "empty" values. */
1777 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1779 memset(flow, 0, sizeof *flow);
1782 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1783 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1784 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1785 * result of the command is expected to be a flow also, which is decoded and
1786 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1787 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1789 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1790 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1792 struct ofpbuf *request_buf;
1795 assert((reply != NULL) == (bufp != NULL));
1798 request->nlmsg_flags |= NLM_F_ECHO;
1801 request_buf = ofpbuf_new(1024);
1802 dpif_linux_flow_to_ofpbuf(request, request_buf);
1803 error = nl_sock_transact(genl_sock, request_buf, bufp);
1804 ofpbuf_delete(request_buf);
1808 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1811 dpif_linux_flow_init(reply);
1812 ofpbuf_delete(*bufp);
1820 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1821 struct dpif_flow_stats *stats)
1824 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1825 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1827 stats->n_packets = 0;
1830 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1831 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;