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 error = netdev_open_tap(devname, &netdev);
385 /* XXX reject loopback devices */
386 /* XXX reject non-Ethernet devices */
388 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
390 netdev_close(netdev);
394 port = xmalloc(sizeof *port);
395 port->port_no = port_no;
396 port->netdev = netdev;
397 port->internal = internal;
399 mtu = netdev_get_mtu(netdev);
404 list_push_back(&dp->port_list, &port->node);
405 dp->ports[port_no] = port;
413 dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
416 struct dp_netdev *dp = get_dp_netdev(dpif);
419 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
420 if (!dp->ports[port_no]) {
422 return do_add_port(dp, devname, flags, port_no);
429 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
431 struct dp_netdev *dp = get_dp_netdev(dpif);
432 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
436 is_valid_port_number(uint16_t port_no)
438 return port_no < MAX_PORTS;
442 get_port_by_number(struct dp_netdev *dp,
443 uint16_t port_no, struct dp_netdev_port **portp)
445 if (!is_valid_port_number(port_no)) {
449 *portp = dp->ports[port_no];
450 return *portp ? 0 : ENOENT;
455 get_port_by_name(struct dp_netdev *dp,
456 const char *devname, struct dp_netdev_port **portp)
458 struct dp_netdev_port *port;
460 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
461 if (!strcmp(netdev_get_name(port->netdev), devname)) {
470 do_del_port(struct dp_netdev *dp, uint16_t port_no)
472 struct dp_netdev_port *port;
475 error = get_port_by_number(dp, port_no, &port);
480 list_remove(&port->node);
481 dp->ports[port->port_no] = NULL;
485 netdev_close(port->netdev);
492 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
494 memset(odp_port, 0, sizeof *odp_port);
495 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
496 sizeof odp_port->devname);
497 odp_port->port = port->port_no;
498 odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
502 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
503 struct odp_port *odp_port)
505 struct dp_netdev *dp = get_dp_netdev(dpif);
506 struct dp_netdev_port *port;
509 error = get_port_by_number(dp, port_no, &port);
511 answer_port_query(port, odp_port);
517 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
518 struct odp_port *odp_port)
520 struct dp_netdev *dp = get_dp_netdev(dpif);
521 struct dp_netdev_port *port;
524 error = get_port_by_name(dp, devname, &port);
526 answer_port_query(port, odp_port);
532 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
534 hmap_remove(&dp->flow_table, &flow->node);
540 dp_netdev_flow_flush(struct dp_netdev *dp)
542 struct dp_netdev_flow *flow, *next;
544 HMAP_FOR_EACH_SAFE (flow, next, struct dp_netdev_flow, node,
546 dp_netdev_free_flow(dp, flow);
551 dpif_netdev_flow_flush(struct dpif *dpif)
553 struct dp_netdev *dp = get_dp_netdev(dpif);
554 dp_netdev_flow_flush(dp);
559 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
561 struct dp_netdev *dp = get_dp_netdev(dpif);
562 struct dp_netdev_port *port;
566 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
567 struct odp_port *odp_port = &ports[i];
571 answer_port_query(port, odp_port);
578 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep UNUSED)
580 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
581 if (dpif->dp_serial != dpif->dp->serial) {
582 dpif->dp_serial = dpif->dp->serial;
590 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
592 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
593 if (dpif->dp_serial != dpif->dp->serial) {
594 poll_immediate_wake();
599 get_port_group(const struct dpif *dpif, int group_no,
600 struct odp_port_group **groupp)
602 struct dp_netdev *dp = get_dp_netdev(dpif);
604 if (group_no >= 0 && group_no < N_GROUPS) {
605 *groupp = &dp->groups[group_no];
614 dpif_netdev_port_group_get(const struct dpif *dpif, int group_no,
615 uint16_t ports[], int n)
617 struct odp_port_group *group;
624 error = get_port_group(dpif, group_no, &group);
626 memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
627 return group->n_ports;
634 dpif_netdev_port_group_set(struct dpif *dpif, int group_no,
635 const uint16_t ports[], int n)
637 struct odp_port_group *group;
640 if (n < 0 || n > MAX_PORTS) {
644 error = get_port_group(dpif, group_no, &group);
647 group->ports = xmemdup(ports, n * sizeof *group->ports);
649 group->group = group_no;
654 static struct dp_netdev_flow *
655 dp_netdev_lookup_flow(const struct dp_netdev *dp, const flow_t *key)
657 struct dp_netdev_flow *flow;
659 assert(key->reserved == 0);
660 HMAP_FOR_EACH_WITH_HASH (flow, struct dp_netdev_flow, node,
661 flow_hash(key, 0), &dp->flow_table) {
662 if (flow_equal(&flow->key, key)) {
670 answer_flow_query(const struct dp_netdev_flow *flow,
671 struct odp_flow *odp_flow)
674 odp_flow->key = flow->key;
675 odp_flow->stats.n_packets = flow->packet_count;
676 odp_flow->stats.n_bytes = flow->byte_count;
677 odp_flow->stats.used_sec = flow->used.tv_sec;
678 odp_flow->stats.used_nsec = flow->used.tv_usec * 1000;
679 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
680 odp_flow->stats.ip_tos = flow->ip_tos;
681 odp_flow->stats.error = 0;
682 if (odp_flow->n_actions > 0) {
683 unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
684 memcpy(odp_flow->actions, flow->actions,
685 n * sizeof *odp_flow->actions);
686 odp_flow->n_actions = flow->n_actions;
689 odp_flow->stats.error = ENOENT;
694 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
696 struct dp_netdev *dp = get_dp_netdev(dpif);
699 for (i = 0; i < n; i++) {
700 struct odp_flow *odp_flow = &flows[i];
701 answer_flow_query(dp_netdev_lookup_flow(dp, &odp_flow->key), odp_flow);
707 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
713 for (i = 0; i < n_actions; i++) {
714 const union odp_action *a = &actions[i];
717 if (a->output.port >= MAX_PORTS) {
722 case ODPAT_OUTPUT_GROUP:
724 if (a->output_group.group >= N_GROUPS) {
729 case ODPAT_CONTROLLER:
732 case ODPAT_SET_VLAN_VID:
734 if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) {
739 case ODPAT_SET_VLAN_PCP:
741 if (a->vlan_pcp.vlan_pcp & ~VLAN_PCP_MASK) {
746 case ODPAT_STRIP_VLAN:
747 case ODPAT_SET_DL_SRC:
748 case ODPAT_SET_DL_DST:
749 case ODPAT_SET_NW_SRC:
750 case ODPAT_SET_NW_DST:
751 case ODPAT_SET_TP_SRC:
752 case ODPAT_SET_TP_DST:
764 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
770 if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
773 error = dpif_netdev_validate_actions(odp_flow->actions,
774 odp_flow->n_actions, &mutates);
779 n_bytes = odp_flow->n_actions * sizeof *flow->actions;
780 flow->actions = xrealloc(flow->actions, n_bytes);
781 flow->n_actions = odp_flow->n_actions;
782 memcpy(flow->actions, odp_flow->actions, n_bytes);
787 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
789 struct dp_netdev *dp = get_dp_netdev(dpif);
790 struct dp_netdev_flow *flow;
793 flow = xcalloc(1, sizeof *flow);
794 flow->key = odp_flow->key;
795 flow->key.reserved = 0;
797 error = set_flow_actions(flow, odp_flow);
803 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
808 clear_stats(struct dp_netdev_flow *flow)
810 flow->used.tv_sec = 0;
811 flow->used.tv_usec = 0;
812 flow->packet_count = 0;
813 flow->byte_count = 0;
819 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
821 struct dp_netdev *dp = get_dp_netdev(dpif);
822 struct dp_netdev_flow *flow;
824 flow = dp_netdev_lookup_flow(dp, &put->flow.key);
826 if (put->flags & ODPPF_CREATE) {
827 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
828 return add_flow(dpif, &put->flow);
836 if (put->flags & ODPPF_MODIFY) {
837 int error = set_flow_actions(flow, &put->flow);
838 if (!error && put->flags & ODPPF_ZERO_STATS) {
850 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
852 struct dp_netdev *dp = get_dp_netdev(dpif);
853 struct dp_netdev_flow *flow;
855 flow = dp_netdev_lookup_flow(dp, &odp_flow->key);
857 answer_flow_query(flow, odp_flow);
858 dp_netdev_free_flow(dp, flow);
866 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
868 struct dp_netdev *dp = get_dp_netdev(dpif);
869 struct dp_netdev_flow *flow;
873 HMAP_FOR_EACH (flow, struct dp_netdev_flow, node, &dp->flow_table) {
877 answer_flow_query(flow, &flows[i++]);
879 return hmap_count(&dp->flow_table);
883 dpif_netdev_execute(struct dpif *dpif, uint16_t in_port,
884 const union odp_action actions[], int n_actions,
885 const struct ofpbuf *packet)
887 struct dp_netdev *dp = get_dp_netdev(dpif);
893 if (packet->size < ETH_HLEN || packet->size > UINT16_MAX) {
897 error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
903 /* We need a deep copy of 'packet' since we're going to modify its
905 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
906 copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
907 ofpbuf_put(©, packet->data, packet->size);
909 /* We still need a shallow copy of 'packet', even though we won't
910 * modify its data, because flow_extract() modifies packet->l2, etc.
911 * We could probably get away with modifying those but it's more polite
915 flow_extract(©, in_port, &flow);
916 error = dp_netdev_execute_actions(dp, ©, &flow, actions, n_actions);
918 ofpbuf_uninit(©);
924 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
926 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
927 *listen_mask = dpif_netdev->listen_mask;
932 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
934 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
935 if (!(listen_mask & ~ODPL_ALL)) {
936 dpif_netdev->listen_mask = listen_mask;
943 static struct ovs_queue *
944 find_nonempty_queue(struct dpif *dpif)
946 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
947 struct dp_netdev *dp = get_dp_netdev(dpif);
948 int mask = dpif_netdev->listen_mask;
951 for (i = 0; i < N_QUEUES; i++) {
952 struct ovs_queue *q = &dp->queues[i];
953 if (q->n && mask & (1u << i)) {
961 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
963 struct ovs_queue *q = find_nonempty_queue(dpif);
965 *bufp = queue_pop_head(q);
973 dpif_netdev_recv_wait(struct dpif *dpif)
975 struct ovs_queue *q = find_nonempty_queue(dpif);
977 poll_immediate_wake();
979 /* No messages ready to be received, and dp_wait() will ensure that we
980 * wake up to queue new messages, so there is nothing to do. */
985 dp_netdev_flow_used(struct dp_netdev_flow *flow, const flow_t *key,
986 const struct ofpbuf *packet)
988 time_timeval(&flow->used);
989 flow->packet_count++;
990 flow->byte_count += packet->size;
991 if (key->dl_type == htons(ETH_P_IP)) {
992 struct ip_header *nh = packet->l3;
993 flow->ip_tos = nh->ip_tos;
995 if (key->nw_proto == IPPROTO_TCP) {
996 struct tcp_header *th = packet->l4;
997 flow->tcp_ctl |= th->tcp_ctl;
1003 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1004 struct ofpbuf *packet)
1006 struct dp_netdev_flow *flow;
1009 if (flow_extract(packet, port->port_no, &key) && dp->drop_frags) {
1014 flow = dp_netdev_lookup_flow(dp, &key);
1016 dp_netdev_flow_used(flow, &key, packet);
1017 dp_netdev_execute_actions(dp, packet, &key,
1018 flow->actions, flow->n_actions);
1022 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
1029 struct ofpbuf packet;
1030 struct dp_netdev *dp;
1032 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
1033 LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1034 struct dp_netdev_port *port;
1036 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1039 /* Reset packet contents. */
1040 packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
1043 error = netdev_recv(port->netdev, &packet);
1045 dp_netdev_port_input(dp, port, &packet);
1046 } else if (error != EAGAIN) {
1047 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1048 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1049 netdev_get_name(port->netdev), strerror(error));
1053 ofpbuf_uninit(&packet);
1057 dp_netdev_wait(void)
1059 struct dp_netdev *dp;
1061 LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1062 struct dp_netdev_port *port;
1063 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1064 netdev_recv_wait(port->netdev);
1070 dp_netdev_modify_vlan_tci(struct ofpbuf *packet, flow_t *key,
1071 uint16_t tci, uint16_t mask)
1073 struct vlan_eth_header *veh;
1075 if (key->dl_vlan != htons(ODP_VLAN_NONE)) {
1076 /* Modify 'mask' bits, but maintain other TCI bits. */
1078 veh->veth_tci &= ~htons(mask);
1079 veh->veth_tci |= htons(tci);
1081 /* Insert new 802.1Q header. */
1082 struct eth_header *eh = packet->l2;
1083 struct vlan_eth_header tmp;
1084 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1085 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1086 tmp.veth_type = htons(ETH_TYPE_VLAN);
1087 tmp.veth_tci = htons(tci);
1088 tmp.veth_next_type = eh->eth_type;
1090 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1091 memcpy(veh, &tmp, sizeof tmp);
1092 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1095 key->dl_vlan = veh->veth_tci & htons(VLAN_VID_MASK);
1099 dp_netdev_strip_vlan(struct ofpbuf *packet, flow_t *key)
1101 struct vlan_eth_header *veh = packet->l2;
1102 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1103 struct eth_header tmp;
1105 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1106 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1107 tmp.eth_type = veh->veth_next_type;
1109 packet->size -= VLAN_HEADER_LEN;
1110 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1111 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1112 memcpy(packet->data, &tmp, sizeof tmp);
1114 key->dl_vlan = htons(ODP_VLAN_NONE);
1119 dp_netdev_set_dl_src(struct ofpbuf *packet,
1120 const uint8_t dl_addr[ETH_ADDR_LEN])
1122 struct eth_header *eh = packet->l2;
1123 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1127 dp_netdev_set_dl_dst(struct ofpbuf *packet,
1128 const uint8_t dl_addr[ETH_ADDR_LEN])
1130 struct eth_header *eh = packet->l2;
1131 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1135 dp_netdev_set_nw_addr(struct ofpbuf *packet, flow_t *key,
1136 const struct odp_action_nw_addr *a)
1138 if (key->dl_type == htons(ETH_TYPE_IP)) {
1139 struct ip_header *nh = packet->l3;
1142 field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1143 if (key->nw_proto == IP_TYPE_TCP) {
1144 struct tcp_header *th = packet->l4;
1145 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1146 } else if (key->nw_proto == IP_TYPE_UDP) {
1147 struct udp_header *uh = packet->l4;
1149 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1150 if (!uh->udp_csum) {
1151 uh->udp_csum = 0xffff;
1155 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1156 *field = a->nw_addr;
1161 dp_netdev_set_tp_port(struct ofpbuf *packet, flow_t *key,
1162 const struct odp_action_tp_port *a)
1164 if (key->dl_type == htons(ETH_P_IP)) {
1166 if (key->nw_proto == IPPROTO_TCP) {
1167 struct tcp_header *th = packet->l4;
1168 field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1169 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1170 *field = a->tp_port;
1171 } else if (key->nw_proto == IPPROTO_UDP) {
1172 struct udp_header *uh = packet->l4;
1173 field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1174 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1175 *field = a->tp_port;
1181 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1184 struct dp_netdev_port *p = dp->ports[out_port];
1186 netdev_send(p->netdev, packet);
1191 dp_netdev_output_group(struct dp_netdev *dp, uint16_t group, uint16_t in_port,
1192 struct ofpbuf *packet)
1194 struct odp_port_group *g = &dp->groups[group];
1197 for (i = 0; i < g->n_ports; i++) {
1198 uint16_t out_port = g->ports[i];
1199 if (out_port != in_port) {
1200 dp_netdev_output_port(dp, packet, out_port);
1206 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1207 int queue_no, int port_no, uint32_t arg)
1209 struct ovs_queue *q = &dp->queues[queue_no];
1210 struct odp_msg *header;
1214 if (q->n >= MAX_QUEUE_LEN) {
1219 msg_size = sizeof *header + packet->size;
1220 msg = ofpbuf_new(msg_size);
1221 header = ofpbuf_put_uninit(msg, sizeof *header);
1222 header->type = queue_no;
1223 header->length = msg_size;
1224 header->port = port_no;
1226 ofpbuf_put(msg, packet->data, packet->size);
1227 queue_push_tail(q, msg);
1233 dp_netdev_execute_actions(struct dp_netdev *dp,
1234 struct ofpbuf *packet, flow_t *key,
1235 const union odp_action *actions, int n_actions)
1238 for (i = 0; i < n_actions; i++) {
1239 const union odp_action *a = &actions[i];
1243 dp_netdev_output_port(dp, packet, a->output.port);
1246 case ODPAT_OUTPUT_GROUP:
1247 dp_netdev_output_group(dp, a->output_group.group, key->in_port,
1251 case ODPAT_CONTROLLER:
1252 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1253 key->in_port, a->controller.arg);
1256 case ODPAT_SET_VLAN_VID:
1257 dp_netdev_modify_vlan_tci(packet, key, ntohs(a->vlan_vid.vlan_vid),
1261 case ODPAT_SET_VLAN_PCP:
1262 dp_netdev_modify_vlan_tci(packet, key, a->vlan_pcp.vlan_pcp << 13,
1266 case ODPAT_STRIP_VLAN:
1267 dp_netdev_strip_vlan(packet, key);
1270 case ODPAT_SET_DL_SRC:
1271 dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1274 case ODPAT_SET_DL_DST:
1275 dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1278 case ODPAT_SET_NW_SRC:
1279 case ODPAT_SET_NW_DST:
1280 dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1283 case ODPAT_SET_TP_SRC:
1284 case ODPAT_SET_TP_DST:
1285 dp_netdev_set_tp_port(packet, key, &a->tp_port);
1292 const struct dpif_class dpif_netdev_class = {
1297 NULL, /* enumerate */
1300 NULL, /* get_all_names */
1302 dpif_netdev_get_stats,
1303 dpif_netdev_get_drop_frags,
1304 dpif_netdev_set_drop_frags,
1305 dpif_netdev_port_add,
1306 dpif_netdev_port_del,
1307 dpif_netdev_port_query_by_number,
1308 dpif_netdev_port_query_by_name,
1309 dpif_netdev_port_list,
1310 dpif_netdev_port_poll,
1311 dpif_netdev_port_poll_wait,
1312 dpif_netdev_port_group_get,
1313 dpif_netdev_port_group_set,
1314 dpif_netdev_flow_get,
1315 dpif_netdev_flow_put,
1316 dpif_netdev_flow_del,
1317 dpif_netdev_flow_flush,
1318 dpif_netdev_flow_list,
1319 dpif_netdev_execute,
1320 dpif_netdev_recv_get_mask,
1321 dpif_netdev_recv_set_mask,
1323 dpif_netdev_recv_wait,