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"
52 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
54 /* Configuration parameters. */
55 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
56 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
57 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
58 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
60 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
61 * headers to be aligned on a 4-byte boundary. */
62 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
64 /* Datapath based on the network device interface from netdev.h. */
66 const struct dpif_class *class;
71 bool drop_frags; /* Drop all IP fragments, if true. */
72 struct list queues[N_QUEUES]; /* Contain ofpbufs queued for dpif_recv(). */
73 size_t queue_len[N_QUEUES]; /* Number of packets in each queue. */
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? */
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,
134 const char *type, uint16_t port_no);
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 list_init(&dp->queues[i]);
192 hmap_init(&dp->flow_table);
193 list_init(&dp->port_list);
194 error = do_add_port(dp, name, "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 ofpbuf_list_delete(&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, const char *type,
313 struct dp_netdev_port *port;
314 struct netdev_options netdev_options;
315 struct netdev *netdev;
320 /* XXX reject devices already in some dp_netdev. */
321 if (type[0] == '\0' || !strcmp(type, "system")) {
323 } else if (!strcmp(type, "internal")) {
326 VLOG_WARN("%s: unsupported port type %s", devname, type);
330 /* Open and validate network device. */
331 memset(&netdev_options, 0, sizeof netdev_options);
332 netdev_options.name = devname;
333 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
334 if (dp->class == &dpif_dummy_class) {
335 netdev_options.type = "dummy";
336 } else if (internal) {
337 netdev_options.type = "tap";
340 error = netdev_open(&netdev_options, &netdev);
344 /* XXX reject loopback devices */
345 /* XXX reject non-Ethernet devices */
347 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
349 netdev_close(netdev);
353 port = xmalloc(sizeof *port);
354 port->port_no = port_no;
355 port->netdev = netdev;
356 port->internal = internal;
358 netdev_get_mtu(netdev, &mtu);
363 list_push_back(&dp->port_list, &port->node);
364 dp->ports[port_no] = port;
372 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
375 struct dp_netdev *dp = get_dp_netdev(dpif);
378 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
379 if (!dp->ports[port_no]) {
381 return do_add_port(dp, netdev_get_name(netdev),
382 netdev_get_type(netdev), port_no);
389 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
391 struct dp_netdev *dp = get_dp_netdev(dpif);
392 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
396 is_valid_port_number(uint16_t port_no)
398 return port_no < MAX_PORTS;
402 get_port_by_number(struct dp_netdev *dp,
403 uint16_t port_no, struct dp_netdev_port **portp)
405 if (!is_valid_port_number(port_no)) {
409 *portp = dp->ports[port_no];
410 return *portp ? 0 : ENOENT;
415 get_port_by_name(struct dp_netdev *dp,
416 const char *devname, struct dp_netdev_port **portp)
418 struct dp_netdev_port *port;
420 LIST_FOR_EACH (port, node, &dp->port_list) {
421 if (!strcmp(netdev_get_name(port->netdev), devname)) {
430 do_del_port(struct dp_netdev *dp, uint16_t port_no)
432 struct dp_netdev_port *port;
436 error = get_port_by_number(dp, port_no, &port);
441 list_remove(&port->node);
442 dp->ports[port->port_no] = NULL;
446 name = xstrdup(netdev_get_name(port->netdev));
447 netdev_close(port->netdev);
456 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
458 memset(odp_port, 0, sizeof *odp_port);
459 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
460 sizeof odp_port->devname);
461 odp_port->port = port->port_no;
462 strcpy(odp_port->type, port->internal ? "internal" : "system");
466 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
467 struct odp_port *odp_port)
469 struct dp_netdev *dp = get_dp_netdev(dpif);
470 struct dp_netdev_port *port;
473 error = get_port_by_number(dp, port_no, &port);
475 answer_port_query(port, odp_port);
481 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
482 struct odp_port *odp_port)
484 struct dp_netdev *dp = get_dp_netdev(dpif);
485 struct dp_netdev_port *port;
488 error = get_port_by_name(dp, devname, &port);
490 answer_port_query(port, odp_port);
496 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
498 hmap_remove(&dp->flow_table, &flow->node);
504 dp_netdev_flow_flush(struct dp_netdev *dp)
506 struct dp_netdev_flow *flow, *next;
508 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
509 dp_netdev_free_flow(dp, flow);
514 dpif_netdev_flow_flush(struct dpif *dpif)
516 struct dp_netdev *dp = get_dp_netdev(dpif);
517 dp_netdev_flow_flush(dp);
522 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
524 struct dp_netdev *dp = get_dp_netdev(dpif);
525 struct dp_netdev_port *port;
529 LIST_FOR_EACH (port, node, &dp->port_list) {
530 struct odp_port *odp_port = &ports[i];
534 answer_port_query(port, odp_port);
541 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
543 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
544 if (dpif->dp_serial != dpif->dp->serial) {
545 dpif->dp_serial = dpif->dp->serial;
553 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
555 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
556 if (dpif->dp_serial != dpif->dp->serial) {
557 poll_immediate_wake();
561 static struct dp_netdev_flow *
562 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
564 struct dp_netdev_flow *flow;
566 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
567 if (flow_equal(&flow->key, key)) {
574 /* The caller must fill in odp_flow->key itself. */
576 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
577 struct odp_flow *odp_flow)
580 odp_flow->stats.n_packets = flow->packet_count;
581 odp_flow->stats.n_bytes = flow->byte_count;
582 odp_flow->stats.used_sec = flow->used.tv_sec;
583 odp_flow->stats.used_nsec = flow->used.tv_nsec;
584 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
585 odp_flow->stats.reserved = 0;
586 odp_flow->stats.error = 0;
587 if (odp_flow->n_actions > 0) {
588 unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
589 memcpy(odp_flow->actions, flow->actions,
590 n * sizeof *odp_flow->actions);
591 odp_flow->n_actions = flow->n_actions;
594 if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
599 odp_flow->stats.error = ENOENT;
604 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
606 struct dp_netdev *dp = get_dp_netdev(dpif);
609 for (i = 0; i < n; i++) {
610 struct odp_flow *odp_flow = &flows[i];
613 odp_flow_key_to_flow(&odp_flow->key, &key);
614 answer_flow_query(dp_netdev_lookup_flow(dp, &key),
615 odp_flow->flags, odp_flow);
621 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
627 for (i = 0; i < n_actions; i++) {
628 const union odp_action *a = &actions[i];
631 if (a->output.port >= MAX_PORTS) {
636 case ODPAT_CONTROLLER:
639 case ODPAT_SET_DL_TCI:
641 if (a->dl_tci.tci & htons(VLAN_CFI)) {
646 case ODPAT_SET_NW_TOS:
648 if (a->nw_tos.nw_tos & IP_ECN_MASK) {
653 case ODPAT_STRIP_VLAN:
654 case ODPAT_SET_DL_SRC:
655 case ODPAT_SET_DL_DST:
656 case ODPAT_SET_NW_SRC:
657 case ODPAT_SET_NW_DST:
658 case ODPAT_SET_TP_SRC:
659 case ODPAT_SET_TP_DST:
671 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
677 if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
680 error = dpif_netdev_validate_actions(odp_flow->actions,
681 odp_flow->n_actions, &mutates);
686 n_bytes = odp_flow->n_actions * sizeof *flow->actions;
687 flow->actions = xrealloc(flow->actions, n_bytes);
688 flow->n_actions = odp_flow->n_actions;
689 memcpy(flow->actions, odp_flow->actions, n_bytes);
694 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
696 struct dp_netdev *dp = get_dp_netdev(dpif);
697 struct dp_netdev_flow *flow;
700 flow = xzalloc(sizeof *flow);
701 odp_flow_key_to_flow(&odp_flow->key, &flow->key);
703 error = set_flow_actions(flow, odp_flow);
709 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
714 clear_stats(struct dp_netdev_flow *flow)
716 flow->used.tv_sec = 0;
717 flow->used.tv_nsec = 0;
718 flow->packet_count = 0;
719 flow->byte_count = 0;
724 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
726 struct dp_netdev *dp = get_dp_netdev(dpif);
727 struct dp_netdev_flow *flow;
730 odp_flow_key_to_flow(&put->flow.key, &key);
731 flow = dp_netdev_lookup_flow(dp, &key);
733 if (put->flags & ODPPF_CREATE) {
734 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
735 return add_flow(dpif, &put->flow);
743 if (put->flags & ODPPF_MODIFY) {
744 int error = set_flow_actions(flow, &put->flow);
745 if (!error && put->flags & ODPPF_ZERO_STATS) {
757 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
759 struct dp_netdev *dp = get_dp_netdev(dpif);
760 struct dp_netdev_flow *flow;
763 odp_flow_key_to_flow(&odp_flow->key, &key);
764 flow = dp_netdev_lookup_flow(dp, &key);
766 answer_flow_query(flow, 0, odp_flow);
767 dp_netdev_free_flow(dp, flow);
775 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
777 struct dp_netdev *dp = get_dp_netdev(dpif);
778 struct dp_netdev_flow *flow;
782 HMAP_FOR_EACH (flow, node, &dp->flow_table) {
787 odp_flow_key_from_flow(&flows[i].key, &flow->key);
788 answer_flow_query(flow, 0, &flows[i]);
791 return hmap_count(&dp->flow_table);
795 dpif_netdev_execute(struct dpif *dpif,
796 const union odp_action actions[], int n_actions,
797 const struct ofpbuf *packet)
799 struct dp_netdev *dp = get_dp_netdev(dpif);
805 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
809 error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
815 /* We need a deep copy of 'packet' since we're going to modify its
817 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
818 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
819 ofpbuf_put(©, packet->data, packet->size);
821 /* We still need a shallow copy of 'packet', even though we won't
822 * modify its data, because flow_extract() modifies packet->l2, etc.
823 * We could probably get away with modifying those but it's more polite
827 flow_extract(©, 0, -1, &key);
828 error = dp_netdev_execute_actions(dp, ©, &key, actions, n_actions);
830 ofpbuf_uninit(©);
836 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
838 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
839 *listen_mask = dpif_netdev->listen_mask;
844 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
846 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
847 if (!(listen_mask & ~ODPL_ALL)) {
848 dpif_netdev->listen_mask = listen_mask;
856 find_nonempty_queue(struct dpif *dpif)
858 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
859 struct dp_netdev *dp = get_dp_netdev(dpif);
860 int mask = dpif_netdev->listen_mask;
863 for (i = 0; i < N_QUEUES; i++) {
864 struct list *queue = &dp->queues[i];
865 if (!list_is_empty(queue) && mask & (1u << i)) {
873 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
875 int queue_idx = find_nonempty_queue(dpif);
876 if (queue_idx >= 0) {
877 struct dp_netdev *dp = get_dp_netdev(dpif);
879 *bufp = ofpbuf_from_list(list_pop_front(&dp->queues[queue_idx]));
880 dp->queue_len[queue_idx]--;
889 dpif_netdev_recv_wait(struct dpif *dpif)
891 if (find_nonempty_queue(dpif) >= 0) {
892 poll_immediate_wake();
894 /* No messages ready to be received, and dp_wait() will ensure that we
895 * wake up to queue new messages, so there is nothing to do. */
900 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
901 const struct ofpbuf *packet)
903 time_timespec(&flow->used);
904 flow->packet_count++;
905 flow->byte_count += packet->size;
906 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
907 struct tcp_header *th = packet->l4;
908 flow->tcp_ctl |= th->tcp_ctl;
913 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
914 struct ofpbuf *packet)
916 struct dp_netdev_flow *flow;
919 if (packet->size < ETH_HEADER_LEN) {
922 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
927 flow = dp_netdev_lookup_flow(dp, &key);
929 dp_netdev_flow_used(flow, &key, packet);
930 dp_netdev_execute_actions(dp, packet, &key,
931 flow->actions, flow->n_actions);
935 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
942 struct shash_node *node;
943 struct ofpbuf packet;
945 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
946 SHASH_FOR_EACH (node, &dp_netdevs) {
947 struct dp_netdev *dp = node->data;
948 struct dp_netdev_port *port;
950 LIST_FOR_EACH (port, node, &dp->port_list) {
953 /* Reset packet contents. */
954 ofpbuf_clear(&packet);
955 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
957 error = netdev_recv(port->netdev, &packet);
959 dp_netdev_port_input(dp, port, &packet);
960 } else if (error != EAGAIN && error != EOPNOTSUPP) {
961 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
962 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
963 netdev_get_name(port->netdev), strerror(error));
967 ofpbuf_uninit(&packet);
973 struct shash_node *node;
975 SHASH_FOR_EACH (node, &dp_netdevs) {
976 struct dp_netdev *dp = node->data;
977 struct dp_netdev_port *port;
979 LIST_FOR_EACH (port, node, &dp->port_list) {
980 netdev_recv_wait(port->netdev);
986 /* Modify the TCI field of 'packet'. If a VLAN tag is present, its TCI field
987 * is replaced by 'tci'. If a VLAN tag is not present, one is added with the
988 * TCI field set to 'tci'.
991 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
993 struct vlan_eth_header *veh;
994 struct eth_header *eh;
997 if (packet->size >= sizeof(struct vlan_eth_header)
998 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1000 veh->veth_tci = tci;
1002 /* Insert new 802.1Q header. */
1003 struct vlan_eth_header tmp;
1004 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1005 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1006 tmp.veth_type = htons(ETH_TYPE_VLAN);
1008 tmp.veth_next_type = eh->eth_type;
1010 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1011 memcpy(veh, &tmp, sizeof tmp);
1012 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1017 dp_netdev_strip_vlan(struct ofpbuf *packet)
1019 struct vlan_eth_header *veh = packet->l2;
1020 if (packet->size >= sizeof *veh
1021 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1022 struct eth_header tmp;
1024 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1025 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1026 tmp.eth_type = veh->veth_next_type;
1028 ofpbuf_pull(packet, VLAN_HEADER_LEN);
1029 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1030 memcpy(packet->data, &tmp, sizeof tmp);
1035 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1037 struct eth_header *eh = packet->l2;
1038 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1042 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1044 struct eth_header *eh = packet->l2;
1045 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1049 is_ip(const struct ofpbuf *packet, const struct flow *key)
1051 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1055 dp_netdev_set_nw_addr(struct ofpbuf *packet, struct flow *key,
1056 const struct odp_action_nw_addr *a)
1058 if (is_ip(packet, key)) {
1059 struct ip_header *nh = packet->l3;
1062 field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1063 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1064 struct tcp_header *th = packet->l4;
1065 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1066 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1067 struct udp_header *uh = packet->l4;
1069 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1070 if (!uh->udp_csum) {
1071 uh->udp_csum = 0xffff;
1075 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1076 *field = a->nw_addr;
1081 dp_netdev_set_nw_tos(struct ofpbuf *packet, struct flow *key,
1082 const struct odp_action_nw_tos *a)
1084 if (is_ip(packet, key)) {
1085 struct ip_header *nh = packet->l3;
1086 uint8_t *field = &nh->ip_tos;
1088 /* Set the DSCP bits and preserve the ECN bits. */
1089 uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1091 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1092 htons((uint16_t) new));
1098 dp_netdev_set_tp_port(struct ofpbuf *packet, struct flow *key,
1099 const struct odp_action_tp_port *a)
1101 if (is_ip(packet, key)) {
1103 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1104 struct tcp_header *th = packet->l4;
1105 field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1106 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1107 *field = a->tp_port;
1108 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1109 struct udp_header *uh = packet->l4;
1110 field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1111 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1112 *field = a->tp_port;
1120 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1123 struct dp_netdev_port *p = dp->ports[out_port];
1125 netdev_send(p->netdev, packet);
1130 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1131 int queue_no, int port_no, uint32_t arg)
1133 struct odp_msg *header;
1137 if (dp->queue_len[queue_no] >= MAX_QUEUE_LEN) {
1142 msg_size = sizeof *header + packet->size;
1143 msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1144 header = ofpbuf_put_uninit(msg, sizeof *header);
1145 header->type = queue_no;
1146 header->length = msg_size;
1147 header->port = port_no;
1149 ofpbuf_put(msg, packet->data, packet->size);
1150 list_push_back(&dp->queues[queue_no], &msg->list_node);
1151 dp->queue_len[queue_no]++;
1156 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1157 * screwy or truncated header fields or one whose inner and outer Ethernet
1158 * address differ. */
1160 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1162 struct arp_eth_header *arp;
1163 struct eth_header *eth;
1166 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1170 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1171 if (l3_size < sizeof(struct arp_eth_header)) {
1177 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1178 || arp->ar_pro != htons(ARP_PRO_IP)
1179 || arp->ar_hln != ETH_HEADER_LEN
1181 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1185 dp_netdev_execute_actions(struct dp_netdev *dp,
1186 struct ofpbuf *packet, struct flow *key,
1187 const union odp_action *actions, int n_actions)
1190 for (i = 0; i < n_actions; i++) {
1191 const union odp_action *a = &actions[i];
1195 dp_netdev_output_port(dp, packet, a->output.port);
1198 case ODPAT_CONTROLLER:
1199 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1200 key->in_port, a->controller.arg);
1203 case ODPAT_SET_DL_TCI:
1204 dp_netdev_set_dl_tci(packet, a->dl_tci.tci);
1207 case ODPAT_STRIP_VLAN:
1208 dp_netdev_strip_vlan(packet);
1211 case ODPAT_SET_DL_SRC:
1212 dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1215 case ODPAT_SET_DL_DST:
1216 dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1219 case ODPAT_SET_NW_SRC:
1220 case ODPAT_SET_NW_DST:
1221 dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1224 case ODPAT_SET_NW_TOS:
1225 dp_netdev_set_nw_tos(packet, key, &a->nw_tos);
1228 case ODPAT_SET_TP_SRC:
1229 case ODPAT_SET_TP_DST:
1230 dp_netdev_set_tp_port(packet, key, &a->tp_port);
1233 case ODPAT_DROP_SPOOFED_ARP:
1234 if (dp_netdev_is_spoofed_arp(packet, key)) {
1242 const struct dpif_class dpif_netdev_class = {
1246 NULL, /* enumerate */
1249 NULL, /* get_all_names */
1250 dpif_netdev_destroy,
1251 dpif_netdev_get_stats,
1252 dpif_netdev_get_drop_frags,
1253 dpif_netdev_set_drop_frags,
1254 dpif_netdev_port_add,
1255 dpif_netdev_port_del,
1256 dpif_netdev_port_query_by_number,
1257 dpif_netdev_port_query_by_name,
1258 dpif_netdev_port_list,
1259 dpif_netdev_port_poll,
1260 dpif_netdev_port_poll_wait,
1261 dpif_netdev_flow_get,
1262 dpif_netdev_flow_put,
1263 dpif_netdev_flow_del,
1264 dpif_netdev_flow_flush,
1265 dpif_netdev_flow_list,
1266 dpif_netdev_execute,
1267 dpif_netdev_recv_get_mask,
1268 dpif_netdev_recv_set_mask,
1269 NULL, /* get_sflow_probability */
1270 NULL, /* set_sflow_probability */
1271 NULL, /* queue_to_priority */
1273 dpif_netdev_recv_wait,
1277 dpif_dummy_register(void)
1279 if (!dpif_dummy_class.type) {
1280 dpif_dummy_class = dpif_netdev_class;
1281 dpif_dummy_class.type = "dummy";
1282 dp_register_provider(&dpif_dummy_class);