2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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);
62 enum { MAX_PORTS = USHRT_MAX };
64 enum { N_UPCALL_SOCKS = 16 };
65 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
66 BUILD_ASSERT_DECL(N_UPCALL_SOCKS <= 32); /* We use a 32-bit word as a mask. */
68 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
69 * missing if we have old headers. */
70 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
72 struct dpif_linux_dp {
73 /* Generic Netlink header. */
76 /* struct ovs_header. */
80 const char *name; /* OVS_DP_ATTR_NAME. */
81 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
82 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
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 uint32_t ready_mask; /* 1-bit for each sock with unread messages. */
140 int epoll_fd; /* epoll fd that includes the upcall socks. */
142 /* Change notification. */
143 struct sset changed_ports; /* Ports that have changed. */
144 struct nln_notifier *port_notifier;
147 /* Port number allocation. */
148 uint16_t alloc_port_no;
151 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
153 /* Generic Netlink family numbers for OVS. */
154 static int ovs_datapath_family;
155 static int ovs_vport_family;
156 static int ovs_flow_family;
157 static int ovs_packet_family;
159 /* Generic Netlink socket. */
160 static struct nl_sock *genl_sock;
161 static struct nln *nln = NULL;
163 static int dpif_linux_init(void);
164 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
165 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
166 static void dpif_linux_port_changed(const void *vport, void *dpif);
167 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint16_t port_no);
169 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
171 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
172 const struct ofpbuf *);
174 static struct dpif_linux *
175 dpif_linux_cast(const struct dpif *dpif)
177 dpif_assert_class(dpif, &dpif_linux_class);
178 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
182 dpif_linux_enumerate(struct sset *all_dps)
188 error = dpif_linux_init();
193 dpif_linux_dp_dump_start(&dump);
194 while (nl_dump_next(&dump, &msg)) {
195 struct dpif_linux_dp dp;
197 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
198 sset_add(all_dps, dp.name);
201 return nl_dump_done(&dump);
205 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
206 bool create, struct dpif **dpifp)
208 struct dpif_linux_dp dp_request, dp;
213 error = dpif_linux_init();
218 /* Create or look up datapath. */
219 dpif_linux_dp_init(&dp_request);
221 dp_request.cmd = OVS_DP_CMD_NEW;
223 dp_request.upcall_pid = &upcall_pid;
225 dp_request.cmd = OVS_DP_CMD_GET;
227 dp_request.name = name;
228 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
233 open_dpif(&dp, dpifp);
239 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
241 struct dpif_linux *dpif;
243 dpif = xzalloc(sizeof *dpif);
244 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
248 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
249 dp->dp_ifindex, dp->dp_ifindex);
251 dpif->dp_ifindex = dp->dp_ifindex;
252 sset_init(&dpif->changed_ports);
253 *dpifp = &dpif->dpif;
257 destroy_upcall_socks(struct dpif_linux *dpif)
261 if (dpif->epoll_fd >= 0) {
262 close(dpif->epoll_fd);
265 for (i = 0; i < N_UPCALL_SOCKS; i++) {
266 nl_sock_destroy(dpif->upcall_socks[i]);
267 dpif->upcall_socks[i] = NULL;
272 dpif_linux_close(struct dpif *dpif_)
274 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
276 nln_notifier_destroy(dpif->port_notifier);
277 destroy_upcall_socks(dpif);
278 sset_destroy(&dpif->changed_ports);
283 dpif_linux_destroy(struct dpif *dpif_)
285 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
286 struct dpif_linux_dp dp;
288 dpif_linux_dp_init(&dp);
289 dp.cmd = OVS_DP_CMD_DEL;
290 dp.dp_ifindex = dpif->dp_ifindex;
291 return dpif_linux_dp_transact(&dp, NULL, NULL);
295 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
303 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
311 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
313 struct dpif_linux_dp dp;
317 error = dpif_linux_dp_get(dpif_, &dp, &buf);
319 stats->n_hit = dp.stats.n_hit;
320 stats->n_missed = dp.stats.n_missed;
321 stats->n_lost = dp.stats.n_lost;
322 stats->n_flows = dp.stats.n_flows;
329 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
332 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
333 const char *name = netdev_get_name(netdev);
334 const char *type = netdev_get_type(netdev);
335 struct dpif_linux_vport request, reply;
336 const struct ofpbuf *options;
338 int error, i = 0, max_ports = MAX_PORTS;
340 dpif_linux_vport_init(&request);
341 request.cmd = OVS_VPORT_CMD_NEW;
342 request.dp_ifindex = dpif->dp_ifindex;
343 request.type = netdev_vport_get_vport_type(netdev);
344 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
345 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
346 "unsupported type `%s'",
347 dpif_name(dpif_), name, type);
352 options = netdev_vport_get_options(netdev);
353 if (options && options->size) {
354 request.options = options->data;
355 request.options_len = options->size;
358 if (request.type == OVS_VPORT_TYPE_NETDEV) {
359 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
362 /* Loop until we find a port that isn't used. */
366 request.port_no = ++dpif->alloc_port_no;
367 upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
368 request.upcall_pid = &upcall_pid;
369 error = dpif_linux_vport_transact(&request, &reply, &buf);
372 *port_nop = reply.port_no;
373 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
374 dpif_name(dpif_), request.port_no, upcall_pid);
375 } else if (error == EFBIG) {
376 /* Older datapath has lower limit. */
377 max_ports = dpif->alloc_port_no;
378 dpif->alloc_port_no = 0;
382 } while ((i++ < max_ports)
383 && (error == EBUSY || error == EFBIG));
389 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
391 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
392 struct dpif_linux_vport vport;
395 dpif_linux_vport_init(&vport);
396 vport.cmd = OVS_VPORT_CMD_DEL;
397 vport.dp_ifindex = dpif->dp_ifindex;
398 vport.port_no = port_no;
399 error = dpif_linux_vport_transact(&vport, NULL, NULL);
405 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
406 const char *port_name, struct dpif_port *dpif_port)
408 struct dpif_linux_vport request;
409 struct dpif_linux_vport reply;
413 dpif_linux_vport_init(&request);
414 request.cmd = OVS_VPORT_CMD_GET;
415 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
416 request.port_no = port_no;
417 request.name = port_name;
419 error = dpif_linux_vport_transact(&request, &reply, &buf);
421 if (reply.dp_ifindex != request.dp_ifindex) {
422 /* A query by name reported that 'port_name' is in some datapath
423 * other than 'dpif', but the caller wants to know about 'dpif'. */
426 dpif_port->name = xstrdup(reply.name);
427 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
428 dpif_port->port_no = reply.port_no;
436 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
437 struct dpif_port *dpif_port)
439 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
443 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
444 struct dpif_port *dpif_port)
446 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
450 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
456 dpif_linux_port_get_pid(const struct dpif *dpif_, uint16_t port_no)
458 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
460 if (dpif->epoll_fd < 0) {
463 int idx = port_no & (N_UPCALL_SOCKS - 1);
464 return nl_sock_pid(dpif->upcall_socks[idx]);
469 dpif_linux_flow_flush(struct dpif *dpif_)
471 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
472 struct dpif_linux_flow flow;
474 dpif_linux_flow_init(&flow);
475 flow.cmd = OVS_FLOW_CMD_DEL;
476 flow.dp_ifindex = dpif->dp_ifindex;
477 return dpif_linux_flow_transact(&flow, NULL, NULL);
480 struct dpif_linux_port_state {
485 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
487 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
488 struct dpif_linux_port_state *state;
489 struct dpif_linux_vport request;
492 *statep = state = xmalloc(sizeof *state);
494 dpif_linux_vport_init(&request);
495 request.cmd = OVS_DP_CMD_GET;
496 request.dp_ifindex = dpif->dp_ifindex;
498 buf = ofpbuf_new(1024);
499 dpif_linux_vport_to_ofpbuf(&request, buf);
500 nl_dump_start(&state->dump, genl_sock, buf);
507 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
508 struct dpif_port *dpif_port)
510 struct dpif_linux_port_state *state = state_;
511 struct dpif_linux_vport vport;
515 if (!nl_dump_next(&state->dump, &buf)) {
519 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
524 dpif_port->name = (char *) vport.name;
525 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
526 dpif_port->port_no = vport.port_no;
531 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
533 struct dpif_linux_port_state *state = state_;
534 int error = nl_dump_done(&state->dump);
541 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
543 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
545 if (dpif->change_error) {
546 dpif->change_error = false;
547 sset_clear(&dpif->changed_ports);
549 } else if (!sset_is_empty(&dpif->changed_ports)) {
550 *devnamep = sset_pop(&dpif->changed_ports);
558 dpif_linux_port_poll_wait(const struct dpif *dpif_)
560 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
561 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
562 poll_immediate_wake();
567 dpif_linux_flow_get__(const struct dpif *dpif_,
568 const struct nlattr *key, size_t key_len,
569 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
571 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
572 struct dpif_linux_flow request;
574 dpif_linux_flow_init(&request);
575 request.cmd = OVS_FLOW_CMD_GET;
576 request.dp_ifindex = dpif->dp_ifindex;
578 request.key_len = key_len;
579 return dpif_linux_flow_transact(&request, reply, bufp);
583 dpif_linux_flow_get(const struct dpif *dpif_,
584 const struct nlattr *key, size_t key_len,
585 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
587 struct dpif_linux_flow reply;
591 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
594 dpif_linux_flow_get_stats(&reply, stats);
597 buf->data = (void *) reply.actions;
598 buf->size = reply.actions_len;
608 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
609 struct dpif_linux_flow *request)
611 static struct nlattr dummy_action;
613 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
615 dpif_linux_flow_init(request);
616 request->cmd = (put->flags & DPIF_FP_CREATE
617 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
618 request->dp_ifindex = dpif->dp_ifindex;
619 request->key = put->key;
620 request->key_len = put->key_len;
621 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
622 request->actions = put->actions ? put->actions : &dummy_action;
623 request->actions_len = put->actions_len;
624 if (put->flags & DPIF_FP_ZERO_STATS) {
625 request->clear = true;
627 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
631 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
633 struct dpif_linux_flow request, reply;
637 dpif_linux_init_flow_put(dpif_, put, &request);
638 error = dpif_linux_flow_transact(&request,
639 put->stats ? &reply : NULL,
640 put->stats ? &buf : NULL);
641 if (!error && put->stats) {
642 dpif_linux_flow_get_stats(&reply, put->stats);
649 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
650 struct dpif_linux_flow *request)
652 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
654 dpif_linux_flow_init(request);
655 request->cmd = OVS_FLOW_CMD_DEL;
656 request->dp_ifindex = dpif->dp_ifindex;
657 request->key = del->key;
658 request->key_len = del->key_len;
662 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
664 struct dpif_linux_flow request, reply;
668 dpif_linux_init_flow_del(dpif_, del, &request);
669 error = dpif_linux_flow_transact(&request,
670 del->stats ? &reply : NULL,
671 del->stats ? &buf : NULL);
672 if (!error && del->stats) {
673 dpif_linux_flow_get_stats(&reply, del->stats);
679 struct dpif_linux_flow_state {
681 struct dpif_linux_flow flow;
682 struct dpif_flow_stats stats;
687 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
689 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
690 struct dpif_linux_flow_state *state;
691 struct dpif_linux_flow request;
694 *statep = state = xmalloc(sizeof *state);
696 dpif_linux_flow_init(&request);
697 request.cmd = OVS_DP_CMD_GET;
698 request.dp_ifindex = dpif->dp_ifindex;
700 buf = ofpbuf_new(1024);
701 dpif_linux_flow_to_ofpbuf(&request, buf);
702 nl_dump_start(&state->dump, genl_sock, buf);
711 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
712 const struct nlattr **key, size_t *key_len,
713 const struct nlattr **actions, size_t *actions_len,
714 const struct dpif_flow_stats **stats)
716 struct dpif_linux_flow_state *state = state_;
721 ofpbuf_delete(state->buf);
724 if (!nl_dump_next(&state->dump, &buf)) {
728 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
733 if (actions && !state->flow.actions) {
734 error = dpif_linux_flow_get__(dpif_, state->flow.key,
736 &state->flow, &state->buf);
737 if (error == ENOENT) {
738 VLOG_DBG("dumped flow disappeared on get");
740 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
746 *actions = state->flow.actions;
747 *actions_len = state->flow.actions_len;
750 *key = state->flow.key;
751 *key_len = state->flow.key_len;
754 dpif_linux_flow_get_stats(&state->flow, &state->stats);
755 *stats = &state->stats;
761 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
763 struct dpif_linux_flow_state *state = state_;
764 int error = nl_dump_done(&state->dump);
765 ofpbuf_delete(state->buf);
771 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
774 struct ovs_header *k_exec;
776 ofpbuf_prealloc_tailroom(buf, (64
777 + d_exec->packet->size
779 + d_exec->actions_len));
781 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
782 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
784 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
785 k_exec->dp_ifindex = dp_ifindex;
787 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
788 d_exec->packet->data, d_exec->packet->size);
789 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
790 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
791 d_exec->actions, d_exec->actions_len);
795 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
797 uint64_t request_stub[1024 / 8];
798 struct ofpbuf request;
801 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
802 dpif_linux_encode_execute(dp_ifindex, execute, &request);
803 error = nl_sock_transact(genl_sock, &request, NULL);
804 ofpbuf_uninit(&request);
810 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
812 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
814 return dpif_linux_execute__(dpif->dp_ifindex, execute);
820 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
822 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
825 struct nl_transaction txn;
826 struct ofpbuf request;
827 uint64_t request_stub[1024 / 8];
830 struct nl_transaction *txnsp[MAX_OPS];
833 assert(n_ops <= MAX_OPS);
834 for (i = 0; i < n_ops; i++) {
835 struct op_auxdata *aux = &auxes[i];
836 struct dpif_op *op = ops[i];
837 struct dpif_flow_put *put;
838 struct dpif_flow_del *del;
839 struct dpif_execute *execute;
840 struct dpif_linux_flow flow;
842 ofpbuf_use_stub(&aux->request,
843 aux->request_stub, sizeof aux->request_stub);
844 aux->txn.request = &aux->request;
847 case DPIF_OP_FLOW_PUT:
848 put = &op->u.flow_put;
849 dpif_linux_init_flow_put(dpif_, put, &flow);
851 flow.nlmsg_flags |= NLM_F_ECHO;
853 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
856 case DPIF_OP_FLOW_DEL:
857 del = &op->u.flow_del;
858 dpif_linux_init_flow_del(dpif_, del, &flow);
860 flow.nlmsg_flags |= NLM_F_ECHO;
862 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
865 case DPIF_OP_EXECUTE:
866 execute = &op->u.execute;
867 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
876 for (i = 0; i < n_ops; i++) {
877 txnsp[i] = &auxes[i].txn;
879 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
881 for (i = 0; i < n_ops; i++) {
882 struct nl_transaction *txn = &auxes[i].txn;
883 struct dpif_op *op = ops[i];
884 struct dpif_flow_put *put;
885 struct dpif_flow_del *del;
887 op->error = txn->error;
890 case DPIF_OP_FLOW_PUT:
891 put = &op->u.flow_put;
892 if (!op->error && put->stats) {
893 struct dpif_linux_flow reply;
895 op->error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
897 dpif_linux_flow_get_stats(&reply, put->stats);
902 case DPIF_OP_FLOW_DEL:
903 del = &op->u.flow_del;
904 if (!op->error && del->stats) {
905 struct dpif_linux_flow reply;
907 op->error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
909 dpif_linux_flow_get_stats(&reply, del->stats);
914 case DPIF_OP_EXECUTE:
921 ofpbuf_uninit(txn->request);
922 ofpbuf_delete(txn->reply);
927 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
930 size_t chunk = MIN(n_ops, MAX_OPS);
931 dpif_linux_operate__(dpif, ops, chunk);
938 set_upcall_pids(struct dpif *dpif_)
940 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
941 struct dpif_port_dump port_dump;
942 struct dpif_port port;
945 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
946 uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
947 struct dpif_linux_vport vport_request;
949 dpif_linux_vport_init(&vport_request);
950 vport_request.cmd = OVS_VPORT_CMD_SET;
951 vport_request.dp_ifindex = dpif->dp_ifindex;
952 vport_request.port_no = port.port_no;
953 vport_request.upcall_pid = &upcall_pid;
954 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
956 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
957 dpif_name(&dpif->dpif), vport_request.port_no,
960 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
961 dpif_name(&dpif->dpif), strerror(error));
967 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
969 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
971 if ((dpif->epoll_fd >= 0) == enable) {
976 destroy_upcall_socks(dpif);
981 dpif->epoll_fd = epoll_create(N_UPCALL_SOCKS);
982 if (dpif->epoll_fd < 0) {
986 for (i = 0; i < N_UPCALL_SOCKS; i++) {
987 struct epoll_event event;
989 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
991 destroy_upcall_socks(dpif);
995 memset(&event, 0, sizeof event);
996 event.events = EPOLLIN;
998 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD,
999 nl_sock_fd(dpif->upcall_socks[i]), &event) < 0) {
1001 destroy_upcall_socks(dpif);
1006 dpif->ready_mask = 0;
1009 set_upcall_pids(dpif_);
1015 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1016 uint32_t queue_id, uint32_t *priority)
1018 if (queue_id < 0xf000) {
1019 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1027 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1030 static const struct nl_policy ovs_packet_policy[] = {
1031 /* Always present. */
1032 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1033 .min_len = ETH_HEADER_LEN },
1034 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1036 /* OVS_PACKET_CMD_ACTION only. */
1037 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1040 struct ovs_header *ovs_header;
1041 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1042 struct nlmsghdr *nlmsg;
1043 struct genlmsghdr *genl;
1047 ofpbuf_use_const(&b, buf->data, buf->size);
1049 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1050 genl = ofpbuf_try_pull(&b, sizeof *genl);
1051 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1052 if (!nlmsg || !genl || !ovs_header
1053 || nlmsg->nlmsg_type != ovs_packet_family
1054 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1055 ARRAY_SIZE(ovs_packet_policy))) {
1059 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1060 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1066 memset(upcall, 0, sizeof *upcall);
1067 upcall->type = type;
1068 upcall->packet = buf;
1069 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1070 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1071 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1072 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1073 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1074 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1076 *dp_ifindex = ovs_header->dp_ifindex;
1082 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1084 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1087 if (dpif->epoll_fd < 0) {
1091 if (!dpif->ready_mask) {
1092 struct epoll_event events[N_UPCALL_SOCKS];
1097 retval = epoll_wait(dpif->epoll_fd, events, N_UPCALL_SOCKS, 0);
1098 } while (retval < 0 && errno == EINTR);
1100 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1101 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1104 for (i = 0; i < retval; i++) {
1105 dpif->ready_mask |= 1u << events[i].data.u32;
1109 while (dpif->ready_mask) {
1110 int indx = ffs(dpif->ready_mask) - 1;
1111 struct nl_sock *upcall_sock = dpif->upcall_socks[indx];
1113 dpif->ready_mask &= ~(1u << indx);
1120 if (++read_tries > 50) {
1124 error = nl_sock_recv(upcall_sock, &buf, false);
1125 if (error == EAGAIN) {
1131 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1132 if (!error && dp_ifindex == dpif->dp_ifindex) {
1147 dpif_linux_recv_wait(struct dpif *dpif_)
1149 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1151 if (dpif->epoll_fd < 0) {
1155 poll_fd_wait(dpif->epoll_fd, POLLIN);
1159 dpif_linux_recv_purge(struct dpif *dpif_)
1161 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1164 if (dpif->epoll_fd < 0) {
1168 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1169 nl_sock_drain(dpif->upcall_socks[i]);
1173 const struct dpif_class dpif_linux_class = {
1175 dpif_linux_enumerate,
1181 dpif_linux_get_stats,
1182 dpif_linux_port_add,
1183 dpif_linux_port_del,
1184 dpif_linux_port_query_by_number,
1185 dpif_linux_port_query_by_name,
1186 dpif_linux_get_max_ports,
1187 dpif_linux_port_get_pid,
1188 dpif_linux_port_dump_start,
1189 dpif_linux_port_dump_next,
1190 dpif_linux_port_dump_done,
1191 dpif_linux_port_poll,
1192 dpif_linux_port_poll_wait,
1193 dpif_linux_flow_get,
1194 dpif_linux_flow_put,
1195 dpif_linux_flow_del,
1196 dpif_linux_flow_flush,
1197 dpif_linux_flow_dump_start,
1198 dpif_linux_flow_dump_next,
1199 dpif_linux_flow_dump_done,
1202 dpif_linux_recv_set,
1203 dpif_linux_queue_to_priority,
1205 dpif_linux_recv_wait,
1206 dpif_linux_recv_purge,
1210 dpif_linux_init(void)
1212 static int error = -1;
1215 unsigned int ovs_vport_mcgroup;
1217 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1218 &ovs_datapath_family);
1220 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1221 "The Open vSwitch kernel module is probably not loaded.",
1222 OVS_DATAPATH_FAMILY);
1225 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1228 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1231 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1232 &ovs_packet_family);
1235 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1238 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1240 OVS_VPORT_MCGROUP_FALLBACK_ID);
1243 static struct dpif_linux_vport vport;
1244 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1245 dpif_linux_nln_parse, &vport);
1253 dpif_linux_is_internal_device(const char *name)
1255 struct dpif_linux_vport reply;
1259 error = dpif_linux_vport_get(name, &reply, &buf);
1262 } else if (error != ENODEV && error != ENOENT) {
1263 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1264 name, strerror(error));
1267 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1271 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1272 const void *data, size_t size)
1274 struct ofpbuf actions, key, packet;
1275 struct odputil_keybuf keybuf;
1276 struct dpif_execute execute;
1280 ofpbuf_use_const(&packet, data, size);
1281 flow_extract(&packet, 0, htonll(0), 0, &flow);
1283 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1284 odp_flow_key_from_flow(&key, &flow);
1286 ofpbuf_use_stack(&actions, &action, sizeof action);
1287 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1289 execute.key = key.data;
1290 execute.key_len = key.size;
1291 execute.actions = actions.data;
1292 execute.actions_len = actions.size;
1293 execute.packet = &packet;
1294 return dpif_linux_execute__(dp_ifindex, &execute);
1298 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1300 struct dpif_linux_vport *vport = vport_;
1301 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1305 dpif_linux_port_changed(const void *vport_, void *dpif_)
1307 const struct dpif_linux_vport *vport = vport_;
1308 struct dpif_linux *dpif = dpif_;
1311 if (vport->dp_ifindex == dpif->dp_ifindex
1312 && (vport->cmd == OVS_VPORT_CMD_NEW
1313 || vport->cmd == OVS_VPORT_CMD_DEL
1314 || vport->cmd == OVS_VPORT_CMD_SET)) {
1315 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1316 dpif->dpif.full_name, vport->name, vport->cmd);
1317 sset_add(&dpif->changed_ports, vport->name);
1320 dpif->change_error = true;
1324 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1325 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1326 * positive errno value.
1328 * 'vport' will contain pointers into 'buf', so the caller should not free
1329 * 'buf' while 'vport' is still in use. */
1331 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1332 const struct ofpbuf *buf)
1334 static const struct nl_policy ovs_vport_policy[] = {
1335 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1336 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1337 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1338 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1339 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1341 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1342 .min_len = ETH_ADDR_LEN,
1343 .max_len = ETH_ADDR_LEN,
1345 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1348 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1349 struct ovs_header *ovs_header;
1350 struct nlmsghdr *nlmsg;
1351 struct genlmsghdr *genl;
1354 dpif_linux_vport_init(vport);
1356 ofpbuf_use_const(&b, buf->data, buf->size);
1357 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1358 genl = ofpbuf_try_pull(&b, sizeof *genl);
1359 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1360 if (!nlmsg || !genl || !ovs_header
1361 || nlmsg->nlmsg_type != ovs_vport_family
1362 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1363 ARRAY_SIZE(ovs_vport_policy))) {
1367 vport->cmd = genl->cmd;
1368 vport->dp_ifindex = ovs_header->dp_ifindex;
1369 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1370 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1371 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1372 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1373 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1375 if (a[OVS_VPORT_ATTR_STATS]) {
1376 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1378 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1379 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1381 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1382 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1383 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1388 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1389 * followed by Netlink attributes corresponding to 'vport'. */
1391 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1394 struct ovs_header *ovs_header;
1396 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1397 vport->cmd, OVS_VPORT_VERSION);
1399 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1400 ovs_header->dp_ifindex = vport->dp_ifindex;
1402 if (vport->port_no != UINT32_MAX) {
1403 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1406 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1407 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1411 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1414 if (vport->upcall_pid) {
1415 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1419 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1420 vport->stats, sizeof *vport->stats);
1423 if (vport->address) {
1424 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1425 vport->address, ETH_ADDR_LEN);
1428 if (vport->options) {
1429 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1430 vport->options, vport->options_len);
1434 /* Clears 'vport' to "empty" values. */
1436 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1438 memset(vport, 0, sizeof *vport);
1439 vport->port_no = UINT32_MAX;
1442 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1443 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1444 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1445 * result of the command is expected to be an ovs_vport also, which is decoded
1446 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1447 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1449 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1450 struct dpif_linux_vport *reply,
1451 struct ofpbuf **bufp)
1453 struct ofpbuf *request_buf;
1456 assert((reply != NULL) == (bufp != NULL));
1458 error = dpif_linux_init();
1462 dpif_linux_vport_init(reply);
1467 request_buf = ofpbuf_new(1024);
1468 dpif_linux_vport_to_ofpbuf(request, request_buf);
1469 error = nl_sock_transact(genl_sock, request_buf, bufp);
1470 ofpbuf_delete(request_buf);
1474 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1477 dpif_linux_vport_init(reply);
1478 ofpbuf_delete(*bufp);
1485 /* Obtains information about the kernel vport named 'name' and stores it into
1486 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1487 * longer needed ('reply' will contain pointers into '*bufp'). */
1489 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1490 struct ofpbuf **bufp)
1492 struct dpif_linux_vport request;
1494 dpif_linux_vport_init(&request);
1495 request.cmd = OVS_VPORT_CMD_GET;
1496 request.name = name;
1498 return dpif_linux_vport_transact(&request, reply, bufp);
1501 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1502 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1503 * positive errno value.
1505 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1506 * while 'dp' is still in use. */
1508 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1510 static const struct nl_policy ovs_datapath_policy[] = {
1511 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1512 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1516 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1517 struct ovs_header *ovs_header;
1518 struct nlmsghdr *nlmsg;
1519 struct genlmsghdr *genl;
1522 dpif_linux_dp_init(dp);
1524 ofpbuf_use_const(&b, buf->data, buf->size);
1525 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1526 genl = ofpbuf_try_pull(&b, sizeof *genl);
1527 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1528 if (!nlmsg || !genl || !ovs_header
1529 || nlmsg->nlmsg_type != ovs_datapath_family
1530 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1531 ARRAY_SIZE(ovs_datapath_policy))) {
1535 dp->cmd = genl->cmd;
1536 dp->dp_ifindex = ovs_header->dp_ifindex;
1537 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1538 if (a[OVS_DP_ATTR_STATS]) {
1539 /* Can't use structure assignment because Netlink doesn't ensure
1540 * sufficient alignment for 64-bit members. */
1541 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1548 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1550 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1552 struct ovs_header *ovs_header;
1554 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1555 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1556 OVS_DATAPATH_VERSION);
1558 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1559 ovs_header->dp_ifindex = dp->dp_ifindex;
1562 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1565 if (dp->upcall_pid) {
1566 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1569 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1572 /* Clears 'dp' to "empty" values. */
1574 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1576 memset(dp, 0, sizeof *dp);
1580 dpif_linux_dp_dump_start(struct nl_dump *dump)
1582 struct dpif_linux_dp request;
1585 dpif_linux_dp_init(&request);
1586 request.cmd = OVS_DP_CMD_GET;
1588 buf = ofpbuf_new(1024);
1589 dpif_linux_dp_to_ofpbuf(&request, buf);
1590 nl_dump_start(dump, genl_sock, buf);
1594 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1595 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1596 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1597 * result of the command is expected to be of the same form, which is decoded
1598 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1599 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1601 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1602 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1604 struct ofpbuf *request_buf;
1607 assert((reply != NULL) == (bufp != NULL));
1609 request_buf = ofpbuf_new(1024);
1610 dpif_linux_dp_to_ofpbuf(request, request_buf);
1611 error = nl_sock_transact(genl_sock, request_buf, bufp);
1612 ofpbuf_delete(request_buf);
1616 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1619 dpif_linux_dp_init(reply);
1620 ofpbuf_delete(*bufp);
1627 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1628 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1629 * will contain pointers into '*bufp'). */
1631 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1632 struct ofpbuf **bufp)
1634 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1635 struct dpif_linux_dp request;
1637 dpif_linux_dp_init(&request);
1638 request.cmd = OVS_DP_CMD_GET;
1639 request.dp_ifindex = dpif->dp_ifindex;
1641 return dpif_linux_dp_transact(&request, reply, bufp);
1644 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1645 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1646 * positive errno value.
1648 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1649 * while 'flow' is still in use. */
1651 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1652 const struct ofpbuf *buf)
1654 static const struct nl_policy ovs_flow_policy[] = {
1655 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1656 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1657 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1659 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1660 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1661 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1664 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1665 struct ovs_header *ovs_header;
1666 struct nlmsghdr *nlmsg;
1667 struct genlmsghdr *genl;
1670 dpif_linux_flow_init(flow);
1672 ofpbuf_use_const(&b, buf->data, buf->size);
1673 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1674 genl = ofpbuf_try_pull(&b, sizeof *genl);
1675 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1676 if (!nlmsg || !genl || !ovs_header
1677 || nlmsg->nlmsg_type != ovs_flow_family
1678 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1679 ARRAY_SIZE(ovs_flow_policy))) {
1683 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1684 flow->dp_ifindex = ovs_header->dp_ifindex;
1685 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1686 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1687 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1688 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1689 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1691 if (a[OVS_FLOW_ATTR_STATS]) {
1692 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1694 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1695 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1697 if (a[OVS_FLOW_ATTR_USED]) {
1698 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1703 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1704 * followed by Netlink attributes corresponding to 'flow'. */
1706 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1709 struct ovs_header *ovs_header;
1711 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1712 NLM_F_REQUEST | flow->nlmsg_flags,
1713 flow->cmd, OVS_FLOW_VERSION);
1715 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1716 ovs_header->dp_ifindex = flow->dp_ifindex;
1718 if (flow->key_len) {
1719 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1722 if (flow->actions || flow->actions_len) {
1723 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1724 flow->actions, flow->actions_len);
1727 /* We never need to send these to the kernel. */
1728 assert(!flow->stats);
1729 assert(!flow->tcp_flags);
1730 assert(!flow->used);
1733 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1737 /* Clears 'flow' to "empty" values. */
1739 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1741 memset(flow, 0, sizeof *flow);
1744 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1745 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1746 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1747 * result of the command is expected to be a flow also, which is decoded and
1748 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1749 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1751 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1752 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1754 struct ofpbuf *request_buf;
1757 assert((reply != NULL) == (bufp != NULL));
1760 request->nlmsg_flags |= NLM_F_ECHO;
1763 request_buf = ofpbuf_new(1024);
1764 dpif_linux_flow_to_ofpbuf(request, request_buf);
1765 error = nl_sock_transact(genl_sock, request_buf, bufp);
1766 ofpbuf_delete(request_buf);
1770 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1773 dpif_linux_flow_init(reply);
1774 ofpbuf_delete(*bufp);
1782 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1783 struct dpif_flow_stats *stats)
1786 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1787 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1789 stats->n_packets = 0;
1792 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1793 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;