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"
45 #include "ofp-print.h"
48 #include "poll-loop.h"
54 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
56 /* Configuration parameters. */
57 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
58 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
59 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
60 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
62 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
63 * headers to be aligned on a 4-byte boundary. */
64 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
66 /* Datapath based on the network device interface from netdev.h. */
68 const struct dpif_class *class;
73 bool drop_frags; /* Drop all IP fragments, if true. */
74 struct list queues[N_QUEUES]; /* Contain ofpbufs queued for dpif_recv(). */
75 size_t queue_len[N_QUEUES]; /* Number of packets in each queue. */
76 struct hmap flow_table; /* Flow table. */
79 long long int n_frags; /* Number of dropped IP fragments. */
80 long long int n_hit; /* Number of flow table matches. */
81 long long int n_missed; /* Number of flow table misses. */
82 long long int n_lost; /* Number of misses not passed to client. */
86 struct dp_netdev_port *ports[MAX_PORTS];
87 struct list port_list;
91 /* A port in a netdev-based datapath. */
92 struct dp_netdev_port {
93 int port_no; /* Index into dp_netdev's 'ports'. */
94 struct list node; /* Element in dp_netdev's 'port_list'. */
95 struct netdev *netdev;
96 bool internal; /* Internal port? */
99 /* A flow in dp_netdev's 'flow_table'. */
100 struct dp_netdev_flow {
101 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
105 struct timespec used; /* Last used time. */
106 long long int packet_count; /* Number of packets matched. */
107 long long int byte_count; /* Number of bytes matched. */
108 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
111 struct nlattr *actions;
115 /* Interface to netdev-based datapath. */
118 struct dp_netdev *dp;
120 unsigned int dp_serial;
123 /* All netdev-based datapaths. */
124 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
126 /* Maximum port MTU seen so far. */
127 static int max_mtu = ETH_PAYLOAD_MAX;
129 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
130 struct dp_netdev_port **portp);
131 static int get_port_by_name(struct dp_netdev *, const char *devname,
132 struct dp_netdev_port **portp);
133 static void dp_netdev_free(struct dp_netdev *);
134 static void dp_netdev_flow_flush(struct dp_netdev *);
135 static int do_add_port(struct dp_netdev *, const char *devname,
136 const char *type, uint16_t port_no);
137 static int do_del_port(struct dp_netdev *, uint16_t port_no);
138 static int dpif_netdev_open(const struct dpif_class *, const char *name,
139 bool create, struct dpif **);
140 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
141 int queue_no, int port_no, uint64_t arg);
142 static int dp_netdev_execute_actions(struct dp_netdev *,
143 struct ofpbuf *, struct flow *,
144 const struct nlattr *actions,
147 static struct dpif_class dpif_dummy_class;
149 static struct dpif_netdev *
150 dpif_netdev_cast(const struct dpif *dpif)
152 assert(dpif->dpif_class->open == dpif_netdev_open);
153 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
156 static struct dp_netdev *
157 get_dp_netdev(const struct dpif *dpif)
159 return dpif_netdev_cast(dpif)->dp;
163 create_dpif_netdev(struct dp_netdev *dp)
165 uint16_t netflow_id = hash_string(dp->name, 0);
166 struct dpif_netdev *dpif;
170 dpif = xmalloc(sizeof *dpif);
171 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
173 dpif->listen_mask = 0;
174 dpif->dp_serial = dp->serial;
180 create_dp_netdev(const char *name, const struct dpif_class *class,
181 struct dp_netdev **dpp)
183 struct dp_netdev *dp;
187 dp = xzalloc(sizeof *dp);
189 dp->name = xstrdup(name);
191 dp->drop_frags = false;
192 for (i = 0; i < N_QUEUES; i++) {
193 list_init(&dp->queues[i]);
195 hmap_init(&dp->flow_table);
196 list_init(&dp->port_list);
197 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
203 shash_add(&dp_netdevs, name, dp);
210 dpif_netdev_open(const struct dpif_class *class, const char *name,
211 bool create, struct dpif **dpifp)
213 struct dp_netdev *dp;
215 dp = shash_find_data(&dp_netdevs, name);
220 int error = create_dp_netdev(name, class, &dp);
227 if (dp->class != class) {
234 *dpifp = create_dpif_netdev(dp);
239 dp_netdev_free(struct dp_netdev *dp)
243 dp_netdev_flow_flush(dp);
244 while (dp->n_ports > 0) {
245 struct dp_netdev_port *port = CONTAINER_OF(
246 dp->port_list.next, struct dp_netdev_port, node);
247 do_del_port(dp, port->port_no);
249 for (i = 0; i < N_QUEUES; i++) {
250 ofpbuf_list_delete(&dp->queues[i]);
252 hmap_destroy(&dp->flow_table);
258 dpif_netdev_close(struct dpif *dpif)
260 struct dp_netdev *dp = get_dp_netdev(dpif);
261 assert(dp->open_cnt > 0);
262 if (--dp->open_cnt == 0 && dp->destroyed) {
263 shash_find_and_delete(&dp_netdevs, dp->name);
270 dpif_netdev_destroy(struct dpif *dpif)
272 struct dp_netdev *dp = get_dp_netdev(dpif);
273 dp->destroyed = true;
278 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
280 struct dp_netdev *dp = get_dp_netdev(dpif);
281 memset(stats, 0, sizeof *stats);
282 stats->n_flows = hmap_count(&dp->flow_table);
283 stats->cur_capacity = hmap_capacity(&dp->flow_table);
284 stats->max_capacity = MAX_FLOWS;
285 stats->n_ports = dp->n_ports;
286 stats->max_ports = MAX_PORTS;
287 stats->n_frags = dp->n_frags;
288 stats->n_hit = dp->n_hit;
289 stats->n_missed = dp->n_missed;
290 stats->n_lost = dp->n_lost;
291 stats->max_miss_queue = MAX_QUEUE_LEN;
292 stats->max_action_queue = MAX_QUEUE_LEN;
297 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
299 struct dp_netdev *dp = get_dp_netdev(dpif);
300 *drop_fragsp = dp->drop_frags;
305 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
307 struct dp_netdev *dp = get_dp_netdev(dpif);
308 dp->drop_frags = drop_frags;
313 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
316 struct dp_netdev_port *port;
317 struct netdev_options netdev_options;
318 struct netdev *netdev;
323 /* XXX reject devices already in some dp_netdev. */
324 if (type[0] == '\0' || !strcmp(type, "system")) {
326 } else if (!strcmp(type, "internal")) {
329 VLOG_WARN("%s: unsupported port type %s", devname, type);
333 /* Open and validate network device. */
334 memset(&netdev_options, 0, sizeof netdev_options);
335 netdev_options.name = devname;
336 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
337 if (dp->class == &dpif_dummy_class) {
338 netdev_options.type = "dummy";
339 } else if (internal) {
340 netdev_options.type = "tap";
343 error = netdev_open(&netdev_options, &netdev);
347 /* XXX reject loopback devices */
348 /* XXX reject non-Ethernet devices */
350 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
352 netdev_close(netdev);
356 port = xmalloc(sizeof *port);
357 port->port_no = port_no;
358 port->netdev = netdev;
359 port->internal = internal;
361 netdev_get_mtu(netdev, &mtu);
366 list_push_back(&dp->port_list, &port->node);
367 dp->ports[port_no] = port;
375 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
378 struct dp_netdev *dp = get_dp_netdev(dpif);
381 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
382 if (!dp->ports[port_no]) {
384 return do_add_port(dp, netdev_get_name(netdev),
385 netdev_get_type(netdev), port_no);
392 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
394 struct dp_netdev *dp = get_dp_netdev(dpif);
395 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
399 is_valid_port_number(uint16_t port_no)
401 return port_no < MAX_PORTS;
405 get_port_by_number(struct dp_netdev *dp,
406 uint16_t port_no, struct dp_netdev_port **portp)
408 if (!is_valid_port_number(port_no)) {
412 *portp = dp->ports[port_no];
413 return *portp ? 0 : ENOENT;
418 get_port_by_name(struct dp_netdev *dp,
419 const char *devname, struct dp_netdev_port **portp)
421 struct dp_netdev_port *port;
423 LIST_FOR_EACH (port, node, &dp->port_list) {
424 if (!strcmp(netdev_get_name(port->netdev), devname)) {
433 do_del_port(struct dp_netdev *dp, uint16_t port_no)
435 struct dp_netdev_port *port;
439 error = get_port_by_number(dp, port_no, &port);
444 list_remove(&port->node);
445 dp->ports[port->port_no] = NULL;
449 name = xstrdup(netdev_get_name(port->netdev));
450 netdev_close(port->netdev);
459 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
461 memset(odp_port, 0, sizeof *odp_port);
462 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
463 sizeof odp_port->devname);
464 odp_port->port = port->port_no;
465 strcpy(odp_port->type, port->internal ? "internal" : "system");
469 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
470 struct odp_port *odp_port)
472 struct dp_netdev *dp = get_dp_netdev(dpif);
473 struct dp_netdev_port *port;
476 error = get_port_by_number(dp, port_no, &port);
478 answer_port_query(port, odp_port);
484 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
485 struct odp_port *odp_port)
487 struct dp_netdev *dp = get_dp_netdev(dpif);
488 struct dp_netdev_port *port;
491 error = get_port_by_name(dp, devname, &port);
493 answer_port_query(port, odp_port);
499 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
501 hmap_remove(&dp->flow_table, &flow->node);
507 dp_netdev_flow_flush(struct dp_netdev *dp)
509 struct dp_netdev_flow *flow, *next;
511 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
512 dp_netdev_free_flow(dp, flow);
517 dpif_netdev_flow_flush(struct dpif *dpif)
519 struct dp_netdev *dp = get_dp_netdev(dpif);
520 dp_netdev_flow_flush(dp);
525 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
527 struct dp_netdev *dp = get_dp_netdev(dpif);
528 struct dp_netdev_port *port;
532 LIST_FOR_EACH (port, node, &dp->port_list) {
533 struct odp_port *odp_port = &ports[i];
537 answer_port_query(port, odp_port);
544 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
546 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
547 if (dpif->dp_serial != dpif->dp->serial) {
548 dpif->dp_serial = dpif->dp->serial;
556 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
558 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
559 if (dpif->dp_serial != dpif->dp->serial) {
560 poll_immediate_wake();
564 static struct dp_netdev_flow *
565 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
567 struct dp_netdev_flow *flow;
569 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
570 if (flow_equal(&flow->key, key)) {
577 /* The caller must fill in odp_flow->key itself. */
579 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
580 struct odp_flow *odp_flow)
583 odp_flow->stats.n_packets = flow->packet_count;
584 odp_flow->stats.n_bytes = flow->byte_count;
585 odp_flow->stats.used_sec = flow->used.tv_sec;
586 odp_flow->stats.used_nsec = flow->used.tv_nsec;
587 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
588 odp_flow->stats.reserved = 0;
589 odp_flow->stats.error = 0;
590 if (odp_flow->actions_len > 0) {
591 memcpy(odp_flow->actions, flow->actions,
592 MIN(odp_flow->actions_len, flow->actions_len));
593 odp_flow->actions_len = flow->actions_len;
596 if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
601 odp_flow->stats.error = ENOENT;
606 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
608 struct dp_netdev *dp = get_dp_netdev(dpif);
611 for (i = 0; i < n; i++) {
612 struct odp_flow *odp_flow = &flows[i];
615 odp_flow_key_to_flow(&odp_flow->key, &key);
616 answer_flow_query(dp_netdev_lookup_flow(dp, &key),
617 odp_flow->flags, odp_flow);
623 dpif_netdev_validate_actions(const struct nlattr *actions,
624 size_t actions_len, bool *mutates)
626 const struct nlattr *a;
630 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
631 uint16_t type = nl_attr_type(a);
632 int len = odp_action_len(type);
634 if (len != nl_attr_get_size(a)) {
640 if (nl_attr_get_u32(a) >= MAX_PORTS) {
645 case ODPAT_CONTROLLER:
646 case ODPAT_DROP_SPOOFED_ARP:
649 case ODPAT_SET_DL_TCI:
651 if (nl_attr_get_be16(a) & htons(VLAN_CFI)) {
656 case ODPAT_SET_NW_TOS:
658 if (nl_attr_get_u8(a) & IP_ECN_MASK) {
663 case ODPAT_STRIP_VLAN:
664 case ODPAT_SET_DL_SRC:
665 case ODPAT_SET_DL_DST:
666 case ODPAT_SET_NW_SRC:
667 case ODPAT_SET_NW_DST:
668 case ODPAT_SET_TP_SRC:
669 case ODPAT_SET_TP_DST:
673 case ODPAT_SET_TUNNEL:
674 case ODPAT_SET_PRIORITY:
675 case ODPAT_POP_PRIORITY:
684 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
689 error = dpif_netdev_validate_actions(odp_flow->actions,
690 odp_flow->actions_len, &mutates);
695 flow->actions = xrealloc(flow->actions, odp_flow->actions_len);
696 flow->actions_len = odp_flow->actions_len;
697 memcpy(flow->actions, odp_flow->actions, odp_flow->actions_len);
702 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
704 struct dp_netdev *dp = get_dp_netdev(dpif);
705 struct dp_netdev_flow *flow;
708 flow = xzalloc(sizeof *flow);
709 odp_flow_key_to_flow(&odp_flow->key, &flow->key);
711 error = set_flow_actions(flow, odp_flow);
717 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
722 clear_stats(struct dp_netdev_flow *flow)
724 flow->used.tv_sec = 0;
725 flow->used.tv_nsec = 0;
726 flow->packet_count = 0;
727 flow->byte_count = 0;
732 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
734 struct dp_netdev *dp = get_dp_netdev(dpif);
735 struct dp_netdev_flow *flow;
738 odp_flow_key_to_flow(&put->flow.key, &key);
739 flow = dp_netdev_lookup_flow(dp, &key);
741 if (put->flags & ODPPF_CREATE) {
742 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
743 return add_flow(dpif, &put->flow);
751 if (put->flags & ODPPF_MODIFY) {
752 int error = set_flow_actions(flow, &put->flow);
753 if (!error && put->flags & ODPPF_ZERO_STATS) {
765 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
767 struct dp_netdev *dp = get_dp_netdev(dpif);
768 struct dp_netdev_flow *flow;
771 odp_flow_key_to_flow(&odp_flow->key, &key);
772 flow = dp_netdev_lookup_flow(dp, &key);
774 answer_flow_query(flow, 0, odp_flow);
775 dp_netdev_free_flow(dp, flow);
782 struct dp_netdev_flow_state {
788 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
790 *statep = xzalloc(sizeof(struct dp_netdev_flow_state));
795 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
796 struct odp_flow *odp_flow)
798 struct dp_netdev_flow_state *state = state_;
799 struct dp_netdev *dp = get_dp_netdev(dpif);
800 struct dp_netdev_flow *flow;
801 struct hmap_node *node;
803 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
808 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
809 odp_flow_key_from_flow(&odp_flow->key, &flow->key);
810 answer_flow_query(flow, 0, odp_flow);
816 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
823 dpif_netdev_execute(struct dpif *dpif,
824 const struct nlattr *actions, size_t actions_len,
825 const struct ofpbuf *packet)
827 struct dp_netdev *dp = get_dp_netdev(dpif);
833 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
837 error = dpif_netdev_validate_actions(actions, actions_len, &mutates);
843 /* We need a deep copy of 'packet' since we're going to modify its
845 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
846 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
847 ofpbuf_put(©, packet->data, packet->size);
849 /* We still need a shallow copy of 'packet', even though we won't
850 * modify its data, because flow_extract() modifies packet->l2, etc.
851 * We could probably get away with modifying those but it's more polite
855 flow_extract(©, 0, -1, &key);
856 error = dp_netdev_execute_actions(dp, ©, &key, actions, actions_len);
858 ofpbuf_uninit(©);
864 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
866 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
867 *listen_mask = dpif_netdev->listen_mask;
872 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
874 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
875 if (!(listen_mask & ~ODPL_ALL)) {
876 dpif_netdev->listen_mask = listen_mask;
884 find_nonempty_queue(struct dpif *dpif)
886 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
887 struct dp_netdev *dp = get_dp_netdev(dpif);
888 int mask = dpif_netdev->listen_mask;
891 for (i = 0; i < N_QUEUES; i++) {
892 struct list *queue = &dp->queues[i];
893 if (!list_is_empty(queue) && mask & (1u << i)) {
901 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
903 int queue_idx = find_nonempty_queue(dpif);
904 if (queue_idx >= 0) {
905 struct dp_netdev *dp = get_dp_netdev(dpif);
907 *bufp = ofpbuf_from_list(list_pop_front(&dp->queues[queue_idx]));
908 dp->queue_len[queue_idx]--;
917 dpif_netdev_recv_wait(struct dpif *dpif)
919 if (find_nonempty_queue(dpif) >= 0) {
920 poll_immediate_wake();
922 /* No messages ready to be received, and dp_wait() will ensure that we
923 * wake up to queue new messages, so there is nothing to do. */
928 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
929 const struct ofpbuf *packet)
931 time_timespec(&flow->used);
932 flow->packet_count++;
933 flow->byte_count += packet->size;
934 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
935 struct tcp_header *th = packet->l4;
936 flow->tcp_ctl |= th->tcp_ctl;
941 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
942 struct ofpbuf *packet)
944 struct dp_netdev_flow *flow;
947 if (packet->size < ETH_HEADER_LEN) {
950 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
955 flow = dp_netdev_lookup_flow(dp, &key);
957 dp_netdev_flow_used(flow, &key, packet);
958 dp_netdev_execute_actions(dp, packet, &key,
959 flow->actions, flow->actions_len);
963 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
970 struct shash_node *node;
971 struct ofpbuf packet;
973 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
974 SHASH_FOR_EACH (node, &dp_netdevs) {
975 struct dp_netdev *dp = node->data;
976 struct dp_netdev_port *port;
978 LIST_FOR_EACH (port, node, &dp->port_list) {
981 /* Reset packet contents. */
982 ofpbuf_clear(&packet);
983 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
985 error = netdev_recv(port->netdev, &packet);
987 dp_netdev_port_input(dp, port, &packet);
988 } else if (error != EAGAIN && error != EOPNOTSUPP) {
989 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
990 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
991 netdev_get_name(port->netdev), strerror(error));
995 ofpbuf_uninit(&packet);
1001 struct shash_node *node;
1003 SHASH_FOR_EACH (node, &dp_netdevs) {
1004 struct dp_netdev *dp = node->data;
1005 struct dp_netdev_port *port;
1007 LIST_FOR_EACH (port, node, &dp->port_list) {
1008 netdev_recv_wait(port->netdev);
1014 /* Modify the TCI field of 'packet'. If a VLAN tag is present, its TCI field
1015 * is replaced by 'tci'. If a VLAN tag is not present, one is added with the
1016 * TCI field set to 'tci'.
1019 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
1021 struct vlan_eth_header *veh;
1022 struct eth_header *eh;
1025 if (packet->size >= sizeof(struct vlan_eth_header)
1026 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1028 veh->veth_tci = tci;
1030 /* Insert new 802.1Q header. */
1031 struct vlan_eth_header tmp;
1032 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1033 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1034 tmp.veth_type = htons(ETH_TYPE_VLAN);
1036 tmp.veth_next_type = eh->eth_type;
1038 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1039 memcpy(veh, &tmp, sizeof tmp);
1040 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1045 dp_netdev_strip_vlan(struct ofpbuf *packet)
1047 struct vlan_eth_header *veh = packet->l2;
1048 if (packet->size >= sizeof *veh
1049 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1050 struct eth_header tmp;
1052 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1053 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1054 tmp.eth_type = veh->veth_next_type;
1056 ofpbuf_pull(packet, VLAN_HEADER_LEN);
1057 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1058 memcpy(packet->data, &tmp, sizeof tmp);
1063 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1065 struct eth_header *eh = packet->l2;
1066 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1070 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1072 struct eth_header *eh = packet->l2;
1073 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1077 is_ip(const struct ofpbuf *packet, const struct flow *key)
1079 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1083 dp_netdev_set_nw_addr(struct ofpbuf *packet, const struct flow *key,
1084 const struct nlattr *a)
1086 if (is_ip(packet, key)) {
1087 struct ip_header *nh = packet->l3;
1088 ovs_be32 ip = nl_attr_get_be32(a);
1089 uint16_t type = nl_attr_type(a);
1092 field = type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1093 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1094 struct tcp_header *th = packet->l4;
1095 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, ip);
1096 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1097 struct udp_header *uh = packet->l4;
1099 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, ip);
1100 if (!uh->udp_csum) {
1101 uh->udp_csum = 0xffff;
1105 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, ip);
1111 dp_netdev_set_nw_tos(struct ofpbuf *packet, const struct flow *key,
1114 if (is_ip(packet, key)) {
1115 struct ip_header *nh = packet->l3;
1116 uint8_t *field = &nh->ip_tos;
1118 /* Set the DSCP bits and preserve the ECN bits. */
1119 uint8_t new = nw_tos | (nh->ip_tos & IP_ECN_MASK);
1121 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1122 htons((uint16_t) new));
1128 dp_netdev_set_tp_port(struct ofpbuf *packet, const struct flow *key,
1129 const struct nlattr *a)
1131 if (is_ip(packet, key)) {
1132 uint16_t type = nl_attr_type(a);
1133 ovs_be16 port = nl_attr_get_be16(a);
1136 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1137 struct tcp_header *th = packet->l4;
1138 field = type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1139 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, port);
1141 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1142 struct udp_header *uh = packet->l4;
1143 field = type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1144 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, port);
1153 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1156 struct dp_netdev_port *p = dp->ports[out_port];
1158 netdev_send(p->netdev, packet);
1163 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1164 int queue_no, int port_no, uint64_t arg)
1166 struct odp_msg *header;
1170 if (dp->queue_len[queue_no] >= MAX_QUEUE_LEN) {
1175 msg_size = sizeof *header + packet->size;
1176 msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1177 header = ofpbuf_put_uninit(msg, sizeof *header);
1178 header->type = queue_no;
1179 header->length = msg_size;
1180 header->port = port_no;
1182 ofpbuf_put(msg, packet->data, packet->size);
1183 list_push_back(&dp->queues[queue_no], &msg->list_node);
1184 dp->queue_len[queue_no]++;
1189 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1190 * screwy or truncated header fields or one whose inner and outer Ethernet
1191 * address differ. */
1193 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1195 struct arp_eth_header *arp;
1196 struct eth_header *eth;
1199 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1203 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1204 if (l3_size < sizeof(struct arp_eth_header)) {
1210 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1211 || arp->ar_pro != htons(ARP_PRO_IP)
1212 || arp->ar_hln != ETH_HEADER_LEN
1214 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1218 dp_netdev_execute_actions(struct dp_netdev *dp,
1219 struct ofpbuf *packet, struct flow *key,
1220 const struct nlattr *actions,
1223 const struct nlattr *a;
1226 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1227 switch (nl_attr_type(a)) {
1229 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1232 case ODPAT_CONTROLLER:
1233 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1234 key->in_port, nl_attr_get_u64(a));
1237 case ODPAT_SET_DL_TCI:
1238 dp_netdev_set_dl_tci(packet, nl_attr_get_be16(a));
1241 case ODPAT_STRIP_VLAN:
1242 dp_netdev_strip_vlan(packet);
1245 case ODPAT_SET_DL_SRC:
1246 dp_netdev_set_dl_src(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1249 case ODPAT_SET_DL_DST:
1250 dp_netdev_set_dl_dst(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1253 case ODPAT_SET_NW_SRC:
1254 case ODPAT_SET_NW_DST:
1255 dp_netdev_set_nw_addr(packet, key, a);
1258 case ODPAT_SET_NW_TOS:
1259 dp_netdev_set_nw_tos(packet, key, nl_attr_get_u8(a));
1262 case ODPAT_SET_TP_SRC:
1263 case ODPAT_SET_TP_DST:
1264 dp_netdev_set_tp_port(packet, key, a);
1267 case ODPAT_DROP_SPOOFED_ARP:
1268 if (dp_netdev_is_spoofed_arp(packet, key)) {
1276 const struct dpif_class dpif_netdev_class = {
1280 NULL, /* enumerate */
1283 NULL, /* get_all_names */
1284 dpif_netdev_destroy,
1285 dpif_netdev_get_stats,
1286 dpif_netdev_get_drop_frags,
1287 dpif_netdev_set_drop_frags,
1288 dpif_netdev_port_add,
1289 dpif_netdev_port_del,
1290 dpif_netdev_port_query_by_number,
1291 dpif_netdev_port_query_by_name,
1292 dpif_netdev_port_list,
1293 dpif_netdev_port_poll,
1294 dpif_netdev_port_poll_wait,
1295 dpif_netdev_flow_get,
1296 dpif_netdev_flow_put,
1297 dpif_netdev_flow_del,
1298 dpif_netdev_flow_flush,
1299 dpif_netdev_flow_dump_start,
1300 dpif_netdev_flow_dump_next,
1301 dpif_netdev_flow_dump_done,
1302 dpif_netdev_execute,
1303 dpif_netdev_recv_get_mask,
1304 dpif_netdev_recv_set_mask,
1305 NULL, /* get_sflow_probability */
1306 NULL, /* set_sflow_probability */
1307 NULL, /* queue_to_priority */
1309 dpif_netdev_recv_wait,
1313 dpif_dummy_register(void)
1315 if (!dpif_dummy_class.type) {
1316 dpif_dummy_class = dpif_netdev_class;
1317 dpif_dummy_class.type = "dummy";
1318 dp_register_provider(&dpif_dummy_class);