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, OVS_PACKET_VERSION);
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;
1102 dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
1103 (N_UPCALL_SOCKS - 1);
1104 upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
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)) {
1140 dpif_linux_recv_wait(struct dpif *dpif_)
1142 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1145 if (!dpif->listen_mask) {
1149 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1150 nl_sock_wait(dpif->upcall_socks[i], POLLIN);
1155 dpif_linux_recv_purge(struct dpif *dpif_)
1157 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1160 if (!dpif->listen_mask) {
1164 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1165 nl_sock_drain(dpif->upcall_socks[i]);
1169 const struct dpif_class dpif_linux_class = {
1171 dpif_linux_enumerate,
1177 dpif_linux_get_stats,
1178 dpif_linux_port_add,
1179 dpif_linux_port_del,
1180 dpif_linux_port_query_by_number,
1181 dpif_linux_port_query_by_name,
1182 dpif_linux_get_max_ports,
1183 dpif_linux_port_get_pid,
1184 dpif_linux_port_dump_start,
1185 dpif_linux_port_dump_next,
1186 dpif_linux_port_dump_done,
1187 dpif_linux_port_poll,
1188 dpif_linux_port_poll_wait,
1189 dpif_linux_flow_get,
1190 dpif_linux_flow_put,
1191 dpif_linux_flow_del,
1192 dpif_linux_flow_flush,
1193 dpif_linux_flow_dump_start,
1194 dpif_linux_flow_dump_next,
1195 dpif_linux_flow_dump_done,
1198 dpif_linux_recv_get_mask,
1199 dpif_linux_recv_set_mask,
1200 dpif_linux_queue_to_priority,
1202 dpif_linux_recv_wait,
1203 dpif_linux_recv_purge,
1207 dpif_linux_init(void)
1209 static int error = -1;
1212 unsigned int ovs_vport_mcgroup;
1214 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1215 &ovs_datapath_family);
1217 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1218 "The Open vSwitch kernel module is probably not loaded.",
1219 OVS_DATAPATH_FAMILY);
1222 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1225 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1228 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1229 &ovs_packet_family);
1232 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1235 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1237 OVS_VPORT_MCGROUP_FALLBACK_ID);
1240 static struct dpif_linux_vport vport;
1241 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1242 dpif_linux_nln_parse, &vport);
1250 dpif_linux_is_internal_device(const char *name)
1252 struct dpif_linux_vport reply;
1256 error = dpif_linux_vport_get(name, &reply, &buf);
1259 } else if (error != ENODEV && error != ENOENT) {
1260 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1261 name, strerror(error));
1264 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1268 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1269 const void *data, size_t size)
1271 struct ofpbuf actions, key, packet;
1272 struct odputil_keybuf keybuf;
1276 ofpbuf_use_const(&packet, data, size);
1277 flow_extract(&packet, 0, htonll(0), 0, &flow);
1279 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1280 odp_flow_key_from_flow(&key, &flow);
1282 ofpbuf_use_stack(&actions, &action, sizeof action);
1283 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1285 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1286 actions.data, actions.size, &packet);
1290 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1292 struct dpif_linux_vport *vport = vport_;
1293 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1297 dpif_linux_port_changed(const void *vport_, void *dpif_)
1299 const struct dpif_linux_vport *vport = vport_;
1300 struct dpif_linux *dpif = dpif_;
1303 if (vport->dp_ifindex == dpif->dp_ifindex
1304 && (vport->cmd == OVS_VPORT_CMD_NEW
1305 || vport->cmd == OVS_VPORT_CMD_DEL
1306 || vport->cmd == OVS_VPORT_CMD_SET)) {
1307 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1308 dpif->dpif.full_name, vport->name, vport->cmd);
1309 sset_add(&dpif->changed_ports, vport->name);
1312 dpif->change_error = true;
1316 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1317 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1318 * positive errno value.
1320 * 'vport' will contain pointers into 'buf', so the caller should not free
1321 * 'buf' while 'vport' is still in use. */
1323 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1324 const struct ofpbuf *buf)
1326 static const struct nl_policy ovs_vport_policy[] = {
1327 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1328 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1329 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1330 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1331 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1333 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1334 .min_len = ETH_ADDR_LEN,
1335 .max_len = ETH_ADDR_LEN,
1337 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1340 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1341 struct ovs_header *ovs_header;
1342 struct nlmsghdr *nlmsg;
1343 struct genlmsghdr *genl;
1346 dpif_linux_vport_init(vport);
1348 ofpbuf_use_const(&b, buf->data, buf->size);
1349 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1350 genl = ofpbuf_try_pull(&b, sizeof *genl);
1351 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1352 if (!nlmsg || !genl || !ovs_header
1353 || nlmsg->nlmsg_type != ovs_vport_family
1354 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1355 ARRAY_SIZE(ovs_vport_policy))) {
1359 vport->cmd = genl->cmd;
1360 vport->dp_ifindex = ovs_header->dp_ifindex;
1361 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1362 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1363 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1364 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1365 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1367 if (a[OVS_VPORT_ATTR_STATS]) {
1368 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1370 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1371 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1373 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1374 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1375 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1380 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1381 * followed by Netlink attributes corresponding to 'vport'. */
1383 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1386 struct ovs_header *ovs_header;
1388 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1389 vport->cmd, OVS_VPORT_VERSION);
1391 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1392 ovs_header->dp_ifindex = vport->dp_ifindex;
1394 if (vport->port_no != UINT32_MAX) {
1395 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1398 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1399 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1403 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1406 if (vport->upcall_pid) {
1407 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1411 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1412 vport->stats, sizeof *vport->stats);
1415 if (vport->address) {
1416 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1417 vport->address, ETH_ADDR_LEN);
1420 if (vport->options) {
1421 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1422 vport->options, vport->options_len);
1426 /* Clears 'vport' to "empty" values. */
1428 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1430 memset(vport, 0, sizeof *vport);
1431 vport->port_no = UINT32_MAX;
1434 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1435 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1436 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1437 * result of the command is expected to be an ovs_vport also, which is decoded
1438 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1439 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1441 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1442 struct dpif_linux_vport *reply,
1443 struct ofpbuf **bufp)
1445 struct ofpbuf *request_buf;
1448 assert((reply != NULL) == (bufp != NULL));
1450 error = dpif_linux_init();
1454 dpif_linux_vport_init(reply);
1459 request_buf = ofpbuf_new(1024);
1460 dpif_linux_vport_to_ofpbuf(request, request_buf);
1461 error = nl_sock_transact(genl_sock, request_buf, bufp);
1462 ofpbuf_delete(request_buf);
1466 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1469 dpif_linux_vport_init(reply);
1470 ofpbuf_delete(*bufp);
1477 /* Obtains information about the kernel vport named 'name' and stores it into
1478 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1479 * longer needed ('reply' will contain pointers into '*bufp'). */
1481 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1482 struct ofpbuf **bufp)
1484 struct dpif_linux_vport request;
1486 dpif_linux_vport_init(&request);
1487 request.cmd = OVS_VPORT_CMD_GET;
1488 request.name = name;
1490 return dpif_linux_vport_transact(&request, reply, bufp);
1493 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1494 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1495 * positive errno value.
1497 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1498 * while 'dp' is still in use. */
1500 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1502 static const struct nl_policy ovs_datapath_policy[] = {
1503 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1504 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1508 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1509 struct ovs_header *ovs_header;
1510 struct nlmsghdr *nlmsg;
1511 struct genlmsghdr *genl;
1514 dpif_linux_dp_init(dp);
1516 ofpbuf_use_const(&b, buf->data, buf->size);
1517 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1518 genl = ofpbuf_try_pull(&b, sizeof *genl);
1519 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1520 if (!nlmsg || !genl || !ovs_header
1521 || nlmsg->nlmsg_type != ovs_datapath_family
1522 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1523 ARRAY_SIZE(ovs_datapath_policy))) {
1527 dp->cmd = genl->cmd;
1528 dp->dp_ifindex = ovs_header->dp_ifindex;
1529 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1530 if (a[OVS_DP_ATTR_STATS]) {
1531 /* Can't use structure assignment because Netlink doesn't ensure
1532 * sufficient alignment for 64-bit members. */
1533 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1540 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1542 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1544 struct ovs_header *ovs_header;
1546 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1547 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1548 OVS_DATAPATH_VERSION);
1550 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1551 ovs_header->dp_ifindex = dp->dp_ifindex;
1554 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1557 if (dp->upcall_pid) {
1558 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1561 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1564 /* Clears 'dp' to "empty" values. */
1566 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1568 memset(dp, 0, sizeof *dp);
1572 dpif_linux_dp_dump_start(struct nl_dump *dump)
1574 struct dpif_linux_dp request;
1577 dpif_linux_dp_init(&request);
1578 request.cmd = OVS_DP_CMD_GET;
1580 buf = ofpbuf_new(1024);
1581 dpif_linux_dp_to_ofpbuf(&request, buf);
1582 nl_dump_start(dump, genl_sock, buf);
1586 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1587 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1588 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1589 * result of the command is expected to be of the same form, which is decoded
1590 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1591 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1593 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1594 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1596 struct ofpbuf *request_buf;
1599 assert((reply != NULL) == (bufp != NULL));
1601 request_buf = ofpbuf_new(1024);
1602 dpif_linux_dp_to_ofpbuf(request, request_buf);
1603 error = nl_sock_transact(genl_sock, request_buf, bufp);
1604 ofpbuf_delete(request_buf);
1608 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1611 dpif_linux_dp_init(reply);
1612 ofpbuf_delete(*bufp);
1619 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1620 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1621 * will contain pointers into '*bufp'). */
1623 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1624 struct ofpbuf **bufp)
1626 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1627 struct dpif_linux_dp request;
1629 dpif_linux_dp_init(&request);
1630 request.cmd = OVS_DP_CMD_GET;
1631 request.dp_ifindex = dpif->dp_ifindex;
1633 return dpif_linux_dp_transact(&request, reply, bufp);
1636 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1637 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1638 * positive errno value.
1640 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1641 * while 'flow' is still in use. */
1643 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1644 const struct ofpbuf *buf)
1646 static const struct nl_policy ovs_flow_policy[] = {
1647 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1648 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1649 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1651 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1652 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1653 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1656 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1657 struct ovs_header *ovs_header;
1658 struct nlmsghdr *nlmsg;
1659 struct genlmsghdr *genl;
1662 dpif_linux_flow_init(flow);
1664 ofpbuf_use_const(&b, buf->data, buf->size);
1665 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1666 genl = ofpbuf_try_pull(&b, sizeof *genl);
1667 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1668 if (!nlmsg || !genl || !ovs_header
1669 || nlmsg->nlmsg_type != ovs_flow_family
1670 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1671 ARRAY_SIZE(ovs_flow_policy))) {
1675 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1676 flow->dp_ifindex = ovs_header->dp_ifindex;
1677 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1678 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1679 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1680 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1681 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1683 if (a[OVS_FLOW_ATTR_STATS]) {
1684 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1686 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1687 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1689 if (a[OVS_FLOW_ATTR_USED]) {
1690 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1695 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1696 * followed by Netlink attributes corresponding to 'flow'. */
1698 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1701 struct ovs_header *ovs_header;
1703 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1704 NLM_F_REQUEST | flow->nlmsg_flags,
1705 flow->cmd, OVS_FLOW_VERSION);
1707 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1708 ovs_header->dp_ifindex = flow->dp_ifindex;
1710 if (flow->key_len) {
1711 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1714 if (flow->actions || flow->actions_len) {
1715 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1716 flow->actions, flow->actions_len);
1719 /* We never need to send these to the kernel. */
1720 assert(!flow->stats);
1721 assert(!flow->tcp_flags);
1722 assert(!flow->used);
1725 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1729 /* Clears 'flow' to "empty" values. */
1731 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1733 memset(flow, 0, sizeof *flow);
1736 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1737 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1738 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1739 * result of the command is expected to be a flow also, which is decoded and
1740 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1741 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1743 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1744 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1746 struct ofpbuf *request_buf;
1749 assert((reply != NULL) == (bufp != NULL));
1752 request->nlmsg_flags |= NLM_F_ECHO;
1755 request_buf = ofpbuf_new(1024);
1756 dpif_linux_flow_to_ofpbuf(request, request_buf);
1757 error = nl_sock_transact(genl_sock, request_buf, bufp);
1758 ofpbuf_delete(request_buf);
1762 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1765 dpif_linux_flow_init(reply);
1766 ofpbuf_delete(*bufp);
1774 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1775 struct dpif_flow_stats *stats)
1778 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1779 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1781 stats->n_packets = 0;
1784 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1785 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;