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>
34 #include <sys/epoll.h>
39 #include "dpif-provider.h"
40 #include "dynamic-string.h"
43 #include "netdev-linux.h"
44 #include "netdev-vport.h"
45 #include "netlink-notifier.h"
46 #include "netlink-socket.h"
50 #include "openvswitch/datapath-compat.h"
51 #include "openvswitch/tunnel.h"
53 #include "poll-loop.h"
57 #include "unaligned.h"
61 VLOG_DEFINE_THIS_MODULE(dpif_linux);
63 enum { LRU_MAX_PORTS = 1024 };
64 enum { LRU_MASK = LRU_MAX_PORTS - 1};
65 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
67 enum { N_UPCALL_SOCKS = 16 };
68 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
69 BUILD_ASSERT_DECL(N_UPCALL_SOCKS <= 32); /* We use a 32-bit word as a mask. */
71 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
72 * missing if we have old headers. */
73 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
75 struct dpif_linux_dp {
76 /* Generic Netlink header. */
79 /* struct ovs_header. */
83 const char *name; /* OVS_DP_ATTR_NAME. */
84 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
85 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
88 static void dpif_linux_dp_init(struct dpif_linux_dp *);
89 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
90 const struct ofpbuf *);
91 static void dpif_linux_dp_dump_start(struct nl_dump *);
92 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
93 struct dpif_linux_dp *reply,
94 struct ofpbuf **bufp);
95 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
96 struct ofpbuf **bufp);
98 struct dpif_linux_flow {
99 /* Generic Netlink header. */
102 /* struct ovs_header. */
103 unsigned int nlmsg_flags;
108 * The 'stats' member points to 64-bit data that might only be aligned on
109 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
112 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
113 * the Netlink version of the command, even if actions_len is zero. */
114 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
116 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
118 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
119 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
120 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
121 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
124 static void dpif_linux_flow_init(struct dpif_linux_flow *);
125 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
126 const struct ofpbuf *);
127 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
129 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
130 struct dpif_linux_flow *reply,
131 struct ofpbuf **bufp);
132 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
133 struct dpif_flow_stats *);
135 /* Datapath interface for the openvswitch Linux kernel module. */
140 /* Upcall messages. */
141 struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
142 uint32_t ready_mask; /* 1-bit for each sock with unread messages. */
143 int epoll_fd; /* epoll fd that includes the upcall socks. */
145 /* Change notification. */
146 struct sset changed_ports; /* Ports that have changed. */
147 struct nln_notifier *port_notifier;
150 /* Queue of unused ports. */
151 unsigned long *lru_bitmap;
152 uint16_t lru_ports[LRU_MAX_PORTS];
157 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
159 /* Generic Netlink family numbers for OVS. */
160 static int ovs_datapath_family;
161 static int ovs_vport_family;
162 static int ovs_flow_family;
163 static int ovs_packet_family;
165 /* Generic Netlink socket. */
166 static struct nl_sock *genl_sock;
167 static struct nln *nln = NULL;
169 static int dpif_linux_init(void);
170 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
171 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
172 static void dpif_linux_port_changed(const void *vport, void *dpif);
173 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint16_t port_no);
175 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
177 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
178 const struct ofpbuf *);
180 static struct dpif_linux *
181 dpif_linux_cast(const struct dpif *dpif)
183 dpif_assert_class(dpif, &dpif_linux_class);
184 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
188 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
190 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
191 bitmap_set1(dp->lru_bitmap, port);
192 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
197 dpif_linux_pop_port(struct dpif_linux *dp)
201 if (dp->lru_head == dp->lru_tail) {
205 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
206 bitmap_set0(dp->lru_bitmap, port);
211 dpif_linux_enumerate(struct sset *all_dps)
217 error = dpif_linux_init();
222 dpif_linux_dp_dump_start(&dump);
223 while (nl_dump_next(&dump, &msg)) {
224 struct dpif_linux_dp dp;
226 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
227 sset_add(all_dps, dp.name);
230 return nl_dump_done(&dump);
234 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
235 bool create, struct dpif **dpifp)
237 struct dpif_linux_dp dp_request, dp;
242 error = dpif_linux_init();
247 /* Create or look up datapath. */
248 dpif_linux_dp_init(&dp_request);
250 dp_request.cmd = OVS_DP_CMD_NEW;
252 dp_request.upcall_pid = &upcall_pid;
254 dp_request.cmd = OVS_DP_CMD_GET;
256 dp_request.name = name;
257 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
262 open_dpif(&dp, dpifp);
268 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
270 struct dpif_linux *dpif;
273 dpif = xzalloc(sizeof *dpif);
274 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
278 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
279 dp->dp_ifindex, dp->dp_ifindex);
281 dpif->dp_ifindex = dp->dp_ifindex;
282 sset_init(&dpif->changed_ports);
283 *dpifp = &dpif->dpif;
285 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
286 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
287 for (i = 1; i < LRU_MAX_PORTS; i++) {
288 dpif_linux_push_port(dpif, i);
293 destroy_upcall_socks(struct dpif_linux *dpif)
297 if (dpif->epoll_fd >= 0) {
298 close(dpif->epoll_fd);
301 for (i = 0; i < N_UPCALL_SOCKS; i++) {
302 nl_sock_destroy(dpif->upcall_socks[i]);
303 dpif->upcall_socks[i] = NULL;
308 dpif_linux_close(struct dpif *dpif_)
310 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
312 nln_notifier_destroy(dpif->port_notifier);
313 destroy_upcall_socks(dpif);
314 sset_destroy(&dpif->changed_ports);
315 free(dpif->lru_bitmap);
320 dpif_linux_destroy(struct dpif *dpif_)
322 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
323 struct dpif_linux_dp dp;
325 dpif_linux_dp_init(&dp);
326 dp.cmd = OVS_DP_CMD_DEL;
327 dp.dp_ifindex = dpif->dp_ifindex;
328 return dpif_linux_dp_transact(&dp, NULL, NULL);
332 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
340 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
348 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
350 struct dpif_linux_dp dp;
354 error = dpif_linux_dp_get(dpif_, &dp, &buf);
356 stats->n_hit = dp.stats.n_hit;
357 stats->n_missed = dp.stats.n_missed;
358 stats->n_lost = dp.stats.n_lost;
359 stats->n_flows = dp.stats.n_flows;
366 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
369 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
370 const char *name = netdev_get_name(netdev);
371 const char *type = netdev_get_type(netdev);
372 struct dpif_linux_vport request, reply;
373 const struct ofpbuf *options;
377 dpif_linux_vport_init(&request);
378 request.cmd = OVS_VPORT_CMD_NEW;
379 request.dp_ifindex = dpif->dp_ifindex;
380 request.type = netdev_vport_get_vport_type(netdev);
381 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
382 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
383 "unsupported type `%s'",
384 dpif_name(dpif_), name, type);
389 options = netdev_vport_get_options(netdev);
390 if (options && options->size) {
391 request.options = options->data;
392 request.options_len = options->size;
395 if (request.type == OVS_VPORT_TYPE_NETDEV) {
396 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
399 /* Loop until we find a port that isn't used. */
403 request.port_no = dpif_linux_pop_port(dpif);
404 upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
405 request.upcall_pid = &upcall_pid;
406 error = dpif_linux_vport_transact(&request, &reply, &buf);
409 *port_nop = reply.port_no;
410 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
411 dpif_name(dpif_), request.port_no, upcall_pid);
414 } while (request.port_no != UINT32_MAX
415 && (error == EBUSY || error == EFBIG));
421 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
423 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
424 struct dpif_linux_vport vport;
427 dpif_linux_vport_init(&vport);
428 vport.cmd = OVS_VPORT_CMD_DEL;
429 vport.dp_ifindex = dpif->dp_ifindex;
430 vport.port_no = port_no;
431 error = dpif_linux_vport_transact(&vport, NULL, NULL);
434 dpif_linux_push_port(dpif, port_no);
440 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
441 const char *port_name, struct dpif_port *dpif_port)
443 struct dpif_linux_vport request;
444 struct dpif_linux_vport reply;
448 dpif_linux_vport_init(&request);
449 request.cmd = OVS_VPORT_CMD_GET;
450 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
451 request.port_no = port_no;
452 request.name = port_name;
454 error = dpif_linux_vport_transact(&request, &reply, &buf);
456 dpif_port->name = xstrdup(reply.name);
457 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
458 dpif_port->port_no = reply.port_no;
465 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
466 struct dpif_port *dpif_port)
468 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
472 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
473 struct dpif_port *dpif_port)
475 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
479 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
481 /* If the datapath increases its range of supported ports, then it should
482 * start reporting that. */
487 dpif_linux_port_get_pid(const struct dpif *dpif_, uint16_t port_no)
489 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
491 if (dpif->epoll_fd < 0) {
494 int idx = port_no & (N_UPCALL_SOCKS - 1);
495 return nl_sock_pid(dpif->upcall_socks[idx]);
500 dpif_linux_flow_flush(struct dpif *dpif_)
502 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
503 struct dpif_linux_flow flow;
505 dpif_linux_flow_init(&flow);
506 flow.cmd = OVS_FLOW_CMD_DEL;
507 flow.dp_ifindex = dpif->dp_ifindex;
508 return dpif_linux_flow_transact(&flow, NULL, NULL);
511 struct dpif_linux_port_state {
513 unsigned long *port_bitmap; /* Ports in the datapath. */
514 bool complete; /* Dump completed without error. */
518 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
520 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
521 struct dpif_linux_port_state *state;
522 struct dpif_linux_vport request;
525 *statep = state = xmalloc(sizeof *state);
526 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
527 state->complete = false;
529 dpif_linux_vport_init(&request);
530 request.cmd = OVS_DP_CMD_GET;
531 request.dp_ifindex = dpif->dp_ifindex;
533 buf = ofpbuf_new(1024);
534 dpif_linux_vport_to_ofpbuf(&request, buf);
535 nl_dump_start(&state->dump, genl_sock, buf);
542 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
543 struct dpif_port *dpif_port)
545 struct dpif_linux_port_state *state = state_;
546 struct dpif_linux_vport vport;
550 if (!nl_dump_next(&state->dump, &buf)) {
551 state->complete = true;
555 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
560 if (vport.port_no < LRU_MAX_PORTS) {
561 bitmap_set1(state->port_bitmap, vport.port_no);
564 dpif_port->name = (char *) vport.name;
565 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
566 dpif_port->port_no = vport.port_no;
571 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
573 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
574 struct dpif_linux_port_state *state = state_;
575 int error = nl_dump_done(&state->dump);
577 if (state->complete) {
580 for (i = 0; i < LRU_MAX_PORTS; i++) {
581 if (!bitmap_is_set(state->port_bitmap, i)) {
582 dpif_linux_push_port(dpif, i);
587 free(state->port_bitmap);
593 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
595 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
597 if (dpif->change_error) {
598 dpif->change_error = false;
599 sset_clear(&dpif->changed_ports);
601 } else if (!sset_is_empty(&dpif->changed_ports)) {
602 *devnamep = sset_pop(&dpif->changed_ports);
610 dpif_linux_port_poll_wait(const struct dpif *dpif_)
612 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
613 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
614 poll_immediate_wake();
619 dpif_linux_flow_get__(const struct dpif *dpif_,
620 const struct nlattr *key, size_t key_len,
621 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
623 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
624 struct dpif_linux_flow request;
626 dpif_linux_flow_init(&request);
627 request.cmd = OVS_FLOW_CMD_GET;
628 request.dp_ifindex = dpif->dp_ifindex;
630 request.key_len = key_len;
631 return dpif_linux_flow_transact(&request, reply, bufp);
635 dpif_linux_flow_get(const struct dpif *dpif_,
636 const struct nlattr *key, size_t key_len,
637 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
639 struct dpif_linux_flow reply;
643 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
646 dpif_linux_flow_get_stats(&reply, stats);
649 buf->data = (void *) reply.actions;
650 buf->size = reply.actions_len;
660 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
661 struct dpif_linux_flow *request)
663 static struct nlattr dummy_action;
665 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
667 dpif_linux_flow_init(request);
668 request->cmd = (put->flags & DPIF_FP_CREATE
669 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
670 request->dp_ifindex = dpif->dp_ifindex;
671 request->key = put->key;
672 request->key_len = put->key_len;
673 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
674 request->actions = put->actions ? put->actions : &dummy_action;
675 request->actions_len = put->actions_len;
676 if (put->flags & DPIF_FP_ZERO_STATS) {
677 request->clear = true;
679 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
683 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
685 struct dpif_linux_flow request, reply;
689 dpif_linux_init_flow_put(dpif_, put, &request);
690 error = dpif_linux_flow_transact(&request,
691 put->stats ? &reply : NULL,
692 put->stats ? &buf : NULL);
693 if (!error && put->stats) {
694 dpif_linux_flow_get_stats(&reply, put->stats);
701 dpif_linux_flow_del(struct dpif *dpif_,
702 const struct nlattr *key, size_t key_len,
703 struct dpif_flow_stats *stats)
705 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
706 struct dpif_linux_flow request, reply;
710 dpif_linux_flow_init(&request);
711 request.cmd = OVS_FLOW_CMD_DEL;
712 request.dp_ifindex = dpif->dp_ifindex;
714 request.key_len = key_len;
715 error = dpif_linux_flow_transact(&request,
716 stats ? &reply : NULL,
717 stats ? &buf : NULL);
718 if (!error && stats) {
719 dpif_linux_flow_get_stats(&reply, stats);
725 struct dpif_linux_flow_state {
727 struct dpif_linux_flow flow;
728 struct dpif_flow_stats stats;
733 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
735 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
736 struct dpif_linux_flow_state *state;
737 struct dpif_linux_flow request;
740 *statep = state = xmalloc(sizeof *state);
742 dpif_linux_flow_init(&request);
743 request.cmd = OVS_DP_CMD_GET;
744 request.dp_ifindex = dpif->dp_ifindex;
746 buf = ofpbuf_new(1024);
747 dpif_linux_flow_to_ofpbuf(&request, buf);
748 nl_dump_start(&state->dump, genl_sock, buf);
757 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
758 const struct nlattr **key, size_t *key_len,
759 const struct nlattr **actions, size_t *actions_len,
760 const struct dpif_flow_stats **stats)
762 struct dpif_linux_flow_state *state = state_;
767 ofpbuf_delete(state->buf);
770 if (!nl_dump_next(&state->dump, &buf)) {
774 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
779 if (actions && !state->flow.actions) {
780 error = dpif_linux_flow_get__(dpif_, state->flow.key,
782 &state->flow, &state->buf);
783 if (error == ENOENT) {
784 VLOG_DBG("dumped flow disappeared on get");
786 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
792 *actions = state->flow.actions;
793 *actions_len = state->flow.actions_len;
796 *key = state->flow.key;
797 *key_len = state->flow.key_len;
800 dpif_linux_flow_get_stats(&state->flow, &state->stats);
801 *stats = &state->stats;
807 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
809 struct dpif_linux_flow_state *state = state_;
810 int error = nl_dump_done(&state->dump);
811 ofpbuf_delete(state->buf);
816 static struct ofpbuf *
817 dpif_linux_encode_execute(int dp_ifindex,
818 const struct dpif_execute *d_exec)
820 struct ovs_header *k_exec;
823 buf = ofpbuf_new(128 + d_exec->actions_len + d_exec->packet->size);
825 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
826 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
828 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
829 k_exec->dp_ifindex = dp_ifindex;
831 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
832 d_exec->packet->data, d_exec->packet->size);
833 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
834 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
835 d_exec->actions, d_exec->actions_len);
841 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
843 struct ofpbuf *request;
846 request = dpif_linux_encode_execute(dp_ifindex, execute);
847 error = nl_sock_transact(genl_sock, request, NULL);
848 ofpbuf_delete(request);
854 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
856 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
858 return dpif_linux_execute__(dpif->dp_ifindex, execute);
862 dpif_linux_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
864 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
865 struct nl_transaction **txnsp;
866 struct nl_transaction *txns;
869 txns = xmalloc(n_ops * sizeof *txns);
870 for (i = 0; i < n_ops; i++) {
871 struct nl_transaction *txn = &txns[i];
872 struct dpif_op *op = ops[i];
874 if (op->type == DPIF_OP_FLOW_PUT) {
875 struct dpif_flow_put *put = &op->u.flow_put;
876 struct dpif_linux_flow request;
878 dpif_linux_init_flow_put(dpif_, put, &request);
880 request.nlmsg_flags |= NLM_F_ECHO;
882 txn->request = ofpbuf_new(1024);
883 dpif_linux_flow_to_ofpbuf(&request, txn->request);
884 } else if (op->type == DPIF_OP_EXECUTE) {
885 struct dpif_execute *execute = &op->u.execute;
887 txn->request = dpif_linux_encode_execute(dpif->dp_ifindex,
894 txnsp = xmalloc(n_ops * sizeof *txnsp);
895 for (i = 0; i < n_ops; i++) {
899 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
903 for (i = 0; i < n_ops; i++) {
904 struct nl_transaction *txn = &txns[i];
905 struct dpif_op *op = ops[i];
907 if (op->type == DPIF_OP_FLOW_PUT) {
908 struct dpif_flow_put *put = &op->u.flow_put;
909 int error = txn->error;
911 if (!error && put->stats) {
912 struct dpif_linux_flow reply;
914 error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
916 dpif_linux_flow_get_stats(&reply, put->stats);
920 } else if (op->type == DPIF_OP_EXECUTE) {
921 op->error = txn->error;
926 ofpbuf_delete(txn->request);
927 ofpbuf_delete(txn->reply);
933 set_upcall_pids(struct dpif *dpif_)
935 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
936 struct dpif_port_dump port_dump;
937 struct dpif_port port;
940 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
941 uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
942 struct dpif_linux_vport vport_request;
944 dpif_linux_vport_init(&vport_request);
945 vport_request.cmd = OVS_VPORT_CMD_SET;
946 vport_request.dp_ifindex = dpif->dp_ifindex;
947 vport_request.port_no = port.port_no;
948 vport_request.upcall_pid = &upcall_pid;
949 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
951 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
952 dpif_name(&dpif->dpif), vport_request.port_no,
955 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
956 dpif_name(&dpif->dpif), strerror(error));
962 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
964 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
966 if ((dpif->epoll_fd >= 0) == enable) {
971 destroy_upcall_socks(dpif);
976 dpif->epoll_fd = epoll_create(N_UPCALL_SOCKS);
977 if (dpif->epoll_fd < 0) {
981 for (i = 0; i < N_UPCALL_SOCKS; i++) {
982 struct epoll_event event;
984 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
986 destroy_upcall_socks(dpif);
990 memset(&event, 0, sizeof event);
991 event.events = EPOLLIN;
993 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD,
994 nl_sock_fd(dpif->upcall_socks[i]), &event) < 0) {
996 destroy_upcall_socks(dpif);
1001 dpif->ready_mask = 0;
1004 set_upcall_pids(dpif_);
1010 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1011 uint32_t queue_id, uint32_t *priority)
1013 if (queue_id < 0xf000) {
1014 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1022 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1025 static const struct nl_policy ovs_packet_policy[] = {
1026 /* Always present. */
1027 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1028 .min_len = ETH_HEADER_LEN },
1029 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1031 /* OVS_PACKET_CMD_ACTION only. */
1032 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1035 struct ovs_header *ovs_header;
1036 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1037 struct nlmsghdr *nlmsg;
1038 struct genlmsghdr *genl;
1042 ofpbuf_use_const(&b, buf->data, buf->size);
1044 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1045 genl = ofpbuf_try_pull(&b, sizeof *genl);
1046 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1047 if (!nlmsg || !genl || !ovs_header
1048 || nlmsg->nlmsg_type != ovs_packet_family
1049 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1050 ARRAY_SIZE(ovs_packet_policy))) {
1054 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1055 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1061 memset(upcall, 0, sizeof *upcall);
1062 upcall->type = type;
1063 upcall->packet = buf;
1064 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1065 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1066 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1067 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1068 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1069 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1071 *dp_ifindex = ovs_header->dp_ifindex;
1077 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1079 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1082 if (dpif->epoll_fd < 0) {
1086 if (!dpif->ready_mask) {
1087 struct epoll_event events[N_UPCALL_SOCKS];
1092 retval = epoll_wait(dpif->epoll_fd, events, N_UPCALL_SOCKS, 0);
1093 } while (retval < 0 && errno == EINTR);
1095 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1096 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1099 for (i = 0; i < retval; i++) {
1100 dpif->ready_mask |= 1u << events[i].data.u32;
1104 while (dpif->ready_mask) {
1105 int indx = ffs(dpif->ready_mask) - 1;
1106 struct nl_sock *upcall_sock = dpif->upcall_socks[indx];
1108 dpif->ready_mask &= ~(1u << indx);
1115 if (++read_tries > 50) {
1119 error = nl_sock_recv(upcall_sock, &buf, false);
1120 if (error == EAGAIN) {
1126 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1127 if (!error && dp_ifindex == dpif->dp_ifindex) {
1142 dpif_linux_recv_wait(struct dpif *dpif_)
1144 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1146 if (dpif->epoll_fd < 0) {
1150 poll_fd_wait(dpif->epoll_fd, POLLIN);
1154 dpif_linux_recv_purge(struct dpif *dpif_)
1156 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1159 if (dpif->epoll_fd < 0) {
1163 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1164 nl_sock_drain(dpif->upcall_socks[i]);
1168 const struct dpif_class dpif_linux_class = {
1170 dpif_linux_enumerate,
1176 dpif_linux_get_stats,
1177 dpif_linux_port_add,
1178 dpif_linux_port_del,
1179 dpif_linux_port_query_by_number,
1180 dpif_linux_port_query_by_name,
1181 dpif_linux_get_max_ports,
1182 dpif_linux_port_get_pid,
1183 dpif_linux_port_dump_start,
1184 dpif_linux_port_dump_next,
1185 dpif_linux_port_dump_done,
1186 dpif_linux_port_poll,
1187 dpif_linux_port_poll_wait,
1188 dpif_linux_flow_get,
1189 dpif_linux_flow_put,
1190 dpif_linux_flow_del,
1191 dpif_linux_flow_flush,
1192 dpif_linux_flow_dump_start,
1193 dpif_linux_flow_dump_next,
1194 dpif_linux_flow_dump_done,
1197 dpif_linux_recv_set,
1198 dpif_linux_queue_to_priority,
1200 dpif_linux_recv_wait,
1201 dpif_linux_recv_purge,
1205 dpif_linux_init(void)
1207 static int error = -1;
1210 unsigned int ovs_vport_mcgroup;
1212 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1213 &ovs_datapath_family);
1215 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1216 "The Open vSwitch kernel module is probably not loaded.",
1217 OVS_DATAPATH_FAMILY);
1220 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1223 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1226 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1227 &ovs_packet_family);
1230 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1233 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1235 OVS_VPORT_MCGROUP_FALLBACK_ID);
1238 static struct dpif_linux_vport vport;
1239 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1240 dpif_linux_nln_parse, &vport);
1248 dpif_linux_is_internal_device(const char *name)
1250 struct dpif_linux_vport reply;
1254 error = dpif_linux_vport_get(name, &reply, &buf);
1257 } else if (error != ENODEV && error != ENOENT) {
1258 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1259 name, strerror(error));
1262 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1266 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1267 const void *data, size_t size)
1269 struct ofpbuf actions, key, packet;
1270 struct odputil_keybuf keybuf;
1271 struct dpif_execute execute;
1275 ofpbuf_use_const(&packet, data, size);
1276 flow_extract(&packet, 0, htonll(0), 0, &flow);
1278 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1279 odp_flow_key_from_flow(&key, &flow);
1281 ofpbuf_use_stack(&actions, &action, sizeof action);
1282 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1284 execute.key = key.data;
1285 execute.key_len = key.size;
1286 execute.actions = actions.data;
1287 execute.actions_len = actions.size;
1288 execute.packet = &packet;
1289 return dpif_linux_execute__(dp_ifindex, &execute);
1293 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1295 struct dpif_linux_vport *vport = vport_;
1296 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1300 dpif_linux_port_changed(const void *vport_, void *dpif_)
1302 const struct dpif_linux_vport *vport = vport_;
1303 struct dpif_linux *dpif = dpif_;
1306 if (vport->dp_ifindex == dpif->dp_ifindex
1307 && (vport->cmd == OVS_VPORT_CMD_NEW
1308 || vport->cmd == OVS_VPORT_CMD_DEL
1309 || vport->cmd == OVS_VPORT_CMD_SET)) {
1310 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1311 dpif->dpif.full_name, vport->name, vport->cmd);
1312 sset_add(&dpif->changed_ports, vport->name);
1315 dpif->change_error = true;
1319 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1320 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1321 * positive errno value.
1323 * 'vport' will contain pointers into 'buf', so the caller should not free
1324 * 'buf' while 'vport' is still in use. */
1326 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1327 const struct ofpbuf *buf)
1329 static const struct nl_policy ovs_vport_policy[] = {
1330 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1331 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1332 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1333 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1334 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1336 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1337 .min_len = ETH_ADDR_LEN,
1338 .max_len = ETH_ADDR_LEN,
1340 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1343 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1344 struct ovs_header *ovs_header;
1345 struct nlmsghdr *nlmsg;
1346 struct genlmsghdr *genl;
1349 dpif_linux_vport_init(vport);
1351 ofpbuf_use_const(&b, buf->data, buf->size);
1352 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1353 genl = ofpbuf_try_pull(&b, sizeof *genl);
1354 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1355 if (!nlmsg || !genl || !ovs_header
1356 || nlmsg->nlmsg_type != ovs_vport_family
1357 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1358 ARRAY_SIZE(ovs_vport_policy))) {
1362 vport->cmd = genl->cmd;
1363 vport->dp_ifindex = ovs_header->dp_ifindex;
1364 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1365 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1366 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1367 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1368 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1370 if (a[OVS_VPORT_ATTR_STATS]) {
1371 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1373 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1374 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1376 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1377 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1378 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1383 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1384 * followed by Netlink attributes corresponding to 'vport'. */
1386 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1389 struct ovs_header *ovs_header;
1391 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1392 vport->cmd, OVS_VPORT_VERSION);
1394 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1395 ovs_header->dp_ifindex = vport->dp_ifindex;
1397 if (vport->port_no != UINT32_MAX) {
1398 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1401 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1402 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1406 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1409 if (vport->upcall_pid) {
1410 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1414 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1415 vport->stats, sizeof *vport->stats);
1418 if (vport->address) {
1419 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1420 vport->address, ETH_ADDR_LEN);
1423 if (vport->options) {
1424 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1425 vport->options, vport->options_len);
1429 /* Clears 'vport' to "empty" values. */
1431 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1433 memset(vport, 0, sizeof *vport);
1434 vport->port_no = UINT32_MAX;
1437 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1438 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1439 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1440 * result of the command is expected to be an ovs_vport also, which is decoded
1441 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1442 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1444 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1445 struct dpif_linux_vport *reply,
1446 struct ofpbuf **bufp)
1448 struct ofpbuf *request_buf;
1451 assert((reply != NULL) == (bufp != NULL));
1453 error = dpif_linux_init();
1457 dpif_linux_vport_init(reply);
1462 request_buf = ofpbuf_new(1024);
1463 dpif_linux_vport_to_ofpbuf(request, request_buf);
1464 error = nl_sock_transact(genl_sock, request_buf, bufp);
1465 ofpbuf_delete(request_buf);
1469 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1472 dpif_linux_vport_init(reply);
1473 ofpbuf_delete(*bufp);
1480 /* Obtains information about the kernel vport named 'name' and stores it into
1481 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1482 * longer needed ('reply' will contain pointers into '*bufp'). */
1484 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1485 struct ofpbuf **bufp)
1487 struct dpif_linux_vport request;
1489 dpif_linux_vport_init(&request);
1490 request.cmd = OVS_VPORT_CMD_GET;
1491 request.name = name;
1493 return dpif_linux_vport_transact(&request, reply, bufp);
1496 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1497 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1498 * positive errno value.
1500 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1501 * while 'dp' is still in use. */
1503 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1505 static const struct nl_policy ovs_datapath_policy[] = {
1506 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1507 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1511 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1512 struct ovs_header *ovs_header;
1513 struct nlmsghdr *nlmsg;
1514 struct genlmsghdr *genl;
1517 dpif_linux_dp_init(dp);
1519 ofpbuf_use_const(&b, buf->data, buf->size);
1520 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1521 genl = ofpbuf_try_pull(&b, sizeof *genl);
1522 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1523 if (!nlmsg || !genl || !ovs_header
1524 || nlmsg->nlmsg_type != ovs_datapath_family
1525 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1526 ARRAY_SIZE(ovs_datapath_policy))) {
1530 dp->cmd = genl->cmd;
1531 dp->dp_ifindex = ovs_header->dp_ifindex;
1532 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1533 if (a[OVS_DP_ATTR_STATS]) {
1534 /* Can't use structure assignment because Netlink doesn't ensure
1535 * sufficient alignment for 64-bit members. */
1536 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1543 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1545 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1547 struct ovs_header *ovs_header;
1549 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1550 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1551 OVS_DATAPATH_VERSION);
1553 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1554 ovs_header->dp_ifindex = dp->dp_ifindex;
1557 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1560 if (dp->upcall_pid) {
1561 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1564 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1567 /* Clears 'dp' to "empty" values. */
1569 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1571 memset(dp, 0, sizeof *dp);
1575 dpif_linux_dp_dump_start(struct nl_dump *dump)
1577 struct dpif_linux_dp request;
1580 dpif_linux_dp_init(&request);
1581 request.cmd = OVS_DP_CMD_GET;
1583 buf = ofpbuf_new(1024);
1584 dpif_linux_dp_to_ofpbuf(&request, buf);
1585 nl_dump_start(dump, genl_sock, buf);
1589 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1590 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1591 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1592 * result of the command is expected to be of the same form, which is decoded
1593 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1594 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1596 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1597 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1599 struct ofpbuf *request_buf;
1602 assert((reply != NULL) == (bufp != NULL));
1604 request_buf = ofpbuf_new(1024);
1605 dpif_linux_dp_to_ofpbuf(request, request_buf);
1606 error = nl_sock_transact(genl_sock, request_buf, bufp);
1607 ofpbuf_delete(request_buf);
1611 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1614 dpif_linux_dp_init(reply);
1615 ofpbuf_delete(*bufp);
1622 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1623 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1624 * will contain pointers into '*bufp'). */
1626 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1627 struct ofpbuf **bufp)
1629 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1630 struct dpif_linux_dp request;
1632 dpif_linux_dp_init(&request);
1633 request.cmd = OVS_DP_CMD_GET;
1634 request.dp_ifindex = dpif->dp_ifindex;
1636 return dpif_linux_dp_transact(&request, reply, bufp);
1639 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1640 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1641 * positive errno value.
1643 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1644 * while 'flow' is still in use. */
1646 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1647 const struct ofpbuf *buf)
1649 static const struct nl_policy ovs_flow_policy[] = {
1650 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1651 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1652 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1654 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1655 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1656 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1659 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1660 struct ovs_header *ovs_header;
1661 struct nlmsghdr *nlmsg;
1662 struct genlmsghdr *genl;
1665 dpif_linux_flow_init(flow);
1667 ofpbuf_use_const(&b, buf->data, buf->size);
1668 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1669 genl = ofpbuf_try_pull(&b, sizeof *genl);
1670 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1671 if (!nlmsg || !genl || !ovs_header
1672 || nlmsg->nlmsg_type != ovs_flow_family
1673 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1674 ARRAY_SIZE(ovs_flow_policy))) {
1678 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1679 flow->dp_ifindex = ovs_header->dp_ifindex;
1680 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1681 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1682 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1683 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1684 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1686 if (a[OVS_FLOW_ATTR_STATS]) {
1687 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1689 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1690 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1692 if (a[OVS_FLOW_ATTR_USED]) {
1693 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1698 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1699 * followed by Netlink attributes corresponding to 'flow'. */
1701 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1704 struct ovs_header *ovs_header;
1706 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1707 NLM_F_REQUEST | flow->nlmsg_flags,
1708 flow->cmd, OVS_FLOW_VERSION);
1710 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1711 ovs_header->dp_ifindex = flow->dp_ifindex;
1713 if (flow->key_len) {
1714 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1717 if (flow->actions || flow->actions_len) {
1718 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1719 flow->actions, flow->actions_len);
1722 /* We never need to send these to the kernel. */
1723 assert(!flow->stats);
1724 assert(!flow->tcp_flags);
1725 assert(!flow->used);
1728 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1732 /* Clears 'flow' to "empty" values. */
1734 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1736 memset(flow, 0, sizeof *flow);
1739 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1740 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1741 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1742 * result of the command is expected to be a flow also, which is decoded and
1743 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1744 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1746 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1747 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1749 struct ofpbuf *request_buf;
1752 assert((reply != NULL) == (bufp != NULL));
1755 request->nlmsg_flags |= NLM_F_ECHO;
1758 request_buf = ofpbuf_new(1024);
1759 dpif_linux_flow_to_ofpbuf(request, request_buf);
1760 error = nl_sock_transact(genl_sock, request_buf, bufp);
1761 ofpbuf_delete(request_buf);
1765 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1768 dpif_linux_flow_init(reply);
1769 ofpbuf_delete(*bufp);
1777 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1778 struct dpif_flow_stats *stats)
1781 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1782 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1784 stats->n_packets = 0;
1787 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1788 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;