2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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.
24 #include <netinet/in.h>
25 #include <sys/socket.h>
30 #include <sys/ioctl.h>
34 #include "classifier.h"
37 #include "dpif-provider.h"
39 #include "dynamic-string.h"
43 #include "meta-flow.h"
45 #include "netdev-vport.h"
47 #include "odp-execute.h"
49 #include "ofp-print.h"
52 #include "poll-loop.h"
62 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
64 /* By default, choose a priority in the middle. */
65 #define NETDEV_RULE_PRIORITY 0x8000
67 /* Configuration parameters. */
68 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
70 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
71 * headers to be aligned on a 4-byte boundary. */
72 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
75 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
76 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
77 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
78 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
80 struct dp_netdev_upcall {
81 struct dpif_upcall upcall; /* Queued upcall information. */
82 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
85 struct dp_netdev_queue {
86 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN];
87 unsigned int head, tail;
90 /* Datapath based on the network device interface from netdev.h. */
92 const struct dpif_class *class;
97 struct dp_netdev_queue queues[N_QUEUES];
98 struct classifier cls; /* Classifier. */
99 struct hmap flow_table; /* Flow table. */
100 struct seq *queue_seq; /* Incremented whenever a packet is queued. */
103 struct ovsthread_counter *n_hit; /* Number of flow table matches. */
104 struct ovsthread_counter *n_missed; /* Number of flow table misses. */
105 struct ovsthread_counter *n_lost; /* Number of misses not passed up. */
109 struct seq *port_seq; /* Incremented whenever a port changes. */
112 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *,
115 /* A port in a netdev-based datapath. */
116 struct dp_netdev_port {
117 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
119 struct netdev *netdev;
120 struct netdev_saved_flags *sf;
121 struct netdev_rx *rx;
122 char *type; /* Port type as requested by user. */
125 /* A flow in dp_netdev's 'flow_table'. */
126 struct dp_netdev_flow {
127 /* Packet classification. */
128 struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
130 /* Hash table index by unmasked flow.*/
131 struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
132 struct flow flow; /* The flow that created this entry. */
135 long long int used; /* Last used time, in monotonic msecs. */
136 long long int packet_count; /* Number of packets matched. */
137 long long int byte_count; /* Number of bytes matched. */
138 uint16_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
141 struct nlattr *actions;
145 /* Interface to netdev-based datapath. */
148 struct dp_netdev *dp;
149 uint64_t last_port_seq;
152 /* All netdev-based datapaths. */
153 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
155 /* Global lock for all data. */
156 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
158 static int get_port_by_number(struct dp_netdev *, odp_port_t port_no,
159 struct dp_netdev_port **portp);
160 static int get_port_by_name(struct dp_netdev *, const char *devname,
161 struct dp_netdev_port **portp);
162 static void dp_netdev_free(struct dp_netdev *);
163 static void dp_netdev_flow_flush(struct dp_netdev *);
164 static int do_add_port(struct dp_netdev *, const char *devname,
165 const char *type, odp_port_t port_no);
166 static int do_del_port(struct dp_netdev *, odp_port_t port_no);
167 static int dpif_netdev_open(const struct dpif_class *, const char *name,
168 bool create, struct dpif **);
169 static int dp_netdev_output_userspace(struct dp_netdev *, struct ofpbuf *,
170 int queue_no, const struct flow *,
171 const struct nlattr *userdata);
172 static void dp_netdev_execute_actions(struct dp_netdev *, const struct flow *,
173 struct ofpbuf *, struct pkt_metadata *,
174 const struct nlattr *actions,
176 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
177 struct pkt_metadata *md);
179 static struct dpif_netdev *
180 dpif_netdev_cast(const struct dpif *dpif)
182 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
183 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
186 static struct dp_netdev *
187 get_dp_netdev(const struct dpif *dpif)
189 return dpif_netdev_cast(dpif)->dp;
193 dpif_netdev_enumerate(struct sset *all_dps)
195 struct shash_node *node;
197 ovs_mutex_lock(&dp_netdev_mutex);
198 SHASH_FOR_EACH(node, &dp_netdevs) {
199 sset_add(all_dps, node->name);
201 ovs_mutex_unlock(&dp_netdev_mutex);
207 dpif_netdev_class_is_dummy(const struct dpif_class *class)
209 return class != &dpif_netdev_class;
213 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
215 return strcmp(type, "internal") ? type
216 : dpif_netdev_class_is_dummy(class) ? "dummy"
221 create_dpif_netdev(struct dp_netdev *dp)
223 uint16_t netflow_id = hash_string(dp->name, 0);
224 struct dpif_netdev *dpif;
228 dpif = xmalloc(sizeof *dpif);
229 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
231 dpif->last_port_seq = seq_read(dp->port_seq);
236 /* Choose an unused, non-zero port number and return it on success.
237 * Return ODPP_NONE on failure. */
239 choose_port(struct dp_netdev *dp, const char *name)
243 if (dp->class != &dpif_netdev_class) {
247 /* If the port name begins with "br", start the number search at
248 * 100 to make writing tests easier. */
249 if (!strncmp(name, "br", 2)) {
253 /* If the port name contains a number, try to assign that port number.
254 * This can make writing unit tests easier because port numbers are
256 for (p = name; *p != '\0'; p++) {
257 if (isdigit((unsigned char) *p)) {
258 port_no = start_no + strtol(p, NULL, 10);
259 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
260 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
261 return u32_to_odp(port_no);
268 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
269 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
270 return u32_to_odp(port_no);
278 create_dp_netdev(const char *name, const struct dpif_class *class,
279 struct dp_netdev **dpp)
281 struct dp_netdev *dp;
285 dp = xzalloc(sizeof *dp);
287 dp->name = xstrdup(name);
289 for (i = 0; i < N_QUEUES; i++) {
290 dp->queues[i].head = dp->queues[i].tail = 0;
292 dp->queue_seq = seq_create();
293 classifier_init(&dp->cls, NULL);
294 hmap_init(&dp->flow_table);
296 dp->n_hit = ovsthread_counter_create();
297 dp->n_missed = ovsthread_counter_create();
298 dp->n_lost = ovsthread_counter_create();
300 hmap_init(&dp->ports);
301 dp->port_seq = seq_create();
303 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
309 shash_add(&dp_netdevs, name, dp);
316 dpif_netdev_open(const struct dpif_class *class, const char *name,
317 bool create, struct dpif **dpifp)
319 struct dp_netdev *dp;
322 ovs_mutex_lock(&dp_netdev_mutex);
323 dp = shash_find_data(&dp_netdevs, name);
325 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
327 error = (dp->class != class ? EINVAL
332 *dpifp = create_dpif_netdev(dp);
334 ovs_mutex_unlock(&dp_netdev_mutex);
340 dp_netdev_purge_queues(struct dp_netdev *dp)
344 for (i = 0; i < N_QUEUES; i++) {
345 struct dp_netdev_queue *q = &dp->queues[i];
347 while (q->tail != q->head) {
348 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
349 ofpbuf_uninit(&u->upcall.packet);
350 ofpbuf_uninit(&u->buf);
356 dp_netdev_free(struct dp_netdev *dp)
358 struct dp_netdev_port *port, *next;
360 dp_netdev_flow_flush(dp);
361 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
362 do_del_port(dp, port->port_no);
364 ovsthread_counter_destroy(dp->n_hit);
365 ovsthread_counter_destroy(dp->n_missed);
366 ovsthread_counter_destroy(dp->n_lost);
367 dp_netdev_purge_queues(dp);
368 seq_destroy(dp->queue_seq);
369 classifier_destroy(&dp->cls);
370 hmap_destroy(&dp->flow_table);
371 seq_destroy(dp->port_seq);
372 hmap_destroy(&dp->ports);
378 dpif_netdev_close(struct dpif *dpif)
380 struct dp_netdev *dp = get_dp_netdev(dpif);
382 ovs_mutex_lock(&dp_netdev_mutex);
384 ovs_assert(dp->open_cnt > 0);
385 if (--dp->open_cnt == 0 && dp->destroyed) {
386 shash_find_and_delete(&dp_netdevs, dp->name);
391 ovs_mutex_unlock(&dp_netdev_mutex);
395 dpif_netdev_destroy(struct dpif *dpif)
397 struct dp_netdev *dp = get_dp_netdev(dpif);
399 ovs_mutex_lock(&dp_netdev_mutex);
400 dp->destroyed = true;
401 ovs_mutex_unlock(&dp_netdev_mutex);
407 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
409 struct dp_netdev *dp = get_dp_netdev(dpif);
411 ovs_mutex_lock(&dp_netdev_mutex);
412 stats->n_flows = hmap_count(&dp->flow_table);
413 stats->n_hit = ovsthread_counter_read(dp->n_hit);
414 stats->n_missed = ovsthread_counter_read(dp->n_missed);
415 stats->n_lost = ovsthread_counter_read(dp->n_lost);
416 stats->n_masks = UINT32_MAX;
417 stats->n_mask_hit = UINT64_MAX;
418 ovs_mutex_unlock(&dp_netdev_mutex);
424 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
427 struct netdev_saved_flags *sf;
428 struct dp_netdev_port *port;
429 struct netdev *netdev;
430 struct netdev_rx *rx;
431 enum netdev_flags flags;
432 const char *open_type;
435 /* XXX reject devices already in some dp_netdev. */
437 /* Open and validate network device. */
438 open_type = dpif_netdev_port_open_type(dp->class, type);
439 error = netdev_open(devname, open_type, &netdev);
443 /* XXX reject non-Ethernet devices */
445 netdev_get_flags(netdev, &flags);
446 if (flags & NETDEV_LOOPBACK) {
447 VLOG_ERR("%s: cannot add a loopback device", devname);
448 netdev_close(netdev);
452 error = netdev_rx_open(netdev, &rx);
454 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
455 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
456 devname, ovs_strerror(errno));
457 netdev_close(netdev);
461 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
464 netdev_close(netdev);
468 port = xmalloc(sizeof *port);
469 port->port_no = port_no;
470 port->netdev = netdev;
473 port->type = xstrdup(type);
475 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
476 seq_change(dp->port_seq);
482 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
483 odp_port_t *port_nop)
485 struct dp_netdev *dp = get_dp_netdev(dpif);
486 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
487 const char *dpif_port;
491 ovs_mutex_lock(&dp_netdev_mutex);
492 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
493 if (*port_nop != ODPP_NONE) {
495 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
497 port_no = choose_port(dp, dpif_port);
498 error = port_no == ODPP_NONE ? EFBIG : 0;
502 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
504 ovs_mutex_unlock(&dp_netdev_mutex);
510 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
512 struct dp_netdev *dp = get_dp_netdev(dpif);
515 ovs_mutex_lock(&dp_netdev_mutex);
516 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
517 ovs_mutex_unlock(&dp_netdev_mutex);
523 is_valid_port_number(odp_port_t port_no)
525 return port_no != ODPP_NONE;
528 static struct dp_netdev_port *
529 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
531 struct dp_netdev_port *port;
533 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
535 if (port->port_no == port_no) {
543 get_port_by_number(struct dp_netdev *dp,
544 odp_port_t port_no, struct dp_netdev_port **portp)
546 if (!is_valid_port_number(port_no)) {
550 *portp = dp_netdev_lookup_port(dp, port_no);
551 return *portp ? 0 : ENOENT;
556 get_port_by_name(struct dp_netdev *dp,
557 const char *devname, struct dp_netdev_port **portp)
559 struct dp_netdev_port *port;
561 HMAP_FOR_EACH (port, node, &dp->ports) {
562 if (!strcmp(netdev_get_name(port->netdev), devname)) {
571 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
573 struct dp_netdev_port *port;
576 error = get_port_by_number(dp, port_no, &port);
581 hmap_remove(&dp->ports, &port->node);
582 seq_change(dp->port_seq);
584 netdev_close(port->netdev);
585 netdev_restore_flags(port->sf);
586 netdev_rx_close(port->rx);
594 answer_port_query(const struct dp_netdev_port *port,
595 struct dpif_port *dpif_port)
597 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
598 dpif_port->type = xstrdup(port->type);
599 dpif_port->port_no = port->port_no;
603 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
604 struct dpif_port *dpif_port)
606 struct dp_netdev *dp = get_dp_netdev(dpif);
607 struct dp_netdev_port *port;
610 ovs_mutex_lock(&dp_netdev_mutex);
611 error = get_port_by_number(dp, port_no, &port);
612 if (!error && dpif_port) {
613 answer_port_query(port, dpif_port);
615 ovs_mutex_unlock(&dp_netdev_mutex);
621 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
622 struct dpif_port *dpif_port)
624 struct dp_netdev *dp = get_dp_netdev(dpif);
625 struct dp_netdev_port *port;
628 ovs_mutex_lock(&dp_netdev_mutex);
629 error = get_port_by_name(dp, devname, &port);
630 if (!error && dpif_port) {
631 answer_port_query(port, dpif_port);
633 ovs_mutex_unlock(&dp_netdev_mutex);
639 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *netdev_flow)
641 ovs_rwlock_wrlock(&dp->cls.rwlock);
642 classifier_remove(&dp->cls, &netdev_flow->cr);
643 ovs_rwlock_unlock(&dp->cls.rwlock);
644 cls_rule_destroy(&netdev_flow->cr);
646 hmap_remove(&dp->flow_table, &netdev_flow->node);
647 free(netdev_flow->actions);
652 dp_netdev_flow_flush(struct dp_netdev *dp)
654 struct dp_netdev_flow *netdev_flow, *next;
656 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
657 dp_netdev_free_flow(dp, netdev_flow);
662 dpif_netdev_flow_flush(struct dpif *dpif)
664 struct dp_netdev *dp = get_dp_netdev(dpif);
666 ovs_mutex_lock(&dp_netdev_mutex);
667 dp_netdev_flow_flush(dp);
668 ovs_mutex_unlock(&dp_netdev_mutex);
673 struct dp_netdev_port_state {
680 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
682 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
687 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
688 struct dpif_port *dpif_port)
690 struct dp_netdev_port_state *state = state_;
691 struct dp_netdev *dp = get_dp_netdev(dpif);
692 struct hmap_node *node;
695 ovs_mutex_lock(&dp_netdev_mutex);
696 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
698 struct dp_netdev_port *port;
700 port = CONTAINER_OF(node, struct dp_netdev_port, node);
703 state->name = xstrdup(netdev_get_name(port->netdev));
704 dpif_port->name = state->name;
705 dpif_port->type = port->type;
706 dpif_port->port_no = port->port_no;
712 ovs_mutex_unlock(&dp_netdev_mutex);
718 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
720 struct dp_netdev_port_state *state = state_;
727 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
729 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
730 uint64_t new_port_seq;
733 ovs_mutex_lock(&dp_netdev_mutex);
734 new_port_seq = seq_read(dpif->dp->port_seq);
735 if (dpif->last_port_seq != new_port_seq) {
736 dpif->last_port_seq = new_port_seq;
741 ovs_mutex_unlock(&dp_netdev_mutex);
747 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
749 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
751 ovs_mutex_lock(&dp_netdev_mutex);
752 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
753 ovs_mutex_unlock(&dp_netdev_mutex);
756 static struct dp_netdev_flow *
757 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
761 ovs_rwlock_wrlock(&dp->cls.rwlock);
762 cr = classifier_lookup(&dp->cls, flow, NULL);
763 ovs_rwlock_unlock(&dp->cls.rwlock);
766 ? CONTAINER_OF(cr, struct dp_netdev_flow, cr)
770 static struct dp_netdev_flow *
771 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
773 struct dp_netdev_flow *netdev_flow;
775 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
777 if (flow_equal(&netdev_flow->flow, flow)) {
785 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
786 struct dpif_flow_stats *stats)
788 stats->n_packets = netdev_flow->packet_count;
789 stats->n_bytes = netdev_flow->byte_count;
790 stats->used = netdev_flow->used;
791 stats->tcp_flags = netdev_flow->tcp_flags;
795 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
796 const struct nlattr *mask_key,
797 uint32_t mask_key_len, const struct flow *flow,
801 if (odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow)) {
802 /* This should not happen: it indicates that
803 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
804 * disagree on the acceptable form of a mask. Log the problem
805 * as an error, with enough details to enable debugging. */
806 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
808 if (!VLOG_DROP_ERR(&rl)) {
812 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
814 VLOG_ERR("internal error parsing flow mask %s", ds_cstr(&s));
820 /* Force unwildcard the in_port. */
821 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
824 /* No mask key, unwildcard everything except fields whose
825 * prerequisities are not met. */
826 memset(mask, 0x0, sizeof *mask);
828 for (id = 0; id < MFF_N_IDS; ++id) {
829 /* Skip registers and metadata. */
830 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
831 && id != MFF_METADATA) {
832 const struct mf_field *mf = mf_from_id(id);
833 if (mf_are_prereqs_ok(mf, flow)) {
834 mf_mask_field(mf, mask);
844 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
849 if (odp_flow_key_to_flow(key, key_len, flow)) {
850 /* This should not happen: it indicates that odp_flow_key_from_flow()
851 * and odp_flow_key_to_flow() disagree on the acceptable form of a
852 * flow. Log the problem as an error, with enough details to enable
854 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
856 if (!VLOG_DROP_ERR(&rl)) {
860 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
861 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
868 in_port = flow->in_port.odp_port;
869 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
877 dpif_netdev_flow_get(const struct dpif *dpif,
878 const struct nlattr *nl_key, size_t nl_key_len,
879 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
881 struct dp_netdev *dp = get_dp_netdev(dpif);
882 struct dp_netdev_flow *netdev_flow;
886 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
891 ovs_mutex_lock(&dp_netdev_mutex);
892 netdev_flow = dp_netdev_find_flow(dp, &key);
895 get_dpif_flow_stats(netdev_flow, stats);
898 *actionsp = ofpbuf_clone_data(netdev_flow->actions,
899 netdev_flow->actions_len);
904 ovs_mutex_unlock(&dp_netdev_mutex);
910 set_flow_actions(struct dp_netdev_flow *netdev_flow,
911 const struct nlattr *actions, size_t actions_len)
913 netdev_flow->actions = xrealloc(netdev_flow->actions, actions_len);
914 netdev_flow->actions_len = actions_len;
915 memcpy(netdev_flow->actions, actions, actions_len);
920 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
921 const struct flow_wildcards *wc,
922 const struct nlattr *actions,
925 struct dp_netdev_flow *netdev_flow;
929 netdev_flow = xzalloc(sizeof *netdev_flow);
930 netdev_flow->flow = *flow;
932 match_init(&match, flow, wc);
933 cls_rule_init(&netdev_flow->cr, &match, NETDEV_RULE_PRIORITY);
934 ovs_rwlock_wrlock(&dp->cls.rwlock);
935 classifier_insert(&dp->cls, &netdev_flow->cr);
936 ovs_rwlock_unlock(&dp->cls.rwlock);
938 error = set_flow_actions(netdev_flow, actions, actions_len);
940 ovs_rwlock_wrlock(&dp->cls.rwlock);
941 classifier_remove(&dp->cls, &netdev_flow->cr);
942 ovs_rwlock_unlock(&dp->cls.rwlock);
943 cls_rule_destroy(&netdev_flow->cr);
949 hmap_insert(&dp->flow_table, &netdev_flow->node, flow_hash(flow, 0));
954 clear_stats(struct dp_netdev_flow *netdev_flow)
956 netdev_flow->used = 0;
957 netdev_flow->packet_count = 0;
958 netdev_flow->byte_count = 0;
959 netdev_flow->tcp_flags = 0;
963 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
965 struct dp_netdev *dp = get_dp_netdev(dpif);
966 struct dp_netdev_flow *netdev_flow;
968 struct flow_wildcards wc;
971 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
975 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
976 put->mask, put->mask_len,
982 ovs_mutex_lock(&dp_netdev_mutex);
983 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
985 if (put->flags & DPIF_FP_CREATE) {
986 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
988 memset(put->stats, 0, sizeof *put->stats);
990 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
999 if (put->flags & DPIF_FP_MODIFY
1000 && flow_equal(&flow, &netdev_flow->flow)) {
1001 error = set_flow_actions(netdev_flow, put->actions,
1005 get_dpif_flow_stats(netdev_flow, put->stats);
1007 if (put->flags & DPIF_FP_ZERO_STATS) {
1008 clear_stats(netdev_flow);
1011 } else if (put->flags & DPIF_FP_CREATE) {
1014 /* Overlapping flow. */
1018 ovs_mutex_unlock(&dp_netdev_mutex);
1024 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1026 struct dp_netdev *dp = get_dp_netdev(dpif);
1027 struct dp_netdev_flow *netdev_flow;
1031 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1036 ovs_mutex_lock(&dp_netdev_mutex);
1037 netdev_flow = dp_netdev_find_flow(dp, &key);
1040 get_dpif_flow_stats(netdev_flow, del->stats);
1042 dp_netdev_free_flow(dp, netdev_flow);
1046 ovs_mutex_unlock(&dp_netdev_mutex);
1051 struct dp_netdev_flow_state {
1054 struct nlattr *actions;
1055 struct odputil_keybuf keybuf;
1056 struct odputil_keybuf maskbuf;
1057 struct dpif_flow_stats stats;
1061 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
1063 struct dp_netdev_flow_state *state;
1065 *statep = state = xmalloc(sizeof *state);
1068 state->actions = NULL;
1073 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
1074 const struct nlattr **key, size_t *key_len,
1075 const struct nlattr **mask, size_t *mask_len,
1076 const struct nlattr **actions, size_t *actions_len,
1077 const struct dpif_flow_stats **stats)
1079 struct dp_netdev_flow_state *state = state_;
1080 struct dp_netdev *dp = get_dp_netdev(dpif);
1081 struct dp_netdev_flow *netdev_flow;
1082 struct hmap_node *node;
1084 ovs_mutex_lock(&dp_netdev_mutex);
1085 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
1087 ovs_mutex_unlock(&dp_netdev_mutex);
1091 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1096 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1097 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1098 netdev_flow->flow.in_port.odp_port);
1101 *key_len = buf.size;
1106 struct flow_wildcards wc;
1108 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1109 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1110 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1111 odp_to_u32(wc.masks.in_port.odp_port));
1114 *mask_len = buf.size;
1118 free(state->actions);
1119 state->actions = xmemdup(netdev_flow->actions,
1120 netdev_flow->actions_len);
1122 *actions = state->actions;
1123 *actions_len = netdev_flow->actions_len;
1127 get_dpif_flow_stats(netdev_flow, &state->stats);
1128 *stats = &state->stats;
1131 ovs_mutex_unlock(&dp_netdev_mutex);
1136 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1138 struct dp_netdev_flow_state *state = state_;
1140 free(state->actions);
1146 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1148 struct dp_netdev *dp = get_dp_netdev(dpif);
1149 struct pkt_metadata *md = &execute->md;
1152 if (execute->packet->size < ETH_HEADER_LEN ||
1153 execute->packet->size > UINT16_MAX) {
1157 /* Extract flow key. */
1158 flow_extract(execute->packet, md->skb_priority, md->pkt_mark, &md->tunnel,
1159 (union flow_in_port *)&md->in_port, &key);
1160 ovs_mutex_lock(&dp_netdev_mutex);
1161 dp_netdev_execute_actions(dp, &key, execute->packet, md, execute->actions,
1162 execute->actions_len);
1163 ovs_mutex_unlock(&dp_netdev_mutex);
1168 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
1174 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1175 uint32_t queue_id, uint32_t *priority)
1177 *priority = queue_id;
1181 static struct dp_netdev_queue *
1182 find_nonempty_queue(struct dpif *dpif)
1184 struct dp_netdev *dp = get_dp_netdev(dpif);
1187 for (i = 0; i < N_QUEUES; i++) {
1188 struct dp_netdev_queue *q = &dp->queues[i];
1189 if (q->head != q->tail) {
1197 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1200 struct dp_netdev_queue *q;
1203 ovs_mutex_lock(&dp_netdev_mutex);
1204 q = find_nonempty_queue(dpif);
1206 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1208 *upcall = u->upcall;
1217 ovs_mutex_unlock(&dp_netdev_mutex);
1223 dpif_netdev_recv_wait(struct dpif *dpif)
1225 struct dp_netdev *dp = get_dp_netdev(dpif);
1228 ovs_mutex_lock(&dp_netdev_mutex);
1229 seq = seq_read(dp->queue_seq);
1230 if (find_nonempty_queue(dpif)) {
1231 poll_immediate_wake();
1233 seq_wait(dp->queue_seq, seq);
1235 ovs_mutex_unlock(&dp_netdev_mutex);
1239 dpif_netdev_recv_purge(struct dpif *dpif)
1241 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1242 ovs_mutex_lock(&dp_netdev_mutex);
1243 dp_netdev_purge_queues(dpif_netdev->dp);
1244 ovs_mutex_unlock(&dp_netdev_mutex);
1248 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1249 const struct ofpbuf *packet)
1251 netdev_flow->used = time_msec();
1252 netdev_flow->packet_count++;
1253 netdev_flow->byte_count += packet->size;
1254 netdev_flow->tcp_flags |= packet_get_tcp_flags(packet, &netdev_flow->flow);
1258 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1259 struct pkt_metadata *md)
1261 struct dp_netdev_flow *netdev_flow;
1264 if (packet->size < ETH_HEADER_LEN) {
1267 flow_extract(packet, md->skb_priority, md->pkt_mark, &md->tunnel,
1268 (union flow_in_port *)&md->in_port, &key);
1269 netdev_flow = dp_netdev_lookup_flow(dp, &key);
1271 dp_netdev_flow_used(netdev_flow, packet);
1272 dp_netdev_execute_actions(dp, &key, packet, md,
1273 netdev_flow->actions,
1274 netdev_flow->actions_len);
1275 ovsthread_counter_inc(dp->n_hit, 1);
1277 ovsthread_counter_inc(dp->n_missed, 1);
1278 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1283 dpif_netdev_run(struct dpif *dpif)
1285 struct dp_netdev_port *port;
1286 struct dp_netdev *dp;
1287 struct ofpbuf packet;
1289 ovs_mutex_lock(&dp_netdev_mutex);
1290 dp = get_dp_netdev(dpif);
1291 ofpbuf_init(&packet, 0);
1293 HMAP_FOR_EACH (port, node, &dp->ports) {
1298 error = netdev_get_mtu(port->netdev, &mtu);
1300 mtu = ETH_PAYLOAD_MAX;
1302 buf_size = DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + mtu;
1304 ofpbuf_clear(&packet);
1305 ofpbuf_reserve_with_tailroom(&packet, DP_NETDEV_HEADROOM, buf_size);
1307 error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1309 struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1310 dp_netdev_port_input(dp, &packet, &md);
1311 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1312 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1314 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1315 netdev_get_name(port->netdev), ovs_strerror(error));
1318 ofpbuf_uninit(&packet);
1319 ovs_mutex_unlock(&dp_netdev_mutex);
1323 dpif_netdev_wait(struct dpif *dpif)
1325 struct dp_netdev_port *port;
1327 /* There is a race here, if thread A calls dpif_netdev_wait(dpif) and
1328 * thread B calls dpif_port_add(dpif) or dpif_port_remove(dpif) before
1329 * A makes it to poll_block().
1331 * But I think it doesn't matter:
1333 * - In the dpif_port_add() case, A will not wake up when a packet
1334 * arrives on the new port, but this would also happen if the
1335 * ordering were reversed.
1337 * - In the dpif_port_remove() case, A might wake up spuriously, but
1338 * that is harmless. */
1340 ovs_mutex_lock(&dp_netdev_mutex);
1341 HMAP_FOR_EACH (port, node, &get_dp_netdev(dpif)->ports) {
1343 netdev_rx_wait(port->rx);
1346 ovs_mutex_unlock(&dp_netdev_mutex);
1350 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1351 odp_port_t out_port)
1353 struct dp_netdev_port *p = dp_netdev_lookup_port(dp, out_port);
1355 netdev_send(p->netdev, packet);
1360 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
1361 int queue_no, const struct flow *flow,
1362 const struct nlattr *userdata)
1364 struct dp_netdev_queue *q = &dp->queues[queue_no];
1365 if (q->head - q->tail < MAX_QUEUE_LEN) {
1366 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1367 struct dpif_upcall *upcall = &u->upcall;
1368 struct ofpbuf *buf = &u->buf;
1371 upcall->type = queue_no;
1373 /* Allocate buffer big enough for everything. */
1374 buf_size = ODPUTIL_FLOW_KEY_BYTES;
1376 buf_size += NLA_ALIGN(userdata->nla_len);
1378 ofpbuf_init(buf, buf_size);
1381 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1382 upcall->key = buf->data;
1383 upcall->key_len = buf->size;
1387 upcall->userdata = ofpbuf_put(buf, userdata,
1388 NLA_ALIGN(userdata->nla_len));
1391 /* Steal packet data. */
1392 ovs_assert(packet->source == OFPBUF_MALLOC);
1393 upcall->packet = *packet;
1394 ofpbuf_use(packet, NULL, 0);
1396 seq_change(dp->queue_seq);
1400 ovsthread_counter_inc(dp->n_lost, 1);
1405 struct dp_netdev_execute_aux {
1406 struct dp_netdev *dp;
1407 const struct flow *key;
1411 dp_execute_cb(void *aux_, struct ofpbuf *packet,
1412 const struct pkt_metadata *md OVS_UNUSED,
1413 const struct nlattr *a, bool may_steal)
1415 struct dp_netdev_execute_aux *aux = aux_;
1416 int type = nl_attr_type(a);
1418 switch ((enum ovs_action_attr)type) {
1419 case OVS_ACTION_ATTR_OUTPUT:
1420 dp_netdev_output_port(aux->dp, packet, u32_to_odp(nl_attr_get_u32(a)));
1423 case OVS_ACTION_ATTR_USERSPACE: {
1424 const struct nlattr *userdata;
1426 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1428 /* Make a copy if we are not allowed to steal the packet's data. */
1430 packet = ofpbuf_clone_with_headroom(packet, DP_NETDEV_HEADROOM);
1432 dp_netdev_output_userspace(aux->dp, packet, DPIF_UC_ACTION, aux->key,
1435 ofpbuf_uninit(packet);
1439 case OVS_ACTION_ATTR_PUSH_VLAN:
1440 case OVS_ACTION_ATTR_POP_VLAN:
1441 case OVS_ACTION_ATTR_PUSH_MPLS:
1442 case OVS_ACTION_ATTR_POP_MPLS:
1443 case OVS_ACTION_ATTR_SET:
1444 case OVS_ACTION_ATTR_SAMPLE:
1445 case OVS_ACTION_ATTR_UNSPEC:
1446 case __OVS_ACTION_ATTR_MAX:
1452 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
1453 struct ofpbuf *packet, struct pkt_metadata *md,
1454 const struct nlattr *actions, size_t actions_len)
1456 struct dp_netdev_execute_aux aux = {dp, key};
1458 odp_execute_actions(&aux, packet, md, actions, actions_len, dp_execute_cb);
1461 const struct dpif_class dpif_netdev_class = {
1463 dpif_netdev_enumerate,
1464 dpif_netdev_port_open_type,
1467 dpif_netdev_destroy,
1470 dpif_netdev_get_stats,
1471 dpif_netdev_port_add,
1472 dpif_netdev_port_del,
1473 dpif_netdev_port_query_by_number,
1474 dpif_netdev_port_query_by_name,
1475 NULL, /* port_get_pid */
1476 dpif_netdev_port_dump_start,
1477 dpif_netdev_port_dump_next,
1478 dpif_netdev_port_dump_done,
1479 dpif_netdev_port_poll,
1480 dpif_netdev_port_poll_wait,
1481 dpif_netdev_flow_get,
1482 dpif_netdev_flow_put,
1483 dpif_netdev_flow_del,
1484 dpif_netdev_flow_flush,
1485 dpif_netdev_flow_dump_start,
1486 dpif_netdev_flow_dump_next,
1487 dpif_netdev_flow_dump_done,
1488 dpif_netdev_execute,
1490 dpif_netdev_recv_set,
1491 dpif_netdev_queue_to_priority,
1493 dpif_netdev_recv_wait,
1494 dpif_netdev_recv_purge,
1498 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
1499 const char *argv[], void *aux OVS_UNUSED)
1501 struct dp_netdev_port *port;
1502 struct dp_netdev *dp;
1505 dp = shash_find_data(&dp_netdevs, argv[1]);
1506 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
1507 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
1511 if (get_port_by_name(dp, argv[2], &port)) {
1512 unixctl_command_reply_error(conn, "unknown port");
1516 port_no = u32_to_odp(atoi(argv[3]));
1517 if (!port_no || port_no == ODPP_NONE) {
1518 unixctl_command_reply_error(conn, "bad port number");
1521 if (dp_netdev_lookup_port(dp, port_no)) {
1522 unixctl_command_reply_error(conn, "port number already in use");
1525 hmap_remove(&dp->ports, &port->node);
1526 port->port_no = port_no;
1527 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
1528 seq_change(dp->port_seq);
1529 unixctl_command_reply(conn, NULL);
1533 dpif_dummy_register__(const char *type)
1535 struct dpif_class *class;
1537 class = xmalloc(sizeof *class);
1538 *class = dpif_netdev_class;
1539 class->type = xstrdup(type);
1540 dp_register_provider(class);
1544 dpif_dummy_register(bool override)
1551 dp_enumerate_types(&types);
1552 SSET_FOR_EACH (type, &types) {
1553 if (!dp_unregister_provider(type)) {
1554 dpif_dummy_register__(type);
1557 sset_destroy(&types);
1560 dpif_dummy_register__("dummy");
1562 unixctl_command_register("dpif-dummy/change-port-number",
1563 "DP PORT NEW-NUMBER",
1564 3, 3, dpif_dummy_change_port_number, NULL);