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 unsigned int listen_mask; /* Mask of DPIF_UC_* bits. */
144 int epoll_fd; /* epoll fd that includes the upcall socks. */
146 /* Change notification. */
147 struct sset changed_ports; /* Ports that have changed. */
148 struct nln_notifier *port_notifier;
151 /* Queue of unused ports. */
152 unsigned long *lru_bitmap;
153 uint16_t lru_ports[LRU_MAX_PORTS];
158 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
160 /* Generic Netlink family numbers for OVS. */
161 static int ovs_datapath_family;
162 static int ovs_vport_family;
163 static int ovs_flow_family;
164 static int ovs_packet_family;
166 /* Generic Netlink socket. */
167 static struct nl_sock *genl_sock;
168 static struct nln *nln = NULL;
170 static int dpif_linux_init(void);
171 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
172 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
173 static void dpif_linux_port_changed(const void *vport, void *dpif);
174 static uint32_t dpif_linux_port_get_pid__(const struct dpif *,
176 enum dpif_upcall_type);
178 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
180 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
181 const struct ofpbuf *);
183 static struct dpif_linux *
184 dpif_linux_cast(const struct dpif *dpif)
186 dpif_assert_class(dpif, &dpif_linux_class);
187 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
191 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
193 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
194 bitmap_set1(dp->lru_bitmap, port);
195 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
200 dpif_linux_pop_port(struct dpif_linux *dp)
204 if (dp->lru_head == dp->lru_tail) {
208 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
209 bitmap_set0(dp->lru_bitmap, port);
214 dpif_linux_enumerate(struct sset *all_dps)
220 error = dpif_linux_init();
225 dpif_linux_dp_dump_start(&dump);
226 while (nl_dump_next(&dump, &msg)) {
227 struct dpif_linux_dp dp;
229 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
230 sset_add(all_dps, dp.name);
233 return nl_dump_done(&dump);
237 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
238 bool create, struct dpif **dpifp)
240 struct dpif_linux_dp dp_request, dp;
245 error = dpif_linux_init();
250 /* Create or look up datapath. */
251 dpif_linux_dp_init(&dp_request);
253 dp_request.cmd = OVS_DP_CMD_NEW;
255 dp_request.upcall_pid = &upcall_pid;
257 dp_request.cmd = OVS_DP_CMD_GET;
259 dp_request.name = name;
260 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
265 open_dpif(&dp, dpifp);
271 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
273 struct dpif_linux *dpif;
276 dpif = xzalloc(sizeof *dpif);
277 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
281 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
282 dp->dp_ifindex, dp->dp_ifindex);
284 dpif->dp_ifindex = dp->dp_ifindex;
285 sset_init(&dpif->changed_ports);
286 *dpifp = &dpif->dpif;
288 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
289 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
290 for (i = 1; i < LRU_MAX_PORTS; i++) {
291 dpif_linux_push_port(dpif, i);
296 destroy_upcall_socks(struct dpif_linux *dpif)
300 if (dpif->epoll_fd >= 0) {
301 close(dpif->epoll_fd);
304 for (i = 0; i < N_UPCALL_SOCKS; i++) {
305 nl_sock_destroy(dpif->upcall_socks[i]);
306 dpif->upcall_socks[i] = NULL;
311 dpif_linux_close(struct dpif *dpif_)
313 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
315 nln_notifier_destroy(dpif->port_notifier);
316 destroy_upcall_socks(dpif);
317 sset_destroy(&dpif->changed_ports);
318 free(dpif->lru_bitmap);
323 dpif_linux_destroy(struct dpif *dpif_)
325 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
326 struct dpif_linux_dp dp;
328 dpif_linux_dp_init(&dp);
329 dp.cmd = OVS_DP_CMD_DEL;
330 dp.dp_ifindex = dpif->dp_ifindex;
331 return dpif_linux_dp_transact(&dp, NULL, NULL);
335 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
343 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
351 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
353 struct dpif_linux_dp dp;
357 error = dpif_linux_dp_get(dpif_, &dp, &buf);
359 stats->n_hit = dp.stats.n_hit;
360 stats->n_missed = dp.stats.n_missed;
361 stats->n_lost = dp.stats.n_lost;
362 stats->n_flows = dp.stats.n_flows;
369 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
372 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
373 const char *name = netdev_get_name(netdev);
374 const char *type = netdev_get_type(netdev);
375 struct dpif_linux_vport request, reply;
376 const struct ofpbuf *options;
380 dpif_linux_vport_init(&request);
381 request.cmd = OVS_VPORT_CMD_NEW;
382 request.dp_ifindex = dpif->dp_ifindex;
383 request.type = netdev_vport_get_vport_type(netdev);
384 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
385 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
386 "unsupported type `%s'",
387 dpif_name(dpif_), name, type);
392 options = netdev_vport_get_options(netdev);
393 if (options && options->size) {
394 request.options = options->data;
395 request.options_len = options->size;
398 if (request.type == OVS_VPORT_TYPE_NETDEV) {
399 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
402 /* Loop until we find a port that isn't used. */
406 request.port_no = dpif_linux_pop_port(dpif);
407 upcall_pid = dpif_linux_port_get_pid__(dpif_, request.port_no,
409 request.upcall_pid = &upcall_pid;
410 error = dpif_linux_vport_transact(&request, &reply, &buf);
413 *port_nop = reply.port_no;
414 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
415 dpif_name(dpif_), request.port_no, upcall_pid);
418 } while (request.port_no != UINT32_MAX
419 && (error == EBUSY || error == EFBIG));
425 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
427 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
428 struct dpif_linux_vport vport;
431 dpif_linux_vport_init(&vport);
432 vport.cmd = OVS_VPORT_CMD_DEL;
433 vport.dp_ifindex = dpif->dp_ifindex;
434 vport.port_no = port_no;
435 error = dpif_linux_vport_transact(&vport, NULL, NULL);
438 dpif_linux_push_port(dpif, port_no);
444 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
445 const char *port_name, struct dpif_port *dpif_port)
447 struct dpif_linux_vport request;
448 struct dpif_linux_vport reply;
452 dpif_linux_vport_init(&request);
453 request.cmd = OVS_VPORT_CMD_GET;
454 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
455 request.port_no = port_no;
456 request.name = port_name;
458 error = dpif_linux_vport_transact(&request, &reply, &buf);
460 dpif_port->name = xstrdup(reply.name);
461 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
462 dpif_port->port_no = reply.port_no;
469 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
470 struct dpif_port *dpif_port)
472 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
476 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
477 struct dpif_port *dpif_port)
479 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
483 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
485 /* If the datapath increases its range of supported ports, then it should
486 * start reporting that. */
491 dpif_linux_port_get_pid__(const struct dpif *dpif_, uint16_t port_no,
492 enum dpif_upcall_type upcall_type)
494 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
496 if (!(dpif->listen_mask & (1u << upcall_type))) {
499 int idx = port_no & (N_UPCALL_SOCKS - 1);
500 return nl_sock_pid(dpif->upcall_socks[idx]);
505 dpif_linux_port_get_pid(const struct dpif *dpif, uint16_t port_no)
507 return dpif_linux_port_get_pid__(dpif, port_no, DPIF_UC_ACTION);
511 dpif_linux_flow_flush(struct dpif *dpif_)
513 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
514 struct dpif_linux_flow flow;
516 dpif_linux_flow_init(&flow);
517 flow.cmd = OVS_FLOW_CMD_DEL;
518 flow.dp_ifindex = dpif->dp_ifindex;
519 return dpif_linux_flow_transact(&flow, NULL, NULL);
522 struct dpif_linux_port_state {
524 unsigned long *port_bitmap; /* Ports in the datapath. */
525 bool complete; /* Dump completed without error. */
529 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
531 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
532 struct dpif_linux_port_state *state;
533 struct dpif_linux_vport request;
536 *statep = state = xmalloc(sizeof *state);
537 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
538 state->complete = false;
540 dpif_linux_vport_init(&request);
541 request.cmd = OVS_DP_CMD_GET;
542 request.dp_ifindex = dpif->dp_ifindex;
544 buf = ofpbuf_new(1024);
545 dpif_linux_vport_to_ofpbuf(&request, buf);
546 nl_dump_start(&state->dump, genl_sock, buf);
553 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
554 struct dpif_port *dpif_port)
556 struct dpif_linux_port_state *state = state_;
557 struct dpif_linux_vport vport;
561 if (!nl_dump_next(&state->dump, &buf)) {
562 state->complete = true;
566 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
571 if (vport.port_no < LRU_MAX_PORTS) {
572 bitmap_set1(state->port_bitmap, vport.port_no);
575 dpif_port->name = (char *) vport.name;
576 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
577 dpif_port->port_no = vport.port_no;
582 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
584 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
585 struct dpif_linux_port_state *state = state_;
586 int error = nl_dump_done(&state->dump);
588 if (state->complete) {
591 for (i = 0; i < LRU_MAX_PORTS; i++) {
592 if (!bitmap_is_set(state->port_bitmap, i)) {
593 dpif_linux_push_port(dpif, i);
598 free(state->port_bitmap);
604 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
606 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
608 if (dpif->change_error) {
609 dpif->change_error = false;
610 sset_clear(&dpif->changed_ports);
612 } else if (!sset_is_empty(&dpif->changed_ports)) {
613 *devnamep = sset_pop(&dpif->changed_ports);
621 dpif_linux_port_poll_wait(const struct dpif *dpif_)
623 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
624 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
625 poll_immediate_wake();
630 dpif_linux_flow_get__(const struct dpif *dpif_,
631 const struct nlattr *key, size_t key_len,
632 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
634 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
635 struct dpif_linux_flow request;
637 dpif_linux_flow_init(&request);
638 request.cmd = OVS_FLOW_CMD_GET;
639 request.dp_ifindex = dpif->dp_ifindex;
641 request.key_len = key_len;
642 return dpif_linux_flow_transact(&request, reply, bufp);
646 dpif_linux_flow_get(const struct dpif *dpif_,
647 const struct nlattr *key, size_t key_len,
648 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
650 struct dpif_linux_flow reply;
654 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
657 dpif_linux_flow_get_stats(&reply, stats);
660 buf->data = (void *) reply.actions;
661 buf->size = reply.actions_len;
671 dpif_linux_init_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
672 const struct nlattr *key, size_t key_len,
673 const struct nlattr *actions, size_t actions_len,
674 struct dpif_linux_flow *request)
676 static struct nlattr dummy_action;
678 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
680 dpif_linux_flow_init(request);
681 request->cmd = (flags & DPIF_FP_CREATE
682 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
683 request->dp_ifindex = dpif->dp_ifindex;
685 request->key_len = key_len;
686 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
687 request->actions = actions ? actions : &dummy_action;
688 request->actions_len = actions_len;
689 if (flags & DPIF_FP_ZERO_STATS) {
690 request->clear = true;
692 request->nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
696 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
697 const struct nlattr *key, size_t key_len,
698 const struct nlattr *actions, size_t actions_len,
699 struct dpif_flow_stats *stats)
701 struct dpif_linux_flow request, reply;
705 dpif_linux_init_flow_put(dpif_, flags, key, key_len, actions, actions_len,
707 error = dpif_linux_flow_transact(&request,
708 stats ? &reply : NULL,
709 stats ? &buf : NULL);
710 if (!error && stats) {
711 dpif_linux_flow_get_stats(&reply, stats);
718 dpif_linux_flow_del(struct dpif *dpif_,
719 const struct nlattr *key, size_t key_len,
720 struct dpif_flow_stats *stats)
722 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
723 struct dpif_linux_flow request, reply;
727 dpif_linux_flow_init(&request);
728 request.cmd = OVS_FLOW_CMD_DEL;
729 request.dp_ifindex = dpif->dp_ifindex;
731 request.key_len = key_len;
732 error = dpif_linux_flow_transact(&request,
733 stats ? &reply : NULL,
734 stats ? &buf : NULL);
735 if (!error && stats) {
736 dpif_linux_flow_get_stats(&reply, stats);
742 struct dpif_linux_flow_state {
744 struct dpif_linux_flow flow;
745 struct dpif_flow_stats stats;
750 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
752 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
753 struct dpif_linux_flow_state *state;
754 struct dpif_linux_flow request;
757 *statep = state = xmalloc(sizeof *state);
759 dpif_linux_flow_init(&request);
760 request.cmd = OVS_DP_CMD_GET;
761 request.dp_ifindex = dpif->dp_ifindex;
763 buf = ofpbuf_new(1024);
764 dpif_linux_flow_to_ofpbuf(&request, buf);
765 nl_dump_start(&state->dump, genl_sock, buf);
774 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
775 const struct nlattr **key, size_t *key_len,
776 const struct nlattr **actions, size_t *actions_len,
777 const struct dpif_flow_stats **stats)
779 struct dpif_linux_flow_state *state = state_;
784 ofpbuf_delete(state->buf);
787 if (!nl_dump_next(&state->dump, &buf)) {
791 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
796 if (actions && !state->flow.actions) {
797 error = dpif_linux_flow_get__(dpif_, state->flow.key,
799 &state->flow, &state->buf);
800 if (error == ENOENT) {
801 VLOG_DBG("dumped flow disappeared on get");
803 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
809 *actions = state->flow.actions;
810 *actions_len = state->flow.actions_len;
813 *key = state->flow.key;
814 *key_len = state->flow.key_len;
817 dpif_linux_flow_get_stats(&state->flow, &state->stats);
818 *stats = &state->stats;
824 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
826 struct dpif_linux_flow_state *state = state_;
827 int error = nl_dump_done(&state->dump);
828 ofpbuf_delete(state->buf);
833 static struct ofpbuf *
834 dpif_linux_encode_execute(int dp_ifindex,
835 const struct nlattr *key, size_t key_len,
836 const struct nlattr *actions, size_t actions_len,
837 const struct ofpbuf *packet)
839 struct ovs_header *execute;
842 buf = ofpbuf_new(128 + actions_len + packet->size);
844 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
845 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
847 execute = ofpbuf_put_uninit(buf, sizeof *execute);
848 execute->dp_ifindex = dp_ifindex;
850 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
851 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
852 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
858 dpif_linux_execute__(int dp_ifindex, const struct nlattr *key, size_t key_len,
859 const struct nlattr *actions, size_t actions_len,
860 const struct ofpbuf *packet)
862 struct ofpbuf *request;
865 request = dpif_linux_encode_execute(dp_ifindex,
866 key, key_len, actions, actions_len,
868 error = nl_sock_transact(genl_sock, request, NULL);
869 ofpbuf_delete(request);
875 dpif_linux_execute(struct dpif *dpif_,
876 const struct nlattr *key, size_t key_len,
877 const struct nlattr *actions, size_t actions_len,
878 const struct ofpbuf *packet)
880 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
882 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
883 actions, actions_len, packet);
887 dpif_linux_operate(struct dpif *dpif_, union dpif_op **ops, size_t n_ops)
889 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
890 struct nl_transaction **txnsp;
891 struct nl_transaction *txns;
894 txns = xmalloc(n_ops * sizeof *txns);
895 for (i = 0; i < n_ops; i++) {
896 struct nl_transaction *txn = &txns[i];
897 union dpif_op *op = ops[i];
899 if (op->type == DPIF_OP_FLOW_PUT) {
900 struct dpif_flow_put *put = &op->flow_put;
901 struct dpif_linux_flow request;
903 dpif_linux_init_flow_put(dpif_, put->flags, put->key, put->key_len,
904 put->actions, put->actions_len,
907 request.nlmsg_flags |= NLM_F_ECHO;
909 txn->request = ofpbuf_new(1024);
910 dpif_linux_flow_to_ofpbuf(&request, txn->request);
911 } else if (op->type == DPIF_OP_EXECUTE) {
912 struct dpif_execute *execute = &op->execute;
914 txn->request = dpif_linux_encode_execute(
915 dpif->dp_ifindex, execute->key, execute->key_len,
916 execute->actions, execute->actions_len, execute->packet);
922 txnsp = xmalloc(n_ops * sizeof *txnsp);
923 for (i = 0; i < n_ops; i++) {
927 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
931 for (i = 0; i < n_ops; i++) {
932 struct nl_transaction *txn = &txns[i];
933 union dpif_op *op = ops[i];
935 if (op->type == DPIF_OP_FLOW_PUT) {
936 struct dpif_flow_put *put = &op->flow_put;
937 int error = txn->error;
939 if (!error && put->stats) {
940 struct dpif_linux_flow reply;
942 error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
944 dpif_linux_flow_get_stats(&reply, put->stats);
948 } else if (op->type == DPIF_OP_EXECUTE) {
949 struct dpif_execute *execute = &op->execute;
951 execute->error = txn->error;
956 ofpbuf_delete(txn->request);
957 ofpbuf_delete(txn->reply);
963 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
965 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
966 *listen_mask = dpif->listen_mask;
971 set_upcall_pids(struct dpif *dpif_)
973 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
974 struct dpif_port_dump port_dump;
975 struct dpif_port port;
978 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
979 uint32_t upcall_pid = dpif_linux_port_get_pid__(dpif_, port.port_no,
981 struct dpif_linux_vport vport_request;
983 dpif_linux_vport_init(&vport_request);
984 vport_request.cmd = OVS_VPORT_CMD_SET;
985 vport_request.dp_ifindex = dpif->dp_ifindex;
986 vport_request.port_no = port.port_no;
987 vport_request.upcall_pid = &upcall_pid;
988 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
990 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
991 dpif_name(&dpif->dpif), vport_request.port_no,
994 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
995 dpif_name(&dpif->dpif), strerror(error));
1001 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
1003 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1005 if (listen_mask == dpif->listen_mask) {
1010 destroy_upcall_socks(dpif);
1011 } else if (!dpif->listen_mask) {
1015 dpif->epoll_fd = epoll_create(N_UPCALL_SOCKS);
1016 if (dpif->epoll_fd < 0) {
1020 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1021 struct epoll_event event;
1023 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
1025 destroy_upcall_socks(dpif);
1029 memset(&event, 0, sizeof event);
1030 event.events = EPOLLIN;
1032 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD,
1033 nl_sock_fd(dpif->upcall_socks[i]), &event) < 0) {
1035 destroy_upcall_socks(dpif);
1040 dpif->ready_mask = 0;
1043 dpif->listen_mask = listen_mask;
1044 set_upcall_pids(dpif_);
1050 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1051 uint32_t queue_id, uint32_t *priority)
1053 if (queue_id < 0xf000) {
1054 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1062 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1065 static const struct nl_policy ovs_packet_policy[] = {
1066 /* Always present. */
1067 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1068 .min_len = ETH_HEADER_LEN },
1069 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1071 /* OVS_PACKET_CMD_ACTION only. */
1072 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1075 struct ovs_header *ovs_header;
1076 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1077 struct nlmsghdr *nlmsg;
1078 struct genlmsghdr *genl;
1082 ofpbuf_use_const(&b, buf->data, buf->size);
1084 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1085 genl = ofpbuf_try_pull(&b, sizeof *genl);
1086 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1087 if (!nlmsg || !genl || !ovs_header
1088 || nlmsg->nlmsg_type != ovs_packet_family
1089 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1090 ARRAY_SIZE(ovs_packet_policy))) {
1094 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1095 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1101 memset(upcall, 0, sizeof *upcall);
1102 upcall->type = type;
1103 upcall->packet = buf;
1104 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1105 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1106 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1107 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1108 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1109 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1111 *dp_ifindex = ovs_header->dp_ifindex;
1117 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1119 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1122 if (!dpif->listen_mask) {
1126 if (!dpif->ready_mask) {
1127 struct epoll_event events[N_UPCALL_SOCKS];
1132 retval = epoll_wait(dpif->epoll_fd, events, N_UPCALL_SOCKS, 0);
1133 } while (retval < 0 && errno == EINTR);
1135 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1136 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1139 for (i = 0; i < retval; i++) {
1140 dpif->ready_mask |= 1u << events[i].data.u32;
1144 while (dpif->ready_mask) {
1145 int indx = ffs(dpif->ready_mask) - 1;
1146 struct nl_sock *upcall_sock = dpif->upcall_socks[indx];
1148 dpif->ready_mask &= ~(1u << indx);
1155 if (++read_tries > 50) {
1159 error = nl_sock_recv(upcall_sock, &buf, false);
1160 if (error == EAGAIN) {
1166 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1168 && dp_ifindex == dpif->dp_ifindex
1169 && dpif->listen_mask & (1u << upcall->type)) {
1184 dpif_linux_recv_wait(struct dpif *dpif_)
1186 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1188 if (!dpif->listen_mask) {
1192 poll_fd_wait(dpif->epoll_fd, POLLIN);
1196 dpif_linux_recv_purge(struct dpif *dpif_)
1198 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1201 if (!dpif->listen_mask) {
1205 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1206 nl_sock_drain(dpif->upcall_socks[i]);
1210 const struct dpif_class dpif_linux_class = {
1212 dpif_linux_enumerate,
1218 dpif_linux_get_stats,
1219 dpif_linux_port_add,
1220 dpif_linux_port_del,
1221 dpif_linux_port_query_by_number,
1222 dpif_linux_port_query_by_name,
1223 dpif_linux_get_max_ports,
1224 dpif_linux_port_get_pid,
1225 dpif_linux_port_dump_start,
1226 dpif_linux_port_dump_next,
1227 dpif_linux_port_dump_done,
1228 dpif_linux_port_poll,
1229 dpif_linux_port_poll_wait,
1230 dpif_linux_flow_get,
1231 dpif_linux_flow_put,
1232 dpif_linux_flow_del,
1233 dpif_linux_flow_flush,
1234 dpif_linux_flow_dump_start,
1235 dpif_linux_flow_dump_next,
1236 dpif_linux_flow_dump_done,
1239 dpif_linux_recv_get_mask,
1240 dpif_linux_recv_set_mask,
1241 dpif_linux_queue_to_priority,
1243 dpif_linux_recv_wait,
1244 dpif_linux_recv_purge,
1248 dpif_linux_init(void)
1250 static int error = -1;
1253 unsigned int ovs_vport_mcgroup;
1255 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1256 &ovs_datapath_family);
1258 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1259 "The Open vSwitch kernel module is probably not loaded.",
1260 OVS_DATAPATH_FAMILY);
1263 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1266 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1269 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1270 &ovs_packet_family);
1273 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1276 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1278 OVS_VPORT_MCGROUP_FALLBACK_ID);
1281 static struct dpif_linux_vport vport;
1282 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1283 dpif_linux_nln_parse, &vport);
1291 dpif_linux_is_internal_device(const char *name)
1293 struct dpif_linux_vport reply;
1297 error = dpif_linux_vport_get(name, &reply, &buf);
1300 } else if (error != ENODEV && error != ENOENT) {
1301 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1302 name, strerror(error));
1305 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1309 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1310 const void *data, size_t size)
1312 struct ofpbuf actions, key, packet;
1313 struct odputil_keybuf keybuf;
1317 ofpbuf_use_const(&packet, data, size);
1318 flow_extract(&packet, 0, htonll(0), 0, &flow);
1320 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1321 odp_flow_key_from_flow(&key, &flow);
1323 ofpbuf_use_stack(&actions, &action, sizeof action);
1324 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1326 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1327 actions.data, actions.size, &packet);
1331 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1333 struct dpif_linux_vport *vport = vport_;
1334 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1338 dpif_linux_port_changed(const void *vport_, void *dpif_)
1340 const struct dpif_linux_vport *vport = vport_;
1341 struct dpif_linux *dpif = dpif_;
1344 if (vport->dp_ifindex == dpif->dp_ifindex
1345 && (vport->cmd == OVS_VPORT_CMD_NEW
1346 || vport->cmd == OVS_VPORT_CMD_DEL
1347 || vport->cmd == OVS_VPORT_CMD_SET)) {
1348 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1349 dpif->dpif.full_name, vport->name, vport->cmd);
1350 sset_add(&dpif->changed_ports, vport->name);
1353 dpif->change_error = true;
1357 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1358 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1359 * positive errno value.
1361 * 'vport' will contain pointers into 'buf', so the caller should not free
1362 * 'buf' while 'vport' is still in use. */
1364 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1365 const struct ofpbuf *buf)
1367 static const struct nl_policy ovs_vport_policy[] = {
1368 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1369 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1370 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1371 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1372 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1374 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1375 .min_len = ETH_ADDR_LEN,
1376 .max_len = ETH_ADDR_LEN,
1378 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1381 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1382 struct ovs_header *ovs_header;
1383 struct nlmsghdr *nlmsg;
1384 struct genlmsghdr *genl;
1387 dpif_linux_vport_init(vport);
1389 ofpbuf_use_const(&b, buf->data, buf->size);
1390 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1391 genl = ofpbuf_try_pull(&b, sizeof *genl);
1392 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1393 if (!nlmsg || !genl || !ovs_header
1394 || nlmsg->nlmsg_type != ovs_vport_family
1395 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1396 ARRAY_SIZE(ovs_vport_policy))) {
1400 vport->cmd = genl->cmd;
1401 vport->dp_ifindex = ovs_header->dp_ifindex;
1402 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1403 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1404 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1405 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1406 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1408 if (a[OVS_VPORT_ATTR_STATS]) {
1409 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1411 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1412 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1414 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1415 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1416 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1421 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1422 * followed by Netlink attributes corresponding to 'vport'. */
1424 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1427 struct ovs_header *ovs_header;
1429 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1430 vport->cmd, OVS_VPORT_VERSION);
1432 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1433 ovs_header->dp_ifindex = vport->dp_ifindex;
1435 if (vport->port_no != UINT32_MAX) {
1436 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1439 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1440 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1444 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1447 if (vport->upcall_pid) {
1448 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1452 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1453 vport->stats, sizeof *vport->stats);
1456 if (vport->address) {
1457 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1458 vport->address, ETH_ADDR_LEN);
1461 if (vport->options) {
1462 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1463 vport->options, vport->options_len);
1467 /* Clears 'vport' to "empty" values. */
1469 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1471 memset(vport, 0, sizeof *vport);
1472 vport->port_no = UINT32_MAX;
1475 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1476 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1477 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1478 * result of the command is expected to be an ovs_vport also, which is decoded
1479 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1480 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1482 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1483 struct dpif_linux_vport *reply,
1484 struct ofpbuf **bufp)
1486 struct ofpbuf *request_buf;
1489 assert((reply != NULL) == (bufp != NULL));
1491 error = dpif_linux_init();
1495 dpif_linux_vport_init(reply);
1500 request_buf = ofpbuf_new(1024);
1501 dpif_linux_vport_to_ofpbuf(request, request_buf);
1502 error = nl_sock_transact(genl_sock, request_buf, bufp);
1503 ofpbuf_delete(request_buf);
1507 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1510 dpif_linux_vport_init(reply);
1511 ofpbuf_delete(*bufp);
1518 /* Obtains information about the kernel vport named 'name' and stores it into
1519 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1520 * longer needed ('reply' will contain pointers into '*bufp'). */
1522 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1523 struct ofpbuf **bufp)
1525 struct dpif_linux_vport request;
1527 dpif_linux_vport_init(&request);
1528 request.cmd = OVS_VPORT_CMD_GET;
1529 request.name = name;
1531 return dpif_linux_vport_transact(&request, reply, bufp);
1534 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1535 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1536 * positive errno value.
1538 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1539 * while 'dp' is still in use. */
1541 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1543 static const struct nl_policy ovs_datapath_policy[] = {
1544 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1545 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1549 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1550 struct ovs_header *ovs_header;
1551 struct nlmsghdr *nlmsg;
1552 struct genlmsghdr *genl;
1555 dpif_linux_dp_init(dp);
1557 ofpbuf_use_const(&b, buf->data, buf->size);
1558 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1559 genl = ofpbuf_try_pull(&b, sizeof *genl);
1560 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1561 if (!nlmsg || !genl || !ovs_header
1562 || nlmsg->nlmsg_type != ovs_datapath_family
1563 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1564 ARRAY_SIZE(ovs_datapath_policy))) {
1568 dp->cmd = genl->cmd;
1569 dp->dp_ifindex = ovs_header->dp_ifindex;
1570 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1571 if (a[OVS_DP_ATTR_STATS]) {
1572 /* Can't use structure assignment because Netlink doesn't ensure
1573 * sufficient alignment for 64-bit members. */
1574 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1581 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1583 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1585 struct ovs_header *ovs_header;
1587 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1588 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1589 OVS_DATAPATH_VERSION);
1591 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1592 ovs_header->dp_ifindex = dp->dp_ifindex;
1595 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1598 if (dp->upcall_pid) {
1599 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1602 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1605 /* Clears 'dp' to "empty" values. */
1607 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1609 memset(dp, 0, sizeof *dp);
1613 dpif_linux_dp_dump_start(struct nl_dump *dump)
1615 struct dpif_linux_dp request;
1618 dpif_linux_dp_init(&request);
1619 request.cmd = OVS_DP_CMD_GET;
1621 buf = ofpbuf_new(1024);
1622 dpif_linux_dp_to_ofpbuf(&request, buf);
1623 nl_dump_start(dump, genl_sock, buf);
1627 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1628 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1629 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1630 * result of the command is expected to be of the same form, which is decoded
1631 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1632 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1634 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1635 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1637 struct ofpbuf *request_buf;
1640 assert((reply != NULL) == (bufp != NULL));
1642 request_buf = ofpbuf_new(1024);
1643 dpif_linux_dp_to_ofpbuf(request, request_buf);
1644 error = nl_sock_transact(genl_sock, request_buf, bufp);
1645 ofpbuf_delete(request_buf);
1649 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1652 dpif_linux_dp_init(reply);
1653 ofpbuf_delete(*bufp);
1660 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1661 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1662 * will contain pointers into '*bufp'). */
1664 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1665 struct ofpbuf **bufp)
1667 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1668 struct dpif_linux_dp request;
1670 dpif_linux_dp_init(&request);
1671 request.cmd = OVS_DP_CMD_GET;
1672 request.dp_ifindex = dpif->dp_ifindex;
1674 return dpif_linux_dp_transact(&request, reply, bufp);
1677 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1678 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1679 * positive errno value.
1681 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1682 * while 'flow' is still in use. */
1684 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1685 const struct ofpbuf *buf)
1687 static const struct nl_policy ovs_flow_policy[] = {
1688 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1689 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1690 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1692 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1693 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1694 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1697 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1698 struct ovs_header *ovs_header;
1699 struct nlmsghdr *nlmsg;
1700 struct genlmsghdr *genl;
1703 dpif_linux_flow_init(flow);
1705 ofpbuf_use_const(&b, buf->data, buf->size);
1706 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1707 genl = ofpbuf_try_pull(&b, sizeof *genl);
1708 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1709 if (!nlmsg || !genl || !ovs_header
1710 || nlmsg->nlmsg_type != ovs_flow_family
1711 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1712 ARRAY_SIZE(ovs_flow_policy))) {
1716 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1717 flow->dp_ifindex = ovs_header->dp_ifindex;
1718 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1719 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1720 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1721 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1722 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1724 if (a[OVS_FLOW_ATTR_STATS]) {
1725 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1727 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1728 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1730 if (a[OVS_FLOW_ATTR_USED]) {
1731 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1736 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1737 * followed by Netlink attributes corresponding to 'flow'. */
1739 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1742 struct ovs_header *ovs_header;
1744 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1745 NLM_F_REQUEST | flow->nlmsg_flags,
1746 flow->cmd, OVS_FLOW_VERSION);
1748 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1749 ovs_header->dp_ifindex = flow->dp_ifindex;
1751 if (flow->key_len) {
1752 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1755 if (flow->actions || flow->actions_len) {
1756 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1757 flow->actions, flow->actions_len);
1760 /* We never need to send these to the kernel. */
1761 assert(!flow->stats);
1762 assert(!flow->tcp_flags);
1763 assert(!flow->used);
1766 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1770 /* Clears 'flow' to "empty" values. */
1772 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1774 memset(flow, 0, sizeof *flow);
1777 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1778 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1779 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1780 * result of the command is expected to be a flow also, which is decoded and
1781 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1782 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1784 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1785 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1787 struct ofpbuf *request_buf;
1790 assert((reply != NULL) == (bufp != NULL));
1793 request->nlmsg_flags |= NLM_F_ECHO;
1796 request_buf = ofpbuf_new(1024);
1797 dpif_linux_flow_to_ofpbuf(request, request_buf);
1798 error = nl_sock_transact(genl_sock, request_buf, bufp);
1799 ofpbuf_delete(request_buf);
1803 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1806 dpif_linux_flow_init(reply);
1807 ofpbuf_delete(*bufp);
1815 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1816 struct dpif_flow_stats *stats)
1819 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1820 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1822 stats->n_packets = 0;
1825 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1826 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;