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 { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
59 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
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 };
67 /* Datapath based on the network device interface from netdev.h. */
69 const struct dpif_class *class;
74 bool drop_frags; /* Drop all IP fragments, if true. */
75 struct list queues[N_QUEUES]; /* Contain ofpbufs queued for dpif_recv(). */
76 size_t queue_len[N_QUEUES]; /* Number of packets in each queue. */
77 struct hmap flow_table; /* Flow table. */
80 long long int n_frags; /* Number of dropped IP fragments. */
81 long long int n_hit; /* Number of flow table matches. */
82 long long int n_missed; /* Number of flow table misses. */
83 long long int n_lost; /* Number of misses not passed to client. */
87 struct dp_netdev_port *ports[MAX_PORTS];
88 struct list port_list;
92 /* A port in a netdev-based datapath. */
93 struct dp_netdev_port {
94 int port_no; /* Index into dp_netdev's 'ports'. */
95 struct list node; /* Element in dp_netdev's 'port_list'. */
96 struct netdev *netdev;
97 bool internal; /* Internal port? */
100 /* A flow in dp_netdev's 'flow_table'. */
101 struct dp_netdev_flow {
102 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
106 struct timespec used; /* Last used time. */
107 long long int packet_count; /* Number of packets matched. */
108 long long int byte_count; /* Number of bytes matched. */
109 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
112 struct nlattr *actions;
116 /* Interface to netdev-based datapath. */
119 struct dp_netdev *dp;
121 unsigned int dp_serial;
124 /* All netdev-based datapaths. */
125 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
127 /* Maximum port MTU seen so far. */
128 static int max_mtu = ETH_PAYLOAD_MAX;
130 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
131 struct dp_netdev_port **portp);
132 static int get_port_by_name(struct dp_netdev *, const char *devname,
133 struct dp_netdev_port **portp);
134 static void dp_netdev_free(struct dp_netdev *);
135 static void dp_netdev_flow_flush(struct dp_netdev *);
136 static int do_add_port(struct dp_netdev *, const char *devname,
137 const char *type, uint16_t port_no);
138 static int do_del_port(struct dp_netdev *, uint16_t port_no);
139 static int dpif_netdev_open(const struct dpif_class *, const char *name,
140 bool create, struct dpif **);
141 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
142 int queue_no, int port_no, uint64_t arg);
143 static int dp_netdev_execute_actions(struct dp_netdev *,
144 struct ofpbuf *, struct flow *,
145 const struct nlattr *actions,
148 static struct dpif_class dpif_dummy_class;
150 static struct dpif_netdev *
151 dpif_netdev_cast(const struct dpif *dpif)
153 assert(dpif->dpif_class->open == dpif_netdev_open);
154 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
157 static struct dp_netdev *
158 get_dp_netdev(const struct dpif *dpif)
160 return dpif_netdev_cast(dpif)->dp;
164 create_dpif_netdev(struct dp_netdev *dp)
166 uint16_t netflow_id = hash_string(dp->name, 0);
167 struct dpif_netdev *dpif;
171 dpif = xmalloc(sizeof *dpif);
172 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
174 dpif->listen_mask = 0;
175 dpif->dp_serial = dp->serial;
181 create_dp_netdev(const char *name, const struct dpif_class *class,
182 struct dp_netdev **dpp)
184 struct dp_netdev *dp;
188 dp = xzalloc(sizeof *dp);
190 dp->name = xstrdup(name);
192 dp->drop_frags = false;
193 for (i = 0; i < N_QUEUES; i++) {
194 list_init(&dp->queues[i]);
196 hmap_init(&dp->flow_table);
197 list_init(&dp->port_list);
198 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
204 shash_add(&dp_netdevs, name, dp);
211 dpif_netdev_open(const struct dpif_class *class, const char *name,
212 bool create, struct dpif **dpifp)
214 struct dp_netdev *dp;
216 dp = shash_find_data(&dp_netdevs, name);
221 int error = create_dp_netdev(name, class, &dp);
228 if (dp->class != class) {
235 *dpifp = create_dpif_netdev(dp);
240 dp_netdev_free(struct dp_netdev *dp)
244 dp_netdev_flow_flush(dp);
245 while (dp->n_ports > 0) {
246 struct dp_netdev_port *port = CONTAINER_OF(
247 dp->port_list.next, struct dp_netdev_port, node);
248 do_del_port(dp, port->port_no);
250 for (i = 0; i < N_QUEUES; i++) {
251 ofpbuf_list_delete(&dp->queues[i]);
253 hmap_destroy(&dp->flow_table);
259 dpif_netdev_close(struct dpif *dpif)
261 struct dp_netdev *dp = get_dp_netdev(dpif);
262 assert(dp->open_cnt > 0);
263 if (--dp->open_cnt == 0 && dp->destroyed) {
264 shash_find_and_delete(&dp_netdevs, dp->name);
271 dpif_netdev_destroy(struct dpif *dpif)
273 struct dp_netdev *dp = get_dp_netdev(dpif);
274 dp->destroyed = true;
279 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
281 struct dp_netdev *dp = get_dp_netdev(dpif);
282 memset(stats, 0, sizeof *stats);
283 stats->n_flows = hmap_count(&dp->flow_table);
284 stats->cur_capacity = hmap_capacity(&dp->flow_table);
285 stats->max_capacity = MAX_FLOWS;
286 stats->n_ports = dp->n_ports;
287 stats->max_ports = MAX_PORTS;
288 stats->n_frags = dp->n_frags;
289 stats->n_hit = dp->n_hit;
290 stats->n_missed = dp->n_missed;
291 stats->n_lost = dp->n_lost;
292 stats->max_miss_queue = MAX_QUEUE_LEN;
293 stats->max_action_queue = MAX_QUEUE_LEN;
298 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
300 struct dp_netdev *dp = get_dp_netdev(dpif);
301 *drop_fragsp = dp->drop_frags;
306 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
308 struct dp_netdev *dp = get_dp_netdev(dpif);
309 dp->drop_frags = drop_frags;
314 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
317 struct dp_netdev_port *port;
318 struct netdev_options netdev_options;
319 struct netdev *netdev;
324 /* XXX reject devices already in some dp_netdev. */
325 if (type[0] == '\0' || !strcmp(type, "system")) {
327 } else if (!strcmp(type, "internal")) {
330 VLOG_WARN("%s: unsupported port type %s", devname, type);
334 /* Open and validate network device. */
335 memset(&netdev_options, 0, sizeof netdev_options);
336 netdev_options.name = devname;
337 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
338 if (dp->class == &dpif_dummy_class) {
339 netdev_options.type = "dummy";
340 } else if (internal) {
341 netdev_options.type = "tap";
344 error = netdev_open(&netdev_options, &netdev);
348 /* XXX reject loopback devices */
349 /* XXX reject non-Ethernet devices */
351 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
353 netdev_close(netdev);
357 port = xmalloc(sizeof *port);
358 port->port_no = port_no;
359 port->netdev = netdev;
360 port->internal = internal;
362 netdev_get_mtu(netdev, &mtu);
367 list_push_back(&dp->port_list, &port->node);
368 dp->ports[port_no] = port;
376 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
379 struct dp_netdev *dp = get_dp_netdev(dpif);
382 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
383 if (!dp->ports[port_no]) {
385 return do_add_port(dp, netdev_get_name(netdev),
386 netdev_get_type(netdev), port_no);
393 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
395 struct dp_netdev *dp = get_dp_netdev(dpif);
396 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
400 is_valid_port_number(uint16_t port_no)
402 return port_no < MAX_PORTS;
406 get_port_by_number(struct dp_netdev *dp,
407 uint16_t port_no, struct dp_netdev_port **portp)
409 if (!is_valid_port_number(port_no)) {
413 *portp = dp->ports[port_no];
414 return *portp ? 0 : ENOENT;
419 get_port_by_name(struct dp_netdev *dp,
420 const char *devname, struct dp_netdev_port **portp)
422 struct dp_netdev_port *port;
424 LIST_FOR_EACH (port, node, &dp->port_list) {
425 if (!strcmp(netdev_get_name(port->netdev), devname)) {
434 do_del_port(struct dp_netdev *dp, uint16_t port_no)
436 struct dp_netdev_port *port;
440 error = get_port_by_number(dp, port_no, &port);
445 list_remove(&port->node);
446 dp->ports[port->port_no] = NULL;
450 name = xstrdup(netdev_get_name(port->netdev));
451 netdev_close(port->netdev);
460 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
462 memset(odp_port, 0, sizeof *odp_port);
463 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
464 sizeof odp_port->devname);
465 odp_port->port = port->port_no;
466 strcpy(odp_port->type, port->internal ? "internal" : "system");
470 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
471 struct odp_port *odp_port)
473 struct dp_netdev *dp = get_dp_netdev(dpif);
474 struct dp_netdev_port *port;
477 error = get_port_by_number(dp, port_no, &port);
479 answer_port_query(port, odp_port);
485 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
486 struct odp_port *odp_port)
488 struct dp_netdev *dp = get_dp_netdev(dpif);
489 struct dp_netdev_port *port;
492 error = get_port_by_name(dp, devname, &port);
494 answer_port_query(port, odp_port);
500 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
502 hmap_remove(&dp->flow_table, &flow->node);
508 dp_netdev_flow_flush(struct dp_netdev *dp)
510 struct dp_netdev_flow *flow, *next;
512 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
513 dp_netdev_free_flow(dp, flow);
518 dpif_netdev_flow_flush(struct dpif *dpif)
520 struct dp_netdev *dp = get_dp_netdev(dpif);
521 dp_netdev_flow_flush(dp);
526 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
528 struct dp_netdev *dp = get_dp_netdev(dpif);
529 struct dp_netdev_port *port;
533 LIST_FOR_EACH (port, node, &dp->port_list) {
534 struct odp_port *odp_port = &ports[i];
538 answer_port_query(port, odp_port);
545 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
547 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
548 if (dpif->dp_serial != dpif->dp->serial) {
549 dpif->dp_serial = dpif->dp->serial;
557 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
559 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
560 if (dpif->dp_serial != dpif->dp->serial) {
561 poll_immediate_wake();
565 static struct dp_netdev_flow *
566 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
568 struct dp_netdev_flow *flow;
570 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
571 if (flow_equal(&flow->key, key)) {
578 /* The caller must fill in odp_flow->key itself. */
580 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
581 struct odp_flow *odp_flow)
584 odp_flow->stats.n_packets = flow->packet_count;
585 odp_flow->stats.n_bytes = flow->byte_count;
586 odp_flow->stats.used_sec = flow->used.tv_sec;
587 odp_flow->stats.used_nsec = flow->used.tv_nsec;
588 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
589 odp_flow->stats.reserved = 0;
590 odp_flow->stats.error = 0;
591 if (odp_flow->actions_len > 0) {
592 memcpy(odp_flow->actions, flow->actions,
593 MIN(odp_flow->actions_len, flow->actions_len));
594 odp_flow->actions_len = flow->actions_len;
597 if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
602 odp_flow->stats.error = ENOENT;
607 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
610 if (odp_flow_key_to_flow(key, key_len, flow)) {
611 /* This should not happen: it indicates that odp_flow_key_from_flow()
612 * and odp_flow_key_to_flow() disagree on the acceptable form of a
613 * flow. Log the problem as an error, with enough details to enable
615 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
617 if (!VLOG_DROP_ERR(&rl)) {
621 odp_flow_key_format(key, key_len, &s);
622 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
633 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
635 struct dp_netdev *dp = get_dp_netdev(dpif);
638 for (i = 0; i < n; i++) {
639 struct odp_flow *odp_flow = &flows[i];
643 error = dpif_netdev_flow_from_nlattrs(odp_flow->key, odp_flow->key_len,
649 answer_flow_query(dp_netdev_lookup_flow(dp, &key),
650 odp_flow->flags, odp_flow);
656 dpif_netdev_validate_actions(const struct nlattr *actions,
657 size_t actions_len, bool *mutates)
659 const struct nlattr *a;
663 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
664 uint16_t type = nl_attr_type(a);
665 int len = odp_action_len(type);
667 if (len != nl_attr_get_size(a)) {
673 if (nl_attr_get_u32(a) >= MAX_PORTS) {
678 case ODPAT_CONTROLLER:
679 case ODPAT_DROP_SPOOFED_ARP:
682 case ODPAT_SET_DL_TCI:
684 if (nl_attr_get_be16(a) & htons(VLAN_CFI)) {
689 case ODPAT_SET_NW_TOS:
691 if (nl_attr_get_u8(a) & IP_ECN_MASK) {
696 case ODPAT_STRIP_VLAN:
697 case ODPAT_SET_DL_SRC:
698 case ODPAT_SET_DL_DST:
699 case ODPAT_SET_NW_SRC:
700 case ODPAT_SET_NW_DST:
701 case ODPAT_SET_TP_SRC:
702 case ODPAT_SET_TP_DST:
706 case ODPAT_SET_TUNNEL:
707 case ODPAT_SET_PRIORITY:
708 case ODPAT_POP_PRIORITY:
717 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
722 error = dpif_netdev_validate_actions(odp_flow->actions,
723 odp_flow->actions_len, &mutates);
728 flow->actions = xrealloc(flow->actions, odp_flow->actions_len);
729 flow->actions_len = odp_flow->actions_len;
730 memcpy(flow->actions, odp_flow->actions, odp_flow->actions_len);
735 add_flow(struct dpif *dpif, const struct flow *key, struct odp_flow *odp_flow)
737 struct dp_netdev *dp = get_dp_netdev(dpif);
738 struct dp_netdev_flow *flow;
741 flow = xzalloc(sizeof *flow);
744 error = set_flow_actions(flow, odp_flow);
750 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
755 clear_stats(struct dp_netdev_flow *flow)
757 flow->used.tv_sec = 0;
758 flow->used.tv_nsec = 0;
759 flow->packet_count = 0;
760 flow->byte_count = 0;
765 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
767 struct dp_netdev *dp = get_dp_netdev(dpif);
768 struct dp_netdev_flow *flow;
772 error = dpif_netdev_flow_from_nlattrs(put->flow.key, put->flow.key_len,
778 flow = dp_netdev_lookup_flow(dp, &key);
780 if (put->flags & ODPPF_CREATE) {
781 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
782 return add_flow(dpif, &key, &put->flow);
790 if (put->flags & ODPPF_MODIFY) {
791 int error = set_flow_actions(flow, &put->flow);
792 if (!error && put->flags & ODPPF_ZERO_STATS) {
804 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
806 struct dp_netdev *dp = get_dp_netdev(dpif);
807 struct dp_netdev_flow *flow;
811 error = dpif_netdev_flow_from_nlattrs(odp_flow->key, odp_flow->key_len,
817 flow = dp_netdev_lookup_flow(dp, &key);
819 answer_flow_query(flow, 0, odp_flow);
820 dp_netdev_free_flow(dp, flow);
827 struct dp_netdev_flow_state {
833 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
835 *statep = xzalloc(sizeof(struct dp_netdev_flow_state));
840 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
841 struct odp_flow *odp_flow)
843 struct dp_netdev_flow_state *state = state_;
844 struct dp_netdev *dp = get_dp_netdev(dpif);
845 struct dp_netdev_flow *flow;
846 struct hmap_node *node;
849 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
854 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
856 ofpbuf_use_stack(&key, odp_flow->key, odp_flow->key_len);
857 odp_flow_key_from_flow(&key, &flow->key);
858 odp_flow->key_len = key.size;
861 answer_flow_query(flow, 0, odp_flow);
867 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
874 dpif_netdev_execute(struct dpif *dpif,
875 const struct nlattr *actions, size_t actions_len,
876 const struct ofpbuf *packet)
878 struct dp_netdev *dp = get_dp_netdev(dpif);
884 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
888 error = dpif_netdev_validate_actions(actions, actions_len, &mutates);
894 /* We need a deep copy of 'packet' since we're going to modify its
896 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
897 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
898 ofpbuf_put(©, packet->data, packet->size);
900 /* We still need a shallow copy of 'packet', even though we won't
901 * modify its data, because flow_extract() modifies packet->l2, etc.
902 * We could probably get away with modifying those but it's more polite
906 flow_extract(©, 0, -1, &key);
907 error = dp_netdev_execute_actions(dp, ©, &key, actions, actions_len);
909 ofpbuf_uninit(©);
915 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
917 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
918 *listen_mask = dpif_netdev->listen_mask;
923 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
925 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
926 if (!(listen_mask & ~ODPL_ALL)) {
927 dpif_netdev->listen_mask = listen_mask;
935 find_nonempty_queue(struct dpif *dpif)
937 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
938 struct dp_netdev *dp = get_dp_netdev(dpif);
939 int mask = dpif_netdev->listen_mask;
942 for (i = 0; i < N_QUEUES; i++) {
943 struct list *queue = &dp->queues[i];
944 if (!list_is_empty(queue) && mask & (1u << i)) {
952 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
954 int queue_idx = find_nonempty_queue(dpif);
955 if (queue_idx >= 0) {
956 struct dp_netdev *dp = get_dp_netdev(dpif);
958 *bufp = ofpbuf_from_list(list_pop_front(&dp->queues[queue_idx]));
959 dp->queue_len[queue_idx]--;
968 dpif_netdev_recv_wait(struct dpif *dpif)
970 if (find_nonempty_queue(dpif) >= 0) {
971 poll_immediate_wake();
973 /* No messages ready to be received, and dp_wait() will ensure that we
974 * wake up to queue new messages, so there is nothing to do. */
979 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
980 const struct ofpbuf *packet)
982 time_timespec(&flow->used);
983 flow->packet_count++;
984 flow->byte_count += packet->size;
985 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
986 struct tcp_header *th = packet->l4;
987 flow->tcp_ctl |= th->tcp_ctl;
992 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
993 struct ofpbuf *packet)
995 struct dp_netdev_flow *flow;
998 if (packet->size < ETH_HEADER_LEN) {
1001 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
1006 flow = dp_netdev_lookup_flow(dp, &key);
1008 dp_netdev_flow_used(flow, &key, packet);
1009 dp_netdev_execute_actions(dp, packet, &key,
1010 flow->actions, flow->actions_len);
1014 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
1021 struct shash_node *node;
1022 struct ofpbuf packet;
1024 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1025 SHASH_FOR_EACH (node, &dp_netdevs) {
1026 struct dp_netdev *dp = node->data;
1027 struct dp_netdev_port *port;
1029 LIST_FOR_EACH (port, node, &dp->port_list) {
1032 /* Reset packet contents. */
1033 ofpbuf_clear(&packet);
1034 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1036 error = netdev_recv(port->netdev, &packet);
1038 dp_netdev_port_input(dp, port, &packet);
1039 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1040 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1041 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1042 netdev_get_name(port->netdev), strerror(error));
1046 ofpbuf_uninit(&packet);
1050 dp_netdev_wait(void)
1052 struct shash_node *node;
1054 SHASH_FOR_EACH (node, &dp_netdevs) {
1055 struct dp_netdev *dp = node->data;
1056 struct dp_netdev_port *port;
1058 LIST_FOR_EACH (port, node, &dp->port_list) {
1059 netdev_recv_wait(port->netdev);
1065 /* Modify the TCI field of 'packet'. If a VLAN tag is present, its TCI field
1066 * is replaced by 'tci'. If a VLAN tag is not present, one is added with the
1067 * TCI field set to 'tci'.
1070 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
1072 struct vlan_eth_header *veh;
1073 struct eth_header *eh;
1076 if (packet->size >= sizeof(struct vlan_eth_header)
1077 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1079 veh->veth_tci = tci;
1081 /* Insert new 802.1Q header. */
1082 struct vlan_eth_header tmp;
1083 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1084 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1085 tmp.veth_type = htons(ETH_TYPE_VLAN);
1087 tmp.veth_next_type = eh->eth_type;
1089 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1090 memcpy(veh, &tmp, sizeof tmp);
1091 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1096 dp_netdev_strip_vlan(struct ofpbuf *packet)
1098 struct vlan_eth_header *veh = packet->l2;
1099 if (packet->size >= sizeof *veh
1100 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1101 struct eth_header tmp;
1103 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1104 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1105 tmp.eth_type = veh->veth_next_type;
1107 ofpbuf_pull(packet, VLAN_HEADER_LEN);
1108 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1109 memcpy(packet->data, &tmp, sizeof tmp);
1114 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1116 struct eth_header *eh = packet->l2;
1117 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1121 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1123 struct eth_header *eh = packet->l2;
1124 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1128 is_ip(const struct ofpbuf *packet, const struct flow *key)
1130 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1134 dp_netdev_set_nw_addr(struct ofpbuf *packet, const struct flow *key,
1135 const struct nlattr *a)
1137 if (is_ip(packet, key)) {
1138 struct ip_header *nh = packet->l3;
1139 ovs_be32 ip = nl_attr_get_be32(a);
1140 uint16_t type = nl_attr_type(a);
1143 field = type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1144 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1145 struct tcp_header *th = packet->l4;
1146 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, ip);
1147 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1148 struct udp_header *uh = packet->l4;
1150 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, ip);
1151 if (!uh->udp_csum) {
1152 uh->udp_csum = 0xffff;
1156 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, ip);
1162 dp_netdev_set_nw_tos(struct ofpbuf *packet, const struct flow *key,
1165 if (is_ip(packet, key)) {
1166 struct ip_header *nh = packet->l3;
1167 uint8_t *field = &nh->ip_tos;
1169 /* Set the DSCP bits and preserve the ECN bits. */
1170 uint8_t new = nw_tos | (nh->ip_tos & IP_ECN_MASK);
1172 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1173 htons((uint16_t) new));
1179 dp_netdev_set_tp_port(struct ofpbuf *packet, const struct flow *key,
1180 const struct nlattr *a)
1182 if (is_ip(packet, key)) {
1183 uint16_t type = nl_attr_type(a);
1184 ovs_be16 port = nl_attr_get_be16(a);
1187 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1188 struct tcp_header *th = packet->l4;
1189 field = type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1190 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, port);
1192 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1193 struct udp_header *uh = packet->l4;
1194 field = type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1195 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, port);
1204 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1207 struct dp_netdev_port *p = dp->ports[out_port];
1209 netdev_send(p->netdev, packet);
1214 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1215 int queue_no, int port_no, uint64_t arg)
1217 struct odp_msg *header;
1221 if (dp->queue_len[queue_no] >= MAX_QUEUE_LEN) {
1226 msg_size = sizeof *header + packet->size;
1227 msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1228 header = ofpbuf_put_uninit(msg, sizeof *header);
1229 header->type = queue_no;
1230 header->length = msg_size;
1231 header->port = port_no;
1233 ofpbuf_put(msg, packet->data, packet->size);
1234 list_push_back(&dp->queues[queue_no], &msg->list_node);
1235 dp->queue_len[queue_no]++;
1240 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1241 * screwy or truncated header fields or one whose inner and outer Ethernet
1242 * address differ. */
1244 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1246 struct arp_eth_header *arp;
1247 struct eth_header *eth;
1250 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1254 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1255 if (l3_size < sizeof(struct arp_eth_header)) {
1261 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1262 || arp->ar_pro != htons(ARP_PRO_IP)
1263 || arp->ar_hln != ETH_HEADER_LEN
1265 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1269 dp_netdev_execute_actions(struct dp_netdev *dp,
1270 struct ofpbuf *packet, struct flow *key,
1271 const struct nlattr *actions,
1274 const struct nlattr *a;
1277 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1278 switch (nl_attr_type(a)) {
1280 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1283 case ODPAT_CONTROLLER:
1284 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1285 key->in_port, nl_attr_get_u64(a));
1288 case ODPAT_SET_DL_TCI:
1289 dp_netdev_set_dl_tci(packet, nl_attr_get_be16(a));
1292 case ODPAT_STRIP_VLAN:
1293 dp_netdev_strip_vlan(packet);
1296 case ODPAT_SET_DL_SRC:
1297 dp_netdev_set_dl_src(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1300 case ODPAT_SET_DL_DST:
1301 dp_netdev_set_dl_dst(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1304 case ODPAT_SET_NW_SRC:
1305 case ODPAT_SET_NW_DST:
1306 dp_netdev_set_nw_addr(packet, key, a);
1309 case ODPAT_SET_NW_TOS:
1310 dp_netdev_set_nw_tos(packet, key, nl_attr_get_u8(a));
1313 case ODPAT_SET_TP_SRC:
1314 case ODPAT_SET_TP_DST:
1315 dp_netdev_set_tp_port(packet, key, a);
1318 case ODPAT_DROP_SPOOFED_ARP:
1319 if (dp_netdev_is_spoofed_arp(packet, key)) {
1327 const struct dpif_class dpif_netdev_class = {
1331 NULL, /* enumerate */
1334 NULL, /* get_all_names */
1335 dpif_netdev_destroy,
1336 dpif_netdev_get_stats,
1337 dpif_netdev_get_drop_frags,
1338 dpif_netdev_set_drop_frags,
1339 dpif_netdev_port_add,
1340 dpif_netdev_port_del,
1341 dpif_netdev_port_query_by_number,
1342 dpif_netdev_port_query_by_name,
1343 dpif_netdev_port_list,
1344 dpif_netdev_port_poll,
1345 dpif_netdev_port_poll_wait,
1346 dpif_netdev_flow_get,
1347 dpif_netdev_flow_put,
1348 dpif_netdev_flow_del,
1349 dpif_netdev_flow_flush,
1350 dpif_netdev_flow_dump_start,
1351 dpif_netdev_flow_dump_next,
1352 dpif_netdev_flow_dump_done,
1353 dpif_netdev_execute,
1354 dpif_netdev_recv_get_mask,
1355 dpif_netdev_recv_set_mask,
1356 NULL, /* get_sflow_probability */
1357 NULL, /* set_sflow_probability */
1358 NULL, /* queue_to_priority */
1360 dpif_netdev_recv_wait,
1364 dpif_dummy_register(void)
1366 if (!dpif_dummy_class.type) {
1367 dpif_dummy_class = dpif_netdev_class;
1368 dpif_dummy_class.type = "dummy";
1369 dp_register_provider(&dpif_dummy_class);