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"
50 VLOG_DEFINE_THIS_MODULE(dpif_netdev)
52 /* Configuration parameters. */
53 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
54 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
55 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
56 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
58 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
59 * headers to be aligned on a 4-byte boundary. */
60 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
62 /* 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 dp_netdev *dp_netdevs[256];
120 struct list dp_netdev_list = LIST_INITIALIZER(&dp_netdev_list);
121 enum { N_DP_NETDEVS = ARRAY_SIZE(dp_netdevs) };
123 /* Maximum port MTU seen so far. */
124 static int max_mtu = ETH_PAYLOAD_MAX;
126 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
127 struct dp_netdev_port **portp);
128 static int get_port_by_name(struct dp_netdev *, const char *devname,
129 struct dp_netdev_port **portp);
130 static void dp_netdev_free(struct dp_netdev *);
131 static void dp_netdev_flow_flush(struct dp_netdev *);
132 static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags,
134 static int do_del_port(struct dp_netdev *, uint16_t port_no);
135 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
136 int queue_no, int port_no, uint32_t arg);
137 static int dp_netdev_execute_actions(struct dp_netdev *,
138 struct ofpbuf *, struct flow *,
139 const union odp_action *, int n);
141 static struct dpif_netdev *
142 dpif_netdev_cast(const struct dpif *dpif)
144 dpif_assert_class(dpif, &dpif_netdev_class);
145 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
148 static struct dp_netdev *
149 get_dp_netdev(const struct dpif *dpif)
151 return dpif_netdev_cast(dpif)->dp;
155 name_to_dp_idx(const char *name)
157 if (!strncmp(name, "dp", 2) && isdigit((unsigned char)name[2])) {
158 int dp_idx = atoi(name + 2);
159 if (dp_idx >= 0 && dp_idx < N_DP_NETDEVS) {
166 static struct dp_netdev *
167 find_dp_netdev(const char *name)
172 dp_idx = name_to_dp_idx(name);
174 return dp_netdevs[dp_idx];
177 for (i = 0; i < N_DP_NETDEVS; i++) {
178 struct dp_netdev *dp = dp_netdevs[i];
180 struct dp_netdev_port *port;
181 if (!get_port_by_name(dp, name, &port)) {
190 create_dpif_netdev(struct dp_netdev *dp)
192 struct dpif_netdev *dpif;
197 dpname = xasprintf("dp%d", dp->dp_idx);
198 dpif = xmalloc(sizeof *dpif);
199 dpif_init(&dpif->dpif, &dpif_netdev_class, dpname, dp->dp_idx, dp->dp_idx);
201 dpif->listen_mask = 0;
202 dpif->dp_serial = dp->serial;
209 create_dp_netdev(const char *name, int dp_idx, struct dpif **dpifp)
211 struct dp_netdev *dp;
215 if (dp_netdevs[dp_idx]) {
219 /* Create datapath. */
220 dp_netdevs[dp_idx] = dp = xzalloc(sizeof *dp);
221 list_push_back(&dp_netdev_list, &dp->node);
224 dp->drop_frags = false;
225 for (i = 0; i < N_QUEUES; i++) {
226 queue_init(&dp->queues[i]);
228 hmap_init(&dp->flow_table);
229 list_init(&dp->port_list);
230 error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL);
236 *dpifp = create_dpif_netdev(dp);
241 dpif_netdev_open(const char *name, const char *type OVS_UNUSED, bool create,
245 if (find_dp_netdev(name)) {
248 int dp_idx = name_to_dp_idx(name);
250 return create_dp_netdev(name, dp_idx, dpifp);
252 /* Scan for unused dp_idx number. */
253 for (dp_idx = 0; dp_idx < N_DP_NETDEVS; dp_idx++) {
254 int error = create_dp_netdev(name, dp_idx, dpifp);
255 if (error != EBUSY) {
260 /* All datapath numbers in use. */
265 struct dp_netdev *dp = find_dp_netdev(name);
267 *dpifp = create_dpif_netdev(dp);
276 dp_netdev_free(struct dp_netdev *dp)
280 dp_netdev_flow_flush(dp);
281 while (dp->n_ports > 0) {
282 struct dp_netdev_port *port = CONTAINER_OF(
283 dp->port_list.next, struct dp_netdev_port, node);
284 do_del_port(dp, port->port_no);
286 for (i = 0; i < N_QUEUES; i++) {
287 queue_destroy(&dp->queues[i]);
289 hmap_destroy(&dp->flow_table);
290 dp_netdevs[dp->dp_idx] = NULL;
291 list_remove(&dp->node);
296 dpif_netdev_close(struct dpif *dpif)
298 struct dp_netdev *dp = get_dp_netdev(dpif);
299 assert(dp->open_cnt > 0);
300 if (--dp->open_cnt == 0 && dp->destroyed) {
307 dpif_netdev_destroy(struct dpif *dpif)
309 struct dp_netdev *dp = get_dp_netdev(dpif);
310 dp->destroyed = true;
315 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
317 struct dp_netdev *dp = get_dp_netdev(dpif);
318 memset(stats, 0, sizeof *stats);
319 stats->n_flows = hmap_count(&dp->flow_table);
320 stats->cur_capacity = hmap_capacity(&dp->flow_table);
321 stats->max_capacity = MAX_FLOWS;
322 stats->n_ports = dp->n_ports;
323 stats->max_ports = MAX_PORTS;
324 stats->n_frags = dp->n_frags;
325 stats->n_hit = dp->n_hit;
326 stats->n_missed = dp->n_missed;
327 stats->n_lost = dp->n_lost;
328 stats->max_miss_queue = MAX_QUEUE_LEN;
329 stats->max_action_queue = MAX_QUEUE_LEN;
334 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
336 struct dp_netdev *dp = get_dp_netdev(dpif);
337 *drop_fragsp = dp->drop_frags;
342 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
344 struct dp_netdev *dp = get_dp_netdev(dpif);
345 dp->drop_frags = drop_frags;
350 do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
353 bool internal = (flags & ODP_PORT_INTERNAL) != 0;
354 struct dp_netdev_port *port;
355 struct netdev_options netdev_options;
356 struct netdev *netdev;
360 /* XXX reject devices already in some dp_netdev. */
362 /* Open and validate network device. */
363 memset(&netdev_options, 0, sizeof netdev_options);
364 netdev_options.name = devname;
365 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
367 netdev_options.type = "tap";
370 error = netdev_open(&netdev_options, &netdev);
374 /* XXX reject loopback devices */
375 /* XXX reject non-Ethernet devices */
377 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
379 netdev_close(netdev);
383 port = xmalloc(sizeof *port);
384 port->port_no = port_no;
385 port->netdev = netdev;
386 port->internal = internal;
388 netdev_get_mtu(netdev, &mtu);
393 list_push_back(&dp->port_list, &port->node);
394 dp->ports[port_no] = port;
402 dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
405 struct dp_netdev *dp = get_dp_netdev(dpif);
408 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
409 if (!dp->ports[port_no]) {
411 return do_add_port(dp, devname, flags, port_no);
418 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
420 struct dp_netdev *dp = get_dp_netdev(dpif);
421 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
425 is_valid_port_number(uint16_t port_no)
427 return port_no < MAX_PORTS;
431 get_port_by_number(struct dp_netdev *dp,
432 uint16_t port_no, struct dp_netdev_port **portp)
434 if (!is_valid_port_number(port_no)) {
438 *portp = dp->ports[port_no];
439 return *portp ? 0 : ENOENT;
444 get_port_by_name(struct dp_netdev *dp,
445 const char *devname, struct dp_netdev_port **portp)
447 struct dp_netdev_port *port;
449 LIST_FOR_EACH (port, node, &dp->port_list) {
450 if (!strcmp(netdev_get_name(port->netdev), devname)) {
459 do_del_port(struct dp_netdev *dp, uint16_t port_no)
461 struct dp_netdev_port *port;
465 error = get_port_by_number(dp, port_no, &port);
470 list_remove(&port->node);
471 dp->ports[port->port_no] = NULL;
475 name = xstrdup(netdev_get_name(port->netdev));
476 netdev_close(port->netdev);
485 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
487 memset(odp_port, 0, sizeof *odp_port);
488 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
489 sizeof odp_port->devname);
490 odp_port->port = port->port_no;
491 odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
495 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
496 struct odp_port *odp_port)
498 struct dp_netdev *dp = get_dp_netdev(dpif);
499 struct dp_netdev_port *port;
502 error = get_port_by_number(dp, port_no, &port);
504 answer_port_query(port, odp_port);
510 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
511 struct odp_port *odp_port)
513 struct dp_netdev *dp = get_dp_netdev(dpif);
514 struct dp_netdev_port *port;
517 error = get_port_by_name(dp, devname, &port);
519 answer_port_query(port, odp_port);
525 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
527 hmap_remove(&dp->flow_table, &flow->node);
533 dp_netdev_flow_flush(struct dp_netdev *dp)
535 struct dp_netdev_flow *flow, *next;
537 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
538 dp_netdev_free_flow(dp, flow);
543 dpif_netdev_flow_flush(struct dpif *dpif)
545 struct dp_netdev *dp = get_dp_netdev(dpif);
546 dp_netdev_flow_flush(dp);
551 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
553 struct dp_netdev *dp = get_dp_netdev(dpif);
554 struct dp_netdev_port *port;
558 LIST_FOR_EACH (port, node, &dp->port_list) {
559 struct odp_port *odp_port = &ports[i];
563 answer_port_query(port, odp_port);
570 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
572 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
573 if (dpif->dp_serial != dpif->dp->serial) {
574 dpif->dp_serial = dpif->dp->serial;
582 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
584 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
585 if (dpif->dp_serial != dpif->dp->serial) {
586 poll_immediate_wake();
590 static struct dp_netdev_flow *
591 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
593 struct dp_netdev_flow *flow;
595 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
596 if (flow_equal(&flow->key, key)) {
603 /* The caller must fill in odp_flow->key itself. */
605 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
606 struct odp_flow *odp_flow)
609 odp_flow->stats.n_packets = flow->packet_count;
610 odp_flow->stats.n_bytes = flow->byte_count;
611 odp_flow->stats.used_sec = flow->used.tv_sec;
612 odp_flow->stats.used_nsec = flow->used.tv_nsec;
613 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
614 odp_flow->stats.reserved = 0;
615 odp_flow->stats.error = 0;
616 if (odp_flow->n_actions > 0) {
617 unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
618 memcpy(odp_flow->actions, flow->actions,
619 n * sizeof *odp_flow->actions);
620 odp_flow->n_actions = flow->n_actions;
623 if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
628 odp_flow->stats.error = ENOENT;
633 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
635 struct dp_netdev *dp = get_dp_netdev(dpif);
638 for (i = 0; i < n; i++) {
639 struct odp_flow *odp_flow = &flows[i];
642 odp_flow_key_to_flow(&odp_flow->key, &key);
643 answer_flow_query(dp_netdev_lookup_flow(dp, &key),
644 odp_flow->flags, odp_flow);
650 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
656 for (i = 0; i < n_actions; i++) {
657 const union odp_action *a = &actions[i];
660 if (a->output.port >= MAX_PORTS) {
665 case ODPAT_CONTROLLER:
668 case ODPAT_SET_DL_TCI:
670 if (a->dl_tci.mask != htons(VLAN_VID_MASK)
671 && a->dl_tci.mask != htons(VLAN_PCP_MASK)
672 && a->dl_tci.mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK)) {
675 if (a->dl_tci.tci & ~a->dl_tci.mask){
680 case ODPAT_SET_NW_TOS:
682 if (a->nw_tos.nw_tos & IP_ECN_MASK) {
687 case ODPAT_STRIP_VLAN:
688 case ODPAT_SET_DL_SRC:
689 case ODPAT_SET_DL_DST:
690 case ODPAT_SET_NW_SRC:
691 case ODPAT_SET_NW_DST:
692 case ODPAT_SET_TP_SRC:
693 case ODPAT_SET_TP_DST:
705 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
711 if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
714 error = dpif_netdev_validate_actions(odp_flow->actions,
715 odp_flow->n_actions, &mutates);
720 n_bytes = odp_flow->n_actions * sizeof *flow->actions;
721 flow->actions = xrealloc(flow->actions, n_bytes);
722 flow->n_actions = odp_flow->n_actions;
723 memcpy(flow->actions, odp_flow->actions, n_bytes);
728 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
730 struct dp_netdev *dp = get_dp_netdev(dpif);
731 struct dp_netdev_flow *flow;
734 flow = xzalloc(sizeof *flow);
735 odp_flow_key_to_flow(&odp_flow->key, &flow->key);
737 error = set_flow_actions(flow, odp_flow);
743 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
748 clear_stats(struct dp_netdev_flow *flow)
750 flow->used.tv_sec = 0;
751 flow->used.tv_nsec = 0;
752 flow->packet_count = 0;
753 flow->byte_count = 0;
758 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
760 struct dp_netdev *dp = get_dp_netdev(dpif);
761 struct dp_netdev_flow *flow;
764 odp_flow_key_to_flow(&put->flow.key, &key);
765 flow = dp_netdev_lookup_flow(dp, &key);
767 if (put->flags & ODPPF_CREATE) {
768 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
769 return add_flow(dpif, &put->flow);
777 if (put->flags & ODPPF_MODIFY) {
778 int error = set_flow_actions(flow, &put->flow);
779 if (!error && put->flags & ODPPF_ZERO_STATS) {
791 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
793 struct dp_netdev *dp = get_dp_netdev(dpif);
794 struct dp_netdev_flow *flow;
797 odp_flow_key_to_flow(&odp_flow->key, &key);
798 flow = dp_netdev_lookup_flow(dp, &key);
800 answer_flow_query(flow, 0, odp_flow);
801 dp_netdev_free_flow(dp, flow);
809 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
811 struct dp_netdev *dp = get_dp_netdev(dpif);
812 struct dp_netdev_flow *flow;
816 HMAP_FOR_EACH (flow, node, &dp->flow_table) {
821 odp_flow_key_from_flow(&flows[i].key, &flow->key);
822 answer_flow_query(flow, 0, &flows[i]);
825 return hmap_count(&dp->flow_table);
829 dpif_netdev_execute(struct dpif *dpif,
830 const union odp_action actions[], int n_actions,
831 const struct ofpbuf *packet)
833 struct dp_netdev *dp = get_dp_netdev(dpif);
839 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
843 error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
849 /* We need a deep copy of 'packet' since we're going to modify its
851 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
852 copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
853 ofpbuf_put(©, packet->data, packet->size);
855 /* We still need a shallow copy of 'packet', even though we won't
856 * modify its data, because flow_extract() modifies packet->l2, etc.
857 * We could probably get away with modifying those but it's more polite
861 flow_extract(©, 0, -1, &key);
862 error = dp_netdev_execute_actions(dp, ©, &key, actions, n_actions);
864 ofpbuf_uninit(©);
870 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
872 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
873 *listen_mask = dpif_netdev->listen_mask;
878 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
880 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
881 if (!(listen_mask & ~ODPL_ALL)) {
882 dpif_netdev->listen_mask = listen_mask;
889 static struct ovs_queue *
890 find_nonempty_queue(struct dpif *dpif)
892 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
893 struct dp_netdev *dp = get_dp_netdev(dpif);
894 int mask = dpif_netdev->listen_mask;
897 for (i = 0; i < N_QUEUES; i++) {
898 struct ovs_queue *q = &dp->queues[i];
899 if (q->n && mask & (1u << i)) {
907 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
909 struct ovs_queue *q = find_nonempty_queue(dpif);
911 *bufp = queue_pop_head(q);
919 dpif_netdev_recv_wait(struct dpif *dpif)
921 struct ovs_queue *q = find_nonempty_queue(dpif);
923 poll_immediate_wake();
925 /* No messages ready to be received, and dp_wait() will ensure that we
926 * wake up to queue new messages, so there is nothing to do. */
931 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
932 const struct ofpbuf *packet)
934 time_timespec(&flow->used);
935 flow->packet_count++;
936 flow->byte_count += packet->size;
937 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
938 struct tcp_header *th = packet->l4;
939 flow->tcp_ctl |= th->tcp_ctl;
944 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
945 struct ofpbuf *packet)
947 struct dp_netdev_flow *flow;
950 if (packet->size < ETH_HEADER_LEN) {
953 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
958 flow = dp_netdev_lookup_flow(dp, &key);
960 dp_netdev_flow_used(flow, &key, packet);
961 dp_netdev_execute_actions(dp, packet, &key,
962 flow->actions, flow->n_actions);
966 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
973 struct ofpbuf packet;
974 struct dp_netdev *dp;
976 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
977 LIST_FOR_EACH (dp, node, &dp_netdev_list) {
978 struct dp_netdev_port *port;
980 LIST_FOR_EACH (port, node, &dp->port_list) {
983 /* Reset packet contents. */
984 packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
987 error = netdev_recv(port->netdev, &packet);
989 dp_netdev_port_input(dp, port, &packet);
990 } else if (error != EAGAIN) {
991 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
992 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
993 netdev_get_name(port->netdev), strerror(error));
997 ofpbuf_uninit(&packet);
1001 dp_netdev_wait(void)
1003 struct dp_netdev *dp;
1005 LIST_FOR_EACH (dp, node, &dp_netdev_list) {
1006 struct dp_netdev_port *port;
1007 LIST_FOR_EACH (port, node, &dp->port_list) {
1008 netdev_recv_wait(port->netdev);
1014 /* Modify the TCI field of 'packet'. If a VLAN tag is not present, one is
1015 * added with the TCI field set to 'a->tci'. If a VLAN tag is present, then
1016 * 'a->mask' bits are cleared before 'a->tci' is logically OR'd into the TCI
1019 * Note that the function does not ensure that 'a->tci' does not affect bits
1020 * outside of 'a->mask'.
1023 dp_netdev_set_dl_tci(struct ofpbuf *packet, const struct odp_action_dl_tci *a)
1025 struct vlan_eth_header *veh;
1026 struct eth_header *eh;
1029 if (packet->size >= sizeof(struct vlan_eth_header)
1030 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1031 /* Clear 'mask' bits, but maintain other TCI bits. */
1033 veh->veth_tci = (veh->veth_tci & ~a->mask) | a->tci;
1035 /* Insert new 802.1Q header. */
1036 struct vlan_eth_header tmp;
1037 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1038 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1039 tmp.veth_type = htons(ETH_TYPE_VLAN);
1040 tmp.veth_tci = htons(a->tci);
1041 tmp.veth_next_type = eh->eth_type;
1043 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1044 memcpy(veh, &tmp, sizeof tmp);
1045 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1050 dp_netdev_strip_vlan(struct ofpbuf *packet)
1052 struct vlan_eth_header *veh = packet->l2;
1053 if (packet->size >= sizeof *veh
1054 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1055 struct eth_header tmp;
1057 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1058 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1059 tmp.eth_type = veh->veth_next_type;
1061 packet->size -= VLAN_HEADER_LEN;
1062 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1063 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1064 memcpy(packet->data, &tmp, sizeof tmp);
1069 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1071 struct eth_header *eh = packet->l2;
1072 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1076 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1078 struct eth_header *eh = packet->l2;
1079 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1083 is_ip(const struct ofpbuf *packet, const struct flow *key)
1085 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1089 dp_netdev_set_nw_addr(struct ofpbuf *packet, struct flow *key,
1090 const struct odp_action_nw_addr *a)
1092 if (is_ip(packet, key)) {
1093 struct ip_header *nh = packet->l3;
1096 field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1097 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1098 struct tcp_header *th = packet->l4;
1099 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1100 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1101 struct udp_header *uh = packet->l4;
1103 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1104 if (!uh->udp_csum) {
1105 uh->udp_csum = 0xffff;
1109 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1110 *field = a->nw_addr;
1115 dp_netdev_set_nw_tos(struct ofpbuf *packet, struct flow *key,
1116 const struct odp_action_nw_tos *a)
1118 if (is_ip(packet, key)) {
1119 struct ip_header *nh = packet->l3;
1120 uint8_t *field = &nh->ip_tos;
1122 /* Set the DSCP bits and preserve the ECN bits. */
1123 uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1125 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1126 htons((uint16_t)a->nw_tos));
1132 dp_netdev_set_tp_port(struct ofpbuf *packet, struct flow *key,
1133 const struct odp_action_tp_port *a)
1135 if (is_ip(packet, key)) {
1137 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1138 struct tcp_header *th = packet->l4;
1139 field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1140 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1141 *field = a->tp_port;
1142 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1143 struct udp_header *uh = packet->l4;
1144 field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1145 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1146 *field = a->tp_port;
1154 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1157 struct dp_netdev_port *p = dp->ports[out_port];
1159 netdev_send(p->netdev, packet);
1164 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1165 int queue_no, int port_no, uint32_t arg)
1167 struct ovs_queue *q = &dp->queues[queue_no];
1168 struct odp_msg *header;
1172 if (q->n >= MAX_QUEUE_LEN) {
1177 msg_size = sizeof *header + packet->size;
1178 msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1179 header = ofpbuf_put_uninit(msg, sizeof *header);
1180 header->type = queue_no;
1181 header->length = msg_size;
1182 header->port = port_no;
1184 ofpbuf_put(msg, packet->data, packet->size);
1185 queue_push_tail(q, msg);
1190 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1191 * screwy or truncated header fields or one whose inner and outer Ethernet
1192 * address differ. */
1194 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1196 struct arp_eth_header *arp;
1197 struct eth_header *eth;
1200 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1204 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1205 if (l3_size < sizeof(struct arp_eth_header)) {
1211 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1212 || arp->ar_pro != htons(ARP_PRO_IP)
1213 || arp->ar_hln != ETH_HEADER_LEN
1215 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1219 dp_netdev_execute_actions(struct dp_netdev *dp,
1220 struct ofpbuf *packet, struct flow *key,
1221 const union odp_action *actions, int n_actions)
1224 for (i = 0; i < n_actions; i++) {
1225 const union odp_action *a = &actions[i];
1229 dp_netdev_output_port(dp, packet, a->output.port);
1232 case ODPAT_CONTROLLER:
1233 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1234 key->in_port, a->controller.arg);
1237 case ODPAT_SET_DL_TCI:
1238 dp_netdev_set_dl_tci(packet, &a->dl_tci);
1241 case ODPAT_STRIP_VLAN:
1242 dp_netdev_strip_vlan(packet);
1245 case ODPAT_SET_DL_SRC:
1246 dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1249 case ODPAT_SET_DL_DST:
1250 dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1253 case ODPAT_SET_NW_SRC:
1254 case ODPAT_SET_NW_DST:
1255 dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1258 case ODPAT_SET_NW_TOS:
1259 dp_netdev_set_nw_tos(packet, key, &a->nw_tos);
1262 case ODPAT_SET_TP_SRC:
1263 case ODPAT_SET_TP_DST:
1264 dp_netdev_set_tp_port(packet, key, &a->tp_port);
1267 case ODPAT_DROP_SPOOFED_ARP:
1268 if (dp_netdev_is_spoofed_arp(packet, key)) {
1276 const struct dpif_class dpif_netdev_class = {
1280 NULL, /* enumerate */
1283 NULL, /* get_all_names */
1284 dpif_netdev_destroy,
1285 dpif_netdev_get_stats,
1286 dpif_netdev_get_drop_frags,
1287 dpif_netdev_set_drop_frags,
1288 dpif_netdev_port_add,
1289 dpif_netdev_port_del,
1290 dpif_netdev_port_query_by_number,
1291 dpif_netdev_port_query_by_name,
1292 dpif_netdev_port_list,
1293 dpif_netdev_port_poll,
1294 dpif_netdev_port_poll_wait,
1295 dpif_netdev_flow_get,
1296 dpif_netdev_flow_put,
1297 dpif_netdev_flow_del,
1298 dpif_netdev_flow_flush,
1299 dpif_netdev_flow_list,
1300 dpif_netdev_execute,
1301 dpif_netdev_recv_get_mask,
1302 dpif_netdev_recv_set_mask,
1303 NULL, /* get_sflow_probability */
1304 NULL, /* set_sflow_probability */
1305 NULL, /* queue_to_priority */
1307 dpif_netdev_recv_wait,