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>
35 #include "dpif-provider.h"
41 #include "ofp-print.h"
44 #include "poll-loop.h"
51 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
53 /* Configuration parameters. */
54 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
55 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
56 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
57 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
59 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
60 * headers to be aligned on a 4-byte boundary. */
61 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
63 /* Datapath based on the network device interface from netdev.h. */
69 bool drop_frags; /* Drop all IP fragments, if true. */
70 struct ovs_queue queues[N_QUEUES]; /* Messages queued for dpif_recv(). */
71 struct hmap flow_table; /* Flow table. */
74 long long int n_frags; /* Number of dropped IP fragments. */
75 long long int n_hit; /* Number of flow table matches. */
76 long long int n_missed; /* Number of flow table misses. */
77 long long int n_lost; /* Number of misses not passed to client. */
81 struct dp_netdev_port *ports[MAX_PORTS];
82 struct list port_list;
86 /* A port in a netdev-based datapath. */
87 struct dp_netdev_port {
88 int port_no; /* Index into dp_netdev's 'ports'. */
89 struct list node; /* Element in dp_netdev's 'port_list'. */
90 struct netdev *netdev;
91 bool internal; /* Internal port (as ODP_PORT_INTERNAL)? */
94 /* A flow in dp_netdev's 'flow_table'. */
95 struct dp_netdev_flow {
96 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
100 struct timespec used; /* Last used time. */
101 long long int packet_count; /* Number of packets matched. */
102 long long int byte_count; /* Number of bytes matched. */
103 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
106 union odp_action *actions;
107 unsigned int n_actions;
110 /* Interface to netdev-based datapath. */
113 struct dp_netdev *dp;
115 unsigned int dp_serial;
118 /* All netdev-based datapaths. */
119 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
121 /* Maximum port MTU seen so far. */
122 static int max_mtu = ETH_PAYLOAD_MAX;
124 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
125 struct dp_netdev_port **portp);
126 static int get_port_by_name(struct dp_netdev *, const char *devname,
127 struct dp_netdev_port **portp);
128 static void dp_netdev_free(struct dp_netdev *);
129 static void dp_netdev_flow_flush(struct dp_netdev *);
130 static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags,
132 static int do_del_port(struct dp_netdev *, uint16_t port_no);
133 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
134 int queue_no, int port_no, uint32_t arg);
135 static int dp_netdev_execute_actions(struct dp_netdev *,
136 struct ofpbuf *, struct flow *,
137 const union odp_action *, int n);
139 static struct dpif_netdev *
140 dpif_netdev_cast(const struct dpif *dpif)
142 dpif_assert_class(dpif, &dpif_netdev_class);
143 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
146 static struct dp_netdev *
147 get_dp_netdev(const struct dpif *dpif)
149 return dpif_netdev_cast(dpif)->dp;
153 create_dpif_netdev(struct dp_netdev *dp)
155 uint16_t netflow_id = hash_string(dp->name, 0);
156 struct dpif_netdev *dpif;
160 dpif = xmalloc(sizeof *dpif);
161 dpif_init(&dpif->dpif, &dpif_netdev_class, dp->name,
162 netflow_id >> 8, netflow_id);
164 dpif->listen_mask = 0;
165 dpif->dp_serial = dp->serial;
171 create_dp_netdev(const char *name, struct dp_netdev **dpp)
173 struct dp_netdev *dp;
177 dp = xzalloc(sizeof *dp);
178 dp->name = xstrdup(name);
180 dp->drop_frags = false;
181 for (i = 0; i < N_QUEUES; i++) {
182 queue_init(&dp->queues[i]);
184 hmap_init(&dp->flow_table);
185 list_init(&dp->port_list);
186 error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL);
192 shash_add(&dp_netdevs, name, dp);
199 dpif_netdev_open(const struct dpif_class *class OVS_UNUSED, const char *name,
200 bool create, struct dpif **dpifp)
202 struct dp_netdev *dp;
204 dp = shash_find_data(&dp_netdevs, name);
209 int error = create_dp_netdev(name, &dp);
221 *dpifp = create_dpif_netdev(dp);
226 dp_netdev_free(struct dp_netdev *dp)
230 dp_netdev_flow_flush(dp);
231 while (dp->n_ports > 0) {
232 struct dp_netdev_port *port = CONTAINER_OF(
233 dp->port_list.next, struct dp_netdev_port, node);
234 do_del_port(dp, port->port_no);
236 for (i = 0; i < N_QUEUES; i++) {
237 queue_destroy(&dp->queues[i]);
239 hmap_destroy(&dp->flow_table);
245 dpif_netdev_close(struct dpif *dpif)
247 struct dp_netdev *dp = get_dp_netdev(dpif);
248 assert(dp->open_cnt > 0);
249 if (--dp->open_cnt == 0 && dp->destroyed) {
250 shash_find_and_delete(&dp_netdevs, dp->name);
257 dpif_netdev_destroy(struct dpif *dpif)
259 struct dp_netdev *dp = get_dp_netdev(dpif);
260 dp->destroyed = true;
265 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
267 struct dp_netdev *dp = get_dp_netdev(dpif);
268 memset(stats, 0, sizeof *stats);
269 stats->n_flows = hmap_count(&dp->flow_table);
270 stats->cur_capacity = hmap_capacity(&dp->flow_table);
271 stats->max_capacity = MAX_FLOWS;
272 stats->n_ports = dp->n_ports;
273 stats->max_ports = MAX_PORTS;
274 stats->n_frags = dp->n_frags;
275 stats->n_hit = dp->n_hit;
276 stats->n_missed = dp->n_missed;
277 stats->n_lost = dp->n_lost;
278 stats->max_miss_queue = MAX_QUEUE_LEN;
279 stats->max_action_queue = MAX_QUEUE_LEN;
284 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
286 struct dp_netdev *dp = get_dp_netdev(dpif);
287 *drop_fragsp = dp->drop_frags;
292 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
294 struct dp_netdev *dp = get_dp_netdev(dpif);
295 dp->drop_frags = drop_frags;
300 do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
303 bool internal = (flags & ODP_PORT_INTERNAL) != 0;
304 struct dp_netdev_port *port;
305 struct netdev_options netdev_options;
306 struct netdev *netdev;
310 /* XXX reject devices already in some dp_netdev. */
312 /* Open and validate network device. */
313 memset(&netdev_options, 0, sizeof netdev_options);
314 netdev_options.name = devname;
315 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
317 netdev_options.type = "tap";
320 error = netdev_open(&netdev_options, &netdev);
324 /* XXX reject loopback devices */
325 /* XXX reject non-Ethernet devices */
327 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
329 netdev_close(netdev);
333 port = xmalloc(sizeof *port);
334 port->port_no = port_no;
335 port->netdev = netdev;
336 port->internal = internal;
338 netdev_get_mtu(netdev, &mtu);
343 list_push_back(&dp->port_list, &port->node);
344 dp->ports[port_no] = port;
352 dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
355 struct dp_netdev *dp = get_dp_netdev(dpif);
358 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
359 if (!dp->ports[port_no]) {
361 return do_add_port(dp, devname, flags, port_no);
368 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
370 struct dp_netdev *dp = get_dp_netdev(dpif);
371 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
375 is_valid_port_number(uint16_t port_no)
377 return port_no < MAX_PORTS;
381 get_port_by_number(struct dp_netdev *dp,
382 uint16_t port_no, struct dp_netdev_port **portp)
384 if (!is_valid_port_number(port_no)) {
388 *portp = dp->ports[port_no];
389 return *portp ? 0 : ENOENT;
394 get_port_by_name(struct dp_netdev *dp,
395 const char *devname, struct dp_netdev_port **portp)
397 struct dp_netdev_port *port;
399 LIST_FOR_EACH (port, node, &dp->port_list) {
400 if (!strcmp(netdev_get_name(port->netdev), devname)) {
409 do_del_port(struct dp_netdev *dp, uint16_t port_no)
411 struct dp_netdev_port *port;
415 error = get_port_by_number(dp, port_no, &port);
420 list_remove(&port->node);
421 dp->ports[port->port_no] = NULL;
425 name = xstrdup(netdev_get_name(port->netdev));
426 netdev_close(port->netdev);
435 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
437 memset(odp_port, 0, sizeof *odp_port);
438 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
439 sizeof odp_port->devname);
440 odp_port->port = port->port_no;
441 odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
445 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
446 struct odp_port *odp_port)
448 struct dp_netdev *dp = get_dp_netdev(dpif);
449 struct dp_netdev_port *port;
452 error = get_port_by_number(dp, port_no, &port);
454 answer_port_query(port, odp_port);
460 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
461 struct odp_port *odp_port)
463 struct dp_netdev *dp = get_dp_netdev(dpif);
464 struct dp_netdev_port *port;
467 error = get_port_by_name(dp, devname, &port);
469 answer_port_query(port, odp_port);
475 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
477 hmap_remove(&dp->flow_table, &flow->node);
483 dp_netdev_flow_flush(struct dp_netdev *dp)
485 struct dp_netdev_flow *flow, *next;
487 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
488 dp_netdev_free_flow(dp, flow);
493 dpif_netdev_flow_flush(struct dpif *dpif)
495 struct dp_netdev *dp = get_dp_netdev(dpif);
496 dp_netdev_flow_flush(dp);
501 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
503 struct dp_netdev *dp = get_dp_netdev(dpif);
504 struct dp_netdev_port *port;
508 LIST_FOR_EACH (port, node, &dp->port_list) {
509 struct odp_port *odp_port = &ports[i];
513 answer_port_query(port, odp_port);
520 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
522 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
523 if (dpif->dp_serial != dpif->dp->serial) {
524 dpif->dp_serial = dpif->dp->serial;
532 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
534 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
535 if (dpif->dp_serial != dpif->dp->serial) {
536 poll_immediate_wake();
540 static struct dp_netdev_flow *
541 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
543 struct dp_netdev_flow *flow;
545 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
546 if (flow_equal(&flow->key, key)) {
553 /* The caller must fill in odp_flow->key itself. */
555 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
556 struct odp_flow *odp_flow)
559 odp_flow->stats.n_packets = flow->packet_count;
560 odp_flow->stats.n_bytes = flow->byte_count;
561 odp_flow->stats.used_sec = flow->used.tv_sec;
562 odp_flow->stats.used_nsec = flow->used.tv_nsec;
563 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
564 odp_flow->stats.reserved = 0;
565 odp_flow->stats.error = 0;
566 if (odp_flow->n_actions > 0) {
567 unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
568 memcpy(odp_flow->actions, flow->actions,
569 n * sizeof *odp_flow->actions);
570 odp_flow->n_actions = flow->n_actions;
573 if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
578 odp_flow->stats.error = ENOENT;
583 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
585 struct dp_netdev *dp = get_dp_netdev(dpif);
588 for (i = 0; i < n; i++) {
589 struct odp_flow *odp_flow = &flows[i];
592 odp_flow_key_to_flow(&odp_flow->key, &key);
593 answer_flow_query(dp_netdev_lookup_flow(dp, &key),
594 odp_flow->flags, odp_flow);
600 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
606 for (i = 0; i < n_actions; i++) {
607 const union odp_action *a = &actions[i];
610 if (a->output.port >= MAX_PORTS) {
615 case ODPAT_CONTROLLER:
618 case ODPAT_SET_DL_TCI:
620 if (a->dl_tci.tci & htons(VLAN_CFI)) {
625 case ODPAT_SET_NW_TOS:
627 if (a->nw_tos.nw_tos & IP_ECN_MASK) {
632 case ODPAT_STRIP_VLAN:
633 case ODPAT_SET_DL_SRC:
634 case ODPAT_SET_DL_DST:
635 case ODPAT_SET_NW_SRC:
636 case ODPAT_SET_NW_DST:
637 case ODPAT_SET_TP_SRC:
638 case ODPAT_SET_TP_DST:
650 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
656 if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
659 error = dpif_netdev_validate_actions(odp_flow->actions,
660 odp_flow->n_actions, &mutates);
665 n_bytes = odp_flow->n_actions * sizeof *flow->actions;
666 flow->actions = xrealloc(flow->actions, n_bytes);
667 flow->n_actions = odp_flow->n_actions;
668 memcpy(flow->actions, odp_flow->actions, n_bytes);
673 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
675 struct dp_netdev *dp = get_dp_netdev(dpif);
676 struct dp_netdev_flow *flow;
679 flow = xzalloc(sizeof *flow);
680 odp_flow_key_to_flow(&odp_flow->key, &flow->key);
682 error = set_flow_actions(flow, odp_flow);
688 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
693 clear_stats(struct dp_netdev_flow *flow)
695 flow->used.tv_sec = 0;
696 flow->used.tv_nsec = 0;
697 flow->packet_count = 0;
698 flow->byte_count = 0;
703 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
705 struct dp_netdev *dp = get_dp_netdev(dpif);
706 struct dp_netdev_flow *flow;
709 odp_flow_key_to_flow(&put->flow.key, &key);
710 flow = dp_netdev_lookup_flow(dp, &key);
712 if (put->flags & ODPPF_CREATE) {
713 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
714 return add_flow(dpif, &put->flow);
722 if (put->flags & ODPPF_MODIFY) {
723 int error = set_flow_actions(flow, &put->flow);
724 if (!error && put->flags & ODPPF_ZERO_STATS) {
736 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
738 struct dp_netdev *dp = get_dp_netdev(dpif);
739 struct dp_netdev_flow *flow;
742 odp_flow_key_to_flow(&odp_flow->key, &key);
743 flow = dp_netdev_lookup_flow(dp, &key);
745 answer_flow_query(flow, 0, odp_flow);
746 dp_netdev_free_flow(dp, flow);
754 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
756 struct dp_netdev *dp = get_dp_netdev(dpif);
757 struct dp_netdev_flow *flow;
761 HMAP_FOR_EACH (flow, node, &dp->flow_table) {
766 odp_flow_key_from_flow(&flows[i].key, &flow->key);
767 answer_flow_query(flow, 0, &flows[i]);
770 return hmap_count(&dp->flow_table);
774 dpif_netdev_execute(struct dpif *dpif,
775 const union odp_action actions[], int n_actions,
776 const struct ofpbuf *packet)
778 struct dp_netdev *dp = get_dp_netdev(dpif);
784 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
788 error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
794 /* We need a deep copy of 'packet' since we're going to modify its
796 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
797 copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
798 ofpbuf_put(©, packet->data, packet->size);
800 /* We still need a shallow copy of 'packet', even though we won't
801 * modify its data, because flow_extract() modifies packet->l2, etc.
802 * We could probably get away with modifying those but it's more polite
806 flow_extract(©, 0, -1, &key);
807 error = dp_netdev_execute_actions(dp, ©, &key, actions, n_actions);
809 ofpbuf_uninit(©);
815 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
817 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
818 *listen_mask = dpif_netdev->listen_mask;
823 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
825 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
826 if (!(listen_mask & ~ODPL_ALL)) {
827 dpif_netdev->listen_mask = listen_mask;
834 static struct ovs_queue *
835 find_nonempty_queue(struct dpif *dpif)
837 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
838 struct dp_netdev *dp = get_dp_netdev(dpif);
839 int mask = dpif_netdev->listen_mask;
842 for (i = 0; i < N_QUEUES; i++) {
843 struct ovs_queue *q = &dp->queues[i];
844 if (q->n && mask & (1u << i)) {
852 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
854 struct ovs_queue *q = find_nonempty_queue(dpif);
856 *bufp = queue_pop_head(q);
864 dpif_netdev_recv_wait(struct dpif *dpif)
866 struct ovs_queue *q = find_nonempty_queue(dpif);
868 poll_immediate_wake();
870 /* No messages ready to be received, and dp_wait() will ensure that we
871 * wake up to queue new messages, so there is nothing to do. */
876 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
877 const struct ofpbuf *packet)
879 time_timespec(&flow->used);
880 flow->packet_count++;
881 flow->byte_count += packet->size;
882 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
883 struct tcp_header *th = packet->l4;
884 flow->tcp_ctl |= th->tcp_ctl;
889 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
890 struct ofpbuf *packet)
892 struct dp_netdev_flow *flow;
895 if (packet->size < ETH_HEADER_LEN) {
898 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
903 flow = dp_netdev_lookup_flow(dp, &key);
905 dp_netdev_flow_used(flow, &key, packet);
906 dp_netdev_execute_actions(dp, packet, &key,
907 flow->actions, flow->n_actions);
911 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
918 struct shash_node *node;
919 struct ofpbuf packet;
921 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
922 SHASH_FOR_EACH (node, &dp_netdevs) {
923 struct dp_netdev *dp = node->data;
924 struct dp_netdev_port *port;
926 LIST_FOR_EACH (port, node, &dp->port_list) {
929 /* Reset packet contents. */
930 packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
933 error = netdev_recv(port->netdev, &packet);
935 dp_netdev_port_input(dp, port, &packet);
936 } else if (error != EAGAIN && error != EOPNOTSUPP) {
937 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
938 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
939 netdev_get_name(port->netdev), strerror(error));
943 ofpbuf_uninit(&packet);
949 struct shash_node *node;
951 SHASH_FOR_EACH (node, &dp_netdevs) {
952 struct dp_netdev *dp = node->data;
953 struct dp_netdev_port *port;
955 LIST_FOR_EACH (port, node, &dp->port_list) {
956 netdev_recv_wait(port->netdev);
962 /* Modify the TCI field of 'packet'. If a VLAN tag is present, its TCI field
963 * is replaced by 'tci'. If a VLAN tag is not present, one is added with the
964 * TCI field set to 'tci'.
967 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
969 struct vlan_eth_header *veh;
970 struct eth_header *eh;
973 if (packet->size >= sizeof(struct vlan_eth_header)
974 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
978 /* Insert new 802.1Q header. */
979 struct vlan_eth_header tmp;
980 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
981 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
982 tmp.veth_type = htons(ETH_TYPE_VLAN);
984 tmp.veth_next_type = eh->eth_type;
986 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
987 memcpy(veh, &tmp, sizeof tmp);
988 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
993 dp_netdev_strip_vlan(struct ofpbuf *packet)
995 struct vlan_eth_header *veh = packet->l2;
996 if (packet->size >= sizeof *veh
997 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
998 struct eth_header tmp;
1000 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1001 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1002 tmp.eth_type = veh->veth_next_type;
1004 packet->size -= VLAN_HEADER_LEN;
1005 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1006 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1007 memcpy(packet->data, &tmp, sizeof tmp);
1012 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1014 struct eth_header *eh = packet->l2;
1015 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1019 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1021 struct eth_header *eh = packet->l2;
1022 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1026 is_ip(const struct ofpbuf *packet, const struct flow *key)
1028 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1032 dp_netdev_set_nw_addr(struct ofpbuf *packet, struct flow *key,
1033 const struct odp_action_nw_addr *a)
1035 if (is_ip(packet, key)) {
1036 struct ip_header *nh = packet->l3;
1039 field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1040 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1041 struct tcp_header *th = packet->l4;
1042 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1043 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1044 struct udp_header *uh = packet->l4;
1046 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1047 if (!uh->udp_csum) {
1048 uh->udp_csum = 0xffff;
1052 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1053 *field = a->nw_addr;
1058 dp_netdev_set_nw_tos(struct ofpbuf *packet, struct flow *key,
1059 const struct odp_action_nw_tos *a)
1061 if (is_ip(packet, key)) {
1062 struct ip_header *nh = packet->l3;
1063 uint8_t *field = &nh->ip_tos;
1065 /* Set the DSCP bits and preserve the ECN bits. */
1066 uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1068 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1069 htons((uint16_t)a->nw_tos));
1075 dp_netdev_set_tp_port(struct ofpbuf *packet, struct flow *key,
1076 const struct odp_action_tp_port *a)
1078 if (is_ip(packet, key)) {
1080 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1081 struct tcp_header *th = packet->l4;
1082 field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1083 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1084 *field = a->tp_port;
1085 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1086 struct udp_header *uh = packet->l4;
1087 field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1088 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1089 *field = a->tp_port;
1097 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1100 struct dp_netdev_port *p = dp->ports[out_port];
1102 netdev_send(p->netdev, packet);
1107 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1108 int queue_no, int port_no, uint32_t arg)
1110 struct ovs_queue *q = &dp->queues[queue_no];
1111 struct odp_msg *header;
1115 if (q->n >= MAX_QUEUE_LEN) {
1120 msg_size = sizeof *header + packet->size;
1121 msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1122 header = ofpbuf_put_uninit(msg, sizeof *header);
1123 header->type = queue_no;
1124 header->length = msg_size;
1125 header->port = port_no;
1127 ofpbuf_put(msg, packet->data, packet->size);
1128 queue_push_tail(q, msg);
1133 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1134 * screwy or truncated header fields or one whose inner and outer Ethernet
1135 * address differ. */
1137 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1139 struct arp_eth_header *arp;
1140 struct eth_header *eth;
1143 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1147 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1148 if (l3_size < sizeof(struct arp_eth_header)) {
1154 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1155 || arp->ar_pro != htons(ARP_PRO_IP)
1156 || arp->ar_hln != ETH_HEADER_LEN
1158 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1162 dp_netdev_execute_actions(struct dp_netdev *dp,
1163 struct ofpbuf *packet, struct flow *key,
1164 const union odp_action *actions, int n_actions)
1167 for (i = 0; i < n_actions; i++) {
1168 const union odp_action *a = &actions[i];
1172 dp_netdev_output_port(dp, packet, a->output.port);
1175 case ODPAT_CONTROLLER:
1176 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1177 key->in_port, a->controller.arg);
1180 case ODPAT_SET_DL_TCI:
1181 dp_netdev_set_dl_tci(packet, a->dl_tci.tci);
1184 case ODPAT_STRIP_VLAN:
1185 dp_netdev_strip_vlan(packet);
1188 case ODPAT_SET_DL_SRC:
1189 dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1192 case ODPAT_SET_DL_DST:
1193 dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1196 case ODPAT_SET_NW_SRC:
1197 case ODPAT_SET_NW_DST:
1198 dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1201 case ODPAT_SET_NW_TOS:
1202 dp_netdev_set_nw_tos(packet, key, &a->nw_tos);
1205 case ODPAT_SET_TP_SRC:
1206 case ODPAT_SET_TP_DST:
1207 dp_netdev_set_tp_port(packet, key, &a->tp_port);
1210 case ODPAT_DROP_SPOOFED_ARP:
1211 if (dp_netdev_is_spoofed_arp(packet, key)) {
1219 const struct dpif_class dpif_netdev_class = {
1223 NULL, /* enumerate */
1226 NULL, /* get_all_names */
1227 dpif_netdev_destroy,
1228 dpif_netdev_get_stats,
1229 dpif_netdev_get_drop_frags,
1230 dpif_netdev_set_drop_frags,
1231 dpif_netdev_port_add,
1232 dpif_netdev_port_del,
1233 dpif_netdev_port_query_by_number,
1234 dpif_netdev_port_query_by_name,
1235 dpif_netdev_port_list,
1236 dpif_netdev_port_poll,
1237 dpif_netdev_port_poll_wait,
1238 dpif_netdev_flow_get,
1239 dpif_netdev_flow_put,
1240 dpif_netdev_flow_del,
1241 dpif_netdev_flow_flush,
1242 dpif_netdev_flow_list,
1243 dpif_netdev_execute,
1244 dpif_netdev_recv_get_mask,
1245 dpif_netdev_recv_set_mask,
1246 NULL, /* get_sflow_probability */
1247 NULL, /* set_sflow_probability */
1248 NULL, /* queue_to_priority */
1250 dpif_netdev_recv_wait,