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_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 uint8_t tcp_flags; /* Bitwise-OR of seen tcp_flags 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 = flow->tcp_flags;
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, const struct dpif_flow_del *del)
782 struct dp_netdev *dp = get_dp_netdev(dpif);
783 struct dp_netdev_flow *flow;
787 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
792 flow = dp_netdev_lookup_flow(dp, &key);
795 get_dpif_flow_stats(flow, del->stats);
797 dp_netdev_free_flow(dp, flow);
804 struct dp_netdev_flow_state {
807 struct nlattr *actions;
808 struct odputil_keybuf keybuf;
809 struct dpif_flow_stats stats;
813 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
815 struct dp_netdev_flow_state *state;
817 *statep = state = xmalloc(sizeof *state);
820 state->actions = NULL;
825 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
826 const struct nlattr **key, size_t *key_len,
827 const struct nlattr **actions, size_t *actions_len,
828 const struct dpif_flow_stats **stats)
830 struct dp_netdev_flow_state *state = state_;
831 struct dp_netdev *dp = get_dp_netdev(dpif);
832 struct dp_netdev_flow *flow;
833 struct hmap_node *node;
835 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
840 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
845 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
846 odp_flow_key_from_flow(&buf, &flow->key);
853 free(state->actions);
854 state->actions = xmemdup(flow->actions, flow->actions_len);
856 *actions = state->actions;
857 *actions_len = flow->actions_len;
861 get_dpif_flow_stats(flow, &state->stats);
862 *stats = &state->stats;
869 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
871 struct dp_netdev_flow_state *state = state_;
873 free(state->actions);
879 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
881 struct dp_netdev *dp = get_dp_netdev(dpif);
886 if (execute->packet->size < ETH_HEADER_LEN ||
887 execute->packet->size > UINT16_MAX) {
891 /* Make a deep copy of 'packet', because we might modify its data. */
892 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
893 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
894 ofpbuf_put(©, execute->packet->data, execute->packet->size);
896 flow_extract(©, 0, 0, -1, &key);
897 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
900 dp_netdev_execute_actions(dp, ©, &key,
901 execute->actions, execute->actions_len);
904 ofpbuf_uninit(©);
909 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
915 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
916 uint32_t queue_id, uint32_t *priority)
918 *priority = queue_id;
922 static struct dp_netdev_queue *
923 find_nonempty_queue(struct dpif *dpif)
925 struct dp_netdev *dp = get_dp_netdev(dpif);
928 for (i = 0; i < N_QUEUES; i++) {
929 struct dp_netdev_queue *q = &dp->queues[i];
930 if (q->head != q->tail) {
938 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
941 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
943 struct dpif_upcall *u = q->upcalls[q->tail++ & QUEUE_MASK];
957 dpif_netdev_recv_wait(struct dpif *dpif)
959 if (find_nonempty_queue(dpif)) {
960 poll_immediate_wake();
962 /* No messages ready to be received, and dp_wait() will ensure that we
963 * wake up to queue new messages, so there is nothing to do. */
968 dpif_netdev_recv_purge(struct dpif *dpif)
970 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
971 dp_netdev_purge_queues(dpif_netdev->dp);
975 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
976 const struct ofpbuf *packet)
978 flow->used = time_msec();
979 flow->packet_count++;
980 flow->byte_count += packet->size;
981 flow->tcp_flags |= packet_get_tcp_flags(packet, key);
985 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
986 struct ofpbuf *packet)
988 struct dp_netdev_flow *flow;
991 if (packet->size < ETH_HEADER_LEN) {
994 flow_extract(packet, 0, 0, port->port_no, &key);
995 flow = dp_netdev_lookup_flow(dp, &key);
997 dp_netdev_flow_used(flow, &key, packet);
998 dp_netdev_execute_actions(dp, packet, &key,
999 flow->actions, flow->actions_len);
1003 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, 0);
1008 dpif_netdev_run(struct dpif *dpif)
1010 struct dp_netdev *dp = get_dp_netdev(dpif);
1011 struct dp_netdev_port *port;
1012 struct ofpbuf packet;
1014 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1016 LIST_FOR_EACH (port, node, &dp->port_list) {
1019 /* Reset packet contents. */
1020 ofpbuf_clear(&packet);
1021 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1023 error = netdev_recv(port->netdev, &packet);
1025 dp_netdev_port_input(dp, port, &packet);
1026 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1027 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1028 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1029 netdev_get_name(port->netdev), strerror(error));
1032 ofpbuf_uninit(&packet);
1036 dpif_netdev_wait(struct dpif *dpif)
1038 struct dp_netdev *dp = get_dp_netdev(dpif);
1039 struct dp_netdev_port *port;
1041 LIST_FOR_EACH (port, node, &dp->port_list) {
1042 netdev_recv_wait(port->netdev);
1047 dp_netdev_set_dl(struct ofpbuf *packet, const struct ovs_key_ethernet *eth_key)
1049 struct eth_header *eh = packet->l2;
1051 memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
1052 memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
1056 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1059 struct dp_netdev_port *p = dp->ports[out_port];
1061 netdev_send(p->netdev, packet);
1066 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1067 int queue_no, const struct flow *flow, uint64_t arg)
1069 struct dp_netdev_queue *q = &dp->queues[queue_no];
1070 struct dpif_upcall *upcall;
1074 if (q->head - q->tail >= MAX_QUEUE_LEN) {
1079 buf = ofpbuf_new(ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size);
1080 odp_flow_key_from_flow(buf, flow);
1081 key_len = buf->size;
1082 ofpbuf_pull(buf, key_len);
1083 ofpbuf_reserve(buf, 2);
1084 ofpbuf_put(buf, packet->data, packet->size);
1086 upcall = xzalloc(sizeof *upcall);
1087 upcall->type = queue_no;
1088 upcall->packet = buf;
1089 upcall->key = buf->base;
1090 upcall->key_len = key_len;
1091 upcall->userdata = arg;
1093 q->upcalls[q->head++ & QUEUE_MASK] = upcall;
1099 dp_netdev_sample(struct dp_netdev *dp,
1100 struct ofpbuf *packet, struct flow *key,
1101 const struct nlattr *action)
1103 const struct nlattr *subactions = NULL;
1104 const struct nlattr *a;
1107 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
1108 int type = nl_attr_type(a);
1110 switch ((enum ovs_sample_attr) type) {
1111 case OVS_SAMPLE_ATTR_PROBABILITY:
1112 if (random_uint32() >= nl_attr_get_u32(a)) {
1117 case OVS_SAMPLE_ATTR_ACTIONS:
1121 case OVS_SAMPLE_ATTR_UNSPEC:
1122 case __OVS_SAMPLE_ATTR_MAX:
1128 dp_netdev_execute_actions(dp, packet, key, nl_attr_get(subactions),
1129 nl_attr_get_size(subactions));
1133 dp_netdev_action_userspace(struct dp_netdev *dp,
1134 struct ofpbuf *packet, struct flow *key,
1135 const struct nlattr *a)
1137 const struct nlattr *userdata_attr;
1140 userdata_attr = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1141 userdata = userdata_attr ? nl_attr_get_u64(userdata_attr) : 0;
1142 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1146 execute_set_action(struct ofpbuf *packet, const struct nlattr *a)
1148 enum ovs_key_attr type = nl_attr_type(a);
1149 const struct ovs_key_ipv4 *ipv4_key;
1150 const struct ovs_key_tcp *tcp_key;
1151 const struct ovs_key_udp *udp_key;
1154 case OVS_KEY_ATTR_TUN_ID:
1155 case OVS_KEY_ATTR_PRIORITY:
1156 case OVS_KEY_ATTR_IPV6:
1157 /* not implemented */
1160 case OVS_KEY_ATTR_ETHERNET:
1161 dp_netdev_set_dl(packet,
1162 nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
1165 case OVS_KEY_ATTR_IPV4:
1166 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
1167 packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
1168 ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
1171 case OVS_KEY_ATTR_TCP:
1172 tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
1173 packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
1176 case OVS_KEY_ATTR_UDP:
1177 udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
1178 packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
1181 case OVS_KEY_ATTR_UNSPEC:
1182 case OVS_KEY_ATTR_ENCAP:
1183 case OVS_KEY_ATTR_ETHERTYPE:
1184 case OVS_KEY_ATTR_IN_PORT:
1185 case OVS_KEY_ATTR_VLAN:
1186 case OVS_KEY_ATTR_ICMP:
1187 case OVS_KEY_ATTR_ICMPV6:
1188 case OVS_KEY_ATTR_ARP:
1189 case OVS_KEY_ATTR_ND:
1190 case __OVS_KEY_ATTR_MAX:
1197 dp_netdev_execute_actions(struct dp_netdev *dp,
1198 struct ofpbuf *packet, struct flow *key,
1199 const struct nlattr *actions,
1202 const struct nlattr *a;
1205 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1206 const struct ovs_action_push_vlan *vlan;
1207 int type = nl_attr_type(a);
1209 switch ((enum ovs_action_attr) type) {
1210 case OVS_ACTION_ATTR_OUTPUT:
1211 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1214 case OVS_ACTION_ATTR_USERSPACE:
1215 dp_netdev_action_userspace(dp, packet, key, a);
1218 case OVS_ACTION_ATTR_PUSH_VLAN:
1219 vlan = nl_attr_get(a);
1220 eth_push_vlan(packet, vlan->vlan_tci);
1223 case OVS_ACTION_ATTR_POP_VLAN:
1224 eth_pop_vlan(packet);
1227 case OVS_ACTION_ATTR_SET:
1228 execute_set_action(packet, nl_attr_get(a));
1231 case OVS_ACTION_ATTR_SAMPLE:
1232 dp_netdev_sample(dp, packet, key, a);
1235 case OVS_ACTION_ATTR_UNSPEC:
1236 case __OVS_ACTION_ATTR_MAX:
1242 const struct dpif_class dpif_netdev_class = {
1244 NULL, /* enumerate */
1247 dpif_netdev_destroy,
1250 dpif_netdev_get_stats,
1251 dpif_netdev_port_add,
1252 dpif_netdev_port_del,
1253 dpif_netdev_port_query_by_number,
1254 dpif_netdev_port_query_by_name,
1255 dpif_netdev_get_max_ports,
1256 NULL, /* port_get_pid */
1257 dpif_netdev_port_dump_start,
1258 dpif_netdev_port_dump_next,
1259 dpif_netdev_port_dump_done,
1260 dpif_netdev_port_poll,
1261 dpif_netdev_port_poll_wait,
1262 dpif_netdev_flow_get,
1263 dpif_netdev_flow_put,
1264 dpif_netdev_flow_del,
1265 dpif_netdev_flow_flush,
1266 dpif_netdev_flow_dump_start,
1267 dpif_netdev_flow_dump_next,
1268 dpif_netdev_flow_dump_done,
1269 dpif_netdev_execute,
1271 dpif_netdev_recv_set,
1272 dpif_netdev_queue_to_priority,
1274 dpif_netdev_recv_wait,
1275 dpif_netdev_recv_purge,
1279 dpif_dummy_register__(const char *type)
1281 struct dpif_class *class;
1283 class = xmalloc(sizeof *class);
1284 *class = dpif_netdev_class;
1285 class->type = xstrdup(type);
1286 dp_register_provider(class);
1290 dpif_dummy_register(bool override)
1297 dp_enumerate_types(&types);
1298 SSET_FOR_EACH (type, &types) {
1299 if (!dp_unregister_provider(type)) {
1300 dpif_dummy_register__(type);
1303 sset_destroy(&types);
1306 dpif_dummy_register__("dummy");