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 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
81 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
84 static void dpif_linux_dp_init(struct dpif_linux_dp *);
85 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
86 const struct ofpbuf *);
87 static void dpif_linux_dp_dump_start(struct nl_dump *);
88 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
89 struct dpif_linux_dp *reply,
90 struct ofpbuf **bufp);
91 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
92 struct ofpbuf **bufp);
94 struct dpif_linux_flow {
95 /* Generic Netlink header. */
98 /* struct ovs_header. */
99 unsigned int nlmsg_flags;
104 * The 'stats' member points to 64-bit data that might only be aligned on
105 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
108 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
109 * the Netlink version of the command, even if actions_len is zero. */
110 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
112 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
114 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
115 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
116 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
117 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
120 static void dpif_linux_flow_init(struct dpif_linux_flow *);
121 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
122 const struct ofpbuf *);
123 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
125 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
126 struct dpif_linux_flow *reply,
127 struct ofpbuf **bufp);
128 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
129 struct dpif_flow_stats *);
131 /* Datapath interface for the openvswitch Linux kernel module. */
136 /* Upcall messages. */
137 struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
138 int last_read_upcall;
139 unsigned int listen_mask;
141 /* Change notification. */
142 struct sset changed_ports; /* Ports that have changed. */
143 struct nln_notifier *port_notifier;
146 /* Queue of unused ports. */
147 unsigned long *lru_bitmap;
148 uint16_t lru_ports[LRU_MAX_PORTS];
153 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
155 /* Generic Netlink family numbers for OVS. */
156 static int ovs_datapath_family;
157 static int ovs_vport_family;
158 static int ovs_flow_family;
159 static int ovs_packet_family;
161 /* Generic Netlink socket. */
162 static struct nl_sock *genl_sock;
163 static struct nln *nln = NULL;
165 static int dpif_linux_init(void);
166 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
167 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
168 static void dpif_linux_port_changed(const void *vport, void *dpif);
169 static uint32_t dpif_linux_port_get_pid__(const struct dpif *,
171 enum dpif_upcall_type);
173 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
175 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
176 const struct ofpbuf *);
178 static struct dpif_linux *
179 dpif_linux_cast(const struct dpif *dpif)
181 dpif_assert_class(dpif, &dpif_linux_class);
182 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
186 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
188 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
189 bitmap_set1(dp->lru_bitmap, port);
190 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
195 dpif_linux_pop_port(struct dpif_linux *dp)
199 if (dp->lru_head == dp->lru_tail) {
203 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
204 bitmap_set0(dp->lru_bitmap, port);
209 dpif_linux_enumerate(struct sset *all_dps)
215 error = dpif_linux_init();
220 dpif_linux_dp_dump_start(&dump);
221 while (nl_dump_next(&dump, &msg)) {
222 struct dpif_linux_dp dp;
224 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
225 sset_add(all_dps, dp.name);
228 return nl_dump_done(&dump);
232 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
233 bool create, struct dpif **dpifp)
235 struct dpif_linux_dp dp_request, dp;
240 error = dpif_linux_init();
245 /* Create or look up datapath. */
246 dpif_linux_dp_init(&dp_request);
248 dp_request.cmd = OVS_DP_CMD_NEW;
250 dp_request.upcall_pid = &upcall_pid;
252 dp_request.cmd = OVS_DP_CMD_GET;
254 dp_request.name = name;
255 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
260 open_dpif(&dp, dpifp);
266 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
268 struct dpif_linux *dpif;
271 dpif = xzalloc(sizeof *dpif);
272 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
275 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
276 dp->dp_ifindex, dp->dp_ifindex);
278 dpif->dp_ifindex = dp->dp_ifindex;
279 sset_init(&dpif->changed_ports);
280 *dpifp = &dpif->dpif;
282 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
283 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
284 for (i = 1; i < LRU_MAX_PORTS; i++) {
285 dpif_linux_push_port(dpif, i);
290 destroy_upcall_socks(struct dpif_linux *dpif)
294 for (i = 0; i < N_UPCALL_SOCKS; i++) {
295 nl_sock_destroy(dpif->upcall_socks[i]);
296 dpif->upcall_socks[i] = NULL;
301 dpif_linux_close(struct dpif *dpif_)
303 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
305 nln_notifier_destroy(dpif->port_notifier);
306 destroy_upcall_socks(dpif);
307 sset_destroy(&dpif->changed_ports);
308 free(dpif->lru_bitmap);
313 dpif_linux_destroy(struct dpif *dpif_)
315 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
316 struct dpif_linux_dp dp;
318 dpif_linux_dp_init(&dp);
319 dp.cmd = OVS_DP_CMD_DEL;
320 dp.dp_ifindex = dpif->dp_ifindex;
321 return dpif_linux_dp_transact(&dp, NULL, NULL);
325 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
333 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
341 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
343 struct dpif_linux_dp dp;
347 error = dpif_linux_dp_get(dpif_, &dp, &buf);
349 stats->n_hit = dp.stats.n_hit;
350 stats->n_missed = dp.stats.n_missed;
351 stats->n_lost = dp.stats.n_lost;
352 stats->n_flows = dp.stats.n_flows;
359 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
362 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
363 const char *name = netdev_get_name(netdev);
364 const char *type = netdev_get_type(netdev);
365 struct dpif_linux_vport request, reply;
366 const struct ofpbuf *options;
370 dpif_linux_vport_init(&request);
371 request.cmd = OVS_VPORT_CMD_NEW;
372 request.dp_ifindex = dpif->dp_ifindex;
373 request.type = netdev_vport_get_vport_type(netdev);
374 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
375 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
376 "unsupported type `%s'",
377 dpif_name(dpif_), name, type);
382 options = netdev_vport_get_options(netdev);
383 if (options && options->size) {
384 request.options = options->data;
385 request.options_len = options->size;
388 if (request.type == OVS_VPORT_TYPE_NETDEV) {
389 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
392 /* Loop until we find a port that isn't used. */
396 request.port_no = dpif_linux_pop_port(dpif);
397 upcall_pid = dpif_linux_port_get_pid__(dpif_, request.port_no,
399 request.upcall_pid = &upcall_pid;
400 error = dpif_linux_vport_transact(&request, &reply, &buf);
403 *port_nop = reply.port_no;
404 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
405 dpif_name(dpif_), request.port_no, upcall_pid);
408 } while (request.port_no != UINT32_MAX
409 && (error == EBUSY || error == EFBIG));
415 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
417 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
418 struct dpif_linux_vport vport;
421 dpif_linux_vport_init(&vport);
422 vport.cmd = OVS_VPORT_CMD_DEL;
423 vport.dp_ifindex = dpif->dp_ifindex;
424 vport.port_no = port_no;
425 error = dpif_linux_vport_transact(&vport, NULL, NULL);
428 dpif_linux_push_port(dpif, port_no);
434 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
435 const char *port_name, struct dpif_port *dpif_port)
437 struct dpif_linux_vport request;
438 struct dpif_linux_vport reply;
442 dpif_linux_vport_init(&request);
443 request.cmd = OVS_VPORT_CMD_GET;
444 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
445 request.port_no = port_no;
446 request.name = port_name;
448 error = dpif_linux_vport_transact(&request, &reply, &buf);
450 dpif_port->name = xstrdup(reply.name);
451 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
452 dpif_port->port_no = reply.port_no;
459 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
460 struct dpif_port *dpif_port)
462 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
466 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
467 struct dpif_port *dpif_port)
469 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
473 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
475 /* If the datapath increases its range of supported ports, then it should
476 * start reporting that. */
481 dpif_linux_port_get_pid__(const struct dpif *dpif_, uint16_t port_no,
482 enum dpif_upcall_type upcall_type)
484 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
486 if (!(dpif->listen_mask & (1u << upcall_type))) {
489 int idx = port_no & (N_UPCALL_SOCKS - 1);
490 return nl_sock_pid(dpif->upcall_socks[idx]);
495 dpif_linux_port_get_pid(const struct dpif *dpif, uint16_t port_no)
497 return dpif_linux_port_get_pid__(dpif, port_no, DPIF_UC_ACTION);
501 dpif_linux_flow_flush(struct dpif *dpif_)
503 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
504 struct dpif_linux_flow flow;
506 dpif_linux_flow_init(&flow);
507 flow.cmd = OVS_FLOW_CMD_DEL;
508 flow.dp_ifindex = dpif->dp_ifindex;
509 return dpif_linux_flow_transact(&flow, NULL, NULL);
512 struct dpif_linux_port_state {
514 unsigned long *port_bitmap; /* Ports in the datapath. */
515 bool complete; /* Dump completed without error. */
519 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
521 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
522 struct dpif_linux_port_state *state;
523 struct dpif_linux_vport request;
526 *statep = state = xmalloc(sizeof *state);
527 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
528 state->complete = false;
530 dpif_linux_vport_init(&request);
531 request.cmd = OVS_DP_CMD_GET;
532 request.dp_ifindex = dpif->dp_ifindex;
534 buf = ofpbuf_new(1024);
535 dpif_linux_vport_to_ofpbuf(&request, buf);
536 nl_dump_start(&state->dump, genl_sock, buf);
543 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
544 struct dpif_port *dpif_port)
546 struct dpif_linux_port_state *state = state_;
547 struct dpif_linux_vport vport;
551 if (!nl_dump_next(&state->dump, &buf)) {
552 state->complete = true;
556 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
561 if (vport.port_no < LRU_MAX_PORTS) {
562 bitmap_set1(state->port_bitmap, vport.port_no);
565 dpif_port->name = (char *) vport.name;
566 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
567 dpif_port->port_no = vport.port_no;
572 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
574 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
575 struct dpif_linux_port_state *state = state_;
576 int error = nl_dump_done(&state->dump);
578 if (state->complete) {
581 for (i = 0; i < LRU_MAX_PORTS; i++) {
582 if (!bitmap_is_set(state->port_bitmap, i)) {
583 dpif_linux_push_port(dpif, i);
588 free(state->port_bitmap);
594 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
596 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
598 if (dpif->change_error) {
599 dpif->change_error = false;
600 sset_clear(&dpif->changed_ports);
602 } else if (!sset_is_empty(&dpif->changed_ports)) {
603 *devnamep = sset_pop(&dpif->changed_ports);
611 dpif_linux_port_poll_wait(const struct dpif *dpif_)
613 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
614 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
615 poll_immediate_wake();
620 dpif_linux_flow_get__(const struct dpif *dpif_,
621 const struct nlattr *key, size_t key_len,
622 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
624 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
625 struct dpif_linux_flow request;
627 dpif_linux_flow_init(&request);
628 request.cmd = OVS_FLOW_CMD_GET;
629 request.dp_ifindex = dpif->dp_ifindex;
631 request.key_len = key_len;
632 return dpif_linux_flow_transact(&request, reply, bufp);
636 dpif_linux_flow_get(const struct dpif *dpif_,
637 const struct nlattr *key, size_t key_len,
638 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
640 struct dpif_linux_flow reply;
644 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
647 dpif_linux_flow_get_stats(&reply, stats);
650 buf->data = (void *) reply.actions;
651 buf->size = reply.actions_len;
661 dpif_linux_init_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
662 const struct nlattr *key, size_t key_len,
663 const struct nlattr *actions, size_t actions_len,
664 struct dpif_linux_flow *request)
666 static struct nlattr dummy_action;
668 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
670 dpif_linux_flow_init(request);
671 request->cmd = (flags & DPIF_FP_CREATE
672 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
673 request->dp_ifindex = dpif->dp_ifindex;
675 request->key_len = key_len;
676 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
677 request->actions = actions ? actions : &dummy_action;
678 request->actions_len = actions_len;
679 if (flags & DPIF_FP_ZERO_STATS) {
680 request->clear = true;
682 request->nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
686 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
687 const struct nlattr *key, size_t key_len,
688 const struct nlattr *actions, size_t actions_len,
689 struct dpif_flow_stats *stats)
691 struct dpif_linux_flow request, reply;
695 dpif_linux_init_flow_put(dpif_, flags, key, key_len, actions, actions_len,
697 error = dpif_linux_flow_transact(&request,
698 stats ? &reply : NULL,
699 stats ? &buf : NULL);
700 if (!error && stats) {
701 dpif_linux_flow_get_stats(&reply, stats);
708 dpif_linux_flow_del(struct dpif *dpif_,
709 const struct nlattr *key, size_t key_len,
710 struct dpif_flow_stats *stats)
712 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
713 struct dpif_linux_flow request, reply;
717 dpif_linux_flow_init(&request);
718 request.cmd = OVS_FLOW_CMD_DEL;
719 request.dp_ifindex = dpif->dp_ifindex;
721 request.key_len = key_len;
722 error = dpif_linux_flow_transact(&request,
723 stats ? &reply : NULL,
724 stats ? &buf : NULL);
725 if (!error && stats) {
726 dpif_linux_flow_get_stats(&reply, stats);
732 struct dpif_linux_flow_state {
734 struct dpif_linux_flow flow;
735 struct dpif_flow_stats stats;
740 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
742 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
743 struct dpif_linux_flow_state *state;
744 struct dpif_linux_flow request;
747 *statep = state = xmalloc(sizeof *state);
749 dpif_linux_flow_init(&request);
750 request.cmd = OVS_DP_CMD_GET;
751 request.dp_ifindex = dpif->dp_ifindex;
753 buf = ofpbuf_new(1024);
754 dpif_linux_flow_to_ofpbuf(&request, buf);
755 nl_dump_start(&state->dump, genl_sock, buf);
764 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
765 const struct nlattr **key, size_t *key_len,
766 const struct nlattr **actions, size_t *actions_len,
767 const struct dpif_flow_stats **stats)
769 struct dpif_linux_flow_state *state = state_;
774 ofpbuf_delete(state->buf);
777 if (!nl_dump_next(&state->dump, &buf)) {
781 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
786 if (actions && !state->flow.actions) {
787 error = dpif_linux_flow_get__(dpif_, state->flow.key,
789 &state->flow, &state->buf);
790 if (error == ENOENT) {
791 VLOG_DBG("dumped flow disappeared on get");
793 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
799 *actions = state->flow.actions;
800 *actions_len = state->flow.actions_len;
803 *key = state->flow.key;
804 *key_len = state->flow.key_len;
807 dpif_linux_flow_get_stats(&state->flow, &state->stats);
808 *stats = &state->stats;
814 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
816 struct dpif_linux_flow_state *state = state_;
817 int error = nl_dump_done(&state->dump);
818 ofpbuf_delete(state->buf);
823 static struct ofpbuf *
824 dpif_linux_encode_execute(int dp_ifindex,
825 const struct nlattr *key, size_t key_len,
826 const struct nlattr *actions, size_t actions_len,
827 const struct ofpbuf *packet)
829 struct ovs_header *execute;
832 buf = ofpbuf_new(128 + actions_len + packet->size);
834 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
835 OVS_PACKET_CMD_EXECUTE, 1);
837 execute = ofpbuf_put_uninit(buf, sizeof *execute);
838 execute->dp_ifindex = dp_ifindex;
840 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
841 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
842 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
848 dpif_linux_execute__(int dp_ifindex, const struct nlattr *key, size_t key_len,
849 const struct nlattr *actions, size_t actions_len,
850 const struct ofpbuf *packet)
852 struct ofpbuf *request;
855 request = dpif_linux_encode_execute(dp_ifindex,
856 key, key_len, actions, actions_len,
858 error = nl_sock_transact(genl_sock, request, NULL);
859 ofpbuf_delete(request);
865 dpif_linux_execute(struct dpif *dpif_,
866 const struct nlattr *key, size_t key_len,
867 const struct nlattr *actions, size_t actions_len,
868 const struct ofpbuf *packet)
870 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
872 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
873 actions, actions_len, packet);
877 dpif_linux_operate(struct dpif *dpif_, union dpif_op **ops, size_t n_ops)
879 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
880 struct nl_transaction **txnsp;
881 struct nl_transaction *txns;
884 txns = xmalloc(n_ops * sizeof *txns);
885 for (i = 0; i < n_ops; i++) {
886 struct nl_transaction *txn = &txns[i];
887 union dpif_op *op = ops[i];
889 if (op->type == DPIF_OP_FLOW_PUT) {
890 struct dpif_flow_put *put = &op->flow_put;
891 struct dpif_linux_flow request;
893 dpif_linux_init_flow_put(dpif_, put->flags, put->key, put->key_len,
894 put->actions, put->actions_len,
897 request.nlmsg_flags |= NLM_F_ECHO;
899 txn->request = ofpbuf_new(1024);
900 dpif_linux_flow_to_ofpbuf(&request, txn->request);
901 } else if (op->type == DPIF_OP_EXECUTE) {
902 struct dpif_execute *execute = &op->execute;
904 txn->request = dpif_linux_encode_execute(
905 dpif->dp_ifindex, execute->key, execute->key_len,
906 execute->actions, execute->actions_len, execute->packet);
912 txnsp = xmalloc(n_ops * sizeof *txnsp);
913 for (i = 0; i < n_ops; i++) {
917 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
921 for (i = 0; i < n_ops; i++) {
922 struct nl_transaction *txn = &txns[i];
923 union dpif_op *op = ops[i];
925 if (op->type == DPIF_OP_FLOW_PUT) {
926 struct dpif_flow_put *put = &op->flow_put;
927 int error = txn->error;
929 if (!error && put->stats) {
930 struct dpif_linux_flow reply;
932 error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
934 dpif_linux_flow_get_stats(&reply, put->stats);
938 } else if (op->type == DPIF_OP_EXECUTE) {
939 struct dpif_execute *execute = &op->execute;
941 execute->error = txn->error;
946 ofpbuf_delete(txn->request);
947 ofpbuf_delete(txn->reply);
953 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
955 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
956 *listen_mask = dpif->listen_mask;
961 set_upcall_pids(struct dpif *dpif_)
963 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
964 struct dpif_port_dump port_dump;
965 struct dpif_port port;
968 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
969 uint32_t upcall_pid = dpif_linux_port_get_pid__(dpif_, port.port_no,
971 struct dpif_linux_vport vport_request;
973 dpif_linux_vport_init(&vport_request);
974 vport_request.cmd = OVS_VPORT_CMD_SET;
975 vport_request.dp_ifindex = dpif->dp_ifindex;
976 vport_request.port_no = port.port_no;
977 vport_request.upcall_pid = &upcall_pid;
978 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
980 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
981 dpif_name(&dpif->dpif), vport_request.port_no,
984 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
985 dpif_name(&dpif->dpif), strerror(error));
991 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
993 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
995 if (listen_mask == dpif->listen_mask) {
1000 destroy_upcall_socks(dpif);
1001 } else if (!dpif->listen_mask) {
1005 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1006 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
1008 destroy_upcall_socks(dpif);
1014 dpif->listen_mask = listen_mask;
1015 set_upcall_pids(dpif_);
1021 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1022 uint32_t queue_id, uint32_t *priority)
1024 if (queue_id < 0xf000) {
1025 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1033 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1036 static const struct nl_policy ovs_packet_policy[] = {
1037 /* Always present. */
1038 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1039 .min_len = ETH_HEADER_LEN },
1040 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1042 /* OVS_PACKET_CMD_ACTION only. */
1043 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1046 struct ovs_header *ovs_header;
1047 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1048 struct nlmsghdr *nlmsg;
1049 struct genlmsghdr *genl;
1053 ofpbuf_use_const(&b, buf->data, buf->size);
1055 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1056 genl = ofpbuf_try_pull(&b, sizeof *genl);
1057 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1058 if (!nlmsg || !genl || !ovs_header
1059 || nlmsg->nlmsg_type != ovs_packet_family
1060 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1061 ARRAY_SIZE(ovs_packet_policy))) {
1065 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1066 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1072 memset(upcall, 0, sizeof *upcall);
1073 upcall->type = type;
1074 upcall->packet = buf;
1075 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1076 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1077 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1078 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1079 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1080 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1082 *dp_ifindex = ovs_header->dp_ifindex;
1088 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1090 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1094 if (!dpif->listen_mask) {
1098 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1099 struct nl_sock *upcall_sock;
1100 dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
1101 (N_UPCALL_SOCKS - 1);
1102 upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
1104 if (nl_sock_woke(upcall_sock)) {
1111 if (++read_tries > 50) {
1115 error = nl_sock_recv(upcall_sock, &buf, false);
1116 if (error == EAGAIN) {
1122 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1124 && dp_ifindex == dpif->dp_ifindex
1125 && dpif->listen_mask & (1u << upcall->type)) {
1141 dpif_linux_recv_wait(struct dpif *dpif_)
1143 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1146 if (!dpif->listen_mask) {
1150 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1151 nl_sock_wait(dpif->upcall_socks[i], POLLIN);
1156 dpif_linux_recv_purge(struct dpif *dpif_)
1158 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1161 if (!dpif->listen_mask) {
1165 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1166 nl_sock_drain(dpif->upcall_socks[i]);
1170 const struct dpif_class dpif_linux_class = {
1172 dpif_linux_enumerate,
1178 dpif_linux_get_stats,
1179 dpif_linux_port_add,
1180 dpif_linux_port_del,
1181 dpif_linux_port_query_by_number,
1182 dpif_linux_port_query_by_name,
1183 dpif_linux_get_max_ports,
1184 dpif_linux_port_get_pid,
1185 dpif_linux_port_dump_start,
1186 dpif_linux_port_dump_next,
1187 dpif_linux_port_dump_done,
1188 dpif_linux_port_poll,
1189 dpif_linux_port_poll_wait,
1190 dpif_linux_flow_get,
1191 dpif_linux_flow_put,
1192 dpif_linux_flow_del,
1193 dpif_linux_flow_flush,
1194 dpif_linux_flow_dump_start,
1195 dpif_linux_flow_dump_next,
1196 dpif_linux_flow_dump_done,
1199 dpif_linux_recv_get_mask,
1200 dpif_linux_recv_set_mask,
1201 dpif_linux_queue_to_priority,
1203 dpif_linux_recv_wait,
1204 dpif_linux_recv_purge,
1208 dpif_linux_init(void)
1210 static int error = -1;
1213 unsigned int ovs_vport_mcgroup;
1215 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1216 &ovs_datapath_family);
1218 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1219 "The Open vSwitch kernel module is probably not loaded.",
1220 OVS_DATAPATH_FAMILY);
1223 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1226 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1229 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1230 &ovs_packet_family);
1233 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1236 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1238 OVS_VPORT_MCGROUP_FALLBACK_ID);
1241 static struct dpif_linux_vport vport;
1242 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1243 dpif_linux_nln_parse, &vport);
1251 dpif_linux_is_internal_device(const char *name)
1253 struct dpif_linux_vport reply;
1257 error = dpif_linux_vport_get(name, &reply, &buf);
1260 } else if (error != ENODEV && error != ENOENT) {
1261 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1262 name, strerror(error));
1265 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1269 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1270 const void *data, size_t size)
1272 struct ofpbuf actions, key, packet;
1273 struct odputil_keybuf keybuf;
1277 ofpbuf_use_const(&packet, data, size);
1278 flow_extract(&packet, htonll(0), 0, &flow);
1280 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1281 odp_flow_key_from_flow(&key, &flow);
1283 ofpbuf_use_stack(&actions, &action, sizeof action);
1284 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1286 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1287 actions.data, actions.size, &packet);
1291 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1293 struct dpif_linux_vport *vport = vport_;
1294 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1298 dpif_linux_port_changed(const void *vport_, void *dpif_)
1300 const struct dpif_linux_vport *vport = vport_;
1301 struct dpif_linux *dpif = dpif_;
1304 if (vport->dp_ifindex == dpif->dp_ifindex
1305 && (vport->cmd == OVS_VPORT_CMD_NEW
1306 || vport->cmd == OVS_VPORT_CMD_DEL
1307 || vport->cmd == OVS_VPORT_CMD_SET)) {
1308 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1309 dpif->dpif.full_name, vport->name, vport->cmd);
1310 sset_add(&dpif->changed_ports, vport->name);
1313 dpif->change_error = true;
1317 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1318 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1319 * positive errno value.
1321 * 'vport' will contain pointers into 'buf', so the caller should not free
1322 * 'buf' while 'vport' is still in use. */
1324 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1325 const struct ofpbuf *buf)
1327 static const struct nl_policy ovs_vport_policy[] = {
1328 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1329 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1330 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1331 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1332 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1333 .min_len = sizeof(struct ovs_vport_stats),
1334 .max_len = sizeof(struct ovs_vport_stats),
1336 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1337 .min_len = ETH_ADDR_LEN,
1338 .max_len = ETH_ADDR_LEN,
1340 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1343 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1344 struct ovs_header *ovs_header;
1345 struct nlmsghdr *nlmsg;
1346 struct genlmsghdr *genl;
1349 dpif_linux_vport_init(vport);
1351 ofpbuf_use_const(&b, buf->data, buf->size);
1352 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1353 genl = ofpbuf_try_pull(&b, sizeof *genl);
1354 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1355 if (!nlmsg || !genl || !ovs_header
1356 || nlmsg->nlmsg_type != ovs_vport_family
1357 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1358 ARRAY_SIZE(ovs_vport_policy))) {
1362 vport->cmd = genl->cmd;
1363 vport->dp_ifindex = ovs_header->dp_ifindex;
1364 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1365 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1366 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1367 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1368 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1370 if (a[OVS_VPORT_ATTR_STATS]) {
1371 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1373 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1374 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1376 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1377 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1378 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1383 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1384 * followed by Netlink attributes corresponding to 'vport'. */
1386 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1389 struct ovs_header *ovs_header;
1391 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1394 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1395 ovs_header->dp_ifindex = vport->dp_ifindex;
1397 if (vport->port_no != UINT32_MAX) {
1398 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1401 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1402 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1406 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1409 if (vport->upcall_pid) {
1410 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1414 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1415 vport->stats, sizeof *vport->stats);
1418 if (vport->address) {
1419 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1420 vport->address, ETH_ADDR_LEN);
1423 if (vport->options) {
1424 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1425 vport->options, vport->options_len);
1429 /* Clears 'vport' to "empty" values. */
1431 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1433 memset(vport, 0, sizeof *vport);
1434 vport->port_no = UINT32_MAX;
1437 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1438 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1439 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1440 * result of the command is expected to be an ovs_vport also, which is decoded
1441 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1442 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1444 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1445 struct dpif_linux_vport *reply,
1446 struct ofpbuf **bufp)
1448 struct ofpbuf *request_buf;
1451 assert((reply != NULL) == (bufp != NULL));
1453 error = dpif_linux_init();
1457 dpif_linux_vport_init(reply);
1462 request_buf = ofpbuf_new(1024);
1463 dpif_linux_vport_to_ofpbuf(request, request_buf);
1464 error = nl_sock_transact(genl_sock, request_buf, bufp);
1465 ofpbuf_delete(request_buf);
1469 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1472 dpif_linux_vport_init(reply);
1473 ofpbuf_delete(*bufp);
1480 /* Obtains information about the kernel vport named 'name' and stores it into
1481 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1482 * longer needed ('reply' will contain pointers into '*bufp'). */
1484 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1485 struct ofpbuf **bufp)
1487 struct dpif_linux_vport request;
1489 dpif_linux_vport_init(&request);
1490 request.cmd = OVS_VPORT_CMD_GET;
1491 request.name = name;
1493 return dpif_linux_vport_transact(&request, reply, bufp);
1496 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1497 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1498 * positive errno value.
1500 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1501 * while 'dp' is still in use. */
1503 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1505 static const struct nl_policy ovs_datapath_policy[] = {
1506 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1507 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1508 .min_len = sizeof(struct ovs_dp_stats),
1509 .max_len = sizeof(struct ovs_dp_stats),
1513 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1514 struct ovs_header *ovs_header;
1515 struct nlmsghdr *nlmsg;
1516 struct genlmsghdr *genl;
1519 dpif_linux_dp_init(dp);
1521 ofpbuf_use_const(&b, buf->data, buf->size);
1522 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1523 genl = ofpbuf_try_pull(&b, sizeof *genl);
1524 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1525 if (!nlmsg || !genl || !ovs_header
1526 || nlmsg->nlmsg_type != ovs_datapath_family
1527 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1528 ARRAY_SIZE(ovs_datapath_policy))) {
1532 dp->cmd = genl->cmd;
1533 dp->dp_ifindex = ovs_header->dp_ifindex;
1534 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1535 if (a[OVS_DP_ATTR_STATS]) {
1536 /* Can't use structure assignment because Netlink doesn't ensure
1537 * sufficient alignment for 64-bit members. */
1538 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1545 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1547 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1549 struct ovs_header *ovs_header;
1551 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1552 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1554 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1555 ovs_header->dp_ifindex = dp->dp_ifindex;
1558 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1561 if (dp->upcall_pid) {
1562 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1565 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1568 /* Clears 'dp' to "empty" values. */
1570 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1572 memset(dp, 0, sizeof *dp);
1576 dpif_linux_dp_dump_start(struct nl_dump *dump)
1578 struct dpif_linux_dp request;
1581 dpif_linux_dp_init(&request);
1582 request.cmd = OVS_DP_CMD_GET;
1584 buf = ofpbuf_new(1024);
1585 dpif_linux_dp_to_ofpbuf(&request, buf);
1586 nl_dump_start(dump, genl_sock, buf);
1590 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1591 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1592 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1593 * result of the command is expected to be of the same form, which is decoded
1594 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1595 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1597 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1598 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1600 struct ofpbuf *request_buf;
1603 assert((reply != NULL) == (bufp != NULL));
1605 request_buf = ofpbuf_new(1024);
1606 dpif_linux_dp_to_ofpbuf(request, request_buf);
1607 error = nl_sock_transact(genl_sock, request_buf, bufp);
1608 ofpbuf_delete(request_buf);
1612 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1615 dpif_linux_dp_init(reply);
1616 ofpbuf_delete(*bufp);
1623 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1624 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1625 * will contain pointers into '*bufp'). */
1627 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1628 struct ofpbuf **bufp)
1630 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1631 struct dpif_linux_dp request;
1633 dpif_linux_dp_init(&request);
1634 request.cmd = OVS_DP_CMD_GET;
1635 request.dp_ifindex = dpif->dp_ifindex;
1637 return dpif_linux_dp_transact(&request, reply, bufp);
1640 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1641 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1642 * positive errno value.
1644 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1645 * while 'flow' is still in use. */
1647 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1648 const struct ofpbuf *buf)
1650 static const struct nl_policy ovs_flow_policy[] = {
1651 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1652 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1653 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1654 .min_len = sizeof(struct ovs_flow_stats),
1655 .max_len = sizeof(struct ovs_flow_stats),
1657 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1658 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1659 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1662 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1663 struct ovs_header *ovs_header;
1664 struct nlmsghdr *nlmsg;
1665 struct genlmsghdr *genl;
1668 dpif_linux_flow_init(flow);
1670 ofpbuf_use_const(&b, buf->data, buf->size);
1671 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1672 genl = ofpbuf_try_pull(&b, sizeof *genl);
1673 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1674 if (!nlmsg || !genl || !ovs_header
1675 || nlmsg->nlmsg_type != ovs_flow_family
1676 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1677 ARRAY_SIZE(ovs_flow_policy))) {
1681 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1682 flow->dp_ifindex = ovs_header->dp_ifindex;
1683 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1684 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1685 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1686 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1687 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1689 if (a[OVS_FLOW_ATTR_STATS]) {
1690 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1692 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1693 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1695 if (a[OVS_FLOW_ATTR_USED]) {
1696 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1701 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1702 * followed by Netlink attributes corresponding to 'flow'. */
1704 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1707 struct ovs_header *ovs_header;
1709 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1710 NLM_F_REQUEST | flow->nlmsg_flags,
1713 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1714 ovs_header->dp_ifindex = flow->dp_ifindex;
1716 if (flow->key_len) {
1717 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1720 if (flow->actions || flow->actions_len) {
1721 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1722 flow->actions, flow->actions_len);
1725 /* We never need to send these to the kernel. */
1726 assert(!flow->stats);
1727 assert(!flow->tcp_flags);
1728 assert(!flow->used);
1731 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1735 /* Clears 'flow' to "empty" values. */
1737 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1739 memset(flow, 0, sizeof *flow);
1742 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1743 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1744 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1745 * result of the command is expected to be a flow also, which is decoded and
1746 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1747 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1749 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1750 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1752 struct ofpbuf *request_buf;
1755 assert((reply != NULL) == (bufp != NULL));
1758 request->nlmsg_flags |= NLM_F_ECHO;
1761 request_buf = ofpbuf_new(1024);
1762 dpif_linux_flow_to_ofpbuf(request, request_buf);
1763 error = nl_sock_transact(genl_sock, request_buf, bufp);
1764 ofpbuf_delete(request_buf);
1768 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1771 dpif_linux_flow_init(reply);
1772 ofpbuf_delete(*bufp);
1780 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1781 struct dpif_flow_stats *stats)
1784 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1785 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1787 stats->n_packets = 0;
1790 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1791 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;