2 * Copyright (c) 2009, 2010 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>
30 #include <sys/ioctl.h>
36 #include "dpif-provider.h"
43 #include "ofp-print.h"
46 #include "poll-loop.h"
53 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
55 /* Configuration parameters. */
56 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
57 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
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 };
65 /* Datapath based on the network device interface from netdev.h. */
67 const struct dpif_class *class;
72 bool drop_frags; /* Drop all IP fragments, if true. */
73 struct ovs_queue queues[N_QUEUES]; /* Messages queued for dpif_recv(). */
74 struct hmap flow_table; /* Flow table. */
77 long long int n_frags; /* Number of dropped IP fragments. */
78 long long int n_hit; /* Number of flow table matches. */
79 long long int n_missed; /* Number of flow table misses. */
80 long long int n_lost; /* Number of misses not passed to client. */
84 struct dp_netdev_port *ports[MAX_PORTS];
85 struct list port_list;
89 /* A port in a netdev-based datapath. */
90 struct dp_netdev_port {
91 int port_no; /* Index into dp_netdev's 'ports'. */
92 struct list node; /* Element in dp_netdev's 'port_list'. */
93 struct netdev *netdev;
94 bool internal; /* Internal port (as ODP_PORT_INTERNAL)? */
97 /* A flow in dp_netdev's 'flow_table'. */
98 struct dp_netdev_flow {
99 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
103 struct timespec used; /* Last used time. */
104 long long int packet_count; /* Number of packets matched. */
105 long long int byte_count; /* Number of bytes matched. */
106 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
109 union odp_action *actions;
110 unsigned int n_actions;
113 /* Interface to netdev-based datapath. */
116 struct dp_netdev *dp;
118 unsigned int dp_serial;
121 /* All netdev-based datapaths. */
122 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
124 /* Maximum port MTU seen so far. */
125 static int max_mtu = ETH_PAYLOAD_MAX;
127 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
128 struct dp_netdev_port **portp);
129 static int get_port_by_name(struct dp_netdev *, const char *devname,
130 struct dp_netdev_port **portp);
131 static void dp_netdev_free(struct dp_netdev *);
132 static void dp_netdev_flow_flush(struct dp_netdev *);
133 static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags,
135 static int do_del_port(struct dp_netdev *, uint16_t port_no);
136 static int dpif_netdev_open(const struct dpif_class *, const char *name,
137 bool create, struct dpif **);
138 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
139 int queue_no, int port_no, uint32_t arg);
140 static int dp_netdev_execute_actions(struct dp_netdev *,
141 struct ofpbuf *, struct flow *,
142 const union odp_action *, int n);
144 static struct dpif_class dpif_dummy_class;
146 static struct dpif_netdev *
147 dpif_netdev_cast(const struct dpif *dpif)
149 assert(dpif->dpif_class->open == dpif_netdev_open);
150 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
153 static struct dp_netdev *
154 get_dp_netdev(const struct dpif *dpif)
156 return dpif_netdev_cast(dpif)->dp;
160 create_dpif_netdev(struct dp_netdev *dp)
162 uint16_t netflow_id = hash_string(dp->name, 0);
163 struct dpif_netdev *dpif;
167 dpif = xmalloc(sizeof *dpif);
168 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
170 dpif->listen_mask = 0;
171 dpif->dp_serial = dp->serial;
177 create_dp_netdev(const char *name, const struct dpif_class *class,
178 struct dp_netdev **dpp)
180 struct dp_netdev *dp;
184 dp = xzalloc(sizeof *dp);
186 dp->name = xstrdup(name);
188 dp->drop_frags = false;
189 for (i = 0; i < N_QUEUES; i++) {
190 queue_init(&dp->queues[i]);
192 hmap_init(&dp->flow_table);
193 list_init(&dp->port_list);
194 error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL);
200 shash_add(&dp_netdevs, name, dp);
207 dpif_netdev_open(const struct dpif_class *class, const char *name,
208 bool create, struct dpif **dpifp)
210 struct dp_netdev *dp;
212 dp = shash_find_data(&dp_netdevs, name);
217 int error = create_dp_netdev(name, class, &dp);
224 if (dp->class != class) {
231 *dpifp = create_dpif_netdev(dp);
236 dp_netdev_free(struct dp_netdev *dp)
240 dp_netdev_flow_flush(dp);
241 while (dp->n_ports > 0) {
242 struct dp_netdev_port *port = CONTAINER_OF(
243 dp->port_list.next, struct dp_netdev_port, node);
244 do_del_port(dp, port->port_no);
246 for (i = 0; i < N_QUEUES; i++) {
247 queue_destroy(&dp->queues[i]);
249 hmap_destroy(&dp->flow_table);
255 dpif_netdev_close(struct dpif *dpif)
257 struct dp_netdev *dp = get_dp_netdev(dpif);
258 assert(dp->open_cnt > 0);
259 if (--dp->open_cnt == 0 && dp->destroyed) {
260 shash_find_and_delete(&dp_netdevs, dp->name);
267 dpif_netdev_destroy(struct dpif *dpif)
269 struct dp_netdev *dp = get_dp_netdev(dpif);
270 dp->destroyed = true;
275 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
277 struct dp_netdev *dp = get_dp_netdev(dpif);
278 memset(stats, 0, sizeof *stats);
279 stats->n_flows = hmap_count(&dp->flow_table);
280 stats->cur_capacity = hmap_capacity(&dp->flow_table);
281 stats->max_capacity = MAX_FLOWS;
282 stats->n_ports = dp->n_ports;
283 stats->max_ports = MAX_PORTS;
284 stats->n_frags = dp->n_frags;
285 stats->n_hit = dp->n_hit;
286 stats->n_missed = dp->n_missed;
287 stats->n_lost = dp->n_lost;
288 stats->max_miss_queue = MAX_QUEUE_LEN;
289 stats->max_action_queue = MAX_QUEUE_LEN;
294 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
296 struct dp_netdev *dp = get_dp_netdev(dpif);
297 *drop_fragsp = dp->drop_frags;
302 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
304 struct dp_netdev *dp = get_dp_netdev(dpif);
305 dp->drop_frags = drop_frags;
310 do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
313 bool internal = (flags & ODP_PORT_INTERNAL) != 0;
314 struct dp_netdev_port *port;
315 struct netdev_options netdev_options;
316 struct netdev *netdev;
320 /* XXX reject devices already in some dp_netdev. */
322 /* Open and validate network device. */
323 memset(&netdev_options, 0, sizeof netdev_options);
324 netdev_options.name = devname;
325 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
326 if (dp->class == &dpif_dummy_class) {
327 netdev_options.type = "dummy";
328 } else if (internal) {
329 netdev_options.type = "tap";
332 error = netdev_open(&netdev_options, &netdev);
336 /* XXX reject loopback devices */
337 /* XXX reject non-Ethernet devices */
339 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
341 netdev_close(netdev);
345 port = xmalloc(sizeof *port);
346 port->port_no = port_no;
347 port->netdev = netdev;
348 port->internal = internal;
350 netdev_get_mtu(netdev, &mtu);
355 list_push_back(&dp->port_list, &port->node);
356 dp->ports[port_no] = port;
364 dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
367 struct dp_netdev *dp = get_dp_netdev(dpif);
370 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
371 if (!dp->ports[port_no]) {
373 return do_add_port(dp, devname, flags, port_no);
380 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
382 struct dp_netdev *dp = get_dp_netdev(dpif);
383 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
387 is_valid_port_number(uint16_t port_no)
389 return port_no < MAX_PORTS;
393 get_port_by_number(struct dp_netdev *dp,
394 uint16_t port_no, struct dp_netdev_port **portp)
396 if (!is_valid_port_number(port_no)) {
400 *portp = dp->ports[port_no];
401 return *portp ? 0 : ENOENT;
406 get_port_by_name(struct dp_netdev *dp,
407 const char *devname, struct dp_netdev_port **portp)
409 struct dp_netdev_port *port;
411 LIST_FOR_EACH (port, node, &dp->port_list) {
412 if (!strcmp(netdev_get_name(port->netdev), devname)) {
421 do_del_port(struct dp_netdev *dp, uint16_t port_no)
423 struct dp_netdev_port *port;
427 error = get_port_by_number(dp, port_no, &port);
432 list_remove(&port->node);
433 dp->ports[port->port_no] = NULL;
437 name = xstrdup(netdev_get_name(port->netdev));
438 netdev_close(port->netdev);
447 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
449 memset(odp_port, 0, sizeof *odp_port);
450 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
451 sizeof odp_port->devname);
452 odp_port->port = port->port_no;
453 odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
457 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
458 struct odp_port *odp_port)
460 struct dp_netdev *dp = get_dp_netdev(dpif);
461 struct dp_netdev_port *port;
464 error = get_port_by_number(dp, port_no, &port);
466 answer_port_query(port, odp_port);
472 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
473 struct odp_port *odp_port)
475 struct dp_netdev *dp = get_dp_netdev(dpif);
476 struct dp_netdev_port *port;
479 error = get_port_by_name(dp, devname, &port);
481 answer_port_query(port, odp_port);
487 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
489 hmap_remove(&dp->flow_table, &flow->node);
495 dp_netdev_flow_flush(struct dp_netdev *dp)
497 struct dp_netdev_flow *flow, *next;
499 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
500 dp_netdev_free_flow(dp, flow);
505 dpif_netdev_flow_flush(struct dpif *dpif)
507 struct dp_netdev *dp = get_dp_netdev(dpif);
508 dp_netdev_flow_flush(dp);
513 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
515 struct dp_netdev *dp = get_dp_netdev(dpif);
516 struct dp_netdev_port *port;
520 LIST_FOR_EACH (port, node, &dp->port_list) {
521 struct odp_port *odp_port = &ports[i];
525 answer_port_query(port, odp_port);
532 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
534 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
535 if (dpif->dp_serial != dpif->dp->serial) {
536 dpif->dp_serial = dpif->dp->serial;
544 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
546 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
547 if (dpif->dp_serial != dpif->dp->serial) {
548 poll_immediate_wake();
552 static struct dp_netdev_flow *
553 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
555 struct dp_netdev_flow *flow;
557 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
558 if (flow_equal(&flow->key, key)) {
565 /* The caller must fill in odp_flow->key itself. */
567 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
568 struct odp_flow *odp_flow)
571 odp_flow->stats.n_packets = flow->packet_count;
572 odp_flow->stats.n_bytes = flow->byte_count;
573 odp_flow->stats.used_sec = flow->used.tv_sec;
574 odp_flow->stats.used_nsec = flow->used.tv_nsec;
575 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
576 odp_flow->stats.reserved = 0;
577 odp_flow->stats.error = 0;
578 if (odp_flow->n_actions > 0) {
579 unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
580 memcpy(odp_flow->actions, flow->actions,
581 n * sizeof *odp_flow->actions);
582 odp_flow->n_actions = flow->n_actions;
585 if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
590 odp_flow->stats.error = ENOENT;
595 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
597 struct dp_netdev *dp = get_dp_netdev(dpif);
600 for (i = 0; i < n; i++) {
601 struct odp_flow *odp_flow = &flows[i];
604 odp_flow_key_to_flow(&odp_flow->key, &key);
605 answer_flow_query(dp_netdev_lookup_flow(dp, &key),
606 odp_flow->flags, odp_flow);
612 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
618 for (i = 0; i < n_actions; i++) {
619 const union odp_action *a = &actions[i];
622 if (a->output.port >= MAX_PORTS) {
627 case ODPAT_CONTROLLER:
630 case ODPAT_SET_DL_TCI:
632 if (a->dl_tci.tci & htons(VLAN_CFI)) {
637 case ODPAT_SET_NW_TOS:
639 if (a->nw_tos.nw_tos & IP_ECN_MASK) {
644 case ODPAT_STRIP_VLAN:
645 case ODPAT_SET_DL_SRC:
646 case ODPAT_SET_DL_DST:
647 case ODPAT_SET_NW_SRC:
648 case ODPAT_SET_NW_DST:
649 case ODPAT_SET_TP_SRC:
650 case ODPAT_SET_TP_DST:
662 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
668 if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
671 error = dpif_netdev_validate_actions(odp_flow->actions,
672 odp_flow->n_actions, &mutates);
677 n_bytes = odp_flow->n_actions * sizeof *flow->actions;
678 flow->actions = xrealloc(flow->actions, n_bytes);
679 flow->n_actions = odp_flow->n_actions;
680 memcpy(flow->actions, odp_flow->actions, n_bytes);
685 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
687 struct dp_netdev *dp = get_dp_netdev(dpif);
688 struct dp_netdev_flow *flow;
691 flow = xzalloc(sizeof *flow);
692 odp_flow_key_to_flow(&odp_flow->key, &flow->key);
694 error = set_flow_actions(flow, odp_flow);
700 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
705 clear_stats(struct dp_netdev_flow *flow)
707 flow->used.tv_sec = 0;
708 flow->used.tv_nsec = 0;
709 flow->packet_count = 0;
710 flow->byte_count = 0;
715 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
717 struct dp_netdev *dp = get_dp_netdev(dpif);
718 struct dp_netdev_flow *flow;
721 odp_flow_key_to_flow(&put->flow.key, &key);
722 flow = dp_netdev_lookup_flow(dp, &key);
724 if (put->flags & ODPPF_CREATE) {
725 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
726 return add_flow(dpif, &put->flow);
734 if (put->flags & ODPPF_MODIFY) {
735 int error = set_flow_actions(flow, &put->flow);
736 if (!error && put->flags & ODPPF_ZERO_STATS) {
748 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
750 struct dp_netdev *dp = get_dp_netdev(dpif);
751 struct dp_netdev_flow *flow;
754 odp_flow_key_to_flow(&odp_flow->key, &key);
755 flow = dp_netdev_lookup_flow(dp, &key);
757 answer_flow_query(flow, 0, odp_flow);
758 dp_netdev_free_flow(dp, flow);
766 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
768 struct dp_netdev *dp = get_dp_netdev(dpif);
769 struct dp_netdev_flow *flow;
773 HMAP_FOR_EACH (flow, node, &dp->flow_table) {
778 odp_flow_key_from_flow(&flows[i].key, &flow->key);
779 answer_flow_query(flow, 0, &flows[i]);
782 return hmap_count(&dp->flow_table);
786 dpif_netdev_execute(struct dpif *dpif,
787 const union odp_action actions[], int n_actions,
788 const struct ofpbuf *packet)
790 struct dp_netdev *dp = get_dp_netdev(dpif);
796 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
800 error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
806 /* We need a deep copy of 'packet' since we're going to modify its
808 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
809 copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
810 ofpbuf_put(©, packet->data, packet->size);
812 /* We still need a shallow copy of 'packet', even though we won't
813 * modify its data, because flow_extract() modifies packet->l2, etc.
814 * We could probably get away with modifying those but it's more polite
818 flow_extract(©, 0, -1, &key);
819 error = dp_netdev_execute_actions(dp, ©, &key, actions, n_actions);
821 ofpbuf_uninit(©);
827 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
829 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
830 *listen_mask = dpif_netdev->listen_mask;
835 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
837 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
838 if (!(listen_mask & ~ODPL_ALL)) {
839 dpif_netdev->listen_mask = listen_mask;
846 static struct ovs_queue *
847 find_nonempty_queue(struct dpif *dpif)
849 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
850 struct dp_netdev *dp = get_dp_netdev(dpif);
851 int mask = dpif_netdev->listen_mask;
854 for (i = 0; i < N_QUEUES; i++) {
855 struct ovs_queue *q = &dp->queues[i];
856 if (q->n && mask & (1u << i)) {
864 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
866 struct ovs_queue *q = find_nonempty_queue(dpif);
868 *bufp = queue_pop_head(q);
876 dpif_netdev_recv_wait(struct dpif *dpif)
878 struct ovs_queue *q = find_nonempty_queue(dpif);
880 poll_immediate_wake();
882 /* No messages ready to be received, and dp_wait() will ensure that we
883 * wake up to queue new messages, so there is nothing to do. */
888 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
889 const struct ofpbuf *packet)
891 time_timespec(&flow->used);
892 flow->packet_count++;
893 flow->byte_count += packet->size;
894 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
895 struct tcp_header *th = packet->l4;
896 flow->tcp_ctl |= th->tcp_ctl;
901 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
902 struct ofpbuf *packet)
904 struct dp_netdev_flow *flow;
907 if (packet->size < ETH_HEADER_LEN) {
910 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
915 flow = dp_netdev_lookup_flow(dp, &key);
917 dp_netdev_flow_used(flow, &key, packet);
918 dp_netdev_execute_actions(dp, packet, &key,
919 flow->actions, flow->n_actions);
923 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
930 struct shash_node *node;
931 struct ofpbuf packet;
933 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
934 SHASH_FOR_EACH (node, &dp_netdevs) {
935 struct dp_netdev *dp = node->data;
936 struct dp_netdev_port *port;
938 LIST_FOR_EACH (port, node, &dp->port_list) {
941 /* Reset packet contents. */
942 packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
945 error = netdev_recv(port->netdev, &packet);
947 dp_netdev_port_input(dp, port, &packet);
948 } else if (error != EAGAIN && error != EOPNOTSUPP) {
949 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
950 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
951 netdev_get_name(port->netdev), strerror(error));
955 ofpbuf_uninit(&packet);
961 struct shash_node *node;
963 SHASH_FOR_EACH (node, &dp_netdevs) {
964 struct dp_netdev *dp = node->data;
965 struct dp_netdev_port *port;
967 LIST_FOR_EACH (port, node, &dp->port_list) {
968 netdev_recv_wait(port->netdev);
974 /* Modify the TCI field of 'packet'. If a VLAN tag is present, its TCI field
975 * is replaced by 'tci'. If a VLAN tag is not present, one is added with the
976 * TCI field set to 'tci'.
979 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
981 struct vlan_eth_header *veh;
982 struct eth_header *eh;
985 if (packet->size >= sizeof(struct vlan_eth_header)
986 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
990 /* Insert new 802.1Q header. */
991 struct vlan_eth_header tmp;
992 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
993 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
994 tmp.veth_type = htons(ETH_TYPE_VLAN);
996 tmp.veth_next_type = eh->eth_type;
998 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
999 memcpy(veh, &tmp, sizeof tmp);
1000 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1005 dp_netdev_strip_vlan(struct ofpbuf *packet)
1007 struct vlan_eth_header *veh = packet->l2;
1008 if (packet->size >= sizeof *veh
1009 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1010 struct eth_header tmp;
1012 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1013 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1014 tmp.eth_type = veh->veth_next_type;
1016 packet->size -= VLAN_HEADER_LEN;
1017 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1018 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1019 memcpy(packet->data, &tmp, sizeof tmp);
1024 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1026 struct eth_header *eh = packet->l2;
1027 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1031 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1033 struct eth_header *eh = packet->l2;
1034 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1038 is_ip(const struct ofpbuf *packet, const struct flow *key)
1040 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1044 dp_netdev_set_nw_addr(struct ofpbuf *packet, struct flow *key,
1045 const struct odp_action_nw_addr *a)
1047 if (is_ip(packet, key)) {
1048 struct ip_header *nh = packet->l3;
1051 field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1052 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1053 struct tcp_header *th = packet->l4;
1054 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1055 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1056 struct udp_header *uh = packet->l4;
1058 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1059 if (!uh->udp_csum) {
1060 uh->udp_csum = 0xffff;
1064 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1065 *field = a->nw_addr;
1070 dp_netdev_set_nw_tos(struct ofpbuf *packet, struct flow *key,
1071 const struct odp_action_nw_tos *a)
1073 if (is_ip(packet, key)) {
1074 struct ip_header *nh = packet->l3;
1075 uint8_t *field = &nh->ip_tos;
1077 /* Set the DSCP bits and preserve the ECN bits. */
1078 uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1080 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1081 htons((uint16_t)a->nw_tos));
1087 dp_netdev_set_tp_port(struct ofpbuf *packet, struct flow *key,
1088 const struct odp_action_tp_port *a)
1090 if (is_ip(packet, key)) {
1092 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1093 struct tcp_header *th = packet->l4;
1094 field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1095 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1096 *field = a->tp_port;
1097 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1098 struct udp_header *uh = packet->l4;
1099 field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1100 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1101 *field = a->tp_port;
1109 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1112 struct dp_netdev_port *p = dp->ports[out_port];
1114 netdev_send(p->netdev, packet);
1119 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1120 int queue_no, int port_no, uint32_t arg)
1122 struct ovs_queue *q = &dp->queues[queue_no];
1123 struct odp_msg *header;
1127 if (q->n >= MAX_QUEUE_LEN) {
1132 msg_size = sizeof *header + packet->size;
1133 msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1134 header = ofpbuf_put_uninit(msg, sizeof *header);
1135 header->type = queue_no;
1136 header->length = msg_size;
1137 header->port = port_no;
1139 ofpbuf_put(msg, packet->data, packet->size);
1140 queue_push_tail(q, msg);
1145 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1146 * screwy or truncated header fields or one whose inner and outer Ethernet
1147 * address differ. */
1149 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1151 struct arp_eth_header *arp;
1152 struct eth_header *eth;
1155 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1159 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1160 if (l3_size < sizeof(struct arp_eth_header)) {
1166 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1167 || arp->ar_pro != htons(ARP_PRO_IP)
1168 || arp->ar_hln != ETH_HEADER_LEN
1170 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1174 dp_netdev_execute_actions(struct dp_netdev *dp,
1175 struct ofpbuf *packet, struct flow *key,
1176 const union odp_action *actions, int n_actions)
1179 for (i = 0; i < n_actions; i++) {
1180 const union odp_action *a = &actions[i];
1184 dp_netdev_output_port(dp, packet, a->output.port);
1187 case ODPAT_CONTROLLER:
1188 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1189 key->in_port, a->controller.arg);
1192 case ODPAT_SET_DL_TCI:
1193 dp_netdev_set_dl_tci(packet, a->dl_tci.tci);
1196 case ODPAT_STRIP_VLAN:
1197 dp_netdev_strip_vlan(packet);
1200 case ODPAT_SET_DL_SRC:
1201 dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1204 case ODPAT_SET_DL_DST:
1205 dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1208 case ODPAT_SET_NW_SRC:
1209 case ODPAT_SET_NW_DST:
1210 dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1213 case ODPAT_SET_NW_TOS:
1214 dp_netdev_set_nw_tos(packet, key, &a->nw_tos);
1217 case ODPAT_SET_TP_SRC:
1218 case ODPAT_SET_TP_DST:
1219 dp_netdev_set_tp_port(packet, key, &a->tp_port);
1222 case ODPAT_DROP_SPOOFED_ARP:
1223 if (dp_netdev_is_spoofed_arp(packet, key)) {
1231 const struct dpif_class dpif_netdev_class = {
1235 NULL, /* enumerate */
1238 NULL, /* get_all_names */
1239 dpif_netdev_destroy,
1240 dpif_netdev_get_stats,
1241 dpif_netdev_get_drop_frags,
1242 dpif_netdev_set_drop_frags,
1243 dpif_netdev_port_add,
1244 dpif_netdev_port_del,
1245 dpif_netdev_port_query_by_number,
1246 dpif_netdev_port_query_by_name,
1247 dpif_netdev_port_list,
1248 dpif_netdev_port_poll,
1249 dpif_netdev_port_poll_wait,
1250 dpif_netdev_flow_get,
1251 dpif_netdev_flow_put,
1252 dpif_netdev_flow_del,
1253 dpif_netdev_flow_flush,
1254 dpif_netdev_flow_list,
1255 dpif_netdev_execute,
1256 dpif_netdev_recv_get_mask,
1257 dpif_netdev_recv_set_mask,
1258 NULL, /* get_sflow_probability */
1259 NULL, /* set_sflow_probability */
1260 NULL, /* queue_to_priority */
1262 dpif_netdev_recv_wait,
1266 dpif_dummy_register(void)
1268 if (!dpif_dummy_class.type) {
1269 dpif_dummy_class = dpif_netdev_class;
1270 dpif_dummy_class.type = "dummy";
1271 dp_register_provider(&dpif_dummy_class);