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>
38 #include "socket-util.h"
39 #include "fatal-signal.h"
44 #include "dpif-provider.h"
50 #include "ofp-print.h"
53 #include "poll-loop.h"
59 VLOG_DEFINE_THIS_MODULE(dpif_netdev)
61 /* We could use these macros instead of using #ifdef and #endif every time we
62 * need to call the pthread_mutex_lock/unlock.
64 #define LOCK(mutex) pthread_mutex_lock(mutex)
65 #define UNLOCK(mutex) pthread_mutex_unlock(mutex)
72 /* Configuration parameters. */
73 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
74 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
75 enum { N_GROUPS = 16 }; /* Number of port groups. */
76 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
77 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
79 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
80 * headers to be aligned on a 4-byte boundary. */
81 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
83 /* Datapath based on the network device interface from netdev.h. */
90 bool drop_frags; /* Drop all IP fragments, if true. */
93 /* The pipe is used to signal the presence of a packet on the queue.
94 * - dpif_netdev_recv_wait() waits on p[0]
95 * - dpif_netdev_recv() extract from queue and read p[0]
96 * - dp_netdev_output_control() send to queue and write p[1]
99 /* The access to this queue is protected by the table_mutex mutex */
100 int pipe[2]; /* signal a packet on the queue */
102 pthread_mutex_t table_mutex; /* mutex for the flow table */
103 pthread_mutex_t port_list_mutex; /* port list mutex */
106 struct ovs_queue queues[N_QUEUES]; /* messages queued for dpif_recv(). */
108 struct hmap flow_table; /* Flow table. */
110 struct odp_port_group groups[N_GROUPS];
113 long long int n_frags; /* Number of dropped IP fragments. */
114 long long int n_hit; /* Number of flow table matches. */
115 long long int n_missed; /* Number of flow table misses. */
116 long long int n_lost; /* Number of misses not passed to client. */
119 struct dp_netdev_port *ports[MAX_PORTS];
120 struct list port_list;
125 /* A port in a netdev-based datapath. */
126 struct dp_netdev_port {
127 int port_no; /* Index into dp_netdev's 'ports'. */
128 struct list node; /* Element in dp_netdev's 'port_list'. */
129 struct netdev *netdev;
131 bool internal; /* Internal port (as ODP_PORT_INTERNAL)? */
133 struct pollfd *poll_fd; /* Useful to manage the poll loop in the
138 /* A flow in dp_netdev's 'flow_table'. */
139 struct dp_netdev_flow {
140 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
144 struct timespec used; /* Last used time. */
145 long long int packet_count; /* Number of packets matched. */
146 long long int byte_count; /* Number of bytes matched. */
147 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
150 union odp_action *actions;
151 unsigned int n_actions;
154 /* Interface to netdev-based datapath. */
157 struct dp_netdev *dp;
159 unsigned int dp_serial;
162 /* All netdev-based datapaths. */
163 static struct dp_netdev *dp_netdevs[256];
164 struct list dp_netdev_list = LIST_INITIALIZER(&dp_netdev_list);
165 enum { N_DP_NETDEVS = ARRAY_SIZE(dp_netdevs) };
168 /* Descriptor of the thread that manages the datapaths */
172 /* Maximum port MTU seen so far. */
173 static int max_mtu = ETH_PAYLOAD_MAX;
175 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
176 struct dp_netdev_port **portp);
177 static int get_port_by_name(struct dp_netdev *, const char *devname,
178 struct dp_netdev_port **portp);
179 static void dp_netdev_free(struct dp_netdev *);
180 static void dp_netdev_flow_flush(struct dp_netdev *);
181 static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags,
183 static int do_del_port(struct dp_netdev *, uint16_t port_no);
184 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
185 int queue_no, int port_no, uint32_t arg);
186 static int dp_netdev_execute_actions(struct dp_netdev *,
187 struct ofpbuf *, const flow_t *,
188 const union odp_action *, int n);
190 static struct dpif_netdev *
191 dpif_netdev_cast(const struct dpif *dpif)
193 dpif_assert_class(dpif, &dpif_netdev_class);
194 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
197 static struct dp_netdev *
198 get_dp_netdev(const struct dpif *dpif)
200 return dpif_netdev_cast(dpif)->dp;
204 name_to_dp_idx(const char *name)
206 if (!strncmp(name, "dp", 2) && isdigit((unsigned char)name[2])) {
207 int dp_idx = atoi(name + 2);
208 if (dp_idx >= 0 && dp_idx < N_DP_NETDEVS) {
215 static struct dp_netdev *
216 find_dp_netdev(const char *name)
221 dp_idx = name_to_dp_idx(name);
223 return dp_netdevs[dp_idx];
226 for (i = 0; i < N_DP_NETDEVS; i++) {
227 struct dp_netdev *dp = dp_netdevs[i];
229 struct dp_netdev_port *port;
230 if (!get_port_by_name(dp, name, &port)) {
239 create_dpif_netdev(struct dp_netdev *dp)
241 struct dpif_netdev *dpif;
246 dpname = xasprintf("dp%d", dp->dp_idx);
247 dpif = xmalloc(sizeof *dpif);
248 dpif_init(&dpif->dpif, &dpif_netdev_class, dpname, dp->dp_idx, dp->dp_idx);
250 dpif->listen_mask = 0;
251 dpif->dp_serial = dp->serial;
258 create_dp_netdev(const char *name, int dp_idx, struct dpif **dpifp)
260 struct dp_netdev *dp;
264 if (dp_netdevs[dp_idx]) {
268 /* Create datapath. */
269 dp_netdevs[dp_idx] = dp = xzalloc(sizeof *dp);
270 list_push_back(&dp_netdev_list, &dp->node);
273 dp->drop_frags = false;
276 error = pipe(dp->pipe);
278 fprintf(stderr, "pipe creation error\n");
281 if (set_nonblocking(dp->pipe[0]) || set_nonblocking(dp->pipe[1])) {
282 fprintf(stderr, "error set_nonblock on pipe\n");
286 pthread_mutex_init(&dp->table_mutex, NULL);
287 pthread_mutex_init(&dp->port_list_mutex, NULL);
290 for (i = 0; i < N_QUEUES; i++) {
291 queue_init(&dp->queues[i]);
294 hmap_init(&dp->flow_table);
295 for (i = 0; i < N_GROUPS; i++) {
296 dp->groups[i].ports = NULL;
297 dp->groups[i].n_ports = 0;
298 dp->groups[i].group = i;
301 list_init(&dp->port_list);
302 error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL);
308 *dpifp = create_dpif_netdev(dp);
313 dpif_netdev_open(const char *name, const char *type OVS_UNUSED, bool create,
317 if (find_dp_netdev(name)) {
320 int dp_idx = name_to_dp_idx(name);
322 return create_dp_netdev(name, dp_idx, dpifp);
324 /* Scan for unused dp_idx number. */
325 for (dp_idx = 0; dp_idx < N_DP_NETDEVS; dp_idx++) {
326 int error = create_dp_netdev(name, dp_idx, dpifp);
327 if (error != EBUSY) {
332 /* All datapath numbers in use. */
337 struct dp_netdev *dp = find_dp_netdev(name);
339 *dpifp = create_dpif_netdev(dp);
348 dp_netdev_free(struct dp_netdev *dp)
352 dp_netdev_flow_flush(dp);
354 pthread_mutex_lock(&dp->port_list_mutex);
356 while (dp->n_ports > 0) {
357 struct dp_netdev_port *port = CONTAINER_OF(
358 dp->port_list.next, struct dp_netdev_port, node);
359 do_del_port(dp, port->port_no);
362 pthread_mutex_unlock(&dp->port_list_mutex);
363 pthread_mutex_lock(&dp->table_mutex);
365 for (i = 0; i < N_QUEUES; i++) {
366 queue_destroy(&dp->queues[i]);
368 hmap_destroy(&dp->flow_table);
370 pthread_mutex_unlock(&dp->table_mutex);
371 pthread_mutex_destroy(&dp->table_mutex);
372 pthread_mutex_destroy(&dp->port_list_mutex);
375 for (i = 0; i < N_GROUPS; i++) {
376 free(dp->groups[i].ports);
378 dp_netdevs[dp->dp_idx] = NULL;
379 list_remove(&dp->node);
384 dpif_netdev_close(struct dpif *dpif)
386 struct dp_netdev *dp = get_dp_netdev(dpif);
387 assert(dp->open_cnt > 0);
388 if (--dp->open_cnt == 0 && dp->destroyed) {
395 dpif_netdev_destroy(struct dpif *dpif)
397 struct dp_netdev *dp = get_dp_netdev(dpif);
398 dp->destroyed = true;
403 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
405 struct dp_netdev *dp = get_dp_netdev(dpif);
406 memset(stats, 0, sizeof *stats);
409 pthread_mutex_lock(&dp->table_mutex);
411 stats->n_flows = hmap_count(&dp->flow_table);
412 stats->cur_capacity = hmap_capacity(&dp->flow_table);
414 pthread_mutex_unlock(&dp->table_mutex);
417 stats->max_capacity = MAX_FLOWS;
418 stats->n_ports = dp->n_ports;
419 stats->max_ports = MAX_PORTS;
420 stats->max_groups = N_GROUPS;
421 stats->n_frags = dp->n_frags;
422 stats->n_hit = dp->n_hit;
423 stats->n_missed = dp->n_missed;
424 stats->n_lost = dp->n_lost;
425 stats->max_miss_queue = MAX_QUEUE_LEN;
426 stats->max_action_queue = MAX_QUEUE_LEN;
431 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
433 struct dp_netdev *dp = get_dp_netdev(dpif);
434 *drop_fragsp = dp->drop_frags;
439 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
441 struct dp_netdev *dp = get_dp_netdev(dpif);
442 dp->drop_frags = drop_frags;
447 do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
450 bool internal = (flags & ODP_PORT_INTERNAL) != 0;
451 struct dp_netdev_port *port;
452 struct netdev_options netdev_options;
453 struct netdev *netdev;
457 /* XXX reject devices already in some dp_netdev. */
459 /* Open and validate network device. */
460 memset(&netdev_options, 0, sizeof netdev_options);
461 netdev_options.name = devname;
462 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
464 netdev_options.type = "tap";
467 error = netdev_open(&netdev_options, &netdev);
471 /* XXX reject loopback devices */
472 /* XXX reject non-Ethernet devices */
474 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
476 netdev_close(netdev);
480 port = xmalloc(sizeof *port);
481 port->port_no = port_no;
482 port->netdev = netdev;
483 port->internal = internal;
485 port->poll_fd = NULL;
488 netdev_get_mtu(netdev, &mtu);
494 pthread_mutex_lock(&dp->port_list_mutex);
496 list_push_back(&dp->port_list, &port->node);
499 pthread_mutex_unlock(&dp->port_list_mutex);
501 dp->ports[port_no] = port;
508 dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
511 struct dp_netdev *dp = get_dp_netdev(dpif);
514 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
515 if (!dp->ports[port_no]) {
517 return do_add_port(dp, devname, flags, port_no);
524 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
526 struct dp_netdev *dp = get_dp_netdev(dpif);
527 return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
531 is_valid_port_number(uint16_t port_no)
533 return port_no < MAX_PORTS;
537 get_port_by_number(struct dp_netdev *dp,
538 uint16_t port_no, struct dp_netdev_port **portp)
540 if (!is_valid_port_number(port_no)) {
544 *portp = dp->ports[port_no];
545 return *portp ? 0 : ENOENT;
550 get_port_by_name(struct dp_netdev *dp,
551 const char *devname, struct dp_netdev_port **portp)
553 struct dp_netdev_port *port;
556 pthread_mutex_lock(&dp->port_list_mutex);
558 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
559 if (!strcmp(netdev_get_name(port->netdev), devname)) {
562 pthread_mutex_unlock(&dp->port_list_mutex);
568 pthread_mutex_unlock(&dp->port_list_mutex);
574 do_del_port(struct dp_netdev *dp, uint16_t port_no)
576 struct dp_netdev_port *port;
579 /* XXX why no semaphores?? */
580 error = get_port_by_number(dp, port_no, &port);
585 list_remove(&port->node);
586 dp->ports[port->port_no] = NULL;
590 name = xstrdup(netdev_get_name(port->netdev));
591 netdev_close(port->netdev);
600 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
602 memset(odp_port, 0, sizeof *odp_port);
603 ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
604 sizeof odp_port->devname);
605 odp_port->port = port->port_no;
606 odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
610 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
611 struct odp_port *odp_port)
613 struct dp_netdev *dp = get_dp_netdev(dpif);
614 struct dp_netdev_port *port;
617 error = get_port_by_number(dp, port_no, &port);
619 answer_port_query(port, odp_port);
625 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
626 struct odp_port *odp_port)
628 struct dp_netdev *dp = get_dp_netdev(dpif);
629 struct dp_netdev_port *port;
632 error = get_port_by_name(dp, devname, &port);
634 answer_port_query(port, odp_port);
640 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
643 pthread_mutex_lock(&dp->table_mutex);
645 hmap_remove(&dp->flow_table, &flow->node);
647 pthread_mutex_unlock(&dp->table_mutex);
654 dp_netdev_flow_flush(struct dp_netdev *dp)
656 struct dp_netdev_flow *flow, *next;
658 HMAP_FOR_EACH_SAFE (flow, next, struct dp_netdev_flow, node,
660 dp_netdev_free_flow(dp, flow);
665 dpif_netdev_flow_flush(struct dpif *dpif)
667 struct dp_netdev *dp = get_dp_netdev(dpif);
668 dp_netdev_flow_flush(dp);
673 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
675 struct dp_netdev *dp = get_dp_netdev(dpif);
676 struct dp_netdev_port *port;
681 pthread_mutex_lock(&dp->port_list_mutex);
683 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
684 struct odp_port *odp_port = &ports[i];
688 answer_port_query(port, odp_port);
692 pthread_mutex_unlock(&dp->port_list_mutex);
698 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
700 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
701 if (dpif->dp_serial != dpif->dp->serial) {
702 dpif->dp_serial = dpif->dp->serial;
710 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
712 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
713 if (dpif->dp_serial != dpif->dp->serial) {
714 poll_immediate_wake();
719 get_port_group(const struct dpif *dpif, int group_no,
720 struct odp_port_group **groupp)
722 struct dp_netdev *dp = get_dp_netdev(dpif);
724 if (group_no >= 0 && group_no < N_GROUPS) {
725 *groupp = &dp->groups[group_no];
734 dpif_netdev_port_group_get(const struct dpif *dpif, int group_no,
735 uint16_t ports[], int n)
737 struct odp_port_group *group;
744 error = get_port_group(dpif, group_no, &group);
746 memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
747 return group->n_ports;
754 dpif_netdev_port_group_set(struct dpif *dpif, int group_no,
755 const uint16_t ports[], int n)
757 struct odp_port_group *group;
760 if (n < 0 || n > MAX_PORTS) {
764 error = get_port_group(dpif, group_no, &group);
767 group->ports = xmemdup(ports, n * sizeof *group->ports);
769 group->group = group_no;
774 static struct dp_netdev_flow *
775 dp_netdev_lookup_flow(struct dp_netdev *dp, const flow_t *key)
777 struct dp_netdev_flow *flow;
779 assert(!key->reserved[0] && !key->reserved[1] && !key->reserved[2]);
782 pthread_mutex_lock(&dp->table_mutex);
784 HMAP_FOR_EACH_WITH_HASH (flow, struct dp_netdev_flow, node,
785 flow_hash(key, 0), &dp->flow_table) {
786 if (flow_equal(&flow->key, key)) {
788 pthread_mutex_unlock(&dp->table_mutex);
794 pthread_mutex_unlock(&dp->table_mutex);
800 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
801 struct odp_flow *odp_flow)
804 odp_flow->key = flow->key;
805 odp_flow->stats.n_packets = flow->packet_count;
806 odp_flow->stats.n_bytes = flow->byte_count;
807 odp_flow->stats.used_sec = flow->used.tv_sec;
808 odp_flow->stats.used_nsec = flow->used.tv_nsec;
809 odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
810 odp_flow->stats.reserved = 0;
811 odp_flow->stats.error = 0;
812 if (odp_flow->n_actions > 0) {
813 unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
814 memcpy(odp_flow->actions, flow->actions,
815 n * sizeof *odp_flow->actions);
816 odp_flow->n_actions = flow->n_actions;
819 if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
824 odp_flow->stats.error = ENOENT;
829 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
831 struct dp_netdev *dp = get_dp_netdev(dpif);
834 for (i = 0; i < n; i++) {
835 struct odp_flow *odp_flow = &flows[i];
836 struct dp_netdev_flow *lookup_flow;
838 lookup_flow = dp_netdev_lookup_flow(dp, &odp_flow->key);
839 if ( lookup_flow == NULL )
840 answer_flow_query(lookup_flow, odp_flow->flags, odp_flow);
846 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
852 for (i = 0; i < n_actions; i++) {
853 const union odp_action *a = &actions[i];
856 if (a->output.port >= MAX_PORTS) {
861 case ODPAT_OUTPUT_GROUP:
863 if (a->output_group.group >= N_GROUPS) {
868 case ODPAT_CONTROLLER:
871 case ODPAT_SET_VLAN_VID:
873 if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) {
878 case ODPAT_SET_VLAN_PCP:
880 if (a->vlan_pcp.vlan_pcp & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
885 case ODPAT_SET_NW_TOS:
887 if (a->nw_tos.nw_tos & IP_ECN_MASK) {
892 case ODPAT_STRIP_VLAN:
893 case ODPAT_SET_DL_SRC:
894 case ODPAT_SET_DL_DST:
895 case ODPAT_SET_NW_SRC:
896 case ODPAT_SET_NW_DST:
897 case ODPAT_SET_TP_SRC:
898 case ODPAT_SET_TP_DST:
910 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
916 if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
919 error = dpif_netdev_validate_actions(odp_flow->actions,
920 odp_flow->n_actions, &mutates);
925 n_bytes = odp_flow->n_actions * sizeof *flow->actions;
926 flow->actions = xrealloc(flow->actions, n_bytes);
927 flow->n_actions = odp_flow->n_actions;
928 memcpy(flow->actions, odp_flow->actions, n_bytes);
933 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
935 struct dp_netdev *dp = get_dp_netdev(dpif);
936 struct dp_netdev_flow *flow;
939 flow = xzalloc(sizeof *flow);
940 flow->key = odp_flow->key;
941 memset(flow->key.reserved, 0, sizeof flow->key.reserved);
943 error = set_flow_actions(flow, odp_flow);
950 pthread_mutex_lock(&dp->table_mutex);
952 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
954 pthread_mutex_unlock(&dp->table_mutex);
960 clear_stats(struct dp_netdev_flow *flow)
962 flow->used.tv_sec = 0;
963 flow->used.tv_nsec = 0;
964 flow->packet_count = 0;
965 flow->byte_count = 0;
970 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
972 struct dp_netdev *dp = get_dp_netdev(dpif);
973 struct dp_netdev_flow *flow;
976 flow = dp_netdev_lookup_flow(dp, &put->flow.key);
978 if (put->flags & ODPPF_CREATE) {
980 pthread_mutex_lock(&dp->table_mutex);
982 n_flows = hmap_count(&dp->flow_table);
984 pthread_mutex_unlock(&dp->table_mutex);
986 if (n_flows < MAX_FLOWS) {
987 return add_flow(dpif, &put->flow);
995 if (put->flags & ODPPF_MODIFY) {
996 int error = set_flow_actions(flow, &put->flow);
997 if (!error && put->flags & ODPPF_ZERO_STATS) {
1009 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
1011 struct dp_netdev *dp = get_dp_netdev(dpif);
1012 struct dp_netdev_flow *flow;
1014 flow = dp_netdev_lookup_flow(dp, &odp_flow->key);
1016 answer_flow_query(flow, 0, odp_flow);
1017 dp_netdev_free_flow(dp, flow);
1025 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
1027 struct dp_netdev *dp = get_dp_netdev(dpif);
1028 struct dp_netdev_flow *flow;
1033 pthread_mutex_lock(&dp->table_mutex);
1035 HMAP_FOR_EACH (flow, struct dp_netdev_flow, node, &dp->flow_table) {
1039 answer_flow_query(flow, 0, &flows[i++]);
1041 n_flows = hmap_count(&dp->flow_table);
1043 pthread_mutex_unlock(&dp->table_mutex);
1050 dpif_netdev_execute(struct dpif *dpif, uint16_t in_port,
1051 const union odp_action actions[], int n_actions,
1052 const struct ofpbuf *packet)
1054 struct dp_netdev *dp = get_dp_netdev(dpif);
1060 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
1064 error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
1070 /* We need a deep copy of 'packet' since we're going to modify its
1072 ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size);
1073 copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
1074 ofpbuf_put(©, packet->data, packet->size);
1076 /* We still need a shallow copy of 'packet', even though we won't
1077 * modify its data, because flow_extract() modifies packet->l2, etc.
1078 * We could probably get away with modifying those but it's more polite
1082 flow_extract(©, 0, in_port, &flow);
1083 error = dp_netdev_execute_actions(dp, ©, &flow, actions, n_actions);
1085 ofpbuf_uninit(©);
1091 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
1093 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1094 *listen_mask = dpif_netdev->listen_mask;
1099 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
1101 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1102 if (!(listen_mask & ~ODPL_ALL)) {
1103 dpif_netdev->listen_mask = listen_mask;
1110 static struct ovs_queue *
1111 find_nonempty_queue(struct dpif *dpif)
1113 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1114 struct dp_netdev *dp = get_dp_netdev(dpif);
1115 int mask = dpif_netdev->listen_mask;
1118 for (i = 0; i < N_QUEUES; i++) {
1119 struct ovs_queue *q = &dp->queues[i];
1120 if (q->n && mask & (1u << i)) {
1128 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp OVS_UNUSED)
1130 struct ovs_queue *q;
1133 struct dp_netdev *dp = get_dp_netdev(dpif);
1135 pthread_mutex_lock(&dp->table_mutex);
1137 q = find_nonempty_queue(dpif);
1139 *bufp = queue_pop_head(q);
1141 /* read a byte from the pipe to advertise that a packet has been
1143 if (read(dp->pipe[0], &c, 1) < 0) {
1144 printf("Error reading from the pipe\n");
1146 pthread_mutex_unlock(&dp->table_mutex);
1151 pthread_mutex_unlock(&dp->table_mutex);
1158 dpif_netdev_recv_wait(struct dpif *dpif)
1161 struct dp_netdev *dp = get_dp_netdev(dpif);
1163 poll_fd_wait(dp->pipe[0], POLLIN);
1165 struct ovs_queue *q = find_nonempty_queue(dpif);
1167 poll_immediate_wake();
1169 /* No messages ready to be received, and dp_wait() will ensure that we
1170 * wake up to queue new messages, so there is nothing to do. */
1177 dp_netdev_flow_used(struct dp_netdev_flow *flow, const flow_t *key,
1178 const struct ofpbuf *packet)
1180 time_timespec(&flow->used);
1181 flow->packet_count++;
1182 flow->byte_count += packet->size;
1183 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
1184 struct tcp_header *th = packet->l4;
1185 flow->tcp_ctl |= th->tcp_ctl;
1190 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1191 struct ofpbuf *packet)
1193 struct dp_netdev_flow *flow;
1196 if (packet->size < ETH_HEADER_LEN) {
1199 if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
1204 flow = dp_netdev_lookup_flow(dp, &key);
1206 dp_netdev_flow_used(flow, &key, packet);
1207 dp_netdev_execute_actions(dp, packet, &key,
1208 flow->actions, flow->n_actions);
1212 dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
1217 * This function is no longer called by the threaded version. The same task is
1218 * instead performed in the thread body.
1223 struct ofpbuf packet;
1224 struct dp_netdev *dp;
1226 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1227 LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1228 struct dp_netdev_port *port;
1230 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1233 /* Reset packet contents. */
1234 packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
1237 error = netdev_recv(port->netdev, &packet);
1239 dp_netdev_port_input(dp, port, &packet);
1240 } else if (error != EAGAIN) {
1241 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1242 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1243 netdev_get_name(port->netdev), strerror(error));
1247 ofpbuf_uninit(&packet);
1250 /* This function is no longer called in the threaded version. */
1252 dp_netdev_wait(void)
1254 struct dp_netdev *dp;
1256 LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1257 struct dp_netdev_port *port;
1258 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1259 netdev_recv_wait(port->netdev);
1266 * pcap callback argument
1268 struct dispatch_arg {
1269 struct dp_netdev *dp; /* update statistics */
1270 struct dp_netdev_port *port; /* argument to flow identifier function */
1271 struct ofpbuf buf; /* used to process the packet */
1274 /* Process a packet.
1276 * The port_input function will send immediately if it finds a flow match and
1277 * the associated action is ODPAT_OUTPUT or ODPAT_OUTPUT_GROUP.
1278 * If a flow is not found or for the other actions, the packet is copied.
1281 process_pkt(u_char *arg_p, const struct pkthdr *hdr, const u_char *packet)
1283 struct dispatch_arg *arg = (struct dispatch_arg *)arg_p;
1284 struct ofpbuf *buf = &arg->buf;
1286 /* set packet size and data pointer */
1287 buf->size = hdr->caplen; /* XXX Must the size be equal to hdr->len or
1289 buf->data = (void*)packet;
1291 dp_netdev_port_input(arg->dp, arg->port, buf);
1296 /* Body of the thread that manages the datapaths */
1298 dp_thread_body(void *args OVS_UNUSED)
1300 struct dp_netdev *dp;
1301 struct dp_netdev_port *port;
1302 struct dispatch_arg arg;
1305 uint32_t batch = 50; /* max number of pkts processed by the dispatch */
1306 int processed; /* actual number of pkts processed by the dispatch */
1310 /*XXX Since the poll involves all ports of all datapaths, the right fds
1311 * size should be MAX_PORTS * max_number_of_datapaths */
1312 struct pollfd fds[MAX_PORTS];
1314 /* mask the fatal signals. In this way the main thread is delegate to
1315 * manage this them. */
1316 sigemptyset(&sigmask);
1317 sigaddset(&sigmask, SIGTERM);
1318 sigaddset(&sigmask, SIGALRM);
1319 sigaddset(&sigmask, SIGINT);
1320 sigaddset(&sigmask, SIGHUP);
1322 if (pthread_sigmask(SIG_BLOCK, &sigmask, NULL) != 0) {
1323 printf("Error pthread_sigmask\n");
1326 ofpbuf_init(&arg.buf, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1329 /* build the structure for poll */
1330 LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1331 pthread_mutex_lock(&dp->port_list_mutex);
1332 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1333 /* insert an element in the fds structure */
1334 fds[n_fds].fd = netdev_get_fd(port->netdev);
1335 fds[n_fds].events = POLLIN;
1336 port->poll_fd = &fds[n_fds];
1339 pthread_mutex_unlock(&dp->port_list_mutex);
1342 error = poll(fds, n_fds, 2000);
1345 printf("poll() error: %s\n", strerror(errno));
1349 LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1351 pthread_mutex_lock(&dp->port_list_mutex);
1352 LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1355 arg.buf.data = (char*)arg.buf.base + DP_NETDEV_HEADROOM;
1356 if (port->poll_fd && (port->poll_fd->revents & POLLIN)) {
1357 /* call the dispatch and process the packet into
1358 * its callback. We process 'batch' packets at time */
1359 processed = netdev_dispatch(port->netdev, batch,
1360 process_pkt, (u_char *)&arg);
1361 if (processed < 0) { /* pcap returns error */
1362 struct vlog_rate_limit rl =
1363 VLOG_RATE_LIMIT_INIT(1, 5);
1365 "error receiving data from XXX \n");
1367 } /* end of if poll */
1368 } /* end of port loop */
1369 pthread_mutex_unlock(&dp->port_list_mutex);
1370 } /* end of dp loop */
1373 ofpbuf_uninit(&arg.buf);
1377 /* Starts the datapath */
1379 dp_netdev_start(void)
1383 /* Launch thread which manages the datapath */
1384 error = pthread_create(&thread_p, NULL, dp_thread_body, NULL);
1388 /* This is the function that is called in response of a fatal signal (e.g.
1391 dp_netdev_exit_hook(void *aux OVS_UNUSED)
1393 pthread_cancel(thread_p);
1394 pthread_join(thread_p, NULL);
1396 #endif /* THREADED */
1398 /* Modify the TCI field of 'packet'. If a VLAN tag is not present, one
1399 * is added with the TCI field set to 'tci'. If a VLAN tag is present,
1400 * then 'mask' bits are cleared before 'tci' is logically OR'd into the
1403 * Note that the function does not ensure that 'tci' does not affect
1404 * bits outside of 'mask'.
1407 dp_netdev_modify_vlan_tci(struct ofpbuf *packet, uint16_t tci, uint16_t mask)
1409 struct vlan_eth_header *veh;
1410 struct eth_header *eh;
1413 if (packet->size >= sizeof(struct vlan_eth_header)
1414 && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1415 /* Clear 'mask' bits, but maintain other TCI bits. */
1417 veh->veth_tci &= ~htons(mask);
1418 veh->veth_tci |= htons(tci);
1420 /* Insert new 802.1Q header. */
1421 struct eth_header *eh = packet->l2;
1422 struct vlan_eth_header tmp;
1423 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1424 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1425 tmp.veth_type = htons(ETH_TYPE_VLAN);
1426 tmp.veth_tci = htons(tci);
1427 tmp.veth_next_type = eh->eth_type;
1429 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1430 memcpy(veh, &tmp, sizeof tmp);
1431 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1436 dp_netdev_strip_vlan(struct ofpbuf *packet)
1438 struct vlan_eth_header *veh = packet->l2;
1439 if (packet->size >= sizeof *veh
1440 && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1441 struct eth_header tmp;
1443 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1444 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1445 tmp.eth_type = veh->veth_next_type;
1447 packet->size -= VLAN_HEADER_LEN;
1448 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1449 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1450 memcpy(packet->data, &tmp, sizeof tmp);
1455 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1457 struct eth_header *eh = packet->l2;
1458 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1462 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1464 struct eth_header *eh = packet->l2;
1465 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1469 is_ip(const struct ofpbuf *packet, const flow_t *key)
1471 return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1475 dp_netdev_set_nw_addr(struct ofpbuf *packet, const flow_t *key,
1476 const struct odp_action_nw_addr *a)
1478 if (is_ip(packet, key)) {
1479 struct ip_header *nh = packet->l3;
1482 field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1483 if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1484 struct tcp_header *th = packet->l4;
1485 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1486 } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1487 struct udp_header *uh = packet->l4;
1489 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1490 if (!uh->udp_csum) {
1491 uh->udp_csum = 0xffff;
1495 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1496 *field = a->nw_addr;
1501 dp_netdev_set_nw_tos(struct ofpbuf *packet, const flow_t *key,
1502 const struct odp_action_nw_tos *a)
1504 if (is_ip(packet, key)) {
1505 struct ip_header *nh = packet->l3;
1506 uint8_t *field = &nh->ip_tos;
1508 /* Set the DSCP bits and preserve the ECN bits. */
1509 uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1511 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1512 htons((uint16_t)a->nw_tos));
1518 dp_netdev_set_tp_port(struct ofpbuf *packet, const flow_t *key,
1519 const struct odp_action_tp_port *a)
1521 if (is_ip(packet, key)) {
1523 if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1524 struct tcp_header *th = packet->l4;
1525 field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1526 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1527 *field = a->tp_port;
1528 } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1529 struct udp_header *uh = packet->l4;
1530 field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1531 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1532 *field = a->tp_port;
1540 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1543 struct dp_netdev_port *p = dp->ports[out_port];
1545 netdev_send(p->netdev, packet);
1550 dp_netdev_output_group(struct dp_netdev *dp, uint16_t group, uint16_t in_port,
1551 struct ofpbuf *packet)
1553 struct odp_port_group *g = &dp->groups[group];
1556 for (i = 0; i < g->n_ports; i++) {
1557 uint16_t out_port = g->ports[i];
1558 if (out_port != in_port) {
1559 dp_netdev_output_port(dp, packet, out_port);
1565 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1566 int queue_no, int port_no, uint32_t arg)
1568 struct ovs_queue *q = &dp->queues[queue_no];
1569 struct odp_msg *header;
1576 if (q->n >= MAX_QUEUE_LEN) {
1581 msg_size = sizeof *header + packet->size;
1582 msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1583 header = ofpbuf_put_uninit(msg, sizeof *header);
1584 header->type = queue_no;
1585 header->length = msg_size;
1586 header->port = port_no;
1588 ofpbuf_put(msg, packet->data, packet->size);
1590 pthread_mutex_lock(&dp->table_mutex);
1593 queue_push_tail(q, msg);
1595 /* write a byte on the pipe to advertise that a packet is ready */
1596 if (write(dp->pipe[1], &c, 1) < 0) {
1597 printf("Error writing on the pipe\n");
1599 pthread_mutex_unlock(&dp->table_mutex);
1605 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1606 * screwy or truncated header fields or one whose inner and outer Ethernet
1607 * address differ. */
1609 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct odp_flow_key *key)
1611 struct arp_eth_header *arp;
1612 struct eth_header *eth;
1615 if (key->dl_type != htons(ETH_TYPE_ARP)) {
1619 l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1620 if (l3_size < sizeof(struct arp_eth_header)) {
1626 return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1627 || arp->ar_pro != htons(ARP_PRO_IP)
1628 || arp->ar_hln != ETH_HEADER_LEN
1630 || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1634 * Execute the actions associated to a flow.
1637 dp_netdev_execute_actions(struct dp_netdev *dp,
1638 struct ofpbuf *packet, const flow_t *key,
1639 const union odp_action *actions, int n_actions)
1643 for (i = 0; i < n_actions; i++) {
1644 const union odp_action *a = &actions[i];
1648 dp_netdev_output_port(dp, packet, a->output.port);
1651 case ODPAT_OUTPUT_GROUP:
1652 dp_netdev_output_group(dp, a->output_group.group, key->in_port,
1656 case ODPAT_CONTROLLER:
1657 dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1658 key->in_port, a->controller.arg);
1661 case ODPAT_SET_VLAN_VID:
1662 dp_netdev_modify_vlan_tci(packet, ntohs(a->vlan_vid.vlan_vid),
1666 case ODPAT_SET_VLAN_PCP:
1667 dp_netdev_modify_vlan_tci(packet,
1668 a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT,
1672 case ODPAT_STRIP_VLAN:
1673 dp_netdev_strip_vlan(packet);
1676 case ODPAT_SET_DL_SRC:
1677 dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1680 case ODPAT_SET_DL_DST:
1681 dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1684 case ODPAT_SET_NW_SRC:
1685 case ODPAT_SET_NW_DST:
1686 dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1689 case ODPAT_SET_NW_TOS:
1690 dp_netdev_set_nw_tos(packet, key, &a->nw_tos);
1693 case ODPAT_SET_TP_SRC:
1694 case ODPAT_SET_TP_DST:
1695 dp_netdev_set_tp_port(packet, key, &a->tp_port);
1698 case ODPAT_DROP_SPOOFED_ARP:
1699 if (dp_netdev_is_spoofed_arp(packet, key)) {
1707 const struct dpif_class dpif_netdev_class = {
1713 dp_netdev_exit_hook,
1715 NULL, /* enumerate */
1718 NULL, /* get_all_names */
1719 dpif_netdev_destroy,
1720 dpif_netdev_get_stats,
1721 dpif_netdev_get_drop_frags,
1722 dpif_netdev_set_drop_frags,
1723 dpif_netdev_port_add,
1724 dpif_netdev_port_del,
1725 dpif_netdev_port_query_by_number,
1726 dpif_netdev_port_query_by_name,
1727 dpif_netdev_port_list,
1728 dpif_netdev_port_poll,
1729 dpif_netdev_port_poll_wait,
1730 dpif_netdev_port_group_get,
1731 dpif_netdev_port_group_set,
1732 dpif_netdev_flow_get,
1733 dpif_netdev_flow_put,
1734 dpif_netdev_flow_del,
1735 dpif_netdev_flow_flush,
1736 dpif_netdev_flow_list,
1737 dpif_netdev_execute,
1738 dpif_netdev_recv_get_mask,
1739 dpif_netdev_recv_set_mask,
1740 NULL, /* get_sflow_probability */
1741 NULL, /* set_sflow_probability */
1742 NULL, /* queue_to_priority */
1744 dpif_netdev_recv_wait,