2 * Copyright (c) 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.
25 #include <netinet/in.h>
26 #include <sys/socket.h>
31 #include <sys/ioctl.h>
37 #include "dpif-provider.h"
39 #include "dynamic-string.h"
46 #include "ofp-print.h"
49 #include "poll-loop.h"
56 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
58 /* Configuration parameters. */
59 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
60 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
62 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
63 * headers to be aligned on a 4-byte boundary. */
64 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
67 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
68 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
69 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
70 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
72 struct dp_netdev_queue {
73 struct dpif_upcall *upcalls[MAX_QUEUE_LEN];
74 unsigned int head, tail;
77 /* Datapath based on the network device interface from netdev.h. */
79 const struct dpif_class *class;
84 struct dp_netdev_queue queues[N_QUEUES];
85 struct hmap flow_table; /* Flow table. */
88 long long int n_hit; /* Number of flow table matches. */
89 long long int n_missed; /* Number of flow table misses. */
90 long long int n_lost; /* Number of misses not passed to client. */
93 struct dp_netdev_port *ports[MAX_PORTS];
94 struct list port_list;
98 /* A port in a netdev-based datapath. */
99 struct dp_netdev_port {
100 int port_no; /* Index into dp_netdev's 'ports'. */
101 struct list node; /* Element in dp_netdev's 'port_list'. */
102 struct netdev *netdev;
103 bool internal; /* Internal port? */
106 /* A flow in dp_netdev's 'flow_table'. */
107 struct dp_netdev_flow {
108 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
112 long long int used; /* Last used time, in monotonic msecs. */
113 long long int packet_count; /* Number of packets matched. */
114 long long int byte_count; /* Number of bytes matched. */
115 ovs_be16 tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
118 struct nlattr *actions;
122 /* Interface to netdev-based datapath. */
125 struct dp_netdev *dp;
127 unsigned int dp_serial;
130 /* All netdev-based datapaths. */
131 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
133 /* Maximum port MTU seen so far. */
134 static int max_mtu = ETH_PAYLOAD_MAX;
136 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
137 struct dp_netdev_port **portp);
138 static int get_port_by_name(struct dp_netdev *, const char *devname,
139 struct dp_netdev_port **portp);
140 static void dp_netdev_free(struct dp_netdev *);
141 static void dp_netdev_flow_flush(struct dp_netdev *);
142 static int do_add_port(struct dp_netdev *, const char *devname,
143 const char *type, uint16_t port_no);
144 static int do_del_port(struct dp_netdev *, uint16_t port_no);
145 static int dpif_netdev_open(const struct dpif_class *, const char *name,
146 bool create, struct dpif **);
147 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
148 int queue_no, const struct flow *,
150 static void dp_netdev_execute_actions(struct dp_netdev *,
151 struct ofpbuf *, struct flow *,
152 const struct nlattr *actions,
155 static struct dpif_class dpif_dummy_class;
157 static struct dpif_netdev *
158 dpif_netdev_cast(const struct dpif *dpif)
160 assert(dpif->dpif_class->open == dpif_netdev_open);
161 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
164 static struct dp_netdev *
165 get_dp_netdev(const struct dpif *dpif)
167 return dpif_netdev_cast(dpif)->dp;
171 create_dpif_netdev(struct dp_netdev *dp)
173 uint16_t netflow_id = hash_string(dp->name, 0);
174 struct dpif_netdev *dpif;
178 dpif = xmalloc(sizeof *dpif);
179 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
181 dpif->listen_mask = 0;
182 dpif->dp_serial = dp->serial;
188 create_dp_netdev(const char *name, const struct dpif_class *class,
189 struct dp_netdev **dpp)
191 struct dp_netdev *dp;
195 dp = xzalloc(sizeof *dp);
197 dp->name = xstrdup(name);
199 for (i = 0; i < N_QUEUES; i++) {
200 dp->queues[i].head = dp->queues[i].tail = 0;
202 hmap_init(&dp->flow_table);
203 list_init(&dp->port_list);
204 error = do_add_port(dp, name, "internal", OVSP_LOCAL);
210 shash_add(&dp_netdevs, name, dp);
217 dpif_netdev_open(const struct dpif_class *class, const char *name,
218 bool create, struct dpif **dpifp)
220 struct dp_netdev *dp;
222 dp = shash_find_data(&dp_netdevs, name);
227 int error = create_dp_netdev(name, class, &dp);
234 if (dp->class != class) {
241 *dpifp = create_dpif_netdev(dp);
246 dp_netdev_purge_queues(struct dp_netdev *dp)
250 for (i = 0; i < N_QUEUES; i++) {
251 struct dp_netdev_queue *q = &dp->queues[i];
253 while (q->tail != q->head) {
254 struct dpif_upcall *upcall = q->upcalls[q->tail++ & QUEUE_MASK];
256 ofpbuf_delete(upcall->packet);
263 dp_netdev_free(struct dp_netdev *dp)
265 struct dp_netdev_port *port, *next;
267 dp_netdev_flow_flush(dp);
268 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
269 do_del_port(dp, port->port_no);
271 dp_netdev_purge_queues(dp);
272 hmap_destroy(&dp->flow_table);
278 dpif_netdev_close(struct dpif *dpif)
280 struct dp_netdev *dp = get_dp_netdev(dpif);
281 assert(dp->open_cnt > 0);
282 if (--dp->open_cnt == 0 && dp->destroyed) {
283 shash_find_and_delete(&dp_netdevs, dp->name);
290 dpif_netdev_destroy(struct dpif *dpif)
292 struct dp_netdev *dp = get_dp_netdev(dpif);
293 dp->destroyed = true;
298 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
300 struct dp_netdev *dp = get_dp_netdev(dpif);
301 stats->n_flows = hmap_count(&dp->flow_table);
302 stats->n_hit = dp->n_hit;
303 stats->n_missed = dp->n_missed;
304 stats->n_lost = dp->n_lost;
309 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
312 struct dp_netdev_port *port;
313 struct netdev *netdev;
318 /* XXX reject devices already in some dp_netdev. */
319 if (type[0] == '\0' || !strcmp(type, "system") || !strcmp(type, "dummy")) {
321 } else if (!strcmp(type, "internal")) {
324 VLOG_WARN("%s: unsupported port type %s", devname, type);
328 /* Open and validate network device. */
329 if (dp->class == &dpif_dummy_class) {
331 } else if (internal) {
335 error = netdev_open(devname, type, &netdev);
339 /* XXX reject loopback devices */
340 /* XXX reject non-Ethernet devices */
342 error = netdev_listen(netdev);
344 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
345 devname, strerror(errno));
346 netdev_close(netdev);
350 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
352 netdev_close(netdev);
356 port = xmalloc(sizeof *port);
357 port->port_no = port_no;
358 port->netdev = netdev;
359 port->internal = internal;
361 error = netdev_get_mtu(netdev, &mtu);
366 list_push_back(&dp->port_list, &port->node);
367 dp->ports[port_no] = port;
374 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
377 struct dp_netdev *dp = get_dp_netdev(dpif);
380 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
381 if (!dp->ports[port_no]) {
383 return do_add_port(dp, netdev_get_name(netdev),
384 netdev_get_type(netdev), port_no);
391 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
393 struct dp_netdev *dp = get_dp_netdev(dpif);
394 return port_no == OVSP_LOCAL ? EINVAL : do_del_port(dp, port_no);
398 is_valid_port_number(uint16_t port_no)
400 return port_no < MAX_PORTS;
404 get_port_by_number(struct dp_netdev *dp,
405 uint16_t port_no, struct dp_netdev_port **portp)
407 if (!is_valid_port_number(port_no)) {
411 *portp = dp->ports[port_no];
412 return *portp ? 0 : ENOENT;
417 get_port_by_name(struct dp_netdev *dp,
418 const char *devname, struct dp_netdev_port **portp)
420 struct dp_netdev_port *port;
422 LIST_FOR_EACH (port, node, &dp->port_list) {
423 if (!strcmp(netdev_get_name(port->netdev), devname)) {
432 do_del_port(struct dp_netdev *dp, uint16_t port_no)
434 struct dp_netdev_port *port;
438 error = get_port_by_number(dp, port_no, &port);
443 list_remove(&port->node);
444 dp->ports[port->port_no] = NULL;
447 name = xstrdup(netdev_get_name(port->netdev));
448 netdev_close(port->netdev);
457 answer_port_query(const struct dp_netdev_port *port,
458 struct dpif_port *dpif_port)
460 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
461 dpif_port->type = xstrdup(port->internal ? "internal" : "system");
462 dpif_port->port_no = port->port_no;
466 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
467 struct dpif_port *dpif_port)
469 struct dp_netdev *dp = get_dp_netdev(dpif);
470 struct dp_netdev_port *port;
473 error = get_port_by_number(dp, port_no, &port);
475 answer_port_query(port, dpif_port);
481 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
482 struct dpif_port *dpif_port)
484 struct dp_netdev *dp = get_dp_netdev(dpif);
485 struct dp_netdev_port *port;
488 error = get_port_by_name(dp, devname, &port);
490 answer_port_query(port, dpif_port);
496 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
502 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
504 hmap_remove(&dp->flow_table, &flow->node);
510 dp_netdev_flow_flush(struct dp_netdev *dp)
512 struct dp_netdev_flow *flow, *next;
514 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
515 dp_netdev_free_flow(dp, flow);
520 dpif_netdev_flow_flush(struct dpif *dpif)
522 struct dp_netdev *dp = get_dp_netdev(dpif);
523 dp_netdev_flow_flush(dp);
527 struct dp_netdev_port_state {
533 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
535 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
540 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
541 struct dpif_port *dpif_port)
543 struct dp_netdev_port_state *state = state_;
544 struct dp_netdev *dp = get_dp_netdev(dpif);
547 for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
548 struct dp_netdev_port *port = dp->ports[port_no];
551 state->name = xstrdup(netdev_get_name(port->netdev));
552 dpif_port->name = state->name;
553 dpif_port->type = port->internal ? "internal" : "system";
554 dpif_port->port_no = port->port_no;
555 state->port_no = port_no + 1;
563 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
565 struct dp_netdev_port_state *state = state_;
572 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
574 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
575 if (dpif->dp_serial != dpif->dp->serial) {
576 dpif->dp_serial = dpif->dp->serial;
584 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
586 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
587 if (dpif->dp_serial != dpif->dp->serial) {
588 poll_immediate_wake();
592 static struct dp_netdev_flow *
593 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
595 struct dp_netdev_flow *flow;
597 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
598 if (flow_equal(&flow->key, key)) {
606 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
608 stats->n_packets = flow->packet_count;
609 stats->n_bytes = flow->byte_count;
610 stats->used = flow->used;
611 stats->tcp_flags = TCP_FLAGS(flow->tcp_ctl);
615 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
618 if (odp_flow_key_to_flow(key, key_len, flow)) {
619 /* This should not happen: it indicates that odp_flow_key_from_flow()
620 * and odp_flow_key_to_flow() disagree on the acceptable form of a
621 * flow. Log the problem as an error, with enough details to enable
623 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
625 if (!VLOG_DROP_ERR(&rl)) {
629 odp_flow_key_format(key, key_len, &s);
630 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
637 if (flow->in_port < OFPP_MAX
638 ? flow->in_port >= MAX_PORTS
639 : flow->in_port != OFPP_LOCAL && flow->in_port != OFPP_NONE) {
647 dpif_netdev_flow_get(const struct dpif *dpif,
648 const struct nlattr *nl_key, size_t nl_key_len,
649 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
651 struct dp_netdev *dp = get_dp_netdev(dpif);
652 struct dp_netdev_flow *flow;
656 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
661 flow = dp_netdev_lookup_flow(dp, &key);
667 get_dpif_flow_stats(flow, stats);
670 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
676 set_flow_actions(struct dp_netdev_flow *flow,
677 const struct nlattr *actions, size_t actions_len)
679 flow->actions = xrealloc(flow->actions, actions_len);
680 flow->actions_len = actions_len;
681 memcpy(flow->actions, actions, actions_len);
686 add_flow(struct dpif *dpif, const struct flow *key,
687 const struct nlattr *actions, size_t actions_len)
689 struct dp_netdev *dp = get_dp_netdev(dpif);
690 struct dp_netdev_flow *flow;
693 flow = xzalloc(sizeof *flow);
696 error = set_flow_actions(flow, actions, actions_len);
702 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
707 clear_stats(struct dp_netdev_flow *flow)
710 flow->packet_count = 0;
711 flow->byte_count = 0;
716 dpif_netdev_flow_put(struct dpif *dpif, enum dpif_flow_put_flags flags,
717 const struct nlattr *nl_key, size_t nl_key_len,
718 const struct nlattr *actions, size_t actions_len,
719 struct dpif_flow_stats *stats)
721 struct dp_netdev *dp = get_dp_netdev(dpif);
722 struct dp_netdev_flow *flow;
726 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
731 flow = dp_netdev_lookup_flow(dp, &key);
733 if (flags & DPIF_FP_CREATE) {
734 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
736 memset(stats, 0, sizeof *stats);
738 return add_flow(dpif, &key, actions, actions_len);
746 if (flags & DPIF_FP_MODIFY) {
747 int error = set_flow_actions(flow, actions, actions_len);
750 get_dpif_flow_stats(flow, stats);
752 if (flags & DPIF_FP_ZERO_STATS) {
764 dpif_netdev_flow_del(struct dpif *dpif,
765 const struct nlattr *nl_key, size_t nl_key_len,
766 struct dpif_flow_stats *stats)
768 struct dp_netdev *dp = get_dp_netdev(dpif);
769 struct dp_netdev_flow *flow;
773 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
778 flow = dp_netdev_lookup_flow(dp, &key);
781 get_dpif_flow_stats(flow, stats);
783 dp_netdev_free_flow(dp, flow);
790 struct dp_netdev_flow_state {
793 struct nlattr *actions;
794 struct odputil_keybuf keybuf;
795 struct dpif_flow_stats stats;
799 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
801 struct dp_netdev_flow_state *state;
803 *statep = state = xmalloc(sizeof *state);
806 state->actions = NULL;
811 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
812 const struct nlattr **key, size_t *key_len,
813 const struct nlattr **actions, size_t *actions_len,
814 const struct dpif_flow_stats **stats)
816 struct dp_netdev_flow_state *state = state_;
817 struct dp_netdev *dp = get_dp_netdev(dpif);
818 struct dp_netdev_flow *flow;
819 struct hmap_node *node;
821 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
826 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
831 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
832 odp_flow_key_from_flow(&buf, &flow->key);
839 free(state->actions);
840 state->actions = xmemdup(flow->actions, flow->actions_len);
842 *actions = state->actions;
843 *actions_len = flow->actions_len;
847 get_dpif_flow_stats(flow, &state->stats);
848 *stats = &state->stats;
855 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
857 struct dp_netdev_flow_state *state = state_;
859 free(state->actions);
865 dpif_netdev_execute(struct dpif *dpif,
866 const struct nlattr *key_attrs, size_t key_len,
867 const struct nlattr *actions, size_t actions_len,
868 const struct ofpbuf *packet)
870 struct dp_netdev *dp = get_dp_netdev(dpif);
875 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
879 /* Make a deep copy of 'packet', because we might modify its data. */
880 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
881 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
882 ofpbuf_put(©, packet->data, packet->size);
884 flow_extract(©, 0, 0, -1, &key);
885 error = dpif_netdev_flow_from_nlattrs(key_attrs, key_len, &key);
887 dp_netdev_execute_actions(dp, ©, &key,
888 actions, actions_len);
891 ofpbuf_uninit(©);
896 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
898 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
899 *listen_mask = dpif_netdev->listen_mask;
904 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
906 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
907 dpif_netdev->listen_mask = listen_mask;
912 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
913 uint32_t queue_id, uint32_t *priority)
915 *priority = queue_id;
919 static struct dp_netdev_queue *
920 find_nonempty_queue(struct dpif *dpif)
922 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
923 struct dp_netdev *dp = get_dp_netdev(dpif);
924 int mask = dpif_netdev->listen_mask;
927 for (i = 0; i < N_QUEUES; i++) {
928 struct dp_netdev_queue *q = &dp->queues[i];
929 if (q->head != q->tail && mask & (1u << i)) {
937 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall)
939 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
941 struct dpif_upcall *u = q->upcalls[q->tail++ & QUEUE_MASK];
952 dpif_netdev_recv_wait(struct dpif *dpif)
954 if (find_nonempty_queue(dpif)) {
955 poll_immediate_wake();
957 /* No messages ready to be received, and dp_wait() will ensure that we
958 * wake up to queue new messages, so there is nothing to do. */
963 dpif_netdev_recv_purge(struct dpif *dpif)
965 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
966 dp_netdev_purge_queues(dpif_netdev->dp);
970 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
971 const struct ofpbuf *packet)
973 flow->used = time_msec();
974 flow->packet_count++;
975 flow->byte_count += packet->size;
976 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
977 struct tcp_header *th = packet->l4;
978 flow->tcp_ctl |= th->tcp_ctl;
983 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
984 struct ofpbuf *packet)
986 struct dp_netdev_flow *flow;
989 if (packet->size < ETH_HEADER_LEN) {
992 flow_extract(packet, 0, 0, port->port_no, &key);
993 flow = dp_netdev_lookup_flow(dp, &key);
995 dp_netdev_flow_used(flow, &key, packet);
996 dp_netdev_execute_actions(dp, packet, &key,
997 flow->actions, flow->actions_len);
1001 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, 0);
1006 dpif_netdev_run(struct dpif *dpif)
1008 struct dp_netdev *dp = get_dp_netdev(dpif);
1009 struct dp_netdev_port *port;
1010 struct ofpbuf packet;
1012 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1014 LIST_FOR_EACH (port, node, &dp->port_list) {
1017 /* Reset packet contents. */
1018 ofpbuf_clear(&packet);
1019 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1021 error = netdev_recv(port->netdev, &packet);
1023 dp_netdev_port_input(dp, port, &packet);
1024 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1025 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1026 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1027 netdev_get_name(port->netdev), strerror(error));
1030 ofpbuf_uninit(&packet);
1034 dpif_netdev_wait(struct dpif *dpif)
1036 struct dp_netdev *dp = get_dp_netdev(dpif);
1037 struct dp_netdev_port *port;
1039 LIST_FOR_EACH (port, node, &dp->port_list) {
1040 netdev_recv_wait(port->netdev);
1045 dp_netdev_pop_vlan(struct ofpbuf *packet)
1047 struct vlan_eth_header *veh = packet->l2;
1048 if (packet->size >= sizeof *veh
1049 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1050 struct eth_header tmp;
1052 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1053 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1054 tmp.eth_type = veh->veth_next_type;
1056 ofpbuf_pull(packet, VLAN_HEADER_LEN);
1057 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1058 memcpy(packet->data, &tmp, sizeof tmp);
1063 dp_netdev_set_dl(struct ofpbuf *packet, const struct ovs_key_ethernet *eth_key)
1065 struct eth_header *eh = packet->l2;
1067 memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
1068 memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
1072 dp_netdev_set_ip_addr(struct ofpbuf *packet, ovs_be32 *addr, ovs_be32 new_addr)
1074 struct ip_header *nh = packet->l3;
1076 if (nh->ip_proto == IPPROTO_TCP && packet->l7) {
1077 struct tcp_header *th = packet->l4;
1078 th->tcp_csum = recalc_csum32(th->tcp_csum, *addr, new_addr);
1079 } else if (nh->ip_proto == IPPROTO_UDP && packet->l7) {
1080 struct udp_header *uh = packet->l4;
1082 uh->udp_csum = recalc_csum32(uh->udp_csum, *addr, new_addr);
1083 if (!uh->udp_csum) {
1084 uh->udp_csum = htons(0xffff);
1088 nh->ip_csum = recalc_csum32(nh->ip_csum, *addr, new_addr);
1093 dp_netdev_set_ip_tos(struct ip_header *nh, uint8_t new_tos)
1095 uint8_t *field = &nh->ip_tos;
1097 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1098 htons((uint16_t) new_tos));
1103 dp_netdev_set_ip_ttl(struct ip_header *nh, uint8_t new_ttl)
1105 uint8_t *field = &nh->ip_ttl;
1107 nh->ip_csum = recalc_csum16(nh->ip_csum, htons(*field << 8),
1108 htons(new_ttl << 8));
1113 dp_netdev_set_ipv4(struct ofpbuf *packet, const struct ovs_key_ipv4 *ipv4_key)
1115 struct ip_header *nh = packet->l3;
1117 if (nh->ip_src != ipv4_key->ipv4_src) {
1118 dp_netdev_set_ip_addr(packet, &nh->ip_src, ipv4_key->ipv4_src);
1120 if (nh->ip_dst != ipv4_key->ipv4_dst) {
1121 dp_netdev_set_ip_addr(packet, &nh->ip_dst, ipv4_key->ipv4_dst);
1123 if (nh->ip_tos != ipv4_key->ipv4_tos) {
1124 dp_netdev_set_ip_tos(nh, ipv4_key->ipv4_tos);
1126 if (nh->ip_ttl != ipv4_key->ipv4_ttl) {
1127 dp_netdev_set_ip_ttl(nh, ipv4_key->ipv4_ttl);
1132 dp_netdev_set_port(ovs_be16 *port, ovs_be16 new_port, ovs_be16 *csum)
1134 *csum = recalc_csum16(*csum, *port, new_port);
1139 dp_netdev_set_tcp_port(struct ofpbuf *packet, const struct ovs_key_tcp *tcp_key)
1141 struct tcp_header *th = packet->l4;
1143 if (th->tcp_src != tcp_key->tcp_src) {
1144 dp_netdev_set_port(&th->tcp_src, tcp_key->tcp_src, &th->tcp_csum);
1146 if (th->tcp_dst != tcp_key->tcp_dst) {
1147 dp_netdev_set_port(&th->tcp_dst, tcp_key->tcp_dst, &th->tcp_csum);
1152 dp_netdev_set_udp_port(struct ofpbuf *packet, const struct ovs_key_udp *udp_key)
1154 struct udp_header *uh = packet->l4;
1156 if (uh->udp_src != udp_key->udp_src) {
1157 dp_netdev_set_port(&uh->udp_src, udp_key->udp_src, &uh->udp_csum);
1159 if (uh->udp_dst != udp_key->udp_dst) {
1160 dp_netdev_set_port(&uh->udp_dst, udp_key->udp_dst, &uh->udp_csum);
1165 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1168 struct dp_netdev_port *p = dp->ports[out_port];
1170 netdev_send(p->netdev, packet);
1175 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1176 int queue_no, const struct flow *flow, uint64_t arg)
1178 struct dp_netdev_queue *q = &dp->queues[queue_no];
1179 struct dpif_upcall *upcall;
1183 if (q->head - q->tail >= MAX_QUEUE_LEN) {
1188 buf = ofpbuf_new(ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size);
1189 odp_flow_key_from_flow(buf, flow);
1190 key_len = buf->size;
1191 ofpbuf_pull(buf, key_len);
1192 ofpbuf_reserve(buf, 2);
1193 ofpbuf_put(buf, packet->data, packet->size);
1195 upcall = xzalloc(sizeof *upcall);
1196 upcall->type = queue_no;
1197 upcall->packet = buf;
1198 upcall->key = buf->base;
1199 upcall->key_len = key_len;
1200 upcall->userdata = arg;
1202 q->upcalls[q->head++ & QUEUE_MASK] = upcall;
1208 dp_netdev_sample(struct dp_netdev *dp,
1209 struct ofpbuf *packet, struct flow *key,
1210 const struct nlattr *action)
1212 const struct nlattr *subactions = NULL;
1213 const struct nlattr *a;
1216 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
1217 int type = nl_attr_type(a);
1219 switch ((enum ovs_sample_attr) type) {
1220 case OVS_SAMPLE_ATTR_PROBABILITY:
1221 if (random_uint32() >= nl_attr_get_u32(a)) {
1226 case OVS_SAMPLE_ATTR_ACTIONS:
1230 case OVS_SAMPLE_ATTR_UNSPEC:
1231 case __OVS_SAMPLE_ATTR_MAX:
1237 dp_netdev_execute_actions(dp, packet, key, nl_attr_get(subactions),
1238 nl_attr_get_size(subactions));
1242 dp_netdev_action_userspace(struct dp_netdev *dp,
1243 struct ofpbuf *packet, struct flow *key,
1244 const struct nlattr *a)
1246 const struct nlattr *userdata_attr;
1249 userdata_attr = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1250 userdata = userdata_attr ? nl_attr_get_u64(userdata_attr) : 0;
1251 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1255 execute_set_action(struct ofpbuf *packet, const struct nlattr *a)
1257 enum ovs_key_attr type = nl_attr_type(a);
1259 case OVS_KEY_ATTR_TUN_ID:
1260 case OVS_KEY_ATTR_PRIORITY:
1261 /* not implemented */
1264 case OVS_KEY_ATTR_ETHERNET:
1265 dp_netdev_set_dl(packet,
1266 nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
1269 case OVS_KEY_ATTR_IPV4:
1270 dp_netdev_set_ipv4(packet,
1271 nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4)));
1274 case OVS_KEY_ATTR_TCP:
1275 dp_netdev_set_tcp_port(packet,
1276 nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp)));
1279 case OVS_KEY_ATTR_UDP:
1280 dp_netdev_set_udp_port(packet,
1281 nl_attr_get_unspec(a, sizeof(struct ovs_key_udp)));
1284 case OVS_KEY_ATTR_UNSPEC:
1285 case OVS_KEY_ATTR_ENCAP:
1286 case OVS_KEY_ATTR_ETHERTYPE:
1287 case OVS_KEY_ATTR_IPV6:
1288 case OVS_KEY_ATTR_IN_PORT:
1289 case OVS_KEY_ATTR_VLAN:
1290 case OVS_KEY_ATTR_ICMP:
1291 case OVS_KEY_ATTR_ICMPV6:
1292 case OVS_KEY_ATTR_ARP:
1293 case OVS_KEY_ATTR_ND:
1294 case __OVS_KEY_ATTR_MAX:
1301 dp_netdev_execute_actions(struct dp_netdev *dp,
1302 struct ofpbuf *packet, struct flow *key,
1303 const struct nlattr *actions,
1306 const struct nlattr *a;
1309 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1310 const struct ovs_action_push_vlan *vlan;
1311 int type = nl_attr_type(a);
1313 switch ((enum ovs_action_attr) type) {
1314 case OVS_ACTION_ATTR_OUTPUT:
1315 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1318 case OVS_ACTION_ATTR_USERSPACE:
1319 dp_netdev_action_userspace(dp, packet, key, a);
1322 case OVS_ACTION_ATTR_PUSH_VLAN:
1323 vlan = nl_attr_get(a);
1324 eth_push_vlan(packet, vlan->vlan_tci & ~htons(VLAN_CFI));
1327 case OVS_ACTION_ATTR_POP_VLAN:
1328 dp_netdev_pop_vlan(packet);
1331 case OVS_ACTION_ATTR_SET:
1332 execute_set_action(packet, nl_attr_get(a));
1335 case OVS_ACTION_ATTR_SAMPLE:
1336 dp_netdev_sample(dp, packet, key, a);
1339 case OVS_ACTION_ATTR_UNSPEC:
1340 case __OVS_ACTION_ATTR_MAX:
1346 const struct dpif_class dpif_netdev_class = {
1348 NULL, /* enumerate */
1351 dpif_netdev_destroy,
1354 dpif_netdev_get_stats,
1355 dpif_netdev_port_add,
1356 dpif_netdev_port_del,
1357 dpif_netdev_port_query_by_number,
1358 dpif_netdev_port_query_by_name,
1359 dpif_netdev_get_max_ports,
1360 NULL, /* port_get_pid */
1361 dpif_netdev_port_dump_start,
1362 dpif_netdev_port_dump_next,
1363 dpif_netdev_port_dump_done,
1364 dpif_netdev_port_poll,
1365 dpif_netdev_port_poll_wait,
1366 dpif_netdev_flow_get,
1367 dpif_netdev_flow_put,
1368 dpif_netdev_flow_del,
1369 dpif_netdev_flow_flush,
1370 dpif_netdev_flow_dump_start,
1371 dpif_netdev_flow_dump_next,
1372 dpif_netdev_flow_dump_done,
1373 dpif_netdev_execute,
1375 dpif_netdev_recv_get_mask,
1376 dpif_netdev_recv_set_mask,
1377 dpif_netdev_queue_to_priority,
1379 dpif_netdev_recv_wait,
1380 dpif_netdev_recv_purge,
1384 dpif_dummy_register(void)
1386 if (!dpif_dummy_class.type) {
1387 dpif_dummy_class = dpif_netdev_class;
1388 dpif_dummy_class.type = "dummy";
1389 dp_register_provider(&dpif_dummy_class);