2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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"
57 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
59 /* Configuration parameters. */
60 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
61 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
63 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
64 * headers to be aligned on a 4-byte boundary. */
65 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
68 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
69 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
70 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
71 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
73 struct dp_netdev_upcall {
74 struct dpif_upcall upcall; /* Queued upcall information. */
75 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
78 struct dp_netdev_queue {
79 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN];
80 unsigned int head, tail;
83 /* Datapath based on the network device interface from netdev.h. */
85 const struct dpif_class *class;
90 struct dp_netdev_queue queues[N_QUEUES];
91 struct hmap flow_table; /* Flow table. */
94 long long int n_hit; /* Number of flow table matches. */
95 long long int n_missed; /* Number of flow table misses. */
96 long long int n_lost; /* Number of misses not passed to client. */
99 struct dp_netdev_port *ports[MAX_PORTS];
100 struct list port_list;
104 /* A port in a netdev-based datapath. */
105 struct dp_netdev_port {
106 int port_no; /* Index into dp_netdev's 'ports'. */
107 struct list node; /* Element in dp_netdev's 'port_list'. */
108 struct netdev *netdev;
109 char *type; /* Port type as requested by user. */
112 /* A flow in dp_netdev's 'flow_table'. */
113 struct dp_netdev_flow {
114 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
118 long long int used; /* Last used time, in monotonic msecs. */
119 long long int packet_count; /* Number of packets matched. */
120 long long int byte_count; /* Number of bytes matched. */
121 uint8_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
124 struct nlattr *actions;
128 /* Interface to netdev-based datapath. */
131 struct dp_netdev *dp;
132 unsigned int dp_serial;
135 /* All netdev-based datapaths. */
136 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
138 /* Maximum port MTU seen so far. */
139 static int max_mtu = ETH_PAYLOAD_MAX;
141 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
142 struct dp_netdev_port **portp);
143 static int get_port_by_name(struct dp_netdev *, const char *devname,
144 struct dp_netdev_port **portp);
145 static void dp_netdev_free(struct dp_netdev *);
146 static void dp_netdev_flow_flush(struct dp_netdev *);
147 static int do_add_port(struct dp_netdev *, const char *devname,
148 const char *type, uint16_t port_no);
149 static int do_del_port(struct dp_netdev *, uint16_t port_no);
150 static int dpif_netdev_open(const struct dpif_class *, const char *name,
151 bool create, struct dpif **);
152 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
153 int queue_no, const struct flow *,
155 static void dp_netdev_execute_actions(struct dp_netdev *,
156 struct ofpbuf *, struct flow *,
157 const struct nlattr *actions,
160 static struct dpif_netdev *
161 dpif_netdev_cast(const struct dpif *dpif)
163 assert(dpif->dpif_class->open == dpif_netdev_open);
164 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
167 static struct dp_netdev *
168 get_dp_netdev(const struct dpif *dpif)
170 return dpif_netdev_cast(dpif)->dp;
174 dpif_netdev_enumerate(struct sset *all_dps)
176 struct shash_node *node;
178 SHASH_FOR_EACH(node, &dp_netdevs) {
179 sset_add(all_dps, node->name);
185 create_dpif_netdev(struct dp_netdev *dp)
187 uint16_t netflow_id = hash_string(dp->name, 0);
188 struct dpif_netdev *dpif;
192 dpif = xmalloc(sizeof *dpif);
193 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
195 dpif->dp_serial = dp->serial;
201 create_dp_netdev(const char *name, const struct dpif_class *class,
202 struct dp_netdev **dpp)
204 struct dp_netdev *dp;
208 dp = xzalloc(sizeof *dp);
210 dp->name = xstrdup(name);
212 for (i = 0; i < N_QUEUES; i++) {
213 dp->queues[i].head = dp->queues[i].tail = 0;
215 hmap_init(&dp->flow_table);
216 list_init(&dp->port_list);
217 error = do_add_port(dp, name, "internal", OVSP_LOCAL);
223 shash_add(&dp_netdevs, name, dp);
230 dpif_netdev_open(const struct dpif_class *class, const char *name,
231 bool create, struct dpif **dpifp)
233 struct dp_netdev *dp;
235 dp = shash_find_data(&dp_netdevs, name);
240 int error = create_dp_netdev(name, class, &dp);
247 if (dp->class != class) {
254 *dpifp = create_dpif_netdev(dp);
259 dp_netdev_purge_queues(struct dp_netdev *dp)
263 for (i = 0; i < N_QUEUES; i++) {
264 struct dp_netdev_queue *q = &dp->queues[i];
266 while (q->tail != q->head) {
267 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
268 ofpbuf_uninit(&u->buf);
274 dp_netdev_free(struct dp_netdev *dp)
276 struct dp_netdev_port *port, *next;
278 dp_netdev_flow_flush(dp);
279 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
280 do_del_port(dp, port->port_no);
282 dp_netdev_purge_queues(dp);
283 hmap_destroy(&dp->flow_table);
289 dpif_netdev_close(struct dpif *dpif)
291 struct dp_netdev *dp = get_dp_netdev(dpif);
292 assert(dp->open_cnt > 0);
293 if (--dp->open_cnt == 0 && dp->destroyed) {
294 shash_find_and_delete(&dp_netdevs, dp->name);
301 dpif_netdev_destroy(struct dpif *dpif)
303 struct dp_netdev *dp = get_dp_netdev(dpif);
304 dp->destroyed = true;
309 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
311 struct dp_netdev *dp = get_dp_netdev(dpif);
312 stats->n_flows = hmap_count(&dp->flow_table);
313 stats->n_hit = dp->n_hit;
314 stats->n_missed = dp->n_missed;
315 stats->n_lost = dp->n_lost;
319 static const char* internal_port_type(const struct dp_netdev* dp)
321 if (dp->class == &dpif_netdev_class)
323 if (dp->class == &dpif_planetlab_class)
329 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
332 struct dp_netdev_port *port;
333 struct netdev *netdev;
334 const char *open_type;
338 /* XXX reject devices already in some dp_netdev. */
340 /* Open and validate network device. */
341 open_type = (strcmp(type, "internal") ? type : internal_port_type(dp));
342 error = netdev_open(devname, open_type, &netdev);
346 /* XXX reject loopback devices */
347 /* XXX reject non-Ethernet devices */
349 error = netdev_listen(netdev);
351 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
352 devname, strerror(errno));
353 netdev_close(netdev);
357 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
359 netdev_close(netdev);
363 port = xmalloc(sizeof *port);
364 port->port_no = port_no;
365 port->netdev = netdev;
366 port->type = xstrdup(type);
368 error = netdev_get_mtu(netdev, &mtu);
373 list_push_back(&dp->port_list, &port->node);
374 dp->ports[port_no] = port;
381 choose_port(struct dpif *dpif, struct netdev *netdev)
383 struct dp_netdev *dp = get_dp_netdev(dpif);
386 if (dpif->dpif_class != &dpif_netdev_class &&
387 dpif->dpif_class != &dpif_planetlab_class)
389 /* If the port name contains a number, try to assign that port number.
390 * This can make writing unit tests easier because port numbers are
394 for (p = netdev_get_name(netdev); *p != '\0'; p++) {
395 if (isdigit((unsigned char) *p)) {
396 port_no = strtol(p, NULL, 10);
397 if (port_no > 0 && port_no < MAX_PORTS
398 && !dp->ports[port_no]) {
406 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
407 if (!dp->ports[port_no]) {
416 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
419 struct dp_netdev *dp = get_dp_netdev(dpif);
422 if (*port_nop != UINT16_MAX) {
423 if (*port_nop >= MAX_PORTS) {
425 } else if (dp->ports[*port_nop]) {
430 port_no = choose_port(dpif, netdev);
434 return do_add_port(dp, netdev_get_name(netdev),
435 netdev_get_type(netdev), port_no);
441 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
443 struct dp_netdev *dp = get_dp_netdev(dpif);
444 return port_no == OVSP_LOCAL ? EINVAL : do_del_port(dp, port_no);
448 is_valid_port_number(uint16_t port_no)
450 return port_no < MAX_PORTS;
454 get_port_by_number(struct dp_netdev *dp,
455 uint16_t port_no, struct dp_netdev_port **portp)
457 if (!is_valid_port_number(port_no)) {
461 *portp = dp->ports[port_no];
462 return *portp ? 0 : ENOENT;
467 get_port_by_name(struct dp_netdev *dp,
468 const char *devname, struct dp_netdev_port **portp)
470 struct dp_netdev_port *port;
472 LIST_FOR_EACH (port, node, &dp->port_list) {
473 if (!strcmp(netdev_get_name(port->netdev), devname)) {
482 do_del_port(struct dp_netdev *dp, uint16_t port_no)
484 struct dp_netdev_port *port;
488 error = get_port_by_number(dp, port_no, &port);
493 list_remove(&port->node);
494 dp->ports[port->port_no] = NULL;
497 name = xstrdup(netdev_get_name(port->netdev));
498 netdev_close(port->netdev);
508 answer_port_query(const struct dp_netdev_port *port,
509 struct dpif_port *dpif_port)
511 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
512 dpif_port->type = xstrdup(port->type);
513 dpif_port->port_no = port->port_no;
517 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
518 struct dpif_port *dpif_port)
520 struct dp_netdev *dp = get_dp_netdev(dpif);
521 struct dp_netdev_port *port;
524 error = get_port_by_number(dp, port_no, &port);
526 answer_port_query(port, dpif_port);
532 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
533 struct dpif_port *dpif_port)
535 struct dp_netdev *dp = get_dp_netdev(dpif);
536 struct dp_netdev_port *port;
539 error = get_port_by_name(dp, devname, &port);
541 answer_port_query(port, dpif_port);
547 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
553 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
555 hmap_remove(&dp->flow_table, &flow->node);
561 dp_netdev_flow_flush(struct dp_netdev *dp)
563 struct dp_netdev_flow *flow, *next;
565 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
566 dp_netdev_free_flow(dp, flow);
571 dpif_netdev_flow_flush(struct dpif *dpif)
573 struct dp_netdev *dp = get_dp_netdev(dpif);
574 dp_netdev_flow_flush(dp);
578 struct dp_netdev_port_state {
584 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
586 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
591 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
592 struct dpif_port *dpif_port)
594 struct dp_netdev_port_state *state = state_;
595 struct dp_netdev *dp = get_dp_netdev(dpif);
598 for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
599 struct dp_netdev_port *port = dp->ports[port_no];
602 state->name = xstrdup(netdev_get_name(port->netdev));
603 dpif_port->name = state->name;
604 dpif_port->type = port->type;
605 dpif_port->port_no = port->port_no;
606 state->port_no = port_no + 1;
614 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
616 struct dp_netdev_port_state *state = state_;
623 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
625 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
626 if (dpif->dp_serial != dpif->dp->serial) {
627 dpif->dp_serial = dpif->dp->serial;
635 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
637 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
638 if (dpif->dp_serial != dpif->dp->serial) {
639 poll_immediate_wake();
643 static struct dp_netdev_flow *
644 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
646 struct dp_netdev_flow *flow;
648 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
649 if (flow_equal(&flow->key, key)) {
657 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
659 stats->n_packets = flow->packet_count;
660 stats->n_bytes = flow->byte_count;
661 stats->used = flow->used;
662 stats->tcp_flags = flow->tcp_flags;
666 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
669 if (odp_flow_key_to_flow(key, key_len, flow)) {
670 /* This should not happen: it indicates that odp_flow_key_from_flow()
671 * and odp_flow_key_to_flow() disagree on the acceptable form of a
672 * flow. Log the problem as an error, with enough details to enable
674 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
676 if (!VLOG_DROP_ERR(&rl)) {
680 odp_flow_key_format(key, key_len, &s);
681 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
688 if (flow->in_port < OFPP_MAX
689 ? flow->in_port >= MAX_PORTS
690 : flow->in_port != OFPP_LOCAL && flow->in_port != OFPP_NONE) {
698 dpif_netdev_flow_get(const struct dpif *dpif,
699 const struct nlattr *nl_key, size_t nl_key_len,
700 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
702 struct dp_netdev *dp = get_dp_netdev(dpif);
703 struct dp_netdev_flow *flow;
707 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
712 flow = dp_netdev_lookup_flow(dp, &key);
718 get_dpif_flow_stats(flow, stats);
721 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
727 set_flow_actions(struct dp_netdev_flow *flow,
728 const struct nlattr *actions, size_t actions_len)
730 flow->actions = xrealloc(flow->actions, actions_len);
731 flow->actions_len = actions_len;
732 memcpy(flow->actions, actions, actions_len);
737 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
738 const struct nlattr *actions, size_t actions_len)
740 struct dp_netdev_flow *flow;
743 flow = xzalloc(sizeof *flow);
746 error = set_flow_actions(flow, actions, actions_len);
752 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
757 clear_stats(struct dp_netdev_flow *flow)
760 flow->packet_count = 0;
761 flow->byte_count = 0;
766 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
768 struct dp_netdev *dp = get_dp_netdev(dpif);
769 struct dp_netdev_flow *flow;
773 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
778 flow = dp_netdev_lookup_flow(dp, &key);
780 if (put->flags & DPIF_FP_CREATE) {
781 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
783 memset(put->stats, 0, sizeof *put->stats);
785 return dp_netdev_flow_add(dp, &key, put->actions,
794 if (put->flags & DPIF_FP_MODIFY) {
795 int error = set_flow_actions(flow, put->actions, put->actions_len);
798 get_dpif_flow_stats(flow, put->stats);
800 if (put->flags & DPIF_FP_ZERO_STATS) {
812 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
814 struct dp_netdev *dp = get_dp_netdev(dpif);
815 struct dp_netdev_flow *flow;
819 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
824 flow = dp_netdev_lookup_flow(dp, &key);
827 get_dpif_flow_stats(flow, del->stats);
829 dp_netdev_free_flow(dp, flow);
836 struct dp_netdev_flow_state {
839 struct nlattr *actions;
840 struct odputil_keybuf keybuf;
841 struct dpif_flow_stats stats;
845 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
847 struct dp_netdev_flow_state *state;
849 *statep = state = xmalloc(sizeof *state);
852 state->actions = NULL;
857 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
858 const struct nlattr **key, size_t *key_len,
859 const struct nlattr **actions, size_t *actions_len,
860 const struct dpif_flow_stats **stats)
862 struct dp_netdev_flow_state *state = state_;
863 struct dp_netdev *dp = get_dp_netdev(dpif);
864 struct dp_netdev_flow *flow;
865 struct hmap_node *node;
867 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
872 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
877 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
878 odp_flow_key_from_flow(&buf, &flow->key);
885 free(state->actions);
886 state->actions = xmemdup(flow->actions, flow->actions_len);
888 *actions = state->actions;
889 *actions_len = flow->actions_len;
893 get_dpif_flow_stats(flow, &state->stats);
894 *stats = &state->stats;
901 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
903 struct dp_netdev_flow_state *state = state_;
905 free(state->actions);
911 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
913 struct dp_netdev *dp = get_dp_netdev(dpif);
918 if (execute->packet->size < ETH_HEADER_LEN ||
919 execute->packet->size > UINT16_MAX) {
923 /* Make a deep copy of 'packet', because we might modify its data. */
924 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
925 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
926 ofpbuf_put(©, execute->packet->data, execute->packet->size);
928 flow_extract(©, 0, 0, -1, &key);
929 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
932 dp_netdev_execute_actions(dp, ©, &key,
933 execute->actions, execute->actions_len);
936 ofpbuf_uninit(©);
941 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
947 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
948 uint32_t queue_id, uint32_t *priority)
950 *priority = queue_id;
954 static struct dp_netdev_queue *
955 find_nonempty_queue(struct dpif *dpif)
957 struct dp_netdev *dp = get_dp_netdev(dpif);
960 for (i = 0; i < N_QUEUES; i++) {
961 struct dp_netdev_queue *q = &dp->queues[i];
962 if (q->head != q->tail) {
970 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
973 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
975 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
978 upcall->packet = buf;
990 dpif_netdev_recv_wait(struct dpif *dpif)
992 if (find_nonempty_queue(dpif)) {
993 poll_immediate_wake();
995 /* No messages ready to be received, and dp_wait() will ensure that we
996 * wake up to queue new messages, so there is nothing to do. */
1001 dpif_netdev_recv_purge(struct dpif *dpif)
1003 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1004 dp_netdev_purge_queues(dpif_netdev->dp);
1008 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
1009 const struct ofpbuf *packet)
1011 flow->used = time_msec();
1012 flow->packet_count++;
1013 flow->byte_count += packet->size;
1014 flow->tcp_flags |= packet_get_tcp_flags(packet, key);
1018 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1019 struct ofpbuf *packet)
1021 struct dp_netdev_flow *flow;
1024 if (packet->size < ETH_HEADER_LEN) {
1027 flow_extract(packet, 0, 0, odp_port_to_ofp_port(port->port_no), &key);
1028 flow = dp_netdev_lookup_flow(dp, &key);
1030 dp_netdev_flow_used(flow, &key, packet);
1031 dp_netdev_execute_actions(dp, packet, &key,
1032 flow->actions, flow->actions_len);
1036 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, 0);
1041 dpif_netdev_run(struct dpif *dpif)
1043 struct dp_netdev *dp = get_dp_netdev(dpif);
1044 struct dp_netdev_port *port;
1045 struct ofpbuf packet;
1047 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1049 LIST_FOR_EACH (port, node, &dp->port_list) {
1052 /* Reset packet contents. */
1053 ofpbuf_clear(&packet);
1054 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1056 error = netdev_recv(port->netdev, &packet);
1058 dp_netdev_port_input(dp, port, &packet);
1059 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1060 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1061 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1062 netdev_get_name(port->netdev), strerror(error));
1065 ofpbuf_uninit(&packet);
1069 dpif_netdev_wait(struct dpif *dpif)
1071 struct dp_netdev *dp = get_dp_netdev(dpif);
1072 struct dp_netdev_port *port;
1074 LIST_FOR_EACH (port, node, &dp->port_list) {
1075 netdev_recv_wait(port->netdev);
1080 dp_netdev_set_dl(struct ofpbuf *packet, const struct ovs_key_ethernet *eth_key)
1082 struct eth_header *eh = packet->l2;
1084 memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
1085 memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
1089 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1092 struct dp_netdev_port *p = dp->ports[out_port];
1094 netdev_send(p->netdev, packet);
1099 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1100 int queue_no, const struct flow *flow, uint64_t arg)
1102 struct dp_netdev_queue *q = &dp->queues[queue_no];
1103 struct dp_netdev_upcall *u;
1104 struct dpif_upcall *upcall;
1108 if (q->head - q->tail >= MAX_QUEUE_LEN) {
1113 u = &q->upcalls[q->head++ & QUEUE_MASK];
1116 ofpbuf_init(buf, ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size);
1117 odp_flow_key_from_flow(buf, flow);
1118 key_len = buf->size;
1119 ofpbuf_pull(buf, key_len);
1120 ofpbuf_reserve(buf, 2);
1121 ofpbuf_put(buf, packet->data, packet->size);
1123 upcall = &u->upcall;
1124 upcall->type = queue_no;
1125 upcall->packet = buf;
1126 upcall->key = buf->base;
1127 upcall->key_len = key_len;
1128 upcall->userdata = arg;
1134 dp_netdev_sample(struct dp_netdev *dp,
1135 struct ofpbuf *packet, struct flow *key,
1136 const struct nlattr *action)
1138 const struct nlattr *subactions = NULL;
1139 const struct nlattr *a;
1142 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
1143 int type = nl_attr_type(a);
1145 switch ((enum ovs_sample_attr) type) {
1146 case OVS_SAMPLE_ATTR_PROBABILITY:
1147 if (random_uint32() >= nl_attr_get_u32(a)) {
1152 case OVS_SAMPLE_ATTR_ACTIONS:
1156 case OVS_SAMPLE_ATTR_UNSPEC:
1157 case __OVS_SAMPLE_ATTR_MAX:
1163 dp_netdev_execute_actions(dp, packet, key, nl_attr_get(subactions),
1164 nl_attr_get_size(subactions));
1168 dp_netdev_action_userspace(struct dp_netdev *dp,
1169 struct ofpbuf *packet, struct flow *key,
1170 const struct nlattr *a)
1172 const struct nlattr *userdata_attr;
1175 userdata_attr = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1176 userdata = userdata_attr ? nl_attr_get_u64(userdata_attr) : 0;
1177 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1181 execute_set_action(struct ofpbuf *packet, const struct nlattr *a)
1183 enum ovs_key_attr type = nl_attr_type(a);
1184 const struct ovs_key_ipv4 *ipv4_key;
1185 const struct ovs_key_tcp *tcp_key;
1186 const struct ovs_key_udp *udp_key;
1189 case OVS_KEY_ATTR_TUN_ID:
1190 case OVS_KEY_ATTR_PRIORITY:
1191 case OVS_KEY_ATTR_IPV6:
1192 /* not implemented */
1195 case OVS_KEY_ATTR_ETHERNET:
1196 dp_netdev_set_dl(packet,
1197 nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
1200 case OVS_KEY_ATTR_IPV4:
1201 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
1202 packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
1203 ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
1206 case OVS_KEY_ATTR_TCP:
1207 tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
1208 packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
1211 case OVS_KEY_ATTR_UDP:
1212 udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
1213 packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
1216 case OVS_KEY_ATTR_UNSPEC:
1217 case OVS_KEY_ATTR_ENCAP:
1218 case OVS_KEY_ATTR_ETHERTYPE:
1219 case OVS_KEY_ATTR_IN_PORT:
1220 case OVS_KEY_ATTR_VLAN:
1221 case OVS_KEY_ATTR_ICMP:
1222 case OVS_KEY_ATTR_ICMPV6:
1223 case OVS_KEY_ATTR_ARP:
1224 case OVS_KEY_ATTR_ND:
1225 case __OVS_KEY_ATTR_MAX:
1232 dp_netdev_execute_actions(struct dp_netdev *dp,
1233 struct ofpbuf *packet, struct flow *key,
1234 const struct nlattr *actions,
1237 const struct nlattr *a;
1240 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1241 const struct ovs_action_push_vlan *vlan;
1242 int type = nl_attr_type(a);
1244 switch ((enum ovs_action_attr) type) {
1245 case OVS_ACTION_ATTR_OUTPUT:
1246 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1249 case OVS_ACTION_ATTR_USERSPACE:
1250 dp_netdev_action_userspace(dp, packet, key, a);
1253 case OVS_ACTION_ATTR_PUSH_VLAN:
1254 vlan = nl_attr_get(a);
1255 eth_push_vlan(packet, vlan->vlan_tci);
1258 case OVS_ACTION_ATTR_POP_VLAN:
1259 eth_pop_vlan(packet);
1262 case OVS_ACTION_ATTR_SET:
1263 execute_set_action(packet, nl_attr_get(a));
1266 case OVS_ACTION_ATTR_SAMPLE:
1267 dp_netdev_sample(dp, packet, key, a);
1270 case OVS_ACTION_ATTR_UNSPEC:
1271 case __OVS_ACTION_ATTR_MAX:
1277 #define DPIF_NETDEV_CLASS_FUNCTIONS \
1278 dpif_netdev_enumerate, \
1280 dpif_netdev_close, \
1281 dpif_netdev_destroy, \
1284 dpif_netdev_get_stats, \
1285 dpif_netdev_port_add, \
1286 dpif_netdev_port_del, \
1287 dpif_netdev_port_query_by_number, \
1288 dpif_netdev_port_query_by_name, \
1289 dpif_netdev_get_max_ports, \
1290 NULL, /* port_get_pid */ \
1291 dpif_netdev_port_dump_start, \
1292 dpif_netdev_port_dump_next, \
1293 dpif_netdev_port_dump_done, \
1294 dpif_netdev_port_poll, \
1295 dpif_netdev_port_poll_wait, \
1296 dpif_netdev_flow_get, \
1297 dpif_netdev_flow_put, \
1298 dpif_netdev_flow_del, \
1299 dpif_netdev_flow_flush, \
1300 dpif_netdev_flow_dump_start, \
1301 dpif_netdev_flow_dump_next, \
1302 dpif_netdev_flow_dump_done, \
1303 dpif_netdev_execute, \
1304 NULL, /* operate */ \
1305 dpif_netdev_recv_set, \
1306 dpif_netdev_queue_to_priority, \
1308 dpif_netdev_recv_wait, \
1309 dpif_netdev_recv_purge, \
1311 const struct dpif_class dpif_netdev_class = {
1313 DPIF_NETDEV_CLASS_FUNCTIONS
1316 const struct dpif_class dpif_planetlab_class = {
1318 DPIF_NETDEV_CLASS_FUNCTIONS
1322 dpif_dummy_register__(const char *type)
1324 struct dpif_class *class;
1326 class = xmalloc(sizeof *class);
1327 *class = dpif_netdev_class;
1328 class->type = xstrdup(type);
1329 dp_register_provider(class);
1333 dpif_dummy_register(bool override)
1340 dp_enumerate_types(&types);
1341 SSET_FOR_EACH (type, &types) {
1342 if (!dp_unregister_provider(type)) {
1343 dpif_dummy_register__(type);
1346 sset_destroy(&types);
1349 dpif_dummy_register__("dummy");