2 * Copyright (c) 2009, 2010, 2011, 2012 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"
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_queue {
74 struct dpif_upcall *upcalls[MAX_QUEUE_LEN];
75 unsigned int head, tail;
78 /* Datapath based on the network device interface from netdev.h. */
80 const struct dpif_class *class;
85 struct dp_netdev_queue queues[N_QUEUES];
86 struct hmap flow_table; /* Flow table. */
89 long long int n_hit; /* Number of flow table matches. */
90 long long int n_missed; /* Number of flow table misses. */
91 long long int n_lost; /* Number of misses not passed to client. */
94 struct dp_netdev_port *ports[MAX_PORTS];
95 struct list port_list;
99 /* A port in a netdev-based datapath. */
100 struct dp_netdev_port {
101 int port_no; /* Index into dp_netdev's 'ports'. */
102 struct list node; /* Element in dp_netdev's 'port_list'. */
103 struct netdev *netdev;
104 char *type; /* Port type as requested by user. */
107 /* A flow in dp_netdev's 'flow_table'. */
108 struct dp_netdev_flow {
109 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
113 long long int used; /* Last used time, in monotonic msecs. */
114 long long int packet_count; /* Number of packets matched. */
115 long long int byte_count; /* Number of bytes matched. */
116 ovs_be16 tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
119 struct nlattr *actions;
123 /* Interface to netdev-based datapath. */
126 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_netdev *
156 dpif_netdev_cast(const struct dpif *dpif)
158 assert(dpif->dpif_class->open == dpif_netdev_open);
159 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
162 static struct dp_netdev *
163 get_dp_netdev(const struct dpif *dpif)
165 return dpif_netdev_cast(dpif)->dp;
169 create_dpif_netdev(struct dp_netdev *dp)
171 uint16_t netflow_id = hash_string(dp->name, 0);
172 struct dpif_netdev *dpif;
176 dpif = xmalloc(sizeof *dpif);
177 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
179 dpif->dp_serial = dp->serial;
185 create_dp_netdev(const char *name, const struct dpif_class *class,
186 struct dp_netdev **dpp)
188 struct dp_netdev *dp;
192 dp = xzalloc(sizeof *dp);
194 dp->name = xstrdup(name);
196 for (i = 0; i < N_QUEUES; i++) {
197 dp->queues[i].head = dp->queues[i].tail = 0;
199 hmap_init(&dp->flow_table);
200 list_init(&dp->port_list);
201 error = do_add_port(dp, name, "internal", OVSP_LOCAL);
207 shash_add(&dp_netdevs, name, dp);
214 dpif_netdev_open(const struct dpif_class *class, const char *name,
215 bool create, struct dpif **dpifp)
217 struct dp_netdev *dp;
219 dp = shash_find_data(&dp_netdevs, name);
224 int error = create_dp_netdev(name, class, &dp);
231 if (dp->class != class) {
238 *dpifp = create_dpif_netdev(dp);
243 dp_netdev_purge_queues(struct dp_netdev *dp)
247 for (i = 0; i < N_QUEUES; i++) {
248 struct dp_netdev_queue *q = &dp->queues[i];
250 while (q->tail != q->head) {
251 struct dpif_upcall *upcall = q->upcalls[q->tail++ & QUEUE_MASK];
253 ofpbuf_delete(upcall->packet);
260 dp_netdev_free(struct dp_netdev *dp)
262 struct dp_netdev_port *port, *next;
264 dp_netdev_flow_flush(dp);
265 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
266 do_del_port(dp, port->port_no);
268 dp_netdev_purge_queues(dp);
269 hmap_destroy(&dp->flow_table);
275 dpif_netdev_close(struct dpif *dpif)
277 struct dp_netdev *dp = get_dp_netdev(dpif);
278 assert(dp->open_cnt > 0);
279 if (--dp->open_cnt == 0 && dp->destroyed) {
280 shash_find_and_delete(&dp_netdevs, dp->name);
287 dpif_netdev_destroy(struct dpif *dpif)
289 struct dp_netdev *dp = get_dp_netdev(dpif);
290 dp->destroyed = true;
295 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
297 struct dp_netdev *dp = get_dp_netdev(dpif);
298 stats->n_flows = hmap_count(&dp->flow_table);
299 stats->n_hit = dp->n_hit;
300 stats->n_missed = dp->n_missed;
301 stats->n_lost = dp->n_lost;
306 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
309 struct dp_netdev_port *port;
310 struct netdev *netdev;
311 const char *open_type;
315 /* XXX reject devices already in some dp_netdev. */
317 /* Open and validate network device. */
318 open_type = (strcmp(type, "internal") ? type
319 : dp->class != &dpif_netdev_class ? "dummy"
321 error = netdev_open(devname, open_type, &netdev);
325 /* XXX reject loopback devices */
326 /* XXX reject non-Ethernet devices */
328 error = netdev_listen(netdev);
330 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
331 devname, strerror(errno));
332 netdev_close(netdev);
336 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
338 netdev_close(netdev);
342 port = xmalloc(sizeof *port);
343 port->port_no = port_no;
344 port->netdev = netdev;
345 port->type = xstrdup(type);
347 error = netdev_get_mtu(netdev, &mtu);
352 list_push_back(&dp->port_list, &port->node);
353 dp->ports[port_no] = port;
360 choose_port(struct dpif *dpif, struct netdev *netdev)
362 struct dp_netdev *dp = get_dp_netdev(dpif);
365 if (dpif->dpif_class != &dpif_netdev_class) {
366 /* If the port name contains a number, try to assign that port number.
367 * This can make writing unit tests easier because port numbers are
371 for (p = netdev_get_name(netdev); *p != '\0'; p++) {
372 if (isdigit((unsigned char) *p)) {
373 port_no = strtol(p, NULL, 10);
374 if (port_no > 0 && port_no < MAX_PORTS
375 && !dp->ports[port_no]) {
383 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
384 if (!dp->ports[port_no]) {
393 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
396 struct dp_netdev *dp = get_dp_netdev(dpif);
399 port_no = choose_port(dpif, netdev);
402 return do_add_port(dp, netdev_get_name(netdev),
403 netdev_get_type(netdev), port_no);
409 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
411 struct dp_netdev *dp = get_dp_netdev(dpif);
412 return port_no == OVSP_LOCAL ? EINVAL : do_del_port(dp, port_no);
416 is_valid_port_number(uint16_t port_no)
418 return port_no < MAX_PORTS;
422 get_port_by_number(struct dp_netdev *dp,
423 uint16_t port_no, struct dp_netdev_port **portp)
425 if (!is_valid_port_number(port_no)) {
429 *portp = dp->ports[port_no];
430 return *portp ? 0 : ENOENT;
435 get_port_by_name(struct dp_netdev *dp,
436 const char *devname, struct dp_netdev_port **portp)
438 struct dp_netdev_port *port;
440 LIST_FOR_EACH (port, node, &dp->port_list) {
441 if (!strcmp(netdev_get_name(port->netdev), devname)) {
450 do_del_port(struct dp_netdev *dp, uint16_t port_no)
452 struct dp_netdev_port *port;
456 error = get_port_by_number(dp, port_no, &port);
461 list_remove(&port->node);
462 dp->ports[port->port_no] = NULL;
465 name = xstrdup(netdev_get_name(port->netdev));
466 netdev_close(port->netdev);
476 answer_port_query(const struct dp_netdev_port *port,
477 struct dpif_port *dpif_port)
479 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
480 dpif_port->type = xstrdup(port->type);
481 dpif_port->port_no = port->port_no;
485 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
486 struct dpif_port *dpif_port)
488 struct dp_netdev *dp = get_dp_netdev(dpif);
489 struct dp_netdev_port *port;
492 error = get_port_by_number(dp, port_no, &port);
494 answer_port_query(port, dpif_port);
500 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
501 struct dpif_port *dpif_port)
503 struct dp_netdev *dp = get_dp_netdev(dpif);
504 struct dp_netdev_port *port;
507 error = get_port_by_name(dp, devname, &port);
509 answer_port_query(port, dpif_port);
515 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
521 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
523 hmap_remove(&dp->flow_table, &flow->node);
529 dp_netdev_flow_flush(struct dp_netdev *dp)
531 struct dp_netdev_flow *flow, *next;
533 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
534 dp_netdev_free_flow(dp, flow);
539 dpif_netdev_flow_flush(struct dpif *dpif)
541 struct dp_netdev *dp = get_dp_netdev(dpif);
542 dp_netdev_flow_flush(dp);
546 struct dp_netdev_port_state {
552 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
554 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
559 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
560 struct dpif_port *dpif_port)
562 struct dp_netdev_port_state *state = state_;
563 struct dp_netdev *dp = get_dp_netdev(dpif);
566 for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
567 struct dp_netdev_port *port = dp->ports[port_no];
570 state->name = xstrdup(netdev_get_name(port->netdev));
571 dpif_port->name = state->name;
572 dpif_port->type = port->type;
573 dpif_port->port_no = port->port_no;
574 state->port_no = port_no + 1;
582 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
584 struct dp_netdev_port_state *state = state_;
591 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
593 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
594 if (dpif->dp_serial != dpif->dp->serial) {
595 dpif->dp_serial = dpif->dp->serial;
603 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
605 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
606 if (dpif->dp_serial != dpif->dp->serial) {
607 poll_immediate_wake();
611 static struct dp_netdev_flow *
612 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
614 struct dp_netdev_flow *flow;
616 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
617 if (flow_equal(&flow->key, key)) {
625 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
627 stats->n_packets = flow->packet_count;
628 stats->n_bytes = flow->byte_count;
629 stats->used = flow->used;
630 stats->tcp_flags = TCP_FLAGS(flow->tcp_ctl);
634 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
637 if (odp_flow_key_to_flow(key, key_len, flow)) {
638 /* This should not happen: it indicates that odp_flow_key_from_flow()
639 * and odp_flow_key_to_flow() disagree on the acceptable form of a
640 * flow. Log the problem as an error, with enough details to enable
642 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
644 if (!VLOG_DROP_ERR(&rl)) {
648 odp_flow_key_format(key, key_len, &s);
649 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
656 if (flow->in_port < OFPP_MAX
657 ? flow->in_port >= MAX_PORTS
658 : flow->in_port != OFPP_LOCAL && flow->in_port != OFPP_NONE) {
666 dpif_netdev_flow_get(const struct dpif *dpif,
667 const struct nlattr *nl_key, size_t nl_key_len,
668 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
670 struct dp_netdev *dp = get_dp_netdev(dpif);
671 struct dp_netdev_flow *flow;
675 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
680 flow = dp_netdev_lookup_flow(dp, &key);
686 get_dpif_flow_stats(flow, stats);
689 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
695 set_flow_actions(struct dp_netdev_flow *flow,
696 const struct nlattr *actions, size_t actions_len)
698 flow->actions = xrealloc(flow->actions, actions_len);
699 flow->actions_len = actions_len;
700 memcpy(flow->actions, actions, actions_len);
705 add_flow(struct dpif *dpif, const struct flow *key,
706 const struct nlattr *actions, size_t actions_len)
708 struct dp_netdev *dp = get_dp_netdev(dpif);
709 struct dp_netdev_flow *flow;
712 flow = xzalloc(sizeof *flow);
715 error = set_flow_actions(flow, actions, actions_len);
721 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
726 clear_stats(struct dp_netdev_flow *flow)
729 flow->packet_count = 0;
730 flow->byte_count = 0;
735 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
737 struct dp_netdev *dp = get_dp_netdev(dpif);
738 struct dp_netdev_flow *flow;
742 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
747 flow = dp_netdev_lookup_flow(dp, &key);
749 if (put->flags & DPIF_FP_CREATE) {
750 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
752 memset(put->stats, 0, sizeof *put->stats);
754 return add_flow(dpif, &key, put->actions, put->actions_len);
762 if (put->flags & DPIF_FP_MODIFY) {
763 int error = set_flow_actions(flow, put->actions, put->actions_len);
766 get_dpif_flow_stats(flow, put->stats);
768 if (put->flags & DPIF_FP_ZERO_STATS) {
780 dpif_netdev_flow_del(struct dpif *dpif,
781 const struct nlattr *nl_key, size_t nl_key_len,
782 struct dpif_flow_stats *stats)
784 struct dp_netdev *dp = get_dp_netdev(dpif);
785 struct dp_netdev_flow *flow;
789 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
794 flow = dp_netdev_lookup_flow(dp, &key);
797 get_dpif_flow_stats(flow, stats);
799 dp_netdev_free_flow(dp, flow);
806 struct dp_netdev_flow_state {
809 struct nlattr *actions;
810 struct odputil_keybuf keybuf;
811 struct dpif_flow_stats stats;
815 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
817 struct dp_netdev_flow_state *state;
819 *statep = state = xmalloc(sizeof *state);
822 state->actions = NULL;
827 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
828 const struct nlattr **key, size_t *key_len,
829 const struct nlattr **actions, size_t *actions_len,
830 const struct dpif_flow_stats **stats)
832 struct dp_netdev_flow_state *state = state_;
833 struct dp_netdev *dp = get_dp_netdev(dpif);
834 struct dp_netdev_flow *flow;
835 struct hmap_node *node;
837 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
842 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
847 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
848 odp_flow_key_from_flow(&buf, &flow->key);
855 free(state->actions);
856 state->actions = xmemdup(flow->actions, flow->actions_len);
858 *actions = state->actions;
859 *actions_len = flow->actions_len;
863 get_dpif_flow_stats(flow, &state->stats);
864 *stats = &state->stats;
871 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
873 struct dp_netdev_flow_state *state = state_;
875 free(state->actions);
881 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
883 struct dp_netdev *dp = get_dp_netdev(dpif);
888 if (execute->packet->size < ETH_HEADER_LEN ||
889 execute->packet->size > UINT16_MAX) {
893 /* Make a deep copy of 'packet', because we might modify its data. */
894 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
895 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
896 ofpbuf_put(©, execute->packet->data, execute->packet->size);
898 flow_extract(©, 0, 0, -1, &key);
899 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
902 dp_netdev_execute_actions(dp, ©, &key,
903 execute->actions, execute->actions_len);
906 ofpbuf_uninit(©);
911 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
917 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
918 uint32_t queue_id, uint32_t *priority)
920 *priority = queue_id;
924 static struct dp_netdev_queue *
925 find_nonempty_queue(struct dpif *dpif)
927 struct dp_netdev *dp = get_dp_netdev(dpif);
930 for (i = 0; i < N_QUEUES; i++) {
931 struct dp_netdev_queue *q = &dp->queues[i];
932 if (q->head != q->tail) {
940 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall)
942 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
944 struct dpif_upcall *u = q->upcalls[q->tail++ & QUEUE_MASK];
955 dpif_netdev_recv_wait(struct dpif *dpif)
957 if (find_nonempty_queue(dpif)) {
958 poll_immediate_wake();
960 /* No messages ready to be received, and dp_wait() will ensure that we
961 * wake up to queue new messages, so there is nothing to do. */
966 dpif_netdev_recv_purge(struct dpif *dpif)
968 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
969 dp_netdev_purge_queues(dpif_netdev->dp);
973 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
974 const struct ofpbuf *packet)
976 flow->used = time_msec();
977 flow->packet_count++;
978 flow->byte_count += packet->size;
979 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
980 struct tcp_header *th = packet->l4;
981 flow->tcp_ctl |= th->tcp_ctl;
986 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
987 struct ofpbuf *packet)
989 struct dp_netdev_flow *flow;
992 if (packet->size < ETH_HEADER_LEN) {
995 flow_extract(packet, 0, 0, port->port_no, &key);
996 flow = dp_netdev_lookup_flow(dp, &key);
998 dp_netdev_flow_used(flow, &key, packet);
999 dp_netdev_execute_actions(dp, packet, &key,
1000 flow->actions, flow->actions_len);
1004 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, 0);
1009 dpif_netdev_run(struct dpif *dpif)
1011 struct dp_netdev *dp = get_dp_netdev(dpif);
1012 struct dp_netdev_port *port;
1013 struct ofpbuf packet;
1015 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1017 LIST_FOR_EACH (port, node, &dp->port_list) {
1020 /* Reset packet contents. */
1021 ofpbuf_clear(&packet);
1022 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1024 error = netdev_recv(port->netdev, &packet);
1026 dp_netdev_port_input(dp, port, &packet);
1027 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1028 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1029 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1030 netdev_get_name(port->netdev), strerror(error));
1033 ofpbuf_uninit(&packet);
1037 dpif_netdev_wait(struct dpif *dpif)
1039 struct dp_netdev *dp = get_dp_netdev(dpif);
1040 struct dp_netdev_port *port;
1042 LIST_FOR_EACH (port, node, &dp->port_list) {
1043 netdev_recv_wait(port->netdev);
1048 dp_netdev_set_dl(struct ofpbuf *packet, const struct ovs_key_ethernet *eth_key)
1050 struct eth_header *eh = packet->l2;
1052 memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
1053 memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
1057 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1060 struct dp_netdev_port *p = dp->ports[out_port];
1062 netdev_send(p->netdev, packet);
1067 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1068 int queue_no, const struct flow *flow, uint64_t arg)
1070 struct dp_netdev_queue *q = &dp->queues[queue_no];
1071 struct dpif_upcall *upcall;
1075 if (q->head - q->tail >= MAX_QUEUE_LEN) {
1080 buf = ofpbuf_new(ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size);
1081 odp_flow_key_from_flow(buf, flow);
1082 key_len = buf->size;
1083 ofpbuf_pull(buf, key_len);
1084 ofpbuf_reserve(buf, 2);
1085 ofpbuf_put(buf, packet->data, packet->size);
1087 upcall = xzalloc(sizeof *upcall);
1088 upcall->type = queue_no;
1089 upcall->packet = buf;
1090 upcall->key = buf->base;
1091 upcall->key_len = key_len;
1092 upcall->userdata = arg;
1094 q->upcalls[q->head++ & QUEUE_MASK] = upcall;
1100 dp_netdev_sample(struct dp_netdev *dp,
1101 struct ofpbuf *packet, struct flow *key,
1102 const struct nlattr *action)
1104 const struct nlattr *subactions = NULL;
1105 const struct nlattr *a;
1108 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
1109 int type = nl_attr_type(a);
1111 switch ((enum ovs_sample_attr) type) {
1112 case OVS_SAMPLE_ATTR_PROBABILITY:
1113 if (random_uint32() >= nl_attr_get_u32(a)) {
1118 case OVS_SAMPLE_ATTR_ACTIONS:
1122 case OVS_SAMPLE_ATTR_UNSPEC:
1123 case __OVS_SAMPLE_ATTR_MAX:
1129 dp_netdev_execute_actions(dp, packet, key, nl_attr_get(subactions),
1130 nl_attr_get_size(subactions));
1134 dp_netdev_action_userspace(struct dp_netdev *dp,
1135 struct ofpbuf *packet, struct flow *key,
1136 const struct nlattr *a)
1138 const struct nlattr *userdata_attr;
1141 userdata_attr = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1142 userdata = userdata_attr ? nl_attr_get_u64(userdata_attr) : 0;
1143 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1147 execute_set_action(struct ofpbuf *packet, const struct nlattr *a)
1149 enum ovs_key_attr type = nl_attr_type(a);
1150 const struct ovs_key_ipv4 *ipv4_key;
1151 const struct ovs_key_tcp *tcp_key;
1152 const struct ovs_key_udp *udp_key;
1155 case OVS_KEY_ATTR_TUN_ID:
1156 case OVS_KEY_ATTR_PRIORITY:
1157 case OVS_KEY_ATTR_IPV6:
1158 /* not implemented */
1161 case OVS_KEY_ATTR_ETHERNET:
1162 dp_netdev_set_dl(packet,
1163 nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
1166 case OVS_KEY_ATTR_IPV4:
1167 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
1168 packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
1169 ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
1172 case OVS_KEY_ATTR_TCP:
1173 tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
1174 packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
1177 case OVS_KEY_ATTR_UDP:
1178 udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
1179 packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
1182 case OVS_KEY_ATTR_UNSPEC:
1183 case OVS_KEY_ATTR_ENCAP:
1184 case OVS_KEY_ATTR_ETHERTYPE:
1185 case OVS_KEY_ATTR_IN_PORT:
1186 case OVS_KEY_ATTR_VLAN:
1187 case OVS_KEY_ATTR_ICMP:
1188 case OVS_KEY_ATTR_ICMPV6:
1189 case OVS_KEY_ATTR_ARP:
1190 case OVS_KEY_ATTR_ND:
1191 case __OVS_KEY_ATTR_MAX:
1198 dp_netdev_execute_actions(struct dp_netdev *dp,
1199 struct ofpbuf *packet, struct flow *key,
1200 const struct nlattr *actions,
1203 const struct nlattr *a;
1206 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1207 const struct ovs_action_push_vlan *vlan;
1208 int type = nl_attr_type(a);
1210 switch ((enum ovs_action_attr) type) {
1211 case OVS_ACTION_ATTR_OUTPUT:
1212 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1215 case OVS_ACTION_ATTR_USERSPACE:
1216 dp_netdev_action_userspace(dp, packet, key, a);
1219 case OVS_ACTION_ATTR_PUSH_VLAN:
1220 vlan = nl_attr_get(a);
1221 eth_push_vlan(packet, vlan->vlan_tci);
1224 case OVS_ACTION_ATTR_POP_VLAN:
1225 eth_pop_vlan(packet);
1228 case OVS_ACTION_ATTR_SET:
1229 execute_set_action(packet, nl_attr_get(a));
1232 case OVS_ACTION_ATTR_SAMPLE:
1233 dp_netdev_sample(dp, packet, key, a);
1236 case OVS_ACTION_ATTR_UNSPEC:
1237 case __OVS_ACTION_ATTR_MAX:
1243 const struct dpif_class dpif_netdev_class = {
1245 NULL, /* enumerate */
1248 dpif_netdev_destroy,
1251 dpif_netdev_get_stats,
1252 dpif_netdev_port_add,
1253 dpif_netdev_port_del,
1254 dpif_netdev_port_query_by_number,
1255 dpif_netdev_port_query_by_name,
1256 dpif_netdev_get_max_ports,
1257 NULL, /* port_get_pid */
1258 dpif_netdev_port_dump_start,
1259 dpif_netdev_port_dump_next,
1260 dpif_netdev_port_dump_done,
1261 dpif_netdev_port_poll,
1262 dpif_netdev_port_poll_wait,
1263 dpif_netdev_flow_get,
1264 dpif_netdev_flow_put,
1265 dpif_netdev_flow_del,
1266 dpif_netdev_flow_flush,
1267 dpif_netdev_flow_dump_start,
1268 dpif_netdev_flow_dump_next,
1269 dpif_netdev_flow_dump_done,
1270 dpif_netdev_execute,
1272 dpif_netdev_recv_set,
1273 dpif_netdev_queue_to_priority,
1275 dpif_netdev_recv_wait,
1276 dpif_netdev_recv_purge,
1280 dpif_dummy_register__(const char *type)
1282 struct dpif_class *class;
1284 class = xmalloc(sizeof *class);
1285 *class = dpif_netdev_class;
1286 class->type = xstrdup(type);
1287 dp_register_provider(class);
1291 dpif_dummy_register(bool override)
1298 dp_enumerate_types(&types);
1299 SSET_FOR_EACH (type, &types) {
1300 if (!dp_unregister_provider(type)) {
1301 dpif_dummy_register__(type);
1304 sset_destroy(&types);
1307 dpif_dummy_register__("dummy");