2 * Copyright (c) 2009 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.
26 #include <linux/rtnetlink.h>
27 #include <linux/ethtool.h>
28 #include <linux/sockios.h>
29 #include <netinet/in.h>
32 #include <sys/ioctl.h>
34 #include <sys/sysmacros.h>
38 #include "dpif-provider.h"
44 #include "ofp-print.h"
47 #include "poll-loop.h"
53 #define THIS_MODULE VLM_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 { N_GROUPS = 16 }; /* Number of port groups. */
59 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
60 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
62 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
63 * headers to be aligned on a 4-byte boundary. */
64 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
66 /* Datapath based on the network device interface from netdev.h. */
73 bool drop_frags; /* Drop all IP fragments, if true. */
74 struct ovs_queue queues[N_QUEUES]; /* Messages queued for dpif_recv(). */
75 struct hmap flow_table; /* Flow table. */
76 struct odp_port_group groups[N_GROUPS];
79 long long int n_frags; /* Number of dropped IP fragments. */
80 long long int n_hit; /* Number of flow table matches. */
81 long long int n_missed; /* Number of flow table misses. */
82 long long int n_lost; /* Number of misses not passed to client. */
86 struct dp_netdev_port *ports[MAX_PORTS];
87 struct list port_list;
91 /* A port in a netdev-based datapath. */
92 struct dp_netdev_port {
93 int port_no; /* Index into dp_netdev's 'ports'. */
94 struct list node; /* Element in dp_netdev's 'port_list'. */
95 struct netdev *netdev;
96 bool internal; /* Internal port (as ODP_PORT_INTERNAL)? */
99 /* A flow in dp_netdev's 'flow_table'. */
100 struct dp_netdev_flow {
101 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
105 struct timeval used; /* Last used time, in milliseconds. */
106 long long int packet_count; /* Number of packets matched. */
107 long long int byte_count; /* Number of bytes matched. */
108 uint8_t ip_tos; /* IP TOS value. */
109 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
112 union odp_action *actions;
113 unsigned int n_actions;
116 /* Interface to netdev-based datapath. */
119 struct dp_netdev *dp;
121 unsigned int dp_serial;
124 /* All netdev-based datapaths. */
125 static struct dp_netdev *dp_netdevs[256];
126 struct list dp_netdev_list = LIST_INITIALIZER(&dp_netdev_list);
127 enum { N_DP_NETDEVS = ARRAY_SIZE(dp_netdevs) };
129 /* Maximum port MTU seen so far. */
130 static int max_mtu = ETH_PAYLOAD_MAX;
132 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
133 struct dp_netdev_port **portp);
134 static int get_port_by_name(struct dp_netdev *, const char *devname,
135 struct dp_netdev_port **portp);
136 static void dp_netdev_free(struct dp_netdev *);
137 static void dp_netdev_flow_flush(struct dp_netdev *);
138 static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags,
140 static int do_del_port(struct dp_netdev *, uint16_t port_no);
141 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
142 int queue_no, int port_no, uint32_t arg);
143 static int dp_netdev_execute_actions(struct dp_netdev *,
144 struct ofpbuf *, flow_t *,
145 const union odp_action *, int n);
147 static struct dpif_netdev *
148 dpif_netdev_cast(const struct dpif *dpif)
150 dpif_assert_class(dpif, &dpif_netdev_class);
151 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
154 static struct dp_netdev *
155 get_dp_netdev(const struct dpif *dpif)
157 return dpif_netdev_cast(dpif)->dp;
161 name_to_dp_idx(const char *name)
163 if (!strncmp(name, "dp", 2) && isdigit(name[2])) {
164 int dp_idx = atoi(name + 2);
165 if (dp_idx >= 0 && dp_idx < N_DP_NETDEVS) {
172 static struct dp_netdev *
173 find_dp_netdev(const char *name)
178 dp_idx = name_to_dp_idx(name);
180 return dp_netdevs[dp_idx];
183 for (i = 0; i < N_DP_NETDEVS; i++) {
184 struct dp_netdev *dp = dp_netdevs[i];
186 struct dp_netdev_port *port;
187 if (!get_port_by_name(dp, name, &port)) {
196 create_dpif_netdev(struct dp_netdev *dp)
198 struct dpif_netdev *dpif;
203 dpname = xasprintf("netdev:dp%d", dp->dp_idx);
204 dpif = xmalloc(sizeof *dpif);
205 dpif_init(&dpif->dpif, &dpif_netdev_class, dpname, dp->dp_idx, dp->dp_idx);
207 dpif->listen_mask = 0;
208 dpif->dp_serial = dp->serial;
215 create_dp_netdev(const char *name, int dp_idx, struct dpif **dpifp)
217 struct dp_netdev *dp;
221 if (dp_netdevs[dp_idx]) {
225 /* Create datapath. */
226 dp_netdevs[dp_idx] = dp = xcalloc(1, sizeof *dp);
227 list_push_back(&dp_netdev_list, &dp->node);
230 dp->drop_frags = false;
231 for (i = 0; i < N_QUEUES; i++) {
232 queue_init(&dp->queues[i]);
234 hmap_init(&dp->flow_table);
235 for (i = 0; i < N_GROUPS; i++) {
236 dp->groups[i].ports = NULL;
237 dp->groups[i].n_ports = 0;
238 dp->groups[i].group = i;
240 list_init(&dp->port_list);
241 error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL);
247 *dpifp = create_dpif_netdev(dp);
252 dpif_netdev_open(const char *name UNUSED, char *suffix, bool create,
256 if (find_dp_netdev(suffix)) {
259 int dp_idx = name_to_dp_idx(suffix);
261 return create_dp_netdev(suffix, dp_idx, dpifp);
263 /* Scan for unused dp_idx number. */
264 for (dp_idx = 0; dp_idx < N_DP_NETDEVS; dp_idx++) {
265 int error = create_dp_netdev(suffix, dp_idx, dpifp);
266 if (error != EBUSY) {
271 /* All datapath numbers in use. */
276 struct dp_netdev *dp = find_dp_netdev(suffix);
278 *dpifp = create_dpif_netdev(dp);
287 dp_netdev_free(struct dp_netdev *dp)
291 dp_netdev_flow_flush(dp);
292 while (dp->n_ports > 0) {
293 struct dp_netdev_port *port = CONTAINER_OF(
294 dp->port_list.next, struct dp_netdev_port, node);
295 do_del_port(dp, port->port_no);
297 for (i = 0; i < N_QUEUES; i++) {
298 queue_destroy(&dp->queues[i]);
300 hmap_destroy(&dp->flow_table);
301 for (i = 0; i < N_GROUPS; i++) {
302 free(dp->groups[i].ports);
304 dp_netdevs[dp->dp_idx] = NULL;
305 list_remove(&dp->node);
310 dpif_netdev_close(struct dpif *dpif)
312 struct dp_netdev *dp = get_dp_netdev(dpif);
313 assert(dp->open_cnt > 0);
314 if (--dp->open_cnt == 0 && dp->deleted) {
321 dpif_netdev_delete(struct dpif *dpif)
323 struct dp_netdev *dp = get_dp_netdev(dpif);
329 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
331 struct dp_netdev *dp = get_dp_netdev(dpif);
332 memset(stats, 0, sizeof *stats);
333 stats->n_flows = hmap_count(&dp->flow_table);
334 stats->cur_capacity = hmap_capacity(&dp->flow_table);
335 stats->max_capacity = MAX_FLOWS;
336 stats->n_ports = dp->n_ports;
337 stats->max_ports = MAX_PORTS;
338 stats->max_groups = N_GROUPS;
339 stats->n_frags = dp->n_frags;
340 stats->n_hit = dp->n_hit;
341 stats->n_missed = dp->n_missed;
342 stats->n_lost = dp->n_lost;
343 stats->max_miss_queue = MAX_QUEUE_LEN;
344 stats->max_action_queue = MAX_QUEUE_LEN;
349 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
351 struct dp_netdev *dp = get_dp_netdev(dpif);
352 *drop_fragsp = dp->drop_frags;
357 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
359 struct dp_netdev *dp = get_dp_netdev(dpif);
360 dp->drop_frags = drop_frags;
365 do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
368 bool internal = (flags & ODP_PORT_INTERNAL) != 0;
369 struct dp_netdev_port *port;
370 struct netdev *netdev;
374 /* XXX reject devices already in some dp_netdev. */
376 /* Open and validate network device. */
378 error = netdev_open(devname, NETDEV_ETH_TYPE_ANY, &netdev);
380 char *tapname = xasprintf("tap:%s", devname);
381 error = netdev_open(tapname, NETDEV_ETH_TYPE_ANY, &netdev);
387 /* XXX reject loopback devices */
388 /* XXX reject non-Ethernet devices */
390 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
392 netdev_close(netdev);
396 port = xmalloc(sizeof *port);
397 port->port_no = port_no;
398 port->netdev = netdev;
399 port->internal = internal;
401 netdev_get_mtu(netdev, &mtu);
406 list_push_back(&dp->port_list, &port->node);
407 dp->ports[port_no] = port;
415 dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
418 struct dp_netdev *dp = get_dp_netdev(dpif);
421 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
422 if (!dp->ports[port_no]) {
424 return do_add_port(dp, devname, flags, port_no);
431 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
433 struct dp_netdev *dp = get_dp_netdev(dpif);
434 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
438 is_valid_port_number(uint16_t port_no)
440 return port_no < MAX_PORTS;
444 get_port_by_number(struct dp_netdev *dp,
445 uint16_t port_no, struct dp_netdev_port **portp)
447 if (!is_valid_port_number(port_no)) {
451 *portp = dp->ports[port_no];
452 return *portp ? 0 : ENOENT;
457 get_port_by_name(struct dp_netdev *dp,
458 const char *devname, struct dp_netdev_port **portp)
460 struct dp_netdev_port *port;
462 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
463 if (!strcmp(netdev_get_name(port->netdev), devname)) {
472 do_del_port(struct dp_netdev *dp, uint16_t port_no)
474 struct dp_netdev_port *port;
477 error = get_port_by_number(dp, port_no, &port);
482 list_remove(&port->node);
483 dp->ports[port->port_no] = NULL;
487 netdev_close(port->netdev);
494 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
496 memset(odp_port, 0, sizeof *odp_port);
497 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
498 sizeof odp_port->devname);
499 odp_port->port = port->port_no;
500 odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
504 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
505 struct odp_port *odp_port)
507 struct dp_netdev *dp = get_dp_netdev(dpif);
508 struct dp_netdev_port *port;
511 error = get_port_by_number(dp, port_no, &port);
513 answer_port_query(port, odp_port);
519 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
520 struct odp_port *odp_port)
522 struct dp_netdev *dp = get_dp_netdev(dpif);
523 struct dp_netdev_port *port;
526 error = get_port_by_name(dp, devname, &port);
528 answer_port_query(port, odp_port);
534 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
536 hmap_remove(&dp->flow_table, &flow->node);
542 dp_netdev_flow_flush(struct dp_netdev *dp)
544 struct dp_netdev_flow *flow, *next;
546 HMAP_FOR_EACH_SAFE (flow, next, struct dp_netdev_flow, node,
548 dp_netdev_free_flow(dp, flow);
553 dpif_netdev_flow_flush(struct dpif *dpif)
555 struct dp_netdev *dp = get_dp_netdev(dpif);
556 dp_netdev_flow_flush(dp);
561 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
563 struct dp_netdev *dp = get_dp_netdev(dpif);
564 struct dp_netdev_port *port;
568 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
569 struct odp_port *odp_port = &ports[i];
573 answer_port_query(port, odp_port);
580 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep UNUSED)
582 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
583 if (dpif->dp_serial != dpif->dp->serial) {
584 dpif->dp_serial = dpif->dp->serial;
592 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
594 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
595 if (dpif->dp_serial != dpif->dp->serial) {
596 poll_immediate_wake();
601 get_port_group(const struct dpif *dpif, int group_no,
602 struct odp_port_group **groupp)
604 struct dp_netdev *dp = get_dp_netdev(dpif);
606 if (group_no >= 0 && group_no < N_GROUPS) {
607 *groupp = &dp->groups[group_no];
616 dpif_netdev_port_group_get(const struct dpif *dpif, int group_no,
617 uint16_t ports[], int n)
619 struct odp_port_group *group;
626 error = get_port_group(dpif, group_no, &group);
628 memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
629 return group->n_ports;
636 dpif_netdev_port_group_set(struct dpif *dpif, int group_no,
637 const uint16_t ports[], int n)
639 struct odp_port_group *group;
642 if (n < 0 || n > MAX_PORTS) {
646 error = get_port_group(dpif, group_no, &group);
649 group->ports = xmemdup(ports, n * sizeof *group->ports);
651 group->group = group_no;
656 static struct dp_netdev_flow *
657 dp_netdev_lookup_flow(const struct dp_netdev *dp, const flow_t *key)
659 struct dp_netdev_flow *flow;
661 assert(key->reserved == 0);
662 HMAP_FOR_EACH_WITH_HASH (flow, struct dp_netdev_flow, node,
663 flow_hash(key, 0), &dp->flow_table) {
664 if (flow_equal(&flow->key, key)) {
672 answer_flow_query(const struct dp_netdev_flow *flow,
673 struct odp_flow *odp_flow)
676 odp_flow->key = flow->key;
677 odp_flow->stats.n_packets = flow->packet_count;
678 odp_flow->stats.n_bytes = flow->byte_count;
679 odp_flow->stats.used_sec = flow->used.tv_sec;
680 odp_flow->stats.used_nsec = flow->used.tv_usec * 1000;
681 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
682 odp_flow->stats.ip_tos = flow->ip_tos;
683 odp_flow->stats.error = 0;
684 if (odp_flow->n_actions > 0) {
685 unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
686 memcpy(odp_flow->actions, flow->actions,
687 n * sizeof *odp_flow->actions);
688 odp_flow->n_actions = flow->n_actions;
691 odp_flow->stats.error = ENOENT;
696 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
698 struct dp_netdev *dp = get_dp_netdev(dpif);
701 for (i = 0; i < n; i++) {
702 struct odp_flow *odp_flow = &flows[i];
703 answer_flow_query(dp_netdev_lookup_flow(dp, &odp_flow->key), odp_flow);
709 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
715 for (i = 0; i < n_actions; i++) {
716 const union odp_action *a = &actions[i];
719 if (a->output.port >= MAX_PORTS) {
724 case ODPAT_OUTPUT_GROUP:
726 if (a->output_group.group >= N_GROUPS) {
731 case ODPAT_CONTROLLER:
734 case ODPAT_SET_VLAN_VID:
736 if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) {
741 case ODPAT_SET_VLAN_PCP:
743 if (a->vlan_pcp.vlan_pcp & ~VLAN_PCP_MASK) {
748 case ODPAT_STRIP_VLAN:
749 case ODPAT_SET_DL_SRC:
750 case ODPAT_SET_DL_DST:
751 case ODPAT_SET_NW_SRC:
752 case ODPAT_SET_NW_DST:
753 case ODPAT_SET_TP_SRC:
754 case ODPAT_SET_TP_DST:
766 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
772 if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
775 error = dpif_netdev_validate_actions(odp_flow->actions,
776 odp_flow->n_actions, &mutates);
781 n_bytes = odp_flow->n_actions * sizeof *flow->actions;
782 flow->actions = xrealloc(flow->actions, n_bytes);
783 flow->n_actions = odp_flow->n_actions;
784 memcpy(flow->actions, odp_flow->actions, n_bytes);
789 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
791 struct dp_netdev *dp = get_dp_netdev(dpif);
792 struct dp_netdev_flow *flow;
795 flow = xcalloc(1, sizeof *flow);
796 flow->key = odp_flow->key;
797 flow->key.reserved = 0;
799 error = set_flow_actions(flow, odp_flow);
805 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
810 clear_stats(struct dp_netdev_flow *flow)
812 flow->used.tv_sec = 0;
813 flow->used.tv_usec = 0;
814 flow->packet_count = 0;
815 flow->byte_count = 0;
821 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
823 struct dp_netdev *dp = get_dp_netdev(dpif);
824 struct dp_netdev_flow *flow;
826 flow = dp_netdev_lookup_flow(dp, &put->flow.key);
828 if (put->flags & ODPPF_CREATE) {
829 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
830 return add_flow(dpif, &put->flow);
838 if (put->flags & ODPPF_MODIFY) {
839 int error = set_flow_actions(flow, &put->flow);
840 if (!error && put->flags & ODPPF_ZERO_STATS) {
852 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
854 struct dp_netdev *dp = get_dp_netdev(dpif);
855 struct dp_netdev_flow *flow;
857 flow = dp_netdev_lookup_flow(dp, &odp_flow->key);
859 answer_flow_query(flow, odp_flow);
860 dp_netdev_free_flow(dp, flow);
868 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
870 struct dp_netdev *dp = get_dp_netdev(dpif);
871 struct dp_netdev_flow *flow;
875 HMAP_FOR_EACH (flow, struct dp_netdev_flow, node, &dp->flow_table) {
879 answer_flow_query(flow, &flows[i++]);
881 return hmap_count(&dp->flow_table);
885 dpif_netdev_execute(struct dpif *dpif, uint16_t in_port,
886 const union odp_action actions[], int n_actions,
887 const struct ofpbuf *packet)
889 struct dp_netdev *dp = get_dp_netdev(dpif);
895 if (packet->size < ETH_HLEN || packet->size > UINT16_MAX) {
899 error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
905 /* We need a deep copy of 'packet' since we're going to modify its
907 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
908 copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
909 ofpbuf_put(©, packet->data, packet->size);
911 /* We still need a shallow copy of 'packet', even though we won't
912 * modify its data, because flow_extract() modifies packet->l2, etc.
913 * We could probably get away with modifying those but it's more polite
917 flow_extract(©, in_port, &flow);
918 error = dp_netdev_execute_actions(dp, ©, &flow, actions, n_actions);
920 ofpbuf_uninit(©);
926 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
928 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
929 *listen_mask = dpif_netdev->listen_mask;
934 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
936 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
937 if (!(listen_mask & ~ODPL_ALL)) {
938 dpif_netdev->listen_mask = listen_mask;
945 static struct ovs_queue *
946 find_nonempty_queue(struct dpif *dpif)
948 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
949 struct dp_netdev *dp = get_dp_netdev(dpif);
950 int mask = dpif_netdev->listen_mask;
953 for (i = 0; i < N_QUEUES; i++) {
954 struct ovs_queue *q = &dp->queues[i];
955 if (q->n && mask & (1u << i)) {
963 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
965 struct ovs_queue *q = find_nonempty_queue(dpif);
967 *bufp = queue_pop_head(q);
975 dpif_netdev_recv_wait(struct dpif *dpif)
977 struct ovs_queue *q = find_nonempty_queue(dpif);
979 poll_immediate_wake();
981 /* No messages ready to be received, and dp_wait() will ensure that we
982 * wake up to queue new messages, so there is nothing to do. */
987 dp_netdev_flow_used(struct dp_netdev_flow *flow, const flow_t *key,
988 const struct ofpbuf *packet)
990 time_timeval(&flow->used);
991 flow->packet_count++;
992 flow->byte_count += packet->size;
993 if (key->dl_type == htons(ETH_P_IP)) {
994 struct ip_header *nh = packet->l3;
995 flow->ip_tos = nh->ip_tos;
997 if (key->nw_proto == IPPROTO_TCP) {
998 struct tcp_header *th = packet->l4;
999 flow->tcp_ctl |= th->tcp_ctl;
1005 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1006 struct ofpbuf *packet)
1008 struct dp_netdev_flow *flow;
1011 if (flow_extract(packet, port->port_no, &key) && dp->drop_frags) {
1016 flow = dp_netdev_lookup_flow(dp, &key);
1018 dp_netdev_flow_used(flow, &key, packet);
1019 dp_netdev_execute_actions(dp, packet, &key,
1020 flow->actions, flow->n_actions);
1024 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
1031 struct ofpbuf packet;
1032 struct dp_netdev *dp;
1034 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
1035 LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1036 struct dp_netdev_port *port;
1038 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1041 /* Reset packet contents. */
1042 packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
1045 error = netdev_recv(port->netdev, &packet);
1047 dp_netdev_port_input(dp, port, &packet);
1048 } else if (error != EAGAIN) {
1049 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1050 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1051 netdev_get_name(port->netdev), strerror(error));
1055 ofpbuf_uninit(&packet);
1059 dp_netdev_wait(void)
1061 struct dp_netdev *dp;
1063 LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1064 struct dp_netdev_port *port;
1065 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1066 netdev_recv_wait(port->netdev);
1072 dp_netdev_modify_vlan_tci(struct ofpbuf *packet, flow_t *key,
1073 uint16_t tci, uint16_t mask)
1075 struct vlan_eth_header *veh;
1077 if (key->dl_vlan != htons(ODP_VLAN_NONE)) {
1078 /* Modify 'mask' bits, but maintain other TCI bits. */
1080 veh->veth_tci &= ~htons(mask);
1081 veh->veth_tci |= htons(tci);
1083 /* Insert new 802.1Q header. */
1084 struct eth_header *eh = packet->l2;
1085 struct vlan_eth_header tmp;
1086 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1087 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1088 tmp.veth_type = htons(ETH_TYPE_VLAN);
1089 tmp.veth_tci = htons(tci);
1090 tmp.veth_next_type = eh->eth_type;
1092 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1093 memcpy(veh, &tmp, sizeof tmp);
1094 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1097 key->dl_vlan = veh->veth_tci & htons(VLAN_VID_MASK);
1101 dp_netdev_strip_vlan(struct ofpbuf *packet, flow_t *key)
1103 struct vlan_eth_header *veh = packet->l2;
1104 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1105 struct eth_header tmp;
1107 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1108 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1109 tmp.eth_type = veh->veth_next_type;
1111 packet->size -= VLAN_HEADER_LEN;
1112 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1113 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1114 memcpy(packet->data, &tmp, sizeof tmp);
1116 key->dl_vlan = htons(ODP_VLAN_NONE);
1121 dp_netdev_set_dl_src(struct ofpbuf *packet,
1122 const uint8_t dl_addr[ETH_ADDR_LEN])
1124 struct eth_header *eh = packet->l2;
1125 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1129 dp_netdev_set_dl_dst(struct ofpbuf *packet,
1130 const uint8_t dl_addr[ETH_ADDR_LEN])
1132 struct eth_header *eh = packet->l2;
1133 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1137 dp_netdev_set_nw_addr(struct ofpbuf *packet, flow_t *key,
1138 const struct odp_action_nw_addr *a)
1140 if (key->dl_type == htons(ETH_TYPE_IP)) {
1141 struct ip_header *nh = packet->l3;
1144 field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1145 if (key->nw_proto == IP_TYPE_TCP) {
1146 struct tcp_header *th = packet->l4;
1147 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1148 } else if (key->nw_proto == IP_TYPE_UDP) {
1149 struct udp_header *uh = packet->l4;
1151 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1152 if (!uh->udp_csum) {
1153 uh->udp_csum = 0xffff;
1157 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1158 *field = a->nw_addr;
1163 dp_netdev_set_tp_port(struct ofpbuf *packet, flow_t *key,
1164 const struct odp_action_tp_port *a)
1166 if (key->dl_type == htons(ETH_P_IP)) {
1168 if (key->nw_proto == IPPROTO_TCP) {
1169 struct tcp_header *th = packet->l4;
1170 field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1171 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1172 *field = a->tp_port;
1173 } else if (key->nw_proto == IPPROTO_UDP) {
1174 struct udp_header *uh = packet->l4;
1175 field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1176 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1177 *field = a->tp_port;
1183 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1186 struct dp_netdev_port *p = dp->ports[out_port];
1188 netdev_send(p->netdev, packet);
1193 dp_netdev_output_group(struct dp_netdev *dp, uint16_t group, uint16_t in_port,
1194 struct ofpbuf *packet)
1196 struct odp_port_group *g = &dp->groups[group];
1199 for (i = 0; i < g->n_ports; i++) {
1200 uint16_t out_port = g->ports[i];
1201 if (out_port != in_port) {
1202 dp_netdev_output_port(dp, packet, out_port);
1208 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1209 int queue_no, int port_no, uint32_t arg)
1211 struct ovs_queue *q = &dp->queues[queue_no];
1212 struct odp_msg *header;
1216 if (q->n >= MAX_QUEUE_LEN) {
1221 msg_size = sizeof *header + packet->size;
1222 msg = ofpbuf_new(msg_size);
1223 header = ofpbuf_put_uninit(msg, sizeof *header);
1224 header->type = queue_no;
1225 header->length = msg_size;
1226 header->port = port_no;
1228 ofpbuf_put(msg, packet->data, packet->size);
1229 queue_push_tail(q, msg);
1235 dp_netdev_execute_actions(struct dp_netdev *dp,
1236 struct ofpbuf *packet, flow_t *key,
1237 const union odp_action *actions, int n_actions)
1240 for (i = 0; i < n_actions; i++) {
1241 const union odp_action *a = &actions[i];
1245 dp_netdev_output_port(dp, packet, a->output.port);
1248 case ODPAT_OUTPUT_GROUP:
1249 dp_netdev_output_group(dp, a->output_group.group, key->in_port,
1253 case ODPAT_CONTROLLER:
1254 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1255 key->in_port, a->controller.arg);
1258 case ODPAT_SET_VLAN_VID:
1259 dp_netdev_modify_vlan_tci(packet, key, ntohs(a->vlan_vid.vlan_vid),
1263 case ODPAT_SET_VLAN_PCP:
1264 dp_netdev_modify_vlan_tci(packet, key, a->vlan_pcp.vlan_pcp << 13,
1268 case ODPAT_STRIP_VLAN:
1269 dp_netdev_strip_vlan(packet, key);
1272 case ODPAT_SET_DL_SRC:
1273 dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1276 case ODPAT_SET_DL_DST:
1277 dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1280 case ODPAT_SET_NW_SRC:
1281 case ODPAT_SET_NW_DST:
1282 dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1285 case ODPAT_SET_TP_SRC:
1286 case ODPAT_SET_TP_DST:
1287 dp_netdev_set_tp_port(packet, key, &a->tp_port);
1294 const struct dpif_class dpif_netdev_class = {
1299 NULL, /* enumerate */
1302 NULL, /* get_all_names */
1304 dpif_netdev_get_stats,
1305 dpif_netdev_get_drop_frags,
1306 dpif_netdev_set_drop_frags,
1307 dpif_netdev_port_add,
1308 dpif_netdev_port_del,
1309 dpif_netdev_port_query_by_number,
1310 dpif_netdev_port_query_by_name,
1311 dpif_netdev_port_list,
1312 dpif_netdev_port_poll,
1313 dpif_netdev_port_poll_wait,
1314 dpif_netdev_port_group_get,
1315 dpif_netdev_port_group_set,
1316 dpif_netdev_flow_get,
1317 dpif_netdev_flow_put,
1318 dpif_netdev_flow_del,
1319 dpif_netdev_flow_flush,
1320 dpif_netdev_flow_list,
1321 dpif_netdev_execute,
1322 dpif_netdev_recv_get_mask,
1323 dpif_netdev_recv_set_mask,
1325 dpif_netdev_recv_wait,