2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 = 17 };
65 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS - 1));
66 BUILD_ASSERT_DECL(N_UPCALL_SOCKS > 1);
67 BUILD_ASSERT_DECL(N_UPCALL_SOCKS <= 32); /* We use a 32-bit word as a mask. */
69 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
70 * missing if we have old headers. */
71 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
73 struct dpif_linux_dp {
74 /* Generic Netlink header. */
77 /* struct ovs_header. */
81 const char *name; /* OVS_DP_ATTR_NAME. */
82 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
83 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
86 static void dpif_linux_dp_init(struct dpif_linux_dp *);
87 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
88 const struct ofpbuf *);
89 static void dpif_linux_dp_dump_start(struct nl_dump *);
90 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
91 struct dpif_linux_dp *reply,
92 struct ofpbuf **bufp);
93 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
94 struct ofpbuf **bufp);
96 struct dpif_linux_flow {
97 /* Generic Netlink header. */
100 /* struct ovs_header. */
101 unsigned int nlmsg_flags;
106 * The 'stats' member points to 64-bit data that might only be aligned on
107 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
110 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
111 * the Netlink version of the command, even if actions_len is zero. */
112 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
114 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
116 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
117 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
118 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
119 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
122 static void dpif_linux_flow_init(struct dpif_linux_flow *);
123 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
124 const struct ofpbuf *);
125 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
127 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
128 struct dpif_linux_flow *reply,
129 struct ofpbuf **bufp);
130 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
131 struct dpif_flow_stats *);
133 /* Datapath interface for the openvswitch Linux kernel module. */
138 /* Upcall messages. */
139 struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
140 uint32_t ready_mask; /* 1-bit for each sock with unread messages. */
141 int epoll_fd; /* epoll fd that includes the upcall socks. */
143 /* Change notification. */
144 struct sset changed_ports; /* Ports that have changed. */
145 struct nln_notifier *port_notifier;
148 /* Port number allocation. */
149 uint16_t alloc_port_no;
152 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
154 /* Generic Netlink family numbers for OVS. */
155 static int ovs_datapath_family;
156 static int ovs_vport_family;
157 static int ovs_flow_family;
158 static int ovs_packet_family;
160 /* Generic Netlink socket. */
161 static struct nl_sock *genl_sock;
162 static struct nln *nln = NULL;
164 static int dpif_linux_init(void);
165 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
166 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
167 static void dpif_linux_port_changed(const void *vport, void *dpif);
168 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint16_t port_no);
170 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
172 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
173 const struct ofpbuf *);
175 static struct dpif_linux *
176 dpif_linux_cast(const struct dpif *dpif)
178 dpif_assert_class(dpif, &dpif_linux_class);
179 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
183 dpif_linux_enumerate(struct sset *all_dps)
189 error = dpif_linux_init();
194 dpif_linux_dp_dump_start(&dump);
195 while (nl_dump_next(&dump, &msg)) {
196 struct dpif_linux_dp dp;
198 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
199 sset_add(all_dps, dp.name);
202 return nl_dump_done(&dump);
206 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
207 bool create, struct dpif **dpifp)
209 struct dpif_linux_dp dp_request, dp;
214 error = dpif_linux_init();
219 /* Create or look up datapath. */
220 dpif_linux_dp_init(&dp_request);
222 dp_request.cmd = OVS_DP_CMD_NEW;
224 dp_request.upcall_pid = &upcall_pid;
226 dp_request.cmd = OVS_DP_CMD_GET;
228 dp_request.name = name;
229 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
234 open_dpif(&dp, dpifp);
240 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
242 struct dpif_linux *dpif;
244 dpif = xzalloc(sizeof *dpif);
245 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
249 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
250 dp->dp_ifindex, dp->dp_ifindex);
252 dpif->dp_ifindex = dp->dp_ifindex;
253 sset_init(&dpif->changed_ports);
254 *dpifp = &dpif->dpif;
258 destroy_upcall_socks(struct dpif_linux *dpif)
262 if (dpif->epoll_fd >= 0) {
263 close(dpif->epoll_fd);
266 for (i = 0; i < N_UPCALL_SOCKS; i++) {
267 nl_sock_destroy(dpif->upcall_socks[i]);
268 dpif->upcall_socks[i] = NULL;
273 dpif_linux_close(struct dpif *dpif_)
275 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
277 nln_notifier_destroy(dpif->port_notifier);
278 destroy_upcall_socks(dpif);
279 sset_destroy(&dpif->changed_ports);
284 dpif_linux_destroy(struct dpif *dpif_)
286 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
287 struct dpif_linux_dp dp;
289 dpif_linux_dp_init(&dp);
290 dp.cmd = OVS_DP_CMD_DEL;
291 dp.dp_ifindex = dpif->dp_ifindex;
292 return dpif_linux_dp_transact(&dp, NULL, NULL);
296 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
304 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
312 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
314 struct dpif_linux_dp dp;
318 error = dpif_linux_dp_get(dpif_, &dp, &buf);
320 stats->n_hit = dp.stats.n_hit;
321 stats->n_missed = dp.stats.n_missed;
322 stats->n_lost = dp.stats.n_lost;
323 stats->n_flows = dp.stats.n_flows;
330 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
333 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
334 const char *name = netdev_get_name(netdev);
335 const char *type = netdev_get_type(netdev);
336 struct dpif_linux_vport request, reply;
337 const struct ofpbuf *options;
339 int error, i = 0, max_ports = MAX_PORTS;
341 dpif_linux_vport_init(&request);
342 request.cmd = OVS_VPORT_CMD_NEW;
343 request.dp_ifindex = dpif->dp_ifindex;
344 request.type = netdev_vport_get_vport_type(netdev);
345 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
346 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
347 "unsupported type `%s'",
348 dpif_name(dpif_), name, type);
353 options = netdev_vport_get_options(netdev);
354 if (options && options->size) {
355 request.options = options->data;
356 request.options_len = options->size;
359 if (request.type == OVS_VPORT_TYPE_NETDEV) {
360 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
363 /* Loop until we find a port that isn't used. */
367 request.port_no = ++dpif->alloc_port_no;
368 upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
369 request.upcall_pid = &upcall_pid;
370 error = dpif_linux_vport_transact(&request, &reply, &buf);
373 *port_nop = reply.port_no;
374 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
375 dpif_name(dpif_), request.port_no, upcall_pid);
376 } else if (error == EFBIG) {
377 /* Older datapath has lower limit. */
378 max_ports = dpif->alloc_port_no;
379 dpif->alloc_port_no = 0;
383 } while ((i++ < max_ports)
384 && (error == EBUSY || error == EFBIG));
390 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
392 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
393 struct dpif_linux_vport vport;
396 dpif_linux_vport_init(&vport);
397 vport.cmd = OVS_VPORT_CMD_DEL;
398 vport.dp_ifindex = dpif->dp_ifindex;
399 vport.port_no = port_no;
400 error = dpif_linux_vport_transact(&vport, NULL, NULL);
406 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
407 const char *port_name, struct dpif_port *dpif_port)
409 struct dpif_linux_vport request;
410 struct dpif_linux_vport reply;
414 dpif_linux_vport_init(&request);
415 request.cmd = OVS_VPORT_CMD_GET;
416 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
417 request.port_no = port_no;
418 request.name = port_name;
420 error = dpif_linux_vport_transact(&request, &reply, &buf);
422 if (reply.dp_ifindex != request.dp_ifindex) {
423 /* A query by name reported that 'port_name' is in some datapath
424 * other than 'dpif', but the caller wants to know about 'dpif'. */
427 dpif_port->name = xstrdup(reply.name);
428 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
429 dpif_port->port_no = reply.port_no;
437 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
438 struct dpif_port *dpif_port)
440 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
444 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
445 struct dpif_port *dpif_port)
447 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
451 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
457 dpif_linux_port_get_pid(const struct dpif *dpif_, uint16_t port_no)
459 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
461 if (dpif->epoll_fd < 0) {
466 idx = (port_no != UINT16_MAX
467 ? 1 + (port_no & (N_UPCALL_SOCKS - 2))
469 return nl_sock_pid(dpif->upcall_socks[idx]);
474 dpif_linux_flow_flush(struct dpif *dpif_)
476 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
477 struct dpif_linux_flow flow;
479 dpif_linux_flow_init(&flow);
480 flow.cmd = OVS_FLOW_CMD_DEL;
481 flow.dp_ifindex = dpif->dp_ifindex;
482 return dpif_linux_flow_transact(&flow, NULL, NULL);
485 struct dpif_linux_port_state {
490 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
492 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
493 struct dpif_linux_port_state *state;
494 struct dpif_linux_vport request;
497 *statep = state = xmalloc(sizeof *state);
499 dpif_linux_vport_init(&request);
500 request.cmd = OVS_DP_CMD_GET;
501 request.dp_ifindex = dpif->dp_ifindex;
503 buf = ofpbuf_new(1024);
504 dpif_linux_vport_to_ofpbuf(&request, buf);
505 nl_dump_start(&state->dump, genl_sock, buf);
512 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
513 struct dpif_port *dpif_port)
515 struct dpif_linux_port_state *state = state_;
516 struct dpif_linux_vport vport;
520 if (!nl_dump_next(&state->dump, &buf)) {
524 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
529 dpif_port->name = (char *) vport.name;
530 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
531 dpif_port->port_no = vport.port_no;
536 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
538 struct dpif_linux_port_state *state = state_;
539 int error = nl_dump_done(&state->dump);
546 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
548 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
550 if (dpif->change_error) {
551 dpif->change_error = false;
552 sset_clear(&dpif->changed_ports);
554 } else if (!sset_is_empty(&dpif->changed_ports)) {
555 *devnamep = sset_pop(&dpif->changed_ports);
563 dpif_linux_port_poll_wait(const struct dpif *dpif_)
565 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
566 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
567 poll_immediate_wake();
572 dpif_linux_flow_get__(const struct dpif *dpif_,
573 const struct nlattr *key, size_t key_len,
574 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
576 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
577 struct dpif_linux_flow request;
579 dpif_linux_flow_init(&request);
580 request.cmd = OVS_FLOW_CMD_GET;
581 request.dp_ifindex = dpif->dp_ifindex;
583 request.key_len = key_len;
584 return dpif_linux_flow_transact(&request, reply, bufp);
588 dpif_linux_flow_get(const struct dpif *dpif_,
589 const struct nlattr *key, size_t key_len,
590 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
592 struct dpif_linux_flow reply;
596 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
599 dpif_linux_flow_get_stats(&reply, stats);
602 buf->data = (void *) reply.actions;
603 buf->size = reply.actions_len;
613 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
614 struct dpif_linux_flow *request)
616 static struct nlattr dummy_action;
618 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
620 dpif_linux_flow_init(request);
621 request->cmd = (put->flags & DPIF_FP_CREATE
622 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
623 request->dp_ifindex = dpif->dp_ifindex;
624 request->key = put->key;
625 request->key_len = put->key_len;
626 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
627 request->actions = put->actions ? put->actions : &dummy_action;
628 request->actions_len = put->actions_len;
629 if (put->flags & DPIF_FP_ZERO_STATS) {
630 request->clear = true;
632 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
636 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
638 struct dpif_linux_flow request, reply;
642 dpif_linux_init_flow_put(dpif_, put, &request);
643 error = dpif_linux_flow_transact(&request,
644 put->stats ? &reply : NULL,
645 put->stats ? &buf : NULL);
646 if (!error && put->stats) {
647 dpif_linux_flow_get_stats(&reply, put->stats);
654 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
655 struct dpif_linux_flow *request)
657 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
659 dpif_linux_flow_init(request);
660 request->cmd = OVS_FLOW_CMD_DEL;
661 request->dp_ifindex = dpif->dp_ifindex;
662 request->key = del->key;
663 request->key_len = del->key_len;
667 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
669 struct dpif_linux_flow request, reply;
673 dpif_linux_init_flow_del(dpif_, del, &request);
674 error = dpif_linux_flow_transact(&request,
675 del->stats ? &reply : NULL,
676 del->stats ? &buf : NULL);
677 if (!error && del->stats) {
678 dpif_linux_flow_get_stats(&reply, del->stats);
684 struct dpif_linux_flow_state {
686 struct dpif_linux_flow flow;
687 struct dpif_flow_stats stats;
692 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
694 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
695 struct dpif_linux_flow_state *state;
696 struct dpif_linux_flow request;
699 *statep = state = xmalloc(sizeof *state);
701 dpif_linux_flow_init(&request);
702 request.cmd = OVS_DP_CMD_GET;
703 request.dp_ifindex = dpif->dp_ifindex;
705 buf = ofpbuf_new(1024);
706 dpif_linux_flow_to_ofpbuf(&request, buf);
707 nl_dump_start(&state->dump, genl_sock, buf);
716 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
717 const struct nlattr **key, size_t *key_len,
718 const struct nlattr **actions, size_t *actions_len,
719 const struct dpif_flow_stats **stats)
721 struct dpif_linux_flow_state *state = state_;
726 ofpbuf_delete(state->buf);
729 if (!nl_dump_next(&state->dump, &buf)) {
733 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
738 if (actions && !state->flow.actions) {
739 error = dpif_linux_flow_get__(dpif_, state->flow.key,
741 &state->flow, &state->buf);
742 if (error == ENOENT) {
743 VLOG_DBG("dumped flow disappeared on get");
745 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
751 *actions = state->flow.actions;
752 *actions_len = state->flow.actions_len;
755 *key = state->flow.key;
756 *key_len = state->flow.key_len;
759 dpif_linux_flow_get_stats(&state->flow, &state->stats);
760 *stats = &state->stats;
766 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
768 struct dpif_linux_flow_state *state = state_;
769 int error = nl_dump_done(&state->dump);
770 ofpbuf_delete(state->buf);
776 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
779 struct ovs_header *k_exec;
781 ofpbuf_prealloc_tailroom(buf, (64
782 + d_exec->packet->size
784 + d_exec->actions_len));
786 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
787 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
789 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
790 k_exec->dp_ifindex = dp_ifindex;
792 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
793 d_exec->packet->data, d_exec->packet->size);
794 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
795 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
796 d_exec->actions, d_exec->actions_len);
800 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
802 uint64_t request_stub[1024 / 8];
803 struct ofpbuf request;
806 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
807 dpif_linux_encode_execute(dp_ifindex, execute, &request);
808 error = nl_sock_transact(genl_sock, &request, NULL);
809 ofpbuf_uninit(&request);
815 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
817 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
819 return dpif_linux_execute__(dpif->dp_ifindex, execute);
825 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
827 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
830 struct nl_transaction txn;
832 struct ofpbuf request;
833 uint64_t request_stub[1024 / 8];
836 uint64_t reply_stub[1024 / 8];
839 struct nl_transaction *txnsp[MAX_OPS];
842 assert(n_ops <= MAX_OPS);
843 for (i = 0; i < n_ops; i++) {
844 struct op_auxdata *aux = &auxes[i];
845 struct dpif_op *op = ops[i];
846 struct dpif_flow_put *put;
847 struct dpif_flow_del *del;
848 struct dpif_execute *execute;
849 struct dpif_linux_flow flow;
851 ofpbuf_use_stub(&aux->request,
852 aux->request_stub, sizeof aux->request_stub);
853 aux->txn.request = &aux->request;
855 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
856 aux->txn.reply = NULL;
859 case DPIF_OP_FLOW_PUT:
860 put = &op->u.flow_put;
861 dpif_linux_init_flow_put(dpif_, put, &flow);
863 flow.nlmsg_flags |= NLM_F_ECHO;
864 aux->txn.reply = &aux->reply;
866 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
869 case DPIF_OP_FLOW_DEL:
870 del = &op->u.flow_del;
871 dpif_linux_init_flow_del(dpif_, del, &flow);
873 flow.nlmsg_flags |= NLM_F_ECHO;
874 aux->txn.reply = &aux->reply;
876 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
879 case DPIF_OP_EXECUTE:
880 execute = &op->u.execute;
881 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
890 for (i = 0; i < n_ops; i++) {
891 txnsp[i] = &auxes[i].txn;
893 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
895 for (i = 0; i < n_ops; i++) {
896 struct op_auxdata *aux = &auxes[i];
897 struct nl_transaction *txn = &auxes[i].txn;
898 struct dpif_op *op = ops[i];
899 struct dpif_flow_put *put;
900 struct dpif_flow_del *del;
902 op->error = txn->error;
905 case DPIF_OP_FLOW_PUT:
906 put = &op->u.flow_put;
907 if (!op->error && put->stats) {
908 struct dpif_linux_flow reply;
910 op->error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
912 dpif_linux_flow_get_stats(&reply, put->stats);
917 case DPIF_OP_FLOW_DEL:
918 del = &op->u.flow_del;
919 if (!op->error && del->stats) {
920 struct dpif_linux_flow reply;
922 op->error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
924 dpif_linux_flow_get_stats(&reply, del->stats);
929 case DPIF_OP_EXECUTE:
936 ofpbuf_uninit(&aux->request);
937 ofpbuf_uninit(&aux->reply);
942 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
945 size_t chunk = MIN(n_ops, MAX_OPS);
946 dpif_linux_operate__(dpif, ops, chunk);
953 set_upcall_pids(struct dpif *dpif_)
955 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
956 struct dpif_port_dump port_dump;
957 struct dpif_port port;
960 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
961 uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
962 struct dpif_linux_vport vport_request;
964 dpif_linux_vport_init(&vport_request);
965 vport_request.cmd = OVS_VPORT_CMD_SET;
966 vport_request.dp_ifindex = dpif->dp_ifindex;
967 vport_request.port_no = port.port_no;
968 vport_request.upcall_pid = &upcall_pid;
969 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
971 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
972 dpif_name(&dpif->dpif), vport_request.port_no,
975 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
976 dpif_name(&dpif->dpif), strerror(error));
982 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
984 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
986 if ((dpif->epoll_fd >= 0) == enable) {
991 destroy_upcall_socks(dpif);
996 dpif->epoll_fd = epoll_create(N_UPCALL_SOCKS);
997 if (dpif->epoll_fd < 0) {
1001 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1002 struct epoll_event event;
1004 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
1006 destroy_upcall_socks(dpif);
1010 memset(&event, 0, sizeof event);
1011 event.events = EPOLLIN;
1013 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD,
1014 nl_sock_fd(dpif->upcall_socks[i]), &event) < 0) {
1016 destroy_upcall_socks(dpif);
1021 dpif->ready_mask = 0;
1024 set_upcall_pids(dpif_);
1030 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1031 uint32_t queue_id, uint32_t *priority)
1033 if (queue_id < 0xf000) {
1034 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1042 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1045 static const struct nl_policy ovs_packet_policy[] = {
1046 /* Always present. */
1047 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1048 .min_len = ETH_HEADER_LEN },
1049 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1051 /* OVS_PACKET_CMD_ACTION only. */
1052 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1055 struct ovs_header *ovs_header;
1056 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1057 struct nlmsghdr *nlmsg;
1058 struct genlmsghdr *genl;
1062 ofpbuf_use_const(&b, buf->data, buf->size);
1064 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1065 genl = ofpbuf_try_pull(&b, sizeof *genl);
1066 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1067 if (!nlmsg || !genl || !ovs_header
1068 || nlmsg->nlmsg_type != ovs_packet_family
1069 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1070 ARRAY_SIZE(ovs_packet_policy))) {
1074 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1075 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1081 memset(upcall, 0, sizeof *upcall);
1082 upcall->type = type;
1083 upcall->packet = buf;
1084 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1085 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1086 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1087 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1088 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1089 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1091 *dp_ifindex = ovs_header->dp_ifindex;
1097 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1100 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1103 if (dpif->epoll_fd < 0) {
1107 if (!dpif->ready_mask) {
1108 struct epoll_event events[N_UPCALL_SOCKS];
1113 retval = epoll_wait(dpif->epoll_fd, events, N_UPCALL_SOCKS, 0);
1114 } while (retval < 0 && errno == EINTR);
1116 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1117 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1120 for (i = 0; i < retval; i++) {
1121 dpif->ready_mask |= 1u << events[i].data.u32;
1125 while (dpif->ready_mask) {
1126 int indx = ffs(dpif->ready_mask) - 1;
1127 struct nl_sock *upcall_sock = dpif->upcall_socks[indx];
1129 dpif->ready_mask &= ~(1u << indx);
1135 if (++read_tries > 50) {
1139 error = nl_sock_recv(upcall_sock, buf, false);
1141 if (error == EAGAIN) {
1147 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1148 if (!error && dp_ifindex == dpif->dp_ifindex) {
1161 dpif_linux_recv_wait(struct dpif *dpif_)
1163 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1165 if (dpif->epoll_fd < 0) {
1169 poll_fd_wait(dpif->epoll_fd, POLLIN);
1173 dpif_linux_recv_purge(struct dpif *dpif_)
1175 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1178 if (dpif->epoll_fd < 0) {
1182 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1183 nl_sock_drain(dpif->upcall_socks[i]);
1187 const struct dpif_class dpif_linux_class = {
1189 dpif_linux_enumerate,
1195 dpif_linux_get_stats,
1196 dpif_linux_port_add,
1197 dpif_linux_port_del,
1198 dpif_linux_port_query_by_number,
1199 dpif_linux_port_query_by_name,
1200 dpif_linux_get_max_ports,
1201 dpif_linux_port_get_pid,
1202 dpif_linux_port_dump_start,
1203 dpif_linux_port_dump_next,
1204 dpif_linux_port_dump_done,
1205 dpif_linux_port_poll,
1206 dpif_linux_port_poll_wait,
1207 dpif_linux_flow_get,
1208 dpif_linux_flow_put,
1209 dpif_linux_flow_del,
1210 dpif_linux_flow_flush,
1211 dpif_linux_flow_dump_start,
1212 dpif_linux_flow_dump_next,
1213 dpif_linux_flow_dump_done,
1216 dpif_linux_recv_set,
1217 dpif_linux_queue_to_priority,
1219 dpif_linux_recv_wait,
1220 dpif_linux_recv_purge,
1224 dpif_linux_init(void)
1226 static int error = -1;
1229 unsigned int ovs_vport_mcgroup;
1231 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1232 &ovs_datapath_family);
1234 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1235 "The Open vSwitch kernel module is probably not loaded.",
1236 OVS_DATAPATH_FAMILY);
1239 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1242 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1245 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1246 &ovs_packet_family);
1249 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1252 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1254 OVS_VPORT_MCGROUP_FALLBACK_ID);
1257 static struct dpif_linux_vport vport;
1258 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1259 dpif_linux_nln_parse, &vport);
1267 dpif_linux_is_internal_device(const char *name)
1269 struct dpif_linux_vport reply;
1273 error = dpif_linux_vport_get(name, &reply, &buf);
1276 } else if (error != ENODEV && error != ENOENT) {
1277 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1278 name, strerror(error));
1281 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1285 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1286 const void *data, size_t size)
1288 struct ofpbuf actions, key, packet;
1289 struct odputil_keybuf keybuf;
1290 struct dpif_execute execute;
1294 ofpbuf_use_const(&packet, data, size);
1295 flow_extract(&packet, 0, htonll(0), 0, &flow);
1297 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1298 odp_flow_key_from_flow(&key, &flow);
1300 ofpbuf_use_stack(&actions, &action, sizeof action);
1301 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1303 execute.key = key.data;
1304 execute.key_len = key.size;
1305 execute.actions = actions.data;
1306 execute.actions_len = actions.size;
1307 execute.packet = &packet;
1308 return dpif_linux_execute__(dp_ifindex, &execute);
1312 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1314 struct dpif_linux_vport *vport = vport_;
1315 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1319 dpif_linux_port_changed(const void *vport_, void *dpif_)
1321 const struct dpif_linux_vport *vport = vport_;
1322 struct dpif_linux *dpif = dpif_;
1325 if (vport->dp_ifindex == dpif->dp_ifindex
1326 && (vport->cmd == OVS_VPORT_CMD_NEW
1327 || vport->cmd == OVS_VPORT_CMD_DEL
1328 || vport->cmd == OVS_VPORT_CMD_SET)) {
1329 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1330 dpif->dpif.full_name, vport->name, vport->cmd);
1331 sset_add(&dpif->changed_ports, vport->name);
1334 dpif->change_error = true;
1338 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1339 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1340 * positive errno value.
1342 * 'vport' will contain pointers into 'buf', so the caller should not free
1343 * 'buf' while 'vport' is still in use. */
1345 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1346 const struct ofpbuf *buf)
1348 static const struct nl_policy ovs_vport_policy[] = {
1349 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1350 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1351 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1352 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1353 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1355 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1356 .min_len = ETH_ADDR_LEN,
1357 .max_len = ETH_ADDR_LEN,
1359 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1362 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1363 struct ovs_header *ovs_header;
1364 struct nlmsghdr *nlmsg;
1365 struct genlmsghdr *genl;
1368 dpif_linux_vport_init(vport);
1370 ofpbuf_use_const(&b, buf->data, buf->size);
1371 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1372 genl = ofpbuf_try_pull(&b, sizeof *genl);
1373 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1374 if (!nlmsg || !genl || !ovs_header
1375 || nlmsg->nlmsg_type != ovs_vport_family
1376 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1377 ARRAY_SIZE(ovs_vport_policy))) {
1381 vport->cmd = genl->cmd;
1382 vport->dp_ifindex = ovs_header->dp_ifindex;
1383 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1384 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1385 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1386 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1387 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1389 if (a[OVS_VPORT_ATTR_STATS]) {
1390 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1392 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1393 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1395 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1396 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1397 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1402 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1403 * followed by Netlink attributes corresponding to 'vport'. */
1405 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1408 struct ovs_header *ovs_header;
1410 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1411 vport->cmd, OVS_VPORT_VERSION);
1413 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1414 ovs_header->dp_ifindex = vport->dp_ifindex;
1416 if (vport->port_no != UINT32_MAX) {
1417 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1420 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1421 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1425 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1428 if (vport->upcall_pid) {
1429 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1433 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1434 vport->stats, sizeof *vport->stats);
1437 if (vport->address) {
1438 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1439 vport->address, ETH_ADDR_LEN);
1442 if (vport->options) {
1443 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1444 vport->options, vport->options_len);
1448 /* Clears 'vport' to "empty" values. */
1450 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1452 memset(vport, 0, sizeof *vport);
1453 vport->port_no = UINT32_MAX;
1456 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1457 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1458 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1459 * result of the command is expected to be an ovs_vport also, which is decoded
1460 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1461 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1463 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1464 struct dpif_linux_vport *reply,
1465 struct ofpbuf **bufp)
1467 struct ofpbuf *request_buf;
1470 assert((reply != NULL) == (bufp != NULL));
1472 error = dpif_linux_init();
1476 dpif_linux_vport_init(reply);
1481 request_buf = ofpbuf_new(1024);
1482 dpif_linux_vport_to_ofpbuf(request, request_buf);
1483 error = nl_sock_transact(genl_sock, request_buf, bufp);
1484 ofpbuf_delete(request_buf);
1488 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1491 dpif_linux_vport_init(reply);
1492 ofpbuf_delete(*bufp);
1499 /* Obtains information about the kernel vport named 'name' and stores it into
1500 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1501 * longer needed ('reply' will contain pointers into '*bufp'). */
1503 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1504 struct ofpbuf **bufp)
1506 struct dpif_linux_vport request;
1508 dpif_linux_vport_init(&request);
1509 request.cmd = OVS_VPORT_CMD_GET;
1510 request.name = name;
1512 return dpif_linux_vport_transact(&request, reply, bufp);
1515 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1516 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1517 * positive errno value.
1519 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1520 * while 'dp' is still in use. */
1522 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1524 static const struct nl_policy ovs_datapath_policy[] = {
1525 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1526 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1530 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1531 struct ovs_header *ovs_header;
1532 struct nlmsghdr *nlmsg;
1533 struct genlmsghdr *genl;
1536 dpif_linux_dp_init(dp);
1538 ofpbuf_use_const(&b, buf->data, buf->size);
1539 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1540 genl = ofpbuf_try_pull(&b, sizeof *genl);
1541 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1542 if (!nlmsg || !genl || !ovs_header
1543 || nlmsg->nlmsg_type != ovs_datapath_family
1544 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1545 ARRAY_SIZE(ovs_datapath_policy))) {
1549 dp->cmd = genl->cmd;
1550 dp->dp_ifindex = ovs_header->dp_ifindex;
1551 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1552 if (a[OVS_DP_ATTR_STATS]) {
1553 /* Can't use structure assignment because Netlink doesn't ensure
1554 * sufficient alignment for 64-bit members. */
1555 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1562 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1564 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1566 struct ovs_header *ovs_header;
1568 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1569 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1570 OVS_DATAPATH_VERSION);
1572 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1573 ovs_header->dp_ifindex = dp->dp_ifindex;
1576 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1579 if (dp->upcall_pid) {
1580 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1583 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1586 /* Clears 'dp' to "empty" values. */
1588 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1590 memset(dp, 0, sizeof *dp);
1594 dpif_linux_dp_dump_start(struct nl_dump *dump)
1596 struct dpif_linux_dp request;
1599 dpif_linux_dp_init(&request);
1600 request.cmd = OVS_DP_CMD_GET;
1602 buf = ofpbuf_new(1024);
1603 dpif_linux_dp_to_ofpbuf(&request, buf);
1604 nl_dump_start(dump, genl_sock, buf);
1608 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1609 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1610 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1611 * result of the command is expected to be of the same form, which is decoded
1612 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1613 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1615 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1616 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1618 struct ofpbuf *request_buf;
1621 assert((reply != NULL) == (bufp != NULL));
1623 request_buf = ofpbuf_new(1024);
1624 dpif_linux_dp_to_ofpbuf(request, request_buf);
1625 error = nl_sock_transact(genl_sock, request_buf, bufp);
1626 ofpbuf_delete(request_buf);
1630 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1633 dpif_linux_dp_init(reply);
1634 ofpbuf_delete(*bufp);
1641 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1642 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1643 * will contain pointers into '*bufp'). */
1645 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1646 struct ofpbuf **bufp)
1648 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1649 struct dpif_linux_dp request;
1651 dpif_linux_dp_init(&request);
1652 request.cmd = OVS_DP_CMD_GET;
1653 request.dp_ifindex = dpif->dp_ifindex;
1655 return dpif_linux_dp_transact(&request, reply, bufp);
1658 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1659 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1660 * positive errno value.
1662 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1663 * while 'flow' is still in use. */
1665 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1666 const struct ofpbuf *buf)
1668 static const struct nl_policy ovs_flow_policy[] = {
1669 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1670 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1671 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1673 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1674 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1675 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1678 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1679 struct ovs_header *ovs_header;
1680 struct nlmsghdr *nlmsg;
1681 struct genlmsghdr *genl;
1684 dpif_linux_flow_init(flow);
1686 ofpbuf_use_const(&b, buf->data, buf->size);
1687 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1688 genl = ofpbuf_try_pull(&b, sizeof *genl);
1689 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1690 if (!nlmsg || !genl || !ovs_header
1691 || nlmsg->nlmsg_type != ovs_flow_family
1692 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1693 ARRAY_SIZE(ovs_flow_policy))) {
1697 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1698 flow->dp_ifindex = ovs_header->dp_ifindex;
1699 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1700 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1701 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1702 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1703 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1705 if (a[OVS_FLOW_ATTR_STATS]) {
1706 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1708 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1709 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1711 if (a[OVS_FLOW_ATTR_USED]) {
1712 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1717 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1718 * followed by Netlink attributes corresponding to 'flow'. */
1720 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1723 struct ovs_header *ovs_header;
1725 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1726 NLM_F_REQUEST | flow->nlmsg_flags,
1727 flow->cmd, OVS_FLOW_VERSION);
1729 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1730 ovs_header->dp_ifindex = flow->dp_ifindex;
1732 if (flow->key_len) {
1733 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1736 if (flow->actions || flow->actions_len) {
1737 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1738 flow->actions, flow->actions_len);
1741 /* We never need to send these to the kernel. */
1742 assert(!flow->stats);
1743 assert(!flow->tcp_flags);
1744 assert(!flow->used);
1747 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1751 /* Clears 'flow' to "empty" values. */
1753 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1755 memset(flow, 0, sizeof *flow);
1758 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1759 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1760 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1761 * result of the command is expected to be a flow also, which is decoded and
1762 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1763 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1765 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1766 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1768 struct ofpbuf *request_buf;
1771 assert((reply != NULL) == (bufp != NULL));
1774 request->nlmsg_flags |= NLM_F_ECHO;
1777 request_buf = ofpbuf_new(1024);
1778 dpif_linux_flow_to_ofpbuf(request, request_buf);
1779 error = nl_sock_transact(genl_sock, request_buf, bufp);
1780 ofpbuf_delete(request_buf);
1784 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1787 dpif_linux_flow_init(reply);
1788 ofpbuf_delete(*bufp);
1796 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1797 struct dpif_flow_stats *stats)
1800 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1801 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1803 stats->n_packets = 0;
1806 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1807 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;