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);
783 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
785 struct dp_netdev *dp = get_dp_netdev(dpif);
786 struct dp_netdev_flow *flow;
790 HMAP_FOR_EACH (flow, node, &dp->flow_table) {
795 odp_flow_key_from_flow(&flows[i].key, &flow->key);
796 answer_flow_query(flow, 0, &flows[i]);
799 return hmap_count(&dp->flow_table);
803 dpif_netdev_execute(struct dpif *dpif,
804 const struct nlattr *actions, size_t actions_len,
805 const struct ofpbuf *packet)
807 struct dp_netdev *dp = get_dp_netdev(dpif);
813 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
817 error = dpif_netdev_validate_actions(actions, actions_len, &mutates);
823 /* We need a deep copy of 'packet' since we're going to modify its
825 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
826 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
827 ofpbuf_put(©, packet->data, packet->size);
829 /* We still need a shallow copy of 'packet', even though we won't
830 * modify its data, because flow_extract() modifies packet->l2, etc.
831 * We could probably get away with modifying those but it's more polite
835 flow_extract(©, 0, -1, &key);
836 error = dp_netdev_execute_actions(dp, ©, &key, actions, actions_len);
838 ofpbuf_uninit(©);
844 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
846 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
847 *listen_mask = dpif_netdev->listen_mask;
852 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
854 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
855 if (!(listen_mask & ~ODPL_ALL)) {
856 dpif_netdev->listen_mask = listen_mask;
864 find_nonempty_queue(struct dpif *dpif)
866 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
867 struct dp_netdev *dp = get_dp_netdev(dpif);
868 int mask = dpif_netdev->listen_mask;
871 for (i = 0; i < N_QUEUES; i++) {
872 struct list *queue = &dp->queues[i];
873 if (!list_is_empty(queue) && mask & (1u << i)) {
881 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
883 int queue_idx = find_nonempty_queue(dpif);
884 if (queue_idx >= 0) {
885 struct dp_netdev *dp = get_dp_netdev(dpif);
887 *bufp = ofpbuf_from_list(list_pop_front(&dp->queues[queue_idx]));
888 dp->queue_len[queue_idx]--;
897 dpif_netdev_recv_wait(struct dpif *dpif)
899 if (find_nonempty_queue(dpif) >= 0) {
900 poll_immediate_wake();
902 /* No messages ready to be received, and dp_wait() will ensure that we
903 * wake up to queue new messages, so there is nothing to do. */
908 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
909 const struct ofpbuf *packet)
911 time_timespec(&flow->used);
912 flow->packet_count++;
913 flow->byte_count += packet->size;
914 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
915 struct tcp_header *th = packet->l4;
916 flow->tcp_ctl |= th->tcp_ctl;
921 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
922 struct ofpbuf *packet)
924 struct dp_netdev_flow *flow;
927 if (packet->size < ETH_HEADER_LEN) {
930 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
935 flow = dp_netdev_lookup_flow(dp, &key);
937 dp_netdev_flow_used(flow, &key, packet);
938 dp_netdev_execute_actions(dp, packet, &key,
939 flow->actions, flow->actions_len);
943 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
950 struct shash_node *node;
951 struct ofpbuf packet;
953 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
954 SHASH_FOR_EACH (node, &dp_netdevs) {
955 struct dp_netdev *dp = node->data;
956 struct dp_netdev_port *port;
958 LIST_FOR_EACH (port, node, &dp->port_list) {
961 /* Reset packet contents. */
962 ofpbuf_clear(&packet);
963 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
965 error = netdev_recv(port->netdev, &packet);
967 dp_netdev_port_input(dp, port, &packet);
968 } else if (error != EAGAIN && error != EOPNOTSUPP) {
969 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
970 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
971 netdev_get_name(port->netdev), strerror(error));
975 ofpbuf_uninit(&packet);
981 struct shash_node *node;
983 SHASH_FOR_EACH (node, &dp_netdevs) {
984 struct dp_netdev *dp = node->data;
985 struct dp_netdev_port *port;
987 LIST_FOR_EACH (port, node, &dp->port_list) {
988 netdev_recv_wait(port->netdev);
994 /* Modify the TCI field of 'packet'. If a VLAN tag is present, its TCI field
995 * is replaced by 'tci'. If a VLAN tag is not present, one is added with the
996 * TCI field set to 'tci'.
999 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
1001 struct vlan_eth_header *veh;
1002 struct eth_header *eh;
1005 if (packet->size >= sizeof(struct vlan_eth_header)
1006 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1008 veh->veth_tci = tci;
1010 /* Insert new 802.1Q header. */
1011 struct vlan_eth_header tmp;
1012 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1013 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1014 tmp.veth_type = htons(ETH_TYPE_VLAN);
1016 tmp.veth_next_type = eh->eth_type;
1018 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1019 memcpy(veh, &tmp, sizeof tmp);
1020 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1025 dp_netdev_strip_vlan(struct ofpbuf *packet)
1027 struct vlan_eth_header *veh = packet->l2;
1028 if (packet->size >= sizeof *veh
1029 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1030 struct eth_header tmp;
1032 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1033 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1034 tmp.eth_type = veh->veth_next_type;
1036 ofpbuf_pull(packet, VLAN_HEADER_LEN);
1037 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1038 memcpy(packet->data, &tmp, sizeof tmp);
1043 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1045 struct eth_header *eh = packet->l2;
1046 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1050 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1052 struct eth_header *eh = packet->l2;
1053 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1057 is_ip(const struct ofpbuf *packet, const struct flow *key)
1059 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1063 dp_netdev_set_nw_addr(struct ofpbuf *packet, const struct flow *key,
1064 const struct nlattr *a)
1066 if (is_ip(packet, key)) {
1067 struct ip_header *nh = packet->l3;
1068 ovs_be32 ip = nl_attr_get_be32(a);
1069 uint16_t type = nl_attr_type(a);
1072 field = type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1073 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1074 struct tcp_header *th = packet->l4;
1075 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, ip);
1076 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1077 struct udp_header *uh = packet->l4;
1079 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, ip);
1080 if (!uh->udp_csum) {
1081 uh->udp_csum = 0xffff;
1085 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, ip);
1091 dp_netdev_set_nw_tos(struct ofpbuf *packet, const struct flow *key,
1094 if (is_ip(packet, key)) {
1095 struct ip_header *nh = packet->l3;
1096 uint8_t *field = &nh->ip_tos;
1098 /* Set the DSCP bits and preserve the ECN bits. */
1099 uint8_t new = nw_tos | (nh->ip_tos & IP_ECN_MASK);
1101 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1102 htons((uint16_t) new));
1108 dp_netdev_set_tp_port(struct ofpbuf *packet, const struct flow *key,
1109 const struct nlattr *a)
1111 if (is_ip(packet, key)) {
1112 uint16_t type = nl_attr_type(a);
1113 ovs_be16 port = nl_attr_get_be16(a);
1116 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1117 struct tcp_header *th = packet->l4;
1118 field = type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1119 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, port);
1121 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1122 struct udp_header *uh = packet->l4;
1123 field = type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1124 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, port);
1133 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1136 struct dp_netdev_port *p = dp->ports[out_port];
1138 netdev_send(p->netdev, packet);
1143 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1144 int queue_no, int port_no, uint64_t arg)
1146 struct odp_msg *header;
1150 if (dp->queue_len[queue_no] >= MAX_QUEUE_LEN) {
1155 msg_size = sizeof *header + packet->size;
1156 msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1157 header = ofpbuf_put_uninit(msg, sizeof *header);
1158 header->type = queue_no;
1159 header->length = msg_size;
1160 header->port = port_no;
1162 ofpbuf_put(msg, packet->data, packet->size);
1163 list_push_back(&dp->queues[queue_no], &msg->list_node);
1164 dp->queue_len[queue_no]++;
1169 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1170 * screwy or truncated header fields or one whose inner and outer Ethernet
1171 * address differ. */
1173 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1175 struct arp_eth_header *arp;
1176 struct eth_header *eth;
1179 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1183 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1184 if (l3_size < sizeof(struct arp_eth_header)) {
1190 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1191 || arp->ar_pro != htons(ARP_PRO_IP)
1192 || arp->ar_hln != ETH_HEADER_LEN
1194 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1198 dp_netdev_execute_actions(struct dp_netdev *dp,
1199 struct ofpbuf *packet, struct flow *key,
1200 const struct nlattr *actions,
1203 const struct nlattr *a;
1206 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1207 switch (nl_attr_type(a)) {
1209 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1212 case ODPAT_CONTROLLER:
1213 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1214 key->in_port, nl_attr_get_u64(a));
1217 case ODPAT_SET_DL_TCI:
1218 dp_netdev_set_dl_tci(packet, nl_attr_get_be16(a));
1221 case ODPAT_STRIP_VLAN:
1222 dp_netdev_strip_vlan(packet);
1225 case ODPAT_SET_DL_SRC:
1226 dp_netdev_set_dl_src(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1229 case ODPAT_SET_DL_DST:
1230 dp_netdev_set_dl_dst(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1233 case ODPAT_SET_NW_SRC:
1234 case ODPAT_SET_NW_DST:
1235 dp_netdev_set_nw_addr(packet, key, a);
1238 case ODPAT_SET_NW_TOS:
1239 dp_netdev_set_nw_tos(packet, key, nl_attr_get_u8(a));
1242 case ODPAT_SET_TP_SRC:
1243 case ODPAT_SET_TP_DST:
1244 dp_netdev_set_tp_port(packet, key, a);
1247 case ODPAT_DROP_SPOOFED_ARP:
1248 if (dp_netdev_is_spoofed_arp(packet, key)) {
1256 const struct dpif_class dpif_netdev_class = {
1260 NULL, /* enumerate */
1263 NULL, /* get_all_names */
1264 dpif_netdev_destroy,
1265 dpif_netdev_get_stats,
1266 dpif_netdev_get_drop_frags,
1267 dpif_netdev_set_drop_frags,
1268 dpif_netdev_port_add,
1269 dpif_netdev_port_del,
1270 dpif_netdev_port_query_by_number,
1271 dpif_netdev_port_query_by_name,
1272 dpif_netdev_port_list,
1273 dpif_netdev_port_poll,
1274 dpif_netdev_port_poll_wait,
1275 dpif_netdev_flow_get,
1276 dpif_netdev_flow_put,
1277 dpif_netdev_flow_del,
1278 dpif_netdev_flow_flush,
1279 dpif_netdev_flow_list,
1280 dpif_netdev_execute,
1281 dpif_netdev_recv_get_mask,
1282 dpif_netdev_recv_set_mask,
1283 NULL, /* get_sflow_probability */
1284 NULL, /* set_sflow_probability */
1285 NULL, /* queue_to_priority */
1287 dpif_netdev_recv_wait,
1291 dpif_dummy_register(void)
1293 if (!dpif_dummy_class.type) {
1294 dpif_dummy_class = dpif_netdev_class;
1295 dpif_dummy_class.type = "dummy";
1296 dp_register_provider(&dpif_dummy_class);