2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "dpif-linux.h"
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-notifier.h"
43 #include "netlink-socket.h"
47 #include "openvswitch/datapath-compat.h"
48 #include "openvswitch/tunnel.h"
50 #include "poll-loop.h"
54 #include "unaligned.h"
58 VLOG_DEFINE_THIS_MODULE(dpif_linux);
60 enum { LRU_MAX_PORTS = 1024 };
61 enum { LRU_MASK = LRU_MAX_PORTS - 1};
62 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
64 enum { N_UPCALL_SOCKS = 16 };
65 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
67 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
68 * missing if we have old headers. */
69 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
71 struct dpif_linux_dp {
72 /* Generic Netlink header. */
75 /* struct ovs_header. */
79 const char *name; /* OVS_DP_ATTR_NAME. */
80 uint32_t upcall_pid; /* OVS_DP_UPCALL_PID. */
81 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
82 enum ovs_frag_handling ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
85 static void dpif_linux_dp_init(struct dpif_linux_dp *);
86 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
87 const struct ofpbuf *);
88 static void dpif_linux_dp_dump_start(struct nl_dump *);
89 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
90 struct dpif_linux_dp *reply,
91 struct ofpbuf **bufp);
92 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
93 struct ofpbuf **bufp);
95 struct dpif_linux_flow {
96 /* Generic Netlink header. */
99 /* struct ovs_header. */
100 unsigned int nlmsg_flags;
105 * The 'stats' and 'used' members point to 64-bit data that might only be
106 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
107 * access their values.
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 uint32_t upcall_pid; /* OVS_FLOW_ATTR_UPCALL_PID. */
116 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
117 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
118 const uint64_t *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(const 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 int last_read_upcall;
141 unsigned int listen_mask;
143 /* Change notification. */
144 struct sset changed_ports; /* Ports that have changed. */
145 struct nln_notifier *port_notifier;
148 /* Queue of unused ports. */
149 unsigned long *lru_bitmap;
150 uint16_t lru_ports[LRU_MAX_PORTS];
155 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
157 /* Generic Netlink family numbers for OVS. */
158 static int ovs_datapath_family;
159 static int ovs_vport_family;
160 static int ovs_flow_family;
161 static int ovs_packet_family;
163 /* Generic Netlink socket. */
164 static struct nl_sock *genl_sock;
165 static struct nln *nln = NULL;
167 static int dpif_linux_init(void);
168 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
169 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
170 static void dpif_linux_port_changed(const void *vport, void *dpif);
171 static uint32_t get_upcall_pid_port(struct dpif_linux *, uint32_t port);
172 static uint32_t get_upcall_pid_flow(struct dpif_linux *,
173 const struct nlattr *key, size_t key_len);
175 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
177 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
178 const struct ofpbuf *);
180 static struct dpif_linux *
181 dpif_linux_cast(const struct dpif *dpif)
183 dpif_assert_class(dpif, &dpif_linux_class);
184 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
188 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
190 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
191 bitmap_set1(dp->lru_bitmap, port);
192 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
197 dpif_linux_pop_port(struct dpif_linux *dp)
201 if (dp->lru_head == dp->lru_tail) {
205 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
206 bitmap_set0(dp->lru_bitmap, port);
211 dpif_linux_enumerate(struct sset *all_dps)
217 error = dpif_linux_init();
222 dpif_linux_dp_dump_start(&dump);
223 while (nl_dump_next(&dump, &msg)) {
224 struct dpif_linux_dp dp;
226 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
227 sset_add(all_dps, dp.name);
230 return nl_dump_done(&dump);
234 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
235 bool create, struct dpif **dpifp)
237 struct dpif_linux_dp dp_request, dp;
241 error = dpif_linux_init();
246 /* Create or look up datapath. */
247 dpif_linux_dp_init(&dp_request);
248 dp_request.cmd = create ? OVS_DP_CMD_NEW : OVS_DP_CMD_GET;
249 dp_request.name = name;
250 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
255 open_dpif(&dp, dpifp);
261 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
263 struct dpif_linux *dpif;
266 dpif = xzalloc(sizeof *dpif);
267 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
270 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
271 dp->dp_ifindex, dp->dp_ifindex);
273 dpif->dp_ifindex = dp->dp_ifindex;
274 sset_init(&dpif->changed_ports);
275 *dpifp = &dpif->dpif;
277 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
278 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
279 for (i = 1; i < LRU_MAX_PORTS; i++) {
280 dpif_linux_push_port(dpif, i);
285 destroy_upcall_socks(struct dpif_linux *dpif)
289 for (i = 0; i < N_UPCALL_SOCKS; i++) {
290 nl_sock_destroy(dpif->upcall_socks[i]);
291 dpif->upcall_socks[i] = NULL;
296 dpif_linux_close(struct dpif *dpif_)
298 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
300 nln_notifier_destroy(dpif->port_notifier);
301 destroy_upcall_socks(dpif);
302 sset_destroy(&dpif->changed_ports);
303 free(dpif->lru_bitmap);
308 dpif_linux_destroy(struct dpif *dpif_)
310 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
311 struct dpif_linux_dp dp;
313 dpif_linux_dp_init(&dp);
314 dp.cmd = OVS_DP_CMD_DEL;
315 dp.dp_ifindex = dpif->dp_ifindex;
316 return dpif_linux_dp_transact(&dp, NULL, NULL);
320 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
328 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
336 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
338 struct dpif_linux_dp dp;
342 error = dpif_linux_dp_get(dpif_, &dp, &buf);
351 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
353 struct dpif_linux_dp dp;
357 error = dpif_linux_dp_get(dpif_, &dp, &buf);
359 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
366 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
368 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
369 struct dpif_linux_dp dp;
371 dpif_linux_dp_init(&dp);
372 dp.cmd = OVS_DP_CMD_SET;
373 dp.dp_ifindex = dpif->dp_ifindex;
374 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
375 return dpif_linux_dp_transact(&dp, NULL, NULL);
379 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
382 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
383 const char *name = netdev_get_name(netdev);
384 const char *type = netdev_get_type(netdev);
385 struct dpif_linux_vport request, reply;
386 const struct ofpbuf *options;
390 dpif_linux_vport_init(&request);
391 request.cmd = OVS_VPORT_CMD_NEW;
392 request.dp_ifindex = dpif->dp_ifindex;
393 request.type = netdev_vport_get_vport_type(netdev);
394 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
395 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
396 "unsupported type `%s'",
397 dpif_name(dpif_), name, type);
402 options = netdev_vport_get_options(netdev);
403 if (options && options->size) {
404 request.options = options->data;
405 request.options_len = options->size;
408 if (request.type == OVS_VPORT_TYPE_NETDEV) {
409 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
412 /* Loop until we find a port that isn't used. */
414 request.port_no = dpif_linux_pop_port(dpif);
415 request.upcall_pid = get_upcall_pid_port(dpif, request.port_no);
416 error = dpif_linux_vport_transact(&request, &reply, &buf);
419 *port_nop = reply.port_no;
420 VLOG_DBG("%s: assigning port %"PRIu32" to netlink "
422 dpif_name(dpif_), request.port_no,
426 } while (request.port_no != UINT32_MAX
427 && (error == EBUSY || error == EFBIG));
433 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
435 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
436 struct dpif_linux_vport vport;
439 dpif_linux_vport_init(&vport);
440 vport.cmd = OVS_VPORT_CMD_DEL;
441 vport.dp_ifindex = dpif->dp_ifindex;
442 vport.port_no = port_no;
443 error = dpif_linux_vport_transact(&vport, NULL, NULL);
446 dpif_linux_push_port(dpif, port_no);
452 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
453 const char *port_name, struct dpif_port *dpif_port)
455 struct dpif_linux_vport request;
456 struct dpif_linux_vport reply;
460 dpif_linux_vport_init(&request);
461 request.cmd = OVS_VPORT_CMD_GET;
462 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
463 request.port_no = port_no;
464 request.name = port_name;
466 error = dpif_linux_vport_transact(&request, &reply, &buf);
468 dpif_port->name = xstrdup(reply.name);
469 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
470 dpif_port->port_no = reply.port_no;
477 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
478 struct dpif_port *dpif_port)
480 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
484 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
485 struct dpif_port *dpif_port)
487 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
491 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
493 /* If the datapath increases its range of supported ports, then it should
494 * start reporting that. */
499 dpif_linux_flow_flush(struct dpif *dpif_)
501 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
502 struct dpif_linux_flow flow;
504 dpif_linux_flow_init(&flow);
505 flow.cmd = OVS_FLOW_CMD_DEL;
506 flow.dp_ifindex = dpif->dp_ifindex;
507 return dpif_linux_flow_transact(&flow, NULL, NULL);
510 struct dpif_linux_port_state {
512 unsigned long *port_bitmap; /* Ports in the datapath. */
513 bool complete; /* Dump completed without error. */
517 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
519 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
520 struct dpif_linux_port_state *state;
521 struct dpif_linux_vport request;
524 *statep = state = xmalloc(sizeof *state);
525 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
526 state->complete = false;
528 dpif_linux_vport_init(&request);
529 request.cmd = OVS_DP_CMD_GET;
530 request.dp_ifindex = dpif->dp_ifindex;
532 buf = ofpbuf_new(1024);
533 dpif_linux_vport_to_ofpbuf(&request, buf);
534 nl_dump_start(&state->dump, genl_sock, buf);
541 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
542 struct dpif_port *dpif_port)
544 struct dpif_linux_port_state *state = state_;
545 struct dpif_linux_vport vport;
549 if (!nl_dump_next(&state->dump, &buf)) {
550 state->complete = true;
554 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
559 if (vport.port_no < LRU_MAX_PORTS) {
560 bitmap_set1(state->port_bitmap, vport.port_no);
563 dpif_port->name = (char *) vport.name;
564 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
565 dpif_port->port_no = vport.port_no;
570 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
572 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
573 struct dpif_linux_port_state *state = state_;
574 int error = nl_dump_done(&state->dump);
576 if (state->complete) {
579 for (i = 0; i < LRU_MAX_PORTS; i++) {
580 if (!bitmap_is_set(state->port_bitmap, i)) {
581 dpif_linux_push_port(dpif, i);
586 free(state->port_bitmap);
592 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
594 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
596 if (dpif->change_error) {
597 dpif->change_error = false;
598 sset_clear(&dpif->changed_ports);
600 } else if (!sset_is_empty(&dpif->changed_ports)) {
601 *devnamep = sset_pop(&dpif->changed_ports);
609 dpif_linux_port_poll_wait(const struct dpif *dpif_)
611 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
612 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
613 poll_immediate_wake();
618 dpif_linux_flow_get__(const struct dpif *dpif_,
619 const struct nlattr *key, size_t key_len,
620 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
622 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
623 struct dpif_linux_flow request;
625 dpif_linux_flow_init(&request);
626 request.cmd = OVS_FLOW_CMD_GET;
627 request.dp_ifindex = dpif->dp_ifindex;
629 request.key_len = key_len;
630 return dpif_linux_flow_transact(&request, reply, bufp);
634 dpif_linux_flow_get(const struct dpif *dpif_,
635 const struct nlattr *key, size_t key_len,
636 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
638 struct dpif_linux_flow reply;
642 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
645 dpif_linux_flow_get_stats(&reply, stats);
648 buf->data = (void *) reply.actions;
649 buf->size = reply.actions_len;
659 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
660 const struct nlattr *key, size_t key_len,
661 const struct nlattr *actions, size_t actions_len,
662 struct dpif_flow_stats *stats)
664 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
665 struct dpif_linux_flow request, reply;
666 struct nlattr dummy_action;
670 dpif_linux_flow_init(&request);
671 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
672 request.dp_ifindex = dpif->dp_ifindex;
674 request.key_len = key_len;
675 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
676 request.actions = actions ? actions : &dummy_action;
677 request.actions_len = actions_len;
678 request.upcall_pid = get_upcall_pid_flow(dpif, key, key_len);
679 if (flags & DPIF_FP_ZERO_STATS) {
680 request.clear = true;
682 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
683 error = dpif_linux_flow_transact(&request,
684 stats ? &reply : NULL,
685 stats ? &buf : NULL);
686 if (!error && stats) {
687 dpif_linux_flow_get_stats(&reply, stats);
694 dpif_linux_flow_del(struct dpif *dpif_,
695 const struct nlattr *key, size_t key_len,
696 struct dpif_flow_stats *stats)
698 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
699 struct dpif_linux_flow request, reply;
703 dpif_linux_flow_init(&request);
704 request.cmd = OVS_FLOW_CMD_DEL;
705 request.dp_ifindex = dpif->dp_ifindex;
707 request.key_len = key_len;
708 error = dpif_linux_flow_transact(&request,
709 stats ? &reply : NULL,
710 stats ? &buf : NULL);
711 if (!error && stats) {
712 dpif_linux_flow_get_stats(&reply, stats);
718 struct dpif_linux_flow_state {
720 struct dpif_linux_flow flow;
721 struct dpif_flow_stats stats;
726 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
728 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
729 struct dpif_linux_flow_state *state;
730 struct dpif_linux_flow request;
733 *statep = state = xmalloc(sizeof *state);
735 dpif_linux_flow_init(&request);
736 request.cmd = OVS_DP_CMD_GET;
737 request.dp_ifindex = dpif->dp_ifindex;
739 buf = ofpbuf_new(1024);
740 dpif_linux_flow_to_ofpbuf(&request, buf);
741 nl_dump_start(&state->dump, genl_sock, buf);
750 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
751 const struct nlattr **key, size_t *key_len,
752 const struct nlattr **actions, size_t *actions_len,
753 const struct dpif_flow_stats **stats)
755 struct dpif_linux_flow_state *state = state_;
760 ofpbuf_delete(state->buf);
763 if (!nl_dump_next(&state->dump, &buf)) {
767 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
772 if (actions && !state->flow.actions) {
773 error = dpif_linux_flow_get__(dpif_, state->flow.key,
775 &state->flow, &state->buf);
776 if (error == ENOENT) {
777 VLOG_DBG("dumped flow disappeared on get");
779 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
785 *actions = state->flow.actions;
786 *actions_len = state->flow.actions_len;
789 *key = state->flow.key;
790 *key_len = state->flow.key_len;
793 dpif_linux_flow_get_stats(&state->flow, &state->stats);
794 *stats = &state->stats;
800 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
802 struct dpif_linux_flow_state *state = state_;
803 int error = nl_dump_done(&state->dump);
804 ofpbuf_delete(state->buf);
810 dpif_linux_execute__(int dp_ifindex, uint32_t upcall_pid,
811 const struct nlattr *key, size_t key_len,
812 const struct nlattr *actions, size_t actions_len,
813 const struct ofpbuf *packet)
815 struct ovs_header *execute;
819 buf = ofpbuf_new(128 + actions_len + packet->size);
821 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
822 OVS_PACKET_CMD_EXECUTE, 1);
824 execute = ofpbuf_put_uninit(buf, sizeof *execute);
825 execute->dp_ifindex = dp_ifindex;
827 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
828 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
829 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
830 nl_msg_put_u32(buf, OVS_PACKET_ATTR_UPCALL_PID, upcall_pid);
832 error = nl_sock_transact(genl_sock, buf, NULL);
838 dpif_linux_execute(struct dpif *dpif_,
839 const struct nlattr *key, size_t key_len,
840 const struct nlattr *actions, size_t actions_len,
841 const struct ofpbuf *packet)
843 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
844 uint32_t upcall_pid = get_upcall_pid_flow(dpif, key, key_len);
846 return dpif_linux_execute__(dpif->dp_ifindex, upcall_pid, key, key_len,
847 actions, actions_len, packet);
851 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
853 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
854 *listen_mask = dpif->listen_mask;
859 get_upcall_pid_port__(struct dpif_linux *dpif, uint32_t port)
861 int idx = port & (N_UPCALL_SOCKS - 1);
862 return nl_sock_pid(dpif->upcall_socks[idx]);
866 get_upcall_pid_port(struct dpif_linux *dpif, uint32_t port)
868 if (!(dpif->listen_mask & (1u << DPIF_UC_MISS))) {
872 return get_upcall_pid_port__(dpif, port);
876 get_upcall_pid_flow(struct dpif_linux *dpif,
877 const struct nlattr *key, size_t key_len)
879 const struct nlattr *nla;
882 if (!(dpif->listen_mask & (1u << DPIF_UC_ACTION))) {
886 nla = nl_attr_find__(key, key_len, OVS_KEY_ATTR_IN_PORT);
888 port = nl_attr_get_u32(nla);
890 port = random_uint32();
893 return get_upcall_pid_port__(dpif, port);
897 set_upcall_pids(struct dpif_linux *dpif)
899 struct dpif_port port;
900 struct dpif_port_dump port_dump;
901 struct dpif_flow_dump flow_dump;
902 const struct nlattr *key;
906 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
907 struct dpif_linux_vport vport_request;
909 dpif_linux_vport_init(&vport_request);
910 vport_request.cmd = OVS_VPORT_CMD_SET;
911 vport_request.dp_ifindex = dpif->dp_ifindex;
912 vport_request.port_no = port.port_no;
913 vport_request.upcall_pid = get_upcall_pid_port(dpif,
914 vport_request.port_no);
915 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
917 VLOG_DBG("%s: assigning port %"PRIu32" to netlink "
919 dpif_name(&dpif->dpif), vport_request.port_no,
920 vport_request.upcall_pid);
922 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
923 dpif_name(&dpif->dpif), strerror(error));
927 dpif_flow_dump_start(&flow_dump, &dpif->dpif);
928 while (dpif_flow_dump_next(&flow_dump, &key, &key_len,
930 struct dpif_linux_flow flow_request;
932 dpif_linux_flow_init(&flow_request);
933 flow_request.cmd = OVS_FLOW_CMD_SET;
934 flow_request.dp_ifindex = dpif->dp_ifindex;
935 flow_request.key = key;
936 flow_request.key_len = key_len;
937 flow_request.upcall_pid = get_upcall_pid_flow(dpif, key, key_len);
938 error = dpif_linux_flow_transact(&flow_request, NULL, NULL);
940 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on flow: %s",
941 dpif_name(&dpif->dpif), strerror(error));
944 dpif_flow_dump_done(&flow_dump);
948 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
950 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
952 if (listen_mask == dpif->listen_mask) {
957 destroy_upcall_socks(dpif);
958 } else if (!dpif->listen_mask) {
962 for (i = 0; i < N_UPCALL_SOCKS; i++) {
963 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
965 destroy_upcall_socks(dpif);
971 dpif->listen_mask = listen_mask;
972 set_upcall_pids(dpif);
978 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
979 uint32_t queue_id, uint32_t *priority)
981 if (queue_id < 0xf000) {
982 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
990 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
993 static const struct nl_policy ovs_packet_policy[] = {
994 /* Always present. */
995 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
996 .min_len = ETH_HEADER_LEN },
997 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
999 /* OVS_PACKET_CMD_ACTION only. */
1000 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1003 struct ovs_header *ovs_header;
1004 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1005 struct nlmsghdr *nlmsg;
1006 struct genlmsghdr *genl;
1010 ofpbuf_use_const(&b, buf->data, buf->size);
1012 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1013 genl = ofpbuf_try_pull(&b, sizeof *genl);
1014 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1015 if (!nlmsg || !genl || !ovs_header
1016 || nlmsg->nlmsg_type != ovs_packet_family
1017 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1018 ARRAY_SIZE(ovs_packet_policy))) {
1022 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1023 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1029 memset(upcall, 0, sizeof *upcall);
1030 upcall->type = type;
1031 upcall->packet = buf;
1032 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1033 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1034 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1035 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1036 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1037 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1039 *dp_ifindex = ovs_header->dp_ifindex;
1045 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1047 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1051 if (!dpif->listen_mask) {
1055 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1056 struct nl_sock *upcall_sock;
1057 dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
1058 (N_UPCALL_SOCKS - 1);
1059 upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
1061 if (nl_sock_woke(upcall_sock)) {
1068 if (++read_tries > 50) {
1072 error = nl_sock_recv(upcall_sock, &buf, false);
1073 if (error == EAGAIN) {
1079 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1081 && dp_ifindex == dpif->dp_ifindex
1082 && dpif->listen_mask & (1u << upcall->type)) {
1098 dpif_linux_recv_wait(struct dpif *dpif_)
1100 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1103 if (!dpif->listen_mask) {
1107 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1108 nl_sock_wait(dpif->upcall_socks[i], POLLIN);
1113 dpif_linux_recv_purge(struct dpif *dpif_)
1115 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1118 if (!dpif->listen_mask) {
1122 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1123 nl_sock_drain(dpif->upcall_socks[i]);
1127 const struct dpif_class dpif_linux_class = {
1129 dpif_linux_enumerate,
1135 dpif_linux_get_stats,
1136 dpif_linux_get_drop_frags,
1137 dpif_linux_set_drop_frags,
1138 dpif_linux_port_add,
1139 dpif_linux_port_del,
1140 dpif_linux_port_query_by_number,
1141 dpif_linux_port_query_by_name,
1142 dpif_linux_get_max_ports,
1143 dpif_linux_port_dump_start,
1144 dpif_linux_port_dump_next,
1145 dpif_linux_port_dump_done,
1146 dpif_linux_port_poll,
1147 dpif_linux_port_poll_wait,
1148 dpif_linux_flow_get,
1149 dpif_linux_flow_put,
1150 dpif_linux_flow_del,
1151 dpif_linux_flow_flush,
1152 dpif_linux_flow_dump_start,
1153 dpif_linux_flow_dump_next,
1154 dpif_linux_flow_dump_done,
1156 dpif_linux_recv_get_mask,
1157 dpif_linux_recv_set_mask,
1158 dpif_linux_queue_to_priority,
1160 dpif_linux_recv_wait,
1161 dpif_linux_recv_purge,
1165 dpif_linux_init(void)
1167 static int error = -1;
1170 unsigned int ovs_vport_mcgroup;
1172 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1173 &ovs_datapath_family);
1175 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1176 "The Open vSwitch kernel module is probably not loaded.",
1177 OVS_DATAPATH_FAMILY);
1180 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1183 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1186 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1187 &ovs_packet_family);
1190 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1193 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1195 OVS_VPORT_MCGROUP_FALLBACK_ID);
1198 static struct dpif_linux_vport vport;
1199 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1200 dpif_linux_nln_parse, &vport);
1208 dpif_linux_is_internal_device(const char *name)
1210 struct dpif_linux_vport reply;
1214 error = dpif_linux_vport_get(name, &reply, &buf);
1217 } else if (error != ENODEV && error != ENOENT) {
1218 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1219 name, strerror(error));
1222 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1226 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1227 const void *data, size_t size)
1229 struct ofpbuf actions, key, packet;
1230 struct odputil_keybuf keybuf;
1234 ofpbuf_use_const(&packet, data, size);
1235 flow_extract(&packet, htonll(0), 0, &flow);
1237 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1238 odp_flow_key_from_flow(&key, &flow);
1240 ofpbuf_use_stack(&actions, &action, sizeof action);
1241 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1243 return dpif_linux_execute__(dp_ifindex, 0, key.data, key.size,
1244 actions.data, actions.size, &packet);
1248 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1250 struct dpif_linux_vport *vport = vport_;
1251 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1255 dpif_linux_port_changed(const void *vport_, void *dpif_)
1257 const struct dpif_linux_vport *vport = vport_;
1258 struct dpif_linux *dpif = dpif_;
1261 if (vport->dp_ifindex == dpif->dp_ifindex
1262 && (vport->cmd == OVS_VPORT_CMD_NEW
1263 || vport->cmd == OVS_VPORT_CMD_DEL
1264 || vport->cmd == OVS_VPORT_CMD_SET)) {
1265 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1266 dpif->dpif.full_name, vport->name, vport->cmd);
1267 sset_add(&dpif->changed_ports, vport->name);
1270 dpif->change_error = true;
1274 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1275 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1276 * positive errno value.
1278 * 'vport' will contain pointers into 'buf', so the caller should not free
1279 * 'buf' while 'vport' is still in use. */
1281 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1282 const struct ofpbuf *buf)
1284 static const struct nl_policy ovs_vport_policy[] = {
1285 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1286 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1287 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1288 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1289 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1290 .min_len = sizeof(struct ovs_vport_stats),
1291 .max_len = sizeof(struct ovs_vport_stats),
1293 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1294 .min_len = ETH_ADDR_LEN,
1295 .max_len = ETH_ADDR_LEN,
1297 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1298 [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1301 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1302 struct ovs_header *ovs_header;
1303 struct nlmsghdr *nlmsg;
1304 struct genlmsghdr *genl;
1307 dpif_linux_vport_init(vport);
1309 ofpbuf_use_const(&b, buf->data, buf->size);
1310 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1311 genl = ofpbuf_try_pull(&b, sizeof *genl);
1312 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1313 if (!nlmsg || !genl || !ovs_header
1314 || nlmsg->nlmsg_type != ovs_vport_family
1315 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1316 ARRAY_SIZE(ovs_vport_policy))) {
1320 vport->cmd = genl->cmd;
1321 vport->dp_ifindex = ovs_header->dp_ifindex;
1322 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1323 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1324 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1325 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1326 vport->upcall_pid = nl_attr_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1328 if (a[OVS_VPORT_ATTR_STATS]) {
1329 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1331 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1332 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1334 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1335 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1336 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1338 if (a[OVS_VPORT_ATTR_IFINDEX]) {
1339 vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1344 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1345 * followed by Netlink attributes corresponding to 'vport'. */
1347 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1350 struct ovs_header *ovs_header;
1352 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1355 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1356 ovs_header->dp_ifindex = vport->dp_ifindex;
1358 if (vport->port_no != UINT32_MAX) {
1359 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1362 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1363 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1367 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1370 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1373 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1374 vport->stats, sizeof *vport->stats);
1377 if (vport->address) {
1378 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1379 vport->address, ETH_ADDR_LEN);
1382 if (vport->options) {
1383 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1384 vport->options, vport->options_len);
1387 if (vport->ifindex) {
1388 nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1392 /* Clears 'vport' to "empty" values. */
1394 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1396 memset(vport, 0, sizeof *vport);
1397 vport->port_no = UINT32_MAX;
1400 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1401 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1402 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1403 * result of the command is expected to be an ovs_vport also, which is decoded
1404 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1405 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1407 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1408 struct dpif_linux_vport *reply,
1409 struct ofpbuf **bufp)
1411 struct ofpbuf *request_buf;
1414 assert((reply != NULL) == (bufp != NULL));
1416 error = dpif_linux_init();
1420 dpif_linux_vport_init(reply);
1425 request_buf = ofpbuf_new(1024);
1426 dpif_linux_vport_to_ofpbuf(request, request_buf);
1427 error = nl_sock_transact(genl_sock, request_buf, bufp);
1428 ofpbuf_delete(request_buf);
1432 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1435 dpif_linux_vport_init(reply);
1436 ofpbuf_delete(*bufp);
1443 /* Obtains information about the kernel vport named 'name' and stores it into
1444 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1445 * longer needed ('reply' will contain pointers into '*bufp'). */
1447 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1448 struct ofpbuf **bufp)
1450 struct dpif_linux_vport request;
1452 dpif_linux_vport_init(&request);
1453 request.cmd = OVS_VPORT_CMD_GET;
1454 request.name = name;
1456 return dpif_linux_vport_transact(&request, reply, bufp);
1459 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1460 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1461 * positive errno value.
1463 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1464 * while 'dp' is still in use. */
1466 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1468 static const struct nl_policy ovs_datapath_policy[] = {
1469 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1470 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1471 .min_len = sizeof(struct ovs_dp_stats),
1472 .max_len = sizeof(struct ovs_dp_stats),
1474 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1477 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1478 struct ovs_header *ovs_header;
1479 struct nlmsghdr *nlmsg;
1480 struct genlmsghdr *genl;
1483 dpif_linux_dp_init(dp);
1485 ofpbuf_use_const(&b, buf->data, buf->size);
1486 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1487 genl = ofpbuf_try_pull(&b, sizeof *genl);
1488 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1489 if (!nlmsg || !genl || !ovs_header
1490 || nlmsg->nlmsg_type != ovs_datapath_family
1491 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1492 ARRAY_SIZE(ovs_datapath_policy))) {
1496 dp->cmd = genl->cmd;
1497 dp->dp_ifindex = ovs_header->dp_ifindex;
1498 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1499 if (a[OVS_DP_ATTR_STATS]) {
1500 /* Can't use structure assignment because Netlink doesn't ensure
1501 * sufficient alignment for 64-bit members. */
1502 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1505 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1506 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1512 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1514 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1516 struct ovs_header *ovs_header;
1518 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1519 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1521 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1522 ovs_header->dp_ifindex = dp->dp_ifindex;
1525 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1528 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, dp->upcall_pid);
1530 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1532 if (dp->ipv4_frags) {
1533 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1537 /* Clears 'dp' to "empty" values. */
1539 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1541 memset(dp, 0, sizeof *dp);
1545 dpif_linux_dp_dump_start(struct nl_dump *dump)
1547 struct dpif_linux_dp request;
1550 dpif_linux_dp_init(&request);
1551 request.cmd = OVS_DP_CMD_GET;
1553 buf = ofpbuf_new(1024);
1554 dpif_linux_dp_to_ofpbuf(&request, buf);
1555 nl_dump_start(dump, genl_sock, buf);
1559 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1560 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1561 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1562 * result of the command is expected to be of the same form, which is decoded
1563 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1564 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1566 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1567 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1569 struct ofpbuf *request_buf;
1572 assert((reply != NULL) == (bufp != NULL));
1574 request_buf = ofpbuf_new(1024);
1575 dpif_linux_dp_to_ofpbuf(request, request_buf);
1576 error = nl_sock_transact(genl_sock, request_buf, bufp);
1577 ofpbuf_delete(request_buf);
1581 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1584 dpif_linux_dp_init(reply);
1585 ofpbuf_delete(*bufp);
1592 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1593 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1594 * will contain pointers into '*bufp'). */
1596 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1597 struct ofpbuf **bufp)
1599 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1600 struct dpif_linux_dp request;
1602 dpif_linux_dp_init(&request);
1603 request.cmd = OVS_DP_CMD_GET;
1604 request.dp_ifindex = dpif->dp_ifindex;
1606 return dpif_linux_dp_transact(&request, reply, bufp);
1609 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1610 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1611 * positive errno value.
1613 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1614 * while 'flow' is still in use. */
1616 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1617 const struct ofpbuf *buf)
1619 static const struct nl_policy ovs_flow_policy[] = {
1620 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1621 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1622 [OVS_FLOW_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1623 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1624 .min_len = sizeof(struct ovs_flow_stats),
1625 .max_len = sizeof(struct ovs_flow_stats),
1627 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1628 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1629 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1632 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1633 struct ovs_header *ovs_header;
1634 struct nlmsghdr *nlmsg;
1635 struct genlmsghdr *genl;
1638 dpif_linux_flow_init(flow);
1640 ofpbuf_use_const(&b, buf->data, buf->size);
1641 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1642 genl = ofpbuf_try_pull(&b, sizeof *genl);
1643 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1644 if (!nlmsg || !genl || !ovs_header
1645 || nlmsg->nlmsg_type != ovs_flow_family
1646 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1647 ARRAY_SIZE(ovs_flow_policy))) {
1651 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1652 flow->dp_ifindex = ovs_header->dp_ifindex;
1653 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1654 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1655 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1656 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1657 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1659 if (a[OVS_FLOW_ATTR_UPCALL_PID]) {
1660 flow->upcall_pid = nl_attr_get_u32(a[OVS_FLOW_ATTR_UPCALL_PID]);
1662 if (a[OVS_FLOW_ATTR_STATS]) {
1663 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1665 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1666 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1668 if (a[OVS_FLOW_ATTR_USED]) {
1669 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1674 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1675 * followed by Netlink attributes corresponding to 'flow'. */
1677 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1680 struct ovs_header *ovs_header;
1682 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1683 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1686 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1687 ovs_header->dp_ifindex = flow->dp_ifindex;
1689 if (flow->key_len) {
1690 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1693 if (flow->actions || flow->actions_len) {
1694 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1695 flow->actions, flow->actions_len);
1698 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UPCALL_PID, flow->upcall_pid);
1700 /* We never need to send these to the kernel. */
1701 assert(!flow->stats);
1702 assert(!flow->tcp_flags);
1703 assert(!flow->used);
1706 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1710 /* Clears 'flow' to "empty" values. */
1712 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1714 memset(flow, 0, sizeof *flow);
1717 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1718 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1719 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1720 * result of the command is expected to be a flow also, which is decoded and
1721 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1722 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1724 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1725 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1727 struct ofpbuf *request_buf;
1730 assert((reply != NULL) == (bufp != NULL));
1732 request_buf = ofpbuf_new(1024);
1733 dpif_linux_flow_to_ofpbuf(request, request_buf);
1734 error = nl_sock_transact(genl_sock, request_buf, bufp);
1735 ofpbuf_delete(request_buf);
1739 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1742 dpif_linux_flow_init(reply);
1743 ofpbuf_delete(*bufp);
1751 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1752 struct dpif_flow_stats *stats)
1755 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1756 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1758 stats->n_packets = 0;
1761 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1762 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;