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"
55 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
57 /* Configuration parameters. */
58 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
59 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
61 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
62 * headers to be aligned on a 4-byte boundary. */
63 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
66 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
67 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
68 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
69 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
71 struct dp_netdev_queue {
72 struct dpif_upcall *upcalls[MAX_QUEUE_LEN];
73 unsigned int head, tail;
76 /* Datapath based on the network device interface from netdev.h. */
78 const struct dpif_class *class;
83 bool drop_frags; /* Drop all IP fragments, if true. */
84 struct dp_netdev_queue queues[N_QUEUES];
85 struct hmap flow_table; /* Flow table. */
88 long long int n_frags; /* Number of dropped IP fragments. */
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. */
95 struct dp_netdev_port *ports[MAX_PORTS];
96 struct list port_list;
100 /* A port in a netdev-based datapath. */
101 struct dp_netdev_port {
102 int port_no; /* Index into dp_netdev's 'ports'. */
103 struct list node; /* Element in dp_netdev's 'port_list'. */
104 struct netdev *netdev;
105 bool internal; /* Internal port? */
108 /* A flow in dp_netdev's 'flow_table'. */
109 struct dp_netdev_flow {
110 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
114 struct timespec used; /* Last used time. */
115 long long int packet_count; /* Number of packets matched. */
116 long long int byte_count; /* Number of bytes matched. */
117 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
120 struct nlattr *actions;
124 /* Interface to netdev-based datapath. */
127 struct dp_netdev *dp;
129 unsigned int dp_serial;
132 /* All netdev-based datapaths. */
133 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
135 /* Maximum port MTU seen so far. */
136 static int max_mtu = ETH_PAYLOAD_MAX;
138 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
139 struct dp_netdev_port **portp);
140 static int get_port_by_name(struct dp_netdev *, const char *devname,
141 struct dp_netdev_port **portp);
142 static void dp_netdev_free(struct dp_netdev *);
143 static void dp_netdev_flow_flush(struct dp_netdev *);
144 static int do_add_port(struct dp_netdev *, const char *devname,
145 const char *type, uint16_t port_no);
146 static int do_del_port(struct dp_netdev *, uint16_t port_no);
147 static int dpif_netdev_open(const struct dpif_class *, const char *name,
148 bool create, struct dpif **);
149 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
150 int queue_no, const struct flow *,
152 static int dp_netdev_execute_actions(struct dp_netdev *,
153 struct ofpbuf *, struct flow *,
154 const struct nlattr *actions,
157 static struct dpif_class dpif_dummy_class;
159 static struct dpif_netdev *
160 dpif_netdev_cast(const struct dpif *dpif)
162 assert(dpif->dpif_class->open == dpif_netdev_open);
163 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
166 static struct dp_netdev *
167 get_dp_netdev(const struct dpif *dpif)
169 return dpif_netdev_cast(dpif)->dp;
173 create_dpif_netdev(struct dp_netdev *dp)
175 uint16_t netflow_id = hash_string(dp->name, 0);
176 struct dpif_netdev *dpif;
180 dpif = xmalloc(sizeof *dpif);
181 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
183 dpif->listen_mask = 0;
184 dpif->dp_serial = dp->serial;
190 create_dp_netdev(const char *name, const struct dpif_class *class,
191 struct dp_netdev **dpp)
193 struct dp_netdev *dp;
197 dp = xzalloc(sizeof *dp);
199 dp->name = xstrdup(name);
201 dp->drop_frags = false;
202 for (i = 0; i < N_QUEUES; i++) {
203 dp->queues[i].head = dp->queues[i].tail = 0;
205 hmap_init(&dp->flow_table);
206 list_init(&dp->port_list);
207 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
213 shash_add(&dp_netdevs, name, dp);
220 dpif_netdev_open(const struct dpif_class *class, const char *name,
221 bool create, struct dpif **dpifp)
223 struct dp_netdev *dp;
225 dp = shash_find_data(&dp_netdevs, name);
230 int error = create_dp_netdev(name, class, &dp);
237 if (dp->class != class) {
244 *dpifp = create_dpif_netdev(dp);
249 dp_netdev_free(struct dp_netdev *dp)
253 dp_netdev_flow_flush(dp);
254 while (dp->n_ports > 0) {
255 struct dp_netdev_port *port = CONTAINER_OF(
256 dp->port_list.next, struct dp_netdev_port, node);
257 do_del_port(dp, port->port_no);
259 for (i = 0; i < N_QUEUES; i++) {
260 struct dp_netdev_queue *q = &dp->queues[i];
263 for (j = q->tail; j != q->head; j++) {
264 struct dpif_upcall *upcall = q->upcalls[j & QUEUE_MASK];
266 ofpbuf_delete(upcall->packet);
270 hmap_destroy(&dp->flow_table);
276 dpif_netdev_close(struct dpif *dpif)
278 struct dp_netdev *dp = get_dp_netdev(dpif);
279 assert(dp->open_cnt > 0);
280 if (--dp->open_cnt == 0 && dp->destroyed) {
281 shash_find_and_delete(&dp_netdevs, dp->name);
288 dpif_netdev_destroy(struct dpif *dpif)
290 struct dp_netdev *dp = get_dp_netdev(dpif);
291 dp->destroyed = true;
296 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
298 struct dp_netdev *dp = get_dp_netdev(dpif);
299 memset(stats, 0, sizeof *stats);
300 stats->n_flows = hmap_count(&dp->flow_table);
301 stats->cur_capacity = hmap_capacity(&dp->flow_table);
302 stats->max_capacity = MAX_FLOWS;
303 stats->n_ports = dp->n_ports;
304 stats->max_ports = MAX_PORTS;
305 stats->n_frags = dp->n_frags;
306 stats->n_hit = dp->n_hit;
307 stats->n_missed = dp->n_missed;
308 stats->n_lost = dp->n_lost;
309 stats->max_miss_queue = MAX_QUEUE_LEN;
310 stats->max_action_queue = MAX_QUEUE_LEN;
315 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
317 struct dp_netdev *dp = get_dp_netdev(dpif);
318 *drop_fragsp = dp->drop_frags;
323 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
325 struct dp_netdev *dp = get_dp_netdev(dpif);
326 dp->drop_frags = drop_frags;
331 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
334 struct dp_netdev_port *port;
335 struct netdev_options netdev_options;
336 struct netdev *netdev;
341 /* XXX reject devices already in some dp_netdev. */
342 if (type[0] == '\0' || !strcmp(type, "system")) {
344 } else if (!strcmp(type, "internal")) {
347 VLOG_WARN("%s: unsupported port type %s", devname, type);
351 /* Open and validate network device. */
352 memset(&netdev_options, 0, sizeof netdev_options);
353 netdev_options.name = devname;
354 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
355 if (dp->class == &dpif_dummy_class) {
356 netdev_options.type = "dummy";
357 } else if (internal) {
358 netdev_options.type = "tap";
361 error = netdev_open(&netdev_options, &netdev);
365 /* XXX reject loopback devices */
366 /* XXX reject non-Ethernet devices */
368 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
370 netdev_close(netdev);
374 port = xmalloc(sizeof *port);
375 port->port_no = port_no;
376 port->netdev = netdev;
377 port->internal = internal;
379 netdev_get_mtu(netdev, &mtu);
384 list_push_back(&dp->port_list, &port->node);
385 dp->ports[port_no] = port;
393 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
396 struct dp_netdev *dp = get_dp_netdev(dpif);
399 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
400 if (!dp->ports[port_no]) {
402 return do_add_port(dp, netdev_get_name(netdev),
403 netdev_get_type(netdev), port_no);
410 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
412 struct dp_netdev *dp = get_dp_netdev(dpif);
413 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
417 is_valid_port_number(uint16_t port_no)
419 return port_no < MAX_PORTS;
423 get_port_by_number(struct dp_netdev *dp,
424 uint16_t port_no, struct dp_netdev_port **portp)
426 if (!is_valid_port_number(port_no)) {
430 *portp = dp->ports[port_no];
431 return *portp ? 0 : ENOENT;
436 get_port_by_name(struct dp_netdev *dp,
437 const char *devname, struct dp_netdev_port **portp)
439 struct dp_netdev_port *port;
441 LIST_FOR_EACH (port, node, &dp->port_list) {
442 if (!strcmp(netdev_get_name(port->netdev), devname)) {
451 do_del_port(struct dp_netdev *dp, uint16_t port_no)
453 struct dp_netdev_port *port;
457 error = get_port_by_number(dp, port_no, &port);
462 list_remove(&port->node);
463 dp->ports[port->port_no] = NULL;
467 name = xstrdup(netdev_get_name(port->netdev));
468 netdev_close(port->netdev);
477 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
479 memset(odp_port, 0, sizeof *odp_port);
480 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
481 sizeof odp_port->devname);
482 odp_port->port = port->port_no;
483 strcpy(odp_port->type, port->internal ? "internal" : "system");
487 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
488 struct odp_port *odp_port)
490 struct dp_netdev *dp = get_dp_netdev(dpif);
491 struct dp_netdev_port *port;
494 error = get_port_by_number(dp, port_no, &port);
496 answer_port_query(port, odp_port);
502 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
503 struct odp_port *odp_port)
505 struct dp_netdev *dp = get_dp_netdev(dpif);
506 struct dp_netdev_port *port;
509 error = get_port_by_name(dp, devname, &port);
511 answer_port_query(port, odp_port);
517 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
519 hmap_remove(&dp->flow_table, &flow->node);
525 dp_netdev_flow_flush(struct dp_netdev *dp)
527 struct dp_netdev_flow *flow, *next;
529 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
530 dp_netdev_free_flow(dp, flow);
535 dpif_netdev_flow_flush(struct dpif *dpif)
537 struct dp_netdev *dp = get_dp_netdev(dpif);
538 dp_netdev_flow_flush(dp);
542 struct dp_netdev_port_state {
547 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
549 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
554 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
555 struct odp_port *odp_port)
557 struct dp_netdev_port_state *state = state_;
558 struct dp_netdev *dp = get_dp_netdev(dpif);
561 for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
562 struct dp_netdev_port *port = dp->ports[port_no];
564 answer_port_query(port, odp_port);
565 state->port_no = port_no + 1;
573 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
580 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
582 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
583 if (dpif->dp_serial != dpif->dp->serial) {
584 dpif->dp_serial = dpif->dp->serial;
592 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
594 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
595 if (dpif->dp_serial != dpif->dp->serial) {
596 poll_immediate_wake();
600 static struct dp_netdev_flow *
601 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
603 struct dp_netdev_flow *flow;
605 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
606 if (flow_equal(&flow->key, key)) {
613 /* The caller must fill in odp_flow->key itself. */
615 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
616 struct odp_flow *odp_flow)
619 odp_flow->stats.n_packets = flow->packet_count;
620 odp_flow->stats.n_bytes = flow->byte_count;
621 odp_flow->stats.used_sec = flow->used.tv_sec;
622 odp_flow->stats.used_nsec = flow->used.tv_nsec;
623 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
624 odp_flow->stats.reserved = 0;
625 odp_flow->stats.error = 0;
626 if (odp_flow->actions_len > 0) {
627 memcpy(odp_flow->actions, flow->actions,
628 MIN(odp_flow->actions_len, flow->actions_len));
629 odp_flow->actions_len = flow->actions_len;
632 if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
637 odp_flow->stats.error = ENOENT;
642 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
645 if (odp_flow_key_to_flow(key, key_len, flow)) {
646 /* This should not happen: it indicates that odp_flow_key_from_flow()
647 * and odp_flow_key_to_flow() disagree on the acceptable form of a
648 * flow. Log the problem as an error, with enough details to enable
650 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
652 if (!VLOG_DROP_ERR(&rl)) {
656 odp_flow_key_format(key, key_len, &s);
657 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
668 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
670 struct dp_netdev *dp = get_dp_netdev(dpif);
673 for (i = 0; i < n; i++) {
674 struct odp_flow *odp_flow = &flows[i];
678 error = dpif_netdev_flow_from_nlattrs(odp_flow->key, odp_flow->key_len,
684 answer_flow_query(dp_netdev_lookup_flow(dp, &key),
685 odp_flow->flags, odp_flow);
691 dpif_netdev_validate_actions(const struct nlattr *actions,
692 size_t actions_len, bool *mutates)
694 const struct nlattr *a;
698 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
699 uint16_t type = nl_attr_type(a);
700 int len = odp_action_len(type);
702 if (len != nl_attr_get_size(a)) {
708 if (nl_attr_get_u32(a) >= MAX_PORTS) {
713 case ODPAT_CONTROLLER:
714 case ODPAT_DROP_SPOOFED_ARP:
717 case ODPAT_SET_DL_TCI:
719 if (nl_attr_get_be16(a) & htons(VLAN_CFI)) {
724 case ODPAT_SET_NW_TOS:
726 if (nl_attr_get_u8(a) & IP_ECN_MASK) {
731 case ODPAT_STRIP_VLAN:
732 case ODPAT_SET_DL_SRC:
733 case ODPAT_SET_DL_DST:
734 case ODPAT_SET_NW_SRC:
735 case ODPAT_SET_NW_DST:
736 case ODPAT_SET_TP_SRC:
737 case ODPAT_SET_TP_DST:
741 case ODPAT_SET_TUNNEL:
742 case ODPAT_SET_PRIORITY:
743 case ODPAT_POP_PRIORITY:
752 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
757 error = dpif_netdev_validate_actions(odp_flow->actions,
758 odp_flow->actions_len, &mutates);
763 flow->actions = xrealloc(flow->actions, odp_flow->actions_len);
764 flow->actions_len = odp_flow->actions_len;
765 memcpy(flow->actions, odp_flow->actions, odp_flow->actions_len);
770 add_flow(struct dpif *dpif, const struct flow *key, struct odp_flow *odp_flow)
772 struct dp_netdev *dp = get_dp_netdev(dpif);
773 struct dp_netdev_flow *flow;
776 flow = xzalloc(sizeof *flow);
779 error = set_flow_actions(flow, odp_flow);
785 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
790 clear_stats(struct dp_netdev_flow *flow)
792 flow->used.tv_sec = 0;
793 flow->used.tv_nsec = 0;
794 flow->packet_count = 0;
795 flow->byte_count = 0;
800 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
802 struct dp_netdev *dp = get_dp_netdev(dpif);
803 struct dp_netdev_flow *flow;
807 error = dpif_netdev_flow_from_nlattrs(put->flow.key, put->flow.key_len,
813 flow = dp_netdev_lookup_flow(dp, &key);
815 if (put->flags & ODPPF_CREATE) {
816 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
817 return add_flow(dpif, &key, &put->flow);
825 if (put->flags & ODPPF_MODIFY) {
826 int error = set_flow_actions(flow, &put->flow);
827 if (!error && put->flags & ODPPF_ZERO_STATS) {
839 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
841 struct dp_netdev *dp = get_dp_netdev(dpif);
842 struct dp_netdev_flow *flow;
846 error = dpif_netdev_flow_from_nlattrs(odp_flow->key, odp_flow->key_len,
852 flow = dp_netdev_lookup_flow(dp, &key);
854 answer_flow_query(flow, 0, odp_flow);
855 dp_netdev_free_flow(dp, flow);
862 struct dp_netdev_flow_state {
868 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
870 *statep = xzalloc(sizeof(struct dp_netdev_flow_state));
875 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
876 struct odp_flow *odp_flow)
878 struct dp_netdev_flow_state *state = state_;
879 struct dp_netdev *dp = get_dp_netdev(dpif);
880 struct dp_netdev_flow *flow;
881 struct hmap_node *node;
884 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
889 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
891 ofpbuf_use_stack(&key, odp_flow->key, odp_flow->key_len);
892 odp_flow_key_from_flow(&key, &flow->key);
893 odp_flow->key_len = key.size;
896 answer_flow_query(flow, 0, odp_flow);
902 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
909 dpif_netdev_execute(struct dpif *dpif,
910 const struct nlattr *actions, size_t actions_len,
911 const struct ofpbuf *packet)
913 struct dp_netdev *dp = get_dp_netdev(dpif);
919 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
923 error = dpif_netdev_validate_actions(actions, actions_len, &mutates);
929 /* We need a deep copy of 'packet' since we're going to modify its
931 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
932 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
933 ofpbuf_put(©, packet->data, packet->size);
935 /* We still need a shallow copy of 'packet', even though we won't
936 * modify its data, because flow_extract() modifies packet->l2, etc.
937 * We could probably get away with modifying those but it's more polite
941 flow_extract(©, 0, -1, &key);
942 error = dp_netdev_execute_actions(dp, ©, &key, actions, actions_len);
944 ofpbuf_uninit(©);
950 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
952 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
953 *listen_mask = dpif_netdev->listen_mask;
958 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
960 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
961 if (!(listen_mask & ~ODPL_ALL)) {
962 dpif_netdev->listen_mask = listen_mask;
969 static struct dp_netdev_queue *
970 find_nonempty_queue(struct dpif *dpif)
972 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
973 struct dp_netdev *dp = get_dp_netdev(dpif);
974 int mask = dpif_netdev->listen_mask;
977 for (i = 0; i < N_QUEUES; i++) {
978 struct dp_netdev_queue *q = &dp->queues[i];
979 if (q->head != q->tail && mask & (1u << i)) {
987 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall)
989 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
991 struct dpif_upcall *u = q->upcalls[q->tail++ & QUEUE_MASK];
1002 dpif_netdev_recv_wait(struct dpif *dpif)
1004 if (find_nonempty_queue(dpif)) {
1005 poll_immediate_wake();
1007 /* No messages ready to be received, and dp_wait() will ensure that we
1008 * wake up to queue new messages, so there is nothing to do. */
1013 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
1014 const struct ofpbuf *packet)
1016 time_timespec(&flow->used);
1017 flow->packet_count++;
1018 flow->byte_count += packet->size;
1019 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
1020 struct tcp_header *th = packet->l4;
1021 flow->tcp_ctl |= th->tcp_ctl;
1026 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1027 struct ofpbuf *packet)
1029 struct dp_netdev_flow *flow;
1032 if (packet->size < ETH_HEADER_LEN) {
1035 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
1040 flow = dp_netdev_lookup_flow(dp, &key);
1042 dp_netdev_flow_used(flow, &key, packet);
1043 dp_netdev_execute_actions(dp, packet, &key,
1044 flow->actions, flow->actions_len);
1048 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, &key, 0);
1055 struct shash_node *node;
1056 struct ofpbuf packet;
1058 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1059 SHASH_FOR_EACH (node, &dp_netdevs) {
1060 struct dp_netdev *dp = node->data;
1061 struct dp_netdev_port *port;
1063 LIST_FOR_EACH (port, node, &dp->port_list) {
1066 /* Reset packet contents. */
1067 ofpbuf_clear(&packet);
1068 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1070 error = netdev_recv(port->netdev, &packet);
1072 dp_netdev_port_input(dp, port, &packet);
1073 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1074 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1075 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1076 netdev_get_name(port->netdev), strerror(error));
1080 ofpbuf_uninit(&packet);
1084 dp_netdev_wait(void)
1086 struct shash_node *node;
1088 SHASH_FOR_EACH (node, &dp_netdevs) {
1089 struct dp_netdev *dp = node->data;
1090 struct dp_netdev_port *port;
1092 LIST_FOR_EACH (port, node, &dp->port_list) {
1093 netdev_recv_wait(port->netdev);
1099 /* Modify the TCI field of 'packet'. If a VLAN tag is present, its TCI field
1100 * is replaced by 'tci'. If a VLAN tag is not present, one is added with the
1101 * TCI field set to 'tci'.
1104 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
1106 struct vlan_eth_header *veh;
1107 struct eth_header *eh;
1110 if (packet->size >= sizeof(struct vlan_eth_header)
1111 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1113 veh->veth_tci = tci;
1115 /* Insert new 802.1Q header. */
1116 struct vlan_eth_header tmp;
1117 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1118 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1119 tmp.veth_type = htons(ETH_TYPE_VLAN);
1121 tmp.veth_next_type = eh->eth_type;
1123 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1124 memcpy(veh, &tmp, sizeof tmp);
1125 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1130 dp_netdev_strip_vlan(struct ofpbuf *packet)
1132 struct vlan_eth_header *veh = packet->l2;
1133 if (packet->size >= sizeof *veh
1134 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1135 struct eth_header tmp;
1137 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1138 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1139 tmp.eth_type = veh->veth_next_type;
1141 ofpbuf_pull(packet, VLAN_HEADER_LEN);
1142 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1143 memcpy(packet->data, &tmp, sizeof tmp);
1148 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1150 struct eth_header *eh = packet->l2;
1151 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1155 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1157 struct eth_header *eh = packet->l2;
1158 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1162 is_ip(const struct ofpbuf *packet, const struct flow *key)
1164 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1168 dp_netdev_set_nw_addr(struct ofpbuf *packet, const struct flow *key,
1169 const struct nlattr *a)
1171 if (is_ip(packet, key)) {
1172 struct ip_header *nh = packet->l3;
1173 ovs_be32 ip = nl_attr_get_be32(a);
1174 uint16_t type = nl_attr_type(a);
1177 field = type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1178 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1179 struct tcp_header *th = packet->l4;
1180 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, ip);
1181 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1182 struct udp_header *uh = packet->l4;
1184 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, ip);
1185 if (!uh->udp_csum) {
1186 uh->udp_csum = 0xffff;
1190 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, ip);
1196 dp_netdev_set_nw_tos(struct ofpbuf *packet, const struct flow *key,
1199 if (is_ip(packet, key)) {
1200 struct ip_header *nh = packet->l3;
1201 uint8_t *field = &nh->ip_tos;
1203 /* Set the DSCP bits and preserve the ECN bits. */
1204 uint8_t new = nw_tos | (nh->ip_tos & IP_ECN_MASK);
1206 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1207 htons((uint16_t) new));
1213 dp_netdev_set_tp_port(struct ofpbuf *packet, const struct flow *key,
1214 const struct nlattr *a)
1216 if (is_ip(packet, key)) {
1217 uint16_t type = nl_attr_type(a);
1218 ovs_be16 port = nl_attr_get_be16(a);
1221 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1222 struct tcp_header *th = packet->l4;
1223 field = type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1224 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, port);
1226 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1227 struct udp_header *uh = packet->l4;
1228 field = type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1229 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, port);
1238 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1241 struct dp_netdev_port *p = dp->ports[out_port];
1243 netdev_send(p->netdev, packet);
1248 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1249 int queue_no, const struct flow *flow, uint64_t arg)
1251 struct dp_netdev_queue *q = &dp->queues[queue_no];
1252 struct dpif_upcall *upcall;
1256 if (q->head - q->tail >= MAX_QUEUE_LEN) {
1261 buf = ofpbuf_new(ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size);
1262 odp_flow_key_from_flow(buf, flow);
1263 key_len = buf->size;
1264 ofpbuf_pull(buf, key_len);
1265 ofpbuf_reserve(buf, 2);
1266 ofpbuf_put(buf, packet->data, packet->size);
1268 upcall = xzalloc(sizeof *upcall);
1269 upcall->type = queue_no;
1270 upcall->packet = buf;
1271 upcall->key = buf->base;
1272 upcall->key_len = key_len;
1273 upcall->userdata = arg;
1275 q->upcalls[++q->head & QUEUE_MASK] = upcall;
1280 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1281 * screwy or truncated header fields or one whose inner and outer Ethernet
1282 * address differ. */
1284 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1286 struct arp_eth_header *arp;
1287 struct eth_header *eth;
1290 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1294 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1295 if (l3_size < sizeof(struct arp_eth_header)) {
1301 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1302 || arp->ar_pro != htons(ARP_PRO_IP)
1303 || arp->ar_hln != ETH_HEADER_LEN
1305 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1309 dp_netdev_execute_actions(struct dp_netdev *dp,
1310 struct ofpbuf *packet, struct flow *key,
1311 const struct nlattr *actions,
1314 const struct nlattr *a;
1317 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1318 switch (nl_attr_type(a)) {
1320 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1323 case ODPAT_CONTROLLER:
1324 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1325 key, nl_attr_get_u64(a));
1328 case ODPAT_SET_DL_TCI:
1329 dp_netdev_set_dl_tci(packet, nl_attr_get_be16(a));
1332 case ODPAT_STRIP_VLAN:
1333 dp_netdev_strip_vlan(packet);
1336 case ODPAT_SET_DL_SRC:
1337 dp_netdev_set_dl_src(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1340 case ODPAT_SET_DL_DST:
1341 dp_netdev_set_dl_dst(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1344 case ODPAT_SET_NW_SRC:
1345 case ODPAT_SET_NW_DST:
1346 dp_netdev_set_nw_addr(packet, key, a);
1349 case ODPAT_SET_NW_TOS:
1350 dp_netdev_set_nw_tos(packet, key, nl_attr_get_u8(a));
1353 case ODPAT_SET_TP_SRC:
1354 case ODPAT_SET_TP_DST:
1355 dp_netdev_set_tp_port(packet, key, a);
1358 case ODPAT_DROP_SPOOFED_ARP:
1359 if (dp_netdev_is_spoofed_arp(packet, key)) {
1367 const struct dpif_class dpif_netdev_class = {
1371 NULL, /* enumerate */
1374 NULL, /* get_all_names */
1375 dpif_netdev_destroy,
1376 dpif_netdev_get_stats,
1377 dpif_netdev_get_drop_frags,
1378 dpif_netdev_set_drop_frags,
1379 dpif_netdev_port_add,
1380 dpif_netdev_port_del,
1381 dpif_netdev_port_query_by_number,
1382 dpif_netdev_port_query_by_name,
1383 dpif_netdev_port_dump_start,
1384 dpif_netdev_port_dump_next,
1385 dpif_netdev_port_dump_done,
1386 dpif_netdev_port_poll,
1387 dpif_netdev_port_poll_wait,
1388 dpif_netdev_flow_get,
1389 dpif_netdev_flow_put,
1390 dpif_netdev_flow_del,
1391 dpif_netdev_flow_flush,
1392 dpif_netdev_flow_dump_start,
1393 dpif_netdev_flow_dump_next,
1394 dpif_netdev_flow_dump_done,
1395 dpif_netdev_execute,
1396 dpif_netdev_recv_get_mask,
1397 dpif_netdev_recv_set_mask,
1398 NULL, /* get_sflow_probability */
1399 NULL, /* set_sflow_probability */
1400 NULL, /* queue_to_priority */
1402 dpif_netdev_recv_wait,
1406 dpif_dummy_register(void)
1408 if (!dpif_dummy_class.type) {
1409 dpif_dummy_class = dpif_netdev_class;
1410 dpif_dummy_class.type = "dummy";
1411 dp_register_provider(&dpif_dummy_class);