]> git.proxmox.com Git - mirror_ovs.git/blame - lib/dpif-netdev.c
dpif-netdev: Add poll-mode-device thread.
[mirror_ovs.git] / lib / dpif-netdev.c
CommitLineData
72865317 1/*
ff073a71 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
72865317
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18#include "dpif.h"
19
72865317
BP
20#include <ctype.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <inttypes.h>
72865317 24#include <netinet/in.h>
9d82ec47 25#include <sys/socket.h>
7f3adc00 26#include <net/if.h>
cdee00fd 27#include <stdint.h>
72865317
BP
28#include <stdlib.h>
29#include <string.h>
30#include <sys/ioctl.h>
31#include <sys/stat.h>
72865317
BP
32#include <unistd.h>
33
2c0ea78f 34#include "classifier.h"
72865317 35#include "csum.h"
614c4892 36#include "dpif.h"
72865317 37#include "dpif-provider.h"
614c4892 38#include "dummy.h"
36956a7d 39#include "dynamic-string.h"
72865317
BP
40#include "flow.h"
41#include "hmap.h"
6c3eee82 42#include "latch.h"
72865317 43#include "list.h"
8c301900 44#include "meta-flow.h"
72865317 45#include "netdev.h"
de281153 46#include "netdev-vport.h"
cdee00fd 47#include "netlink.h"
f094af7b 48#include "odp-execute.h"
72865317
BP
49#include "odp-util.h"
50#include "ofp-print.h"
51#include "ofpbuf.h"
61e7deb1 52#include "ovs-rcu.h"
72865317
BP
53#include "packets.h"
54#include "poll-loop.h"
26c6b6cd 55#include "random.h"
d33ed218 56#include "seq.h"
462278db 57#include "shash.h"
0cbfe35d 58#include "sset.h"
72865317 59#include "timeval.h"
74cc3969 60#include "unixctl.h"
72865317 61#include "util.h"
72865317 62#include "vlog.h"
5136ce49 63
d98e6007 64VLOG_DEFINE_THIS_MODULE(dpif_netdev);
72865317 65
2c0ea78f
GS
66/* By default, choose a priority in the middle. */
67#define NETDEV_RULE_PRIORITY 0x8000
68
e4cfed38
PS
69#define NR_THREADS 1
70
72865317 71/* Configuration parameters. */
72865317
BP
72enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
73
856081f6 74/* Queues. */
856081f6
BP
75enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
76enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
77BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
78
8a4e3a85
BP
79/* Protects against changes to 'dp_netdevs'. */
80static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
81
82/* Contains all 'struct dp_netdev's. */
83static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
84 = SHASH_INITIALIZER(&dp_netdevs);
85
d88b629b
BP
86struct dp_netdev_upcall {
87 struct dpif_upcall upcall; /* Queued upcall information. */
88 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
89};
90
63be20be 91/* A queue passing packets from a struct dp_netdev to its clients (handlers).
8a4e3a85
BP
92 *
93 *
94 * Thread-safety
95 * =============
96 *
63be20be
AW
97 * Any access at all requires the owning 'dp_netdev''s queue_rwlock and
98 * its own mutex. */
856081f6 99struct dp_netdev_queue {
63be20be
AW
100 struct ovs_mutex mutex;
101 struct seq *seq; /* Incremented whenever a packet is queued. */
f5126b57
BP
102 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
103 unsigned int head OVS_GUARDED;
104 unsigned int tail OVS_GUARDED;
856081f6
BP
105};
106
8a4e3a85
BP
107/* Datapath based on the network device interface from netdev.h.
108 *
109 *
110 * Thread-safety
111 * =============
112 *
113 * Some members, marked 'const', are immutable. Accessing other members
114 * requires synchronization, as noted in more detail below.
115 *
116 * Acquisition order is, from outermost to innermost:
117 *
118 * dp_netdev_mutex (global)
119 * port_rwlock
120 * flow_mutex
121 * cls.rwlock
63be20be 122 * queue_rwlock
8a4e3a85 123 */
72865317 124struct dp_netdev {
8a4e3a85
BP
125 const struct dpif_class *const class;
126 const char *const name;
6a8267c5
BP
127 struct ovs_refcount ref_cnt;
128 atomic_flag destroyed;
72865317 129
8a4e3a85
BP
130 /* Flows.
131 *
132 * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
133 *
134 * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
135 * the 'cls->rwlock' write lock. (The outer 'flow_mutex' allows writers to
136 * atomically perform multiple operations on 'cls' and 'flow_table'.)
137 */
138 struct ovs_mutex flow_mutex;
139 struct classifier cls; /* Classifier. Protected by cls.rwlock. */
140 struct hmap flow_table OVS_GUARDED; /* Flow table. */
141
142 /* Queues.
143 *
63be20be
AW
144 * 'queue_rwlock' protects the modification of 'handler_queues' and
145 * 'n_handlers'. The queue elements are protected by its
146 * 'handler_queues''s mutex. */
147 struct fat_rwlock queue_rwlock;
148 struct dp_netdev_queue *handler_queues;
149 uint32_t n_handlers;
72865317 150
8a4e3a85
BP
151 /* Statistics.
152 *
51852a57
BP
153 * ovsthread_stats is internally synchronized. */
154 struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
72865317 155
8a4e3a85
BP
156 /* Ports.
157 *
158 * Any lookup into 'ports' or any access to the dp_netdev_ports found
159 * through 'ports' requires taking 'port_rwlock'. */
160 struct ovs_rwlock port_rwlock;
161 struct hmap ports OVS_GUARDED;
d33ed218 162 struct seq *port_seq; /* Incremented whenever a port changes. */
6c3eee82
BP
163
164 /* Forwarding threads. */
165 struct latch exit_latch;
e4cfed38
PS
166 struct pmd_thread *pmd_threads;
167 size_t n_pmd_threads;
168 int pmd_count;
72865317
BP
169};
170
8a4e3a85
BP
171static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
172 odp_port_t)
173 OVS_REQ_RDLOCK(dp->port_rwlock);
ff073a71 174
51852a57
BP
175enum dp_stat_type {
176 DP_STAT_HIT, /* Packets that matched in the flow table. */
177 DP_STAT_MISS, /* Packets that did not match. */
178 DP_STAT_LOST, /* Packets not passed up to the client. */
179 DP_N_STATS
180};
181
182/* Contained by struct dp_netdev's 'stats' member. */
183struct dp_netdev_stats {
184 struct ovs_mutex mutex; /* Protects 'n'. */
185
186 /* Indexed by DP_STAT_*, protected by 'mutex'. */
187 unsigned long long int n[DP_N_STATS] OVS_GUARDED;
188};
189
190
72865317
BP
191/* A port in a netdev-based datapath. */
192struct dp_netdev_port {
ff073a71
BP
193 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
194 odp_port_t port_no;
72865317 195 struct netdev *netdev;
4b609110 196 struct netdev_saved_flags *sf;
796223f5 197 struct netdev_rx *rx;
b284085e 198 struct ovs_refcount ref_cnt;
0cbfe35d 199 char *type; /* Port type as requested by user. */
72865317
BP
200};
201
8a4e3a85
BP
202/* A flow in dp_netdev's 'flow_table'.
203 *
204 *
205 * Thread-safety
206 * =============
207 *
208 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
209 * its dp_netdev's classifier. The text below calls this classifier 'cls'.
210 *
211 * Motivation
212 * ----------
213 *
214 * The thread safety rules described here for "struct dp_netdev_flow" are
215 * motivated by two goals:
216 *
217 * - Prevent threads that read members of "struct dp_netdev_flow" from
218 * reading bad data due to changes by some thread concurrently modifying
219 * those members.
220 *
221 * - Prevent two threads making changes to members of a given "struct
222 * dp_netdev_flow" from interfering with each other.
223 *
224 *
225 * Rules
226 * -----
227 *
228 * A flow 'flow' may be accessed without a risk of being freed by code that
229 * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
230 * 'flow->ref_cnt' (or both). Code that needs to hold onto a flow for a while
231 * should take 'cls->rwlock', find the flow it needs, increment 'flow->ref_cnt'
232 * with dpif_netdev_flow_ref(), and drop 'cls->rwlock'.
233 *
234 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
235 * flow from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
236 * protect members of 'flow' from modification (that's 'flow->mutex').
237 *
238 * 'flow->mutex' protects the members of 'flow' from modification. It doesn't
239 * protect the flow from being deleted from 'cls' (that's 'cls->rwlock') and it
240 * doesn't prevent the flow from being freed (that's 'flow->ref_cnt').
241 *
242 * Some members, marked 'const', are immutable. Accessing other members
243 * requires synchronization, as noted in more detail below.
244 */
72865317 245struct dp_netdev_flow {
2c0ea78f 246 /* Packet classification. */
8a4e3a85 247 const struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
2c0ea78f 248
8a4e3a85
BP
249 /* Hash table index by unmasked flow. */
250 const struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
251 const struct flow flow; /* The flow that created this entry. */
72865317 252
8a4e3a85
BP
253 /* Protects members marked OVS_GUARDED.
254 *
255 * Acquire after datapath's flow_mutex. */
256 struct ovs_mutex mutex OVS_ACQ_AFTER(dp_netdev_mutex);
257
258 /* Statistics.
259 *
260 * Reading or writing these members requires 'mutex'. */
679ba04c 261 struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
8a4e3a85
BP
262
263 /* Actions.
264 *
265 * Reading 'actions' requires 'mutex'.
266 * Writing 'actions' requires 'mutex' and (to allow for transactions) the
267 * datapath's flow_mutex. */
61e7deb1 268 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
72865317
BP
269};
270
61e7deb1 271static void dp_netdev_flow_free(struct dp_netdev_flow *);
8a4e3a85 272
679ba04c
BP
273/* Contained by struct dp_netdev_flow's 'stats' member. */
274struct dp_netdev_flow_stats {
275 struct ovs_mutex mutex; /* Guards all the other members. */
276
277 long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
278 long long int packet_count OVS_GUARDED; /* Number of packets matched. */
279 long long int byte_count OVS_GUARDED; /* Number of bytes matched. */
280 uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
281};
282
a84cb64a
BP
283/* A set of datapath actions within a "struct dp_netdev_flow".
284 *
285 *
286 * Thread-safety
287 * =============
288 *
289 * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
290 * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
291 * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
292 * owns a reference to 'actions->ref_cnt' (or both). */
293struct dp_netdev_actions {
a84cb64a
BP
294 /* These members are immutable: they do not change during the struct's
295 * lifetime. */
296 struct nlattr *actions; /* Sequence of OVS_ACTION_ATTR_* attributes. */
297 unsigned int size; /* Size of 'actions', in bytes. */
298};
299
300struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
301 size_t);
61e7deb1
BP
302struct dp_netdev_actions *dp_netdev_flow_get_actions(
303 const struct dp_netdev_flow *);
304static void dp_netdev_actions_free(struct dp_netdev_actions *);
a84cb64a 305
e4cfed38
PS
306/* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
307 * the performance overhead of interrupt processing. Therefore netdev can
308 * not implement rx-wait for these devices. dpif-netdev needs to poll
309 * these device to check for recv buffer. pmd-thread does polling for
310 * devices assigned to itself thread.
311 *
312 * DPDK used PMD for accessing NIC.
313 *
314 * A thread that receives packets from PMD ports, looks them up in the flow
315 * table, and executes the actions it finds.
316 **/
317struct pmd_thread {
6c3eee82
BP
318 struct dp_netdev *dp;
319 pthread_t thread;
e4cfed38
PS
320 int id;
321 atomic_uint change_seq;
6c3eee82 322 char *name;
6c3eee82
BP
323};
324
72865317
BP
325/* Interface to netdev-based datapath. */
326struct dpif_netdev {
327 struct dpif dpif;
328 struct dp_netdev *dp;
d33ed218 329 uint64_t last_port_seq;
72865317
BP
330};
331
8a4e3a85
BP
332static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
333 struct dp_netdev_port **portp)
334 OVS_REQ_RDLOCK(dp->port_rwlock);
335static int get_port_by_name(struct dp_netdev *dp, const char *devname,
336 struct dp_netdev_port **portp)
337 OVS_REQ_RDLOCK(dp->port_rwlock);
338static void dp_netdev_free(struct dp_netdev *)
339 OVS_REQUIRES(dp_netdev_mutex);
72865317 340static void dp_netdev_flow_flush(struct dp_netdev *);
8a4e3a85
BP
341static int do_add_port(struct dp_netdev *dp, const char *devname,
342 const char *type, odp_port_t port_no)
343 OVS_REQ_WRLOCK(dp->port_rwlock);
344static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
345 OVS_REQ_WRLOCK(dp->port_rwlock);
63be20be
AW
346static void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
347 OVS_REQ_WRLOCK(dp->queue_rwlock);
614c4892
BP
348static int dpif_netdev_open(const struct dpif_class *, const char *name,
349 bool create, struct dpif **);
f5126b57 350static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
63be20be
AW
351 int queue_no, int type,
352 const struct flow *,
e4cfed38 353 const struct nlattr *userdata);
8a4e3a85 354static void dp_netdev_execute_actions(struct dp_netdev *dp,
df1e5a3b 355 const struct flow *, struct ofpbuf *, bool may_steal,
8a4e3a85 356 struct pkt_metadata *,
4edb9ae9 357 const struct nlattr *actions,
e4cfed38 358 size_t actions_len);
758c456d 359static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
e4cfed38
PS
360 struct pkt_metadata *);
361
362static void dp_netdev_set_pmd_threads(struct dp_netdev *, int n);
72865317
BP
363
364static struct dpif_netdev *
365dpif_netdev_cast(const struct dpif *dpif)
366{
cb22974d 367 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
72865317
BP
368 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
369}
370
371static struct dp_netdev *
372get_dp_netdev(const struct dpif *dpif)
373{
374 return dpif_netdev_cast(dpif)->dp;
375}
376
2197d7ab
GL
377static int
378dpif_netdev_enumerate(struct sset *all_dps)
379{
380 struct shash_node *node;
381
97be1538 382 ovs_mutex_lock(&dp_netdev_mutex);
2197d7ab
GL
383 SHASH_FOR_EACH(node, &dp_netdevs) {
384 sset_add(all_dps, node->name);
385 }
97be1538 386 ovs_mutex_unlock(&dp_netdev_mutex);
5279f8fd 387
2197d7ab
GL
388 return 0;
389}
390
add90f6f
EJ
391static bool
392dpif_netdev_class_is_dummy(const struct dpif_class *class)
393{
394 return class != &dpif_netdev_class;
395}
396
0aeaabc8
JP
397static const char *
398dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
399{
400 return strcmp(type, "internal") ? type
add90f6f 401 : dpif_netdev_class_is_dummy(class) ? "dummy"
0aeaabc8
JP
402 : "tap";
403}
404
72865317
BP
405static struct dpif *
406create_dpif_netdev(struct dp_netdev *dp)
407{
462278db 408 uint16_t netflow_id = hash_string(dp->name, 0);
72865317 409 struct dpif_netdev *dpif;
72865317 410
6a8267c5 411 ovs_refcount_ref(&dp->ref_cnt);
72865317 412
72865317 413 dpif = xmalloc(sizeof *dpif);
614c4892 414 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
72865317 415 dpif->dp = dp;
d33ed218 416 dpif->last_port_seq = seq_read(dp->port_seq);
72865317
BP
417
418 return &dpif->dpif;
419}
420
4e022ec0
AW
421/* Choose an unused, non-zero port number and return it on success.
422 * Return ODPP_NONE on failure. */
423static odp_port_t
e44768b7 424choose_port(struct dp_netdev *dp, const char *name)
8a4e3a85 425 OVS_REQ_RDLOCK(dp->port_rwlock)
e44768b7 426{
4e022ec0 427 uint32_t port_no;
e44768b7
JP
428
429 if (dp->class != &dpif_netdev_class) {
430 const char *p;
431 int start_no = 0;
432
433 /* If the port name begins with "br", start the number search at
434 * 100 to make writing tests easier. */
435 if (!strncmp(name, "br", 2)) {
436 start_no = 100;
437 }
438
439 /* If the port name contains a number, try to assign that port number.
440 * This can make writing unit tests easier because port numbers are
441 * predictable. */
442 for (p = name; *p != '\0'; p++) {
443 if (isdigit((unsigned char) *p)) {
444 port_no = start_no + strtol(p, NULL, 10);
ff073a71
BP
445 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
446 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
4e022ec0 447 return u32_to_odp(port_no);
e44768b7
JP
448 }
449 break;
450 }
451 }
452 }
453
ff073a71
BP
454 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
455 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
4e022ec0 456 return u32_to_odp(port_no);
e44768b7
JP
457 }
458 }
459
4e022ec0 460 return ODPP_NONE;
e44768b7
JP
461}
462
72865317 463static int
614c4892
BP
464create_dp_netdev(const char *name, const struct dpif_class *class,
465 struct dp_netdev **dpp)
8a4e3a85 466 OVS_REQUIRES(dp_netdev_mutex)
72865317
BP
467{
468 struct dp_netdev *dp;
469 int error;
72865317 470
462278db 471 dp = xzalloc(sizeof *dp);
8a4e3a85
BP
472 shash_add(&dp_netdevs, name, dp);
473
474 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
475 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
6a8267c5 476 ovs_refcount_init(&dp->ref_cnt);
1a65ba85 477 atomic_flag_clear(&dp->destroyed);
8a4e3a85
BP
478
479 ovs_mutex_init(&dp->flow_mutex);
480 classifier_init(&dp->cls, NULL);
481 hmap_init(&dp->flow_table);
482
63be20be 483 fat_rwlock_init(&dp->queue_rwlock);
ed27e010 484
51852a57 485 ovsthread_stats_init(&dp->stats);
ed27e010 486
8a4e3a85 487 ovs_rwlock_init(&dp->port_rwlock);
ff073a71 488 hmap_init(&dp->ports);
d33ed218 489 dp->port_seq = seq_create();
6c3eee82 490 latch_init(&dp->exit_latch);
e44768b7 491
8a4e3a85 492 ovs_rwlock_wrlock(&dp->port_rwlock);
4e022ec0 493 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
8a4e3a85 494 ovs_rwlock_unlock(&dp->port_rwlock);
72865317
BP
495 if (error) {
496 dp_netdev_free(dp);
462278db 497 return error;
72865317
BP
498 }
499
462278db 500 *dpp = dp;
72865317
BP
501 return 0;
502}
503
504static int
614c4892 505dpif_netdev_open(const struct dpif_class *class, const char *name,
4a387741 506 bool create, struct dpif **dpifp)
72865317 507{
462278db 508 struct dp_netdev *dp;
5279f8fd 509 int error;
462278db 510
97be1538 511 ovs_mutex_lock(&dp_netdev_mutex);
462278db
BP
512 dp = shash_find_data(&dp_netdevs, name);
513 if (!dp) {
5279f8fd 514 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
72865317 515 } else {
5279f8fd
BP
516 error = (dp->class != class ? EINVAL
517 : create ? EEXIST
518 : 0);
519 }
520 if (!error) {
521 *dpifp = create_dpif_netdev(dp);
72865317 522 }
97be1538 523 ovs_mutex_unlock(&dp_netdev_mutex);
462278db 524
5279f8fd 525 return error;
72865317
BP
526}
527
528static void
1ba530f4 529dp_netdev_purge_queues(struct dp_netdev *dp)
63be20be 530 OVS_REQ_WRLOCK(dp->queue_rwlock)
72865317
BP
531{
532 int i;
533
63be20be
AW
534 for (i = 0; i < dp->n_handlers; i++) {
535 struct dp_netdev_queue *q = &dp->handler_queues[i];
856081f6 536
63be20be 537 ovs_mutex_lock(&q->mutex);
1ba530f4 538 while (q->tail != q->head) {
d88b629b 539 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
da546e07 540 ofpbuf_uninit(&u->upcall.packet);
d88b629b 541 ofpbuf_uninit(&u->buf);
856081f6 542 }
63be20be 543 ovs_mutex_unlock(&q->mutex);
72865317 544 }
1ba530f4
BP
545}
546
8a4e3a85
BP
547/* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
548 * through the 'dp_netdevs' shash while freeing 'dp'. */
1ba530f4
BP
549static void
550dp_netdev_free(struct dp_netdev *dp)
8a4e3a85 551 OVS_REQUIRES(dp_netdev_mutex)
1ba530f4 552{
4ad28026 553 struct dp_netdev_port *port, *next;
51852a57
BP
554 struct dp_netdev_stats *bucket;
555 int i;
4ad28026 556
8a4e3a85
BP
557 shash_find_and_delete(&dp_netdevs, dp->name);
558
e4cfed38
PS
559 dp_netdev_set_pmd_threads(dp, 0);
560 free(dp->pmd_threads);
6c3eee82 561
1ba530f4 562 dp_netdev_flow_flush(dp);
8a4e3a85 563 ovs_rwlock_wrlock(&dp->port_rwlock);
ff073a71 564 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
1ba530f4
BP
565 do_del_port(dp, port->port_no);
566 }
8a4e3a85 567 ovs_rwlock_unlock(&dp->port_rwlock);
51852a57
BP
568
569 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
570 ovs_mutex_destroy(&bucket->mutex);
571 free_cacheline(bucket);
572 }
573 ovsthread_stats_destroy(&dp->stats);
f5126b57 574
63be20be
AW
575 fat_rwlock_wrlock(&dp->queue_rwlock);
576 dp_netdev_destroy_all_queues(dp);
577 fat_rwlock_unlock(&dp->queue_rwlock);
578
579 fat_rwlock_destroy(&dp->queue_rwlock);
f5126b57 580
2c0ea78f 581 classifier_destroy(&dp->cls);
72865317 582 hmap_destroy(&dp->flow_table);
8a4e3a85 583 ovs_mutex_destroy(&dp->flow_mutex);
d33ed218 584 seq_destroy(dp->port_seq);
ff073a71 585 hmap_destroy(&dp->ports);
6c3eee82 586 latch_destroy(&dp->exit_latch);
8a4e3a85 587 free(CONST_CAST(char *, dp->name));
72865317
BP
588 free(dp);
589}
590
8a4e3a85
BP
591static void
592dp_netdev_unref(struct dp_netdev *dp)
593{
594 if (dp) {
595 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
596 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
597 ovs_mutex_lock(&dp_netdev_mutex);
598 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
599 dp_netdev_free(dp);
600 }
601 ovs_mutex_unlock(&dp_netdev_mutex);
602 }
603}
604
72865317
BP
605static void
606dpif_netdev_close(struct dpif *dpif)
607{
608 struct dp_netdev *dp = get_dp_netdev(dpif);
5279f8fd 609
8a4e3a85 610 dp_netdev_unref(dp);
72865317
BP
611 free(dpif);
612}
613
614static int
7dab847a 615dpif_netdev_destroy(struct dpif *dpif)
72865317
BP
616{
617 struct dp_netdev *dp = get_dp_netdev(dpif);
5279f8fd 618
6a8267c5
BP
619 if (!atomic_flag_test_and_set(&dp->destroyed)) {
620 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
621 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
622 OVS_NOT_REACHED();
623 }
624 }
5279f8fd 625
72865317
BP
626 return 0;
627}
628
629static int
a8d9304d 630dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
72865317
BP
631{
632 struct dp_netdev *dp = get_dp_netdev(dpif);
51852a57
BP
633 struct dp_netdev_stats *bucket;
634 size_t i;
5279f8fd 635
06f81620 636 fat_rwlock_rdlock(&dp->cls.rwlock);
f180c2e2 637 stats->n_flows = hmap_count(&dp->flow_table);
06f81620 638 fat_rwlock_unlock(&dp->cls.rwlock);
8a4e3a85 639
51852a57
BP
640 stats->n_hit = stats->n_missed = stats->n_lost = 0;
641 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
642 ovs_mutex_lock(&bucket->mutex);
643 stats->n_hit += bucket->n[DP_STAT_HIT];
644 stats->n_missed += bucket->n[DP_STAT_MISS];
645 stats->n_lost += bucket->n[DP_STAT_LOST];
646 ovs_mutex_unlock(&bucket->mutex);
647 }
1ce3fa06 648 stats->n_masks = UINT32_MAX;
847108dc 649 stats->n_mask_hit = UINT64_MAX;
5279f8fd 650
72865317
BP
651 return 0;
652}
653
e4cfed38
PS
654static void
655dp_netdev_reload_pmd_threads(struct dp_netdev *dp)
656{
657 int i;
658
659 for (i = 0; i < dp->n_pmd_threads; i++) {
660 struct pmd_thread *f = &dp->pmd_threads[i];
661 int id;
662
663 atomic_add(&f->change_seq, 1, &id);
664 }
665}
666
72865317 667static int
c3827f61 668do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
4e022ec0 669 odp_port_t port_no)
8a4e3a85 670 OVS_REQ_WRLOCK(dp->port_rwlock)
72865317 671{
4b609110 672 struct netdev_saved_flags *sf;
72865317
BP
673 struct dp_netdev_port *port;
674 struct netdev *netdev;
2499a8ce 675 enum netdev_flags flags;
0cbfe35d 676 const char *open_type;
72865317
BP
677 int error;
678
679 /* XXX reject devices already in some dp_netdev. */
680
681 /* Open and validate network device. */
0aeaabc8 682 open_type = dpif_netdev_port_open_type(dp->class, type);
0cbfe35d 683 error = netdev_open(devname, open_type, &netdev);
72865317
BP
684 if (error) {
685 return error;
686 }
72865317
BP
687 /* XXX reject non-Ethernet devices */
688
2499a8ce
AC
689 netdev_get_flags(netdev, &flags);
690 if (flags & NETDEV_LOOPBACK) {
691 VLOG_ERR("%s: cannot add a loopback device", devname);
692 netdev_close(netdev);
693 return EINVAL;
694 }
695
e4cfed38
PS
696 port = xzalloc(sizeof *port);
697 port->port_no = port_no;
698 port->netdev = netdev;
699 port->type = xstrdup(type);
700 error = netdev_rx_open(netdev, &port->rx);
add90f6f
EJ
701 if (error
702 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
7b6b0ef4 703 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
10a89ef0 704 devname, ovs_strerror(errno));
7b6b0ef4
BP
705 netdev_close(netdev);
706 return error;
707 }
708
4b609110 709 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
72865317 710 if (error) {
e4cfed38 711 netdev_rx_close(port->rx);
72865317 712 netdev_close(netdev);
e4cfed38
PS
713 free(port->rx);
714 free(port);
72865317
BP
715 return error;
716 }
4b609110 717 port->sf = sf;
e4cfed38
PS
718
719 if (netdev_is_pmd(netdev)) {
720 dp->pmd_count++;
721 dp_netdev_set_pmd_threads(dp, NR_THREADS);
722 dp_netdev_reload_pmd_threads(dp);
723 }
724 ovs_refcount_init(&port->ref_cnt);
72865317 725
ff073a71 726 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
d33ed218 727 seq_change(dp->port_seq);
72865317
BP
728
729 return 0;
730}
731
247527db
BP
732static int
733dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
4e022ec0 734 odp_port_t *port_nop)
247527db
BP
735{
736 struct dp_netdev *dp = get_dp_netdev(dpif);
3aa30359
BP
737 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
738 const char *dpif_port;
4e022ec0 739 odp_port_t port_no;
5279f8fd 740 int error;
247527db 741
8a4e3a85 742 ovs_rwlock_wrlock(&dp->port_rwlock);
3aa30359 743 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
4e022ec0 744 if (*port_nop != ODPP_NONE) {
ff073a71
BP
745 port_no = *port_nop;
746 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
232dfa4a 747 } else {
3aa30359 748 port_no = choose_port(dp, dpif_port);
5279f8fd 749 error = port_no == ODPP_NONE ? EFBIG : 0;
232dfa4a 750 }
5279f8fd 751 if (!error) {
247527db 752 *port_nop = port_no;
5279f8fd 753 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
247527db 754 }
8a4e3a85 755 ovs_rwlock_unlock(&dp->port_rwlock);
5279f8fd
BP
756
757 return error;
72865317
BP
758}
759
760static int
4e022ec0 761dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
72865317
BP
762{
763 struct dp_netdev *dp = get_dp_netdev(dpif);
5279f8fd
BP
764 int error;
765
8a4e3a85 766 ovs_rwlock_wrlock(&dp->port_rwlock);
5279f8fd 767 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
8a4e3a85 768 ovs_rwlock_unlock(&dp->port_rwlock);
5279f8fd
BP
769
770 return error;
72865317
BP
771}
772
773static bool
4e022ec0 774is_valid_port_number(odp_port_t port_no)
72865317 775{
ff073a71
BP
776 return port_no != ODPP_NONE;
777}
778
779static struct dp_netdev_port *
780dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
8a4e3a85 781 OVS_REQ_RDLOCK(dp->port_rwlock)
ff073a71
BP
782{
783 struct dp_netdev_port *port;
784
785 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
786 &dp->ports) {
787 if (port->port_no == port_no) {
788 return port;
789 }
790 }
791 return NULL;
72865317
BP
792}
793
794static int
795get_port_by_number(struct dp_netdev *dp,
4e022ec0 796 odp_port_t port_no, struct dp_netdev_port **portp)
8a4e3a85 797 OVS_REQ_RDLOCK(dp->port_rwlock)
72865317
BP
798{
799 if (!is_valid_port_number(port_no)) {
800 *portp = NULL;
801 return EINVAL;
802 } else {
ff073a71 803 *portp = dp_netdev_lookup_port(dp, port_no);
72865317
BP
804 return *portp ? 0 : ENOENT;
805 }
806}
807
b284085e
PS
808static void
809port_ref(struct dp_netdev_port *port)
810{
811 if (port) {
812 ovs_refcount_ref(&port->ref_cnt);
813 }
814}
815
816static void
817port_unref(struct dp_netdev_port *port)
818{
819 if (port && ovs_refcount_unref(&port->ref_cnt) == 1) {
820 netdev_close(port->netdev);
821 netdev_restore_flags(port->sf);
822 netdev_rx_close(port->rx);
823 free(port->type);
824 free(port);
825 }
826}
827
72865317
BP
828static int
829get_port_by_name(struct dp_netdev *dp,
830 const char *devname, struct dp_netdev_port **portp)
8a4e3a85 831 OVS_REQ_RDLOCK(dp->port_rwlock)
72865317
BP
832{
833 struct dp_netdev_port *port;
834
ff073a71 835 HMAP_FOR_EACH (port, node, &dp->ports) {
3efb6063 836 if (!strcmp(netdev_get_name(port->netdev), devname)) {
72865317
BP
837 *portp = port;
838 return 0;
839 }
840 }
841 return ENOENT;
842}
843
844static int
4e022ec0 845do_del_port(struct dp_netdev *dp, odp_port_t port_no)
8a4e3a85 846 OVS_REQ_WRLOCK(dp->port_rwlock)
72865317
BP
847{
848 struct dp_netdev_port *port;
849 int error;
850
851 error = get_port_by_number(dp, port_no, &port);
852 if (error) {
853 return error;
854 }
855
ff073a71 856 hmap_remove(&dp->ports, &port->node);
d33ed218 857 seq_change(dp->port_seq);
e4cfed38
PS
858 if (netdev_is_pmd(port->netdev)) {
859 dp_netdev_reload_pmd_threads(dp);
860 }
72865317 861
b284085e 862 port_unref(port);
72865317
BP
863 return 0;
864}
865
866static void
4c738a8d
BP
867answer_port_query(const struct dp_netdev_port *port,
868 struct dpif_port *dpif_port)
72865317 869{
3efb6063 870 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
0cbfe35d 871 dpif_port->type = xstrdup(port->type);
4c738a8d 872 dpif_port->port_no = port->port_no;
72865317
BP
873}
874
875static int
4e022ec0 876dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
4c738a8d 877 struct dpif_port *dpif_port)
72865317
BP
878{
879 struct dp_netdev *dp = get_dp_netdev(dpif);
880 struct dp_netdev_port *port;
881 int error;
882
8a4e3a85 883 ovs_rwlock_rdlock(&dp->port_rwlock);
72865317 884 error = get_port_by_number(dp, port_no, &port);
4afba28d 885 if (!error && dpif_port) {
4c738a8d 886 answer_port_query(port, dpif_port);
72865317 887 }
8a4e3a85 888 ovs_rwlock_unlock(&dp->port_rwlock);
5279f8fd 889
72865317
BP
890 return error;
891}
892
893static int
894dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
4c738a8d 895 struct dpif_port *dpif_port)
72865317
BP
896{
897 struct dp_netdev *dp = get_dp_netdev(dpif);
898 struct dp_netdev_port *port;
899 int error;
900
8a4e3a85 901 ovs_rwlock_rdlock(&dp->port_rwlock);
72865317 902 error = get_port_by_name(dp, devname, &port);
4afba28d 903 if (!error && dpif_port) {
4c738a8d 904 answer_port_query(port, dpif_port);
72865317 905 }
8a4e3a85 906 ovs_rwlock_unlock(&dp->port_rwlock);
5279f8fd 907
72865317
BP
908 return error;
909}
910
61e7deb1
BP
911static void
912dp_netdev_flow_free(struct dp_netdev_flow *flow)
913{
914 struct dp_netdev_flow_stats *bucket;
915 size_t i;
916
917 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
918 ovs_mutex_destroy(&bucket->mutex);
919 free_cacheline(bucket);
920 }
921 ovsthread_stats_destroy(&flow->stats);
922
923 cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
924 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
925 ovs_mutex_destroy(&flow->mutex);
926 free(flow);
927}
928
72865317 929static void
8a4e3a85
BP
930dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
931 OVS_REQ_WRLOCK(dp->cls.rwlock)
932 OVS_REQUIRES(dp->flow_mutex)
72865317 933{
8a4e3a85
BP
934 struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
935 struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
2c0ea78f 936
8a4e3a85
BP
937 classifier_remove(&dp->cls, cr);
938 hmap_remove(&dp->flow_table, node);
61e7deb1 939 ovsrcu_postpone(dp_netdev_flow_free, flow);
72865317
BP
940}
941
942static void
943dp_netdev_flow_flush(struct dp_netdev *dp)
944{
1763b4b8 945 struct dp_netdev_flow *netdev_flow, *next;
72865317 946
8a4e3a85 947 ovs_mutex_lock(&dp->flow_mutex);
06f81620 948 fat_rwlock_wrlock(&dp->cls.rwlock);
1763b4b8 949 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
8a4e3a85 950 dp_netdev_remove_flow(dp, netdev_flow);
72865317 951 }
06f81620 952 fat_rwlock_unlock(&dp->cls.rwlock);
8a4e3a85 953 ovs_mutex_unlock(&dp->flow_mutex);
72865317
BP
954}
955
956static int
957dpif_netdev_flow_flush(struct dpif *dpif)
958{
959 struct dp_netdev *dp = get_dp_netdev(dpif);
5279f8fd 960
72865317
BP
961 dp_netdev_flow_flush(dp);
962 return 0;
963}
964
b0ec0f27 965struct dp_netdev_port_state {
ff073a71
BP
966 uint32_t bucket;
967 uint32_t offset;
4c738a8d 968 char *name;
b0ec0f27
BP
969};
970
971static int
972dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
973{
974 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
975 return 0;
976}
977
72865317 978static int
b0ec0f27 979dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
4c738a8d 980 struct dpif_port *dpif_port)
72865317 981{
b0ec0f27 982 struct dp_netdev_port_state *state = state_;
72865317 983 struct dp_netdev *dp = get_dp_netdev(dpif);
ff073a71
BP
984 struct hmap_node *node;
985 int retval;
72865317 986
8a4e3a85 987 ovs_rwlock_rdlock(&dp->port_rwlock);
ff073a71
BP
988 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
989 if (node) {
990 struct dp_netdev_port *port;
5279f8fd 991
ff073a71
BP
992 port = CONTAINER_OF(node, struct dp_netdev_port, node);
993
994 free(state->name);
995 state->name = xstrdup(netdev_get_name(port->netdev));
996 dpif_port->name = state->name;
997 dpif_port->type = port->type;
998 dpif_port->port_no = port->port_no;
999
1000 retval = 0;
1001 } else {
1002 retval = EOF;
72865317 1003 }
8a4e3a85 1004 ovs_rwlock_unlock(&dp->port_rwlock);
5279f8fd 1005
ff073a71 1006 return retval;
b0ec0f27
BP
1007}
1008
1009static int
4c738a8d 1010dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
b0ec0f27 1011{
4c738a8d
BP
1012 struct dp_netdev_port_state *state = state_;
1013 free(state->name);
b0ec0f27
BP
1014 free(state);
1015 return 0;
72865317
BP
1016}
1017
1018static int
67a4917b 1019dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
72865317
BP
1020{
1021 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
d33ed218 1022 uint64_t new_port_seq;
5279f8fd
BP
1023 int error;
1024
d33ed218
BP
1025 new_port_seq = seq_read(dpif->dp->port_seq);
1026 if (dpif->last_port_seq != new_port_seq) {
1027 dpif->last_port_seq = new_port_seq;
5279f8fd 1028 error = ENOBUFS;
72865317 1029 } else {
5279f8fd 1030 error = EAGAIN;
72865317 1031 }
5279f8fd
BP
1032
1033 return error;
72865317
BP
1034}
1035
1036static void
1037dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1038{
1039 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
5279f8fd 1040
d33ed218 1041 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
8a4e3a85
BP
1042}
1043
1044static struct dp_netdev_flow *
1045dp_netdev_flow_cast(const struct cls_rule *cr)
1046{
1047 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
72865317
BP
1048}
1049
72865317 1050static struct dp_netdev_flow *
2c0ea78f 1051dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
8a4e3a85 1052 OVS_EXCLUDED(dp->cls.rwlock)
2c0ea78f 1053{
8a4e3a85 1054 struct dp_netdev_flow *netdev_flow;
2c0ea78f 1055
06f81620 1056 fat_rwlock_rdlock(&dp->cls.rwlock);
8a4e3a85 1057 netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
06f81620 1058 fat_rwlock_unlock(&dp->cls.rwlock);
2c0ea78f 1059
8a4e3a85 1060 return netdev_flow;
2c0ea78f
GS
1061}
1062
1063static struct dp_netdev_flow *
1064dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
8a4e3a85 1065 OVS_REQ_RDLOCK(dp->cls.rwlock)
72865317 1066{
1763b4b8 1067 struct dp_netdev_flow *netdev_flow;
72865317 1068
2c0ea78f 1069 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1763b4b8 1070 &dp->flow_table) {
2c0ea78f 1071 if (flow_equal(&netdev_flow->flow, flow)) {
61e7deb1 1072 return netdev_flow;
72865317
BP
1073 }
1074 }
8a4e3a85 1075
72865317
BP
1076 return NULL;
1077}
1078
1079static void
1763b4b8
GS
1080get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1081 struct dpif_flow_stats *stats)
feebdea2 1082{
679ba04c
BP
1083 struct dp_netdev_flow_stats *bucket;
1084 size_t i;
1085
1086 memset(stats, 0, sizeof *stats);
1087 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1088 ovs_mutex_lock(&bucket->mutex);
1089 stats->n_packets += bucket->packet_count;
1090 stats->n_bytes += bucket->byte_count;
1091 stats->used = MAX(stats->used, bucket->used);
1092 stats->tcp_flags |= bucket->tcp_flags;
1093 ovs_mutex_unlock(&bucket->mutex);
1094 }
72865317
BP
1095}
1096
36956a7d 1097static int
8c301900
JR
1098dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1099 const struct nlattr *mask_key,
1100 uint32_t mask_key_len, const struct flow *flow,
1101 struct flow *mask)
1102{
1103 if (mask_key_len) {
80e44883
BP
1104 enum odp_key_fitness fitness;
1105
1106 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1107 if (fitness) {
8c301900
JR
1108 /* This should not happen: it indicates that
1109 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1110 * disagree on the acceptable form of a mask. Log the problem
1111 * as an error, with enough details to enable debugging. */
1112 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1113
1114 if (!VLOG_DROP_ERR(&rl)) {
1115 struct ds s;
1116
1117 ds_init(&s);
1118 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1119 true);
80e44883
BP
1120 VLOG_ERR("internal error parsing flow mask %s (%s)",
1121 ds_cstr(&s), odp_key_fitness_to_string(fitness));
8c301900
JR
1122 ds_destroy(&s);
1123 }
1124
1125 return EINVAL;
1126 }
1127 /* Force unwildcard the in_port. */
1128 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1129 } else {
1130 enum mf_field_id id;
1131 /* No mask key, unwildcard everything except fields whose
1132 * prerequisities are not met. */
1133 memset(mask, 0x0, sizeof *mask);
1134
1135 for (id = 0; id < MFF_N_IDS; ++id) {
1136 /* Skip registers and metadata. */
1137 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1138 && id != MFF_METADATA) {
1139 const struct mf_field *mf = mf_from_id(id);
1140 if (mf_are_prereqs_ok(mf, flow)) {
1141 mf_mask_field(mf, mask);
1142 }
1143 }
1144 }
1145 }
1146
1147 return 0;
1148}
1149
1150static int
1151dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1152 struct flow *flow)
36956a7d 1153{
586ddea5
BP
1154 odp_port_t in_port;
1155
8c301900 1156 if (odp_flow_key_to_flow(key, key_len, flow)) {
36956a7d 1157 /* This should not happen: it indicates that odp_flow_key_from_flow()
8c301900
JR
1158 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1159 * flow. Log the problem as an error, with enough details to enable
1160 * debugging. */
36956a7d
BP
1161 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1162
1163 if (!VLOG_DROP_ERR(&rl)) {
1164 struct ds s;
1165
1166 ds_init(&s);
8c301900 1167 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
36956a7d
BP
1168 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1169 ds_destroy(&s);
1170 }
1171
1172 return EINVAL;
1173 }
1174
586ddea5
BP
1175 in_port = flow->in_port.odp_port;
1176 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
18886b60
BP
1177 return EINVAL;
1178 }
1179
36956a7d
BP
1180 return 0;
1181}
1182
72865317 1183static int
693c4a01 1184dpif_netdev_flow_get(const struct dpif *dpif,
feebdea2 1185 const struct nlattr *nl_key, size_t nl_key_len,
c97fb132 1186 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
72865317
BP
1187{
1188 struct dp_netdev *dp = get_dp_netdev(dpif);
1763b4b8 1189 struct dp_netdev_flow *netdev_flow;
bc4a05c6
BP
1190 struct flow key;
1191 int error;
36956a7d 1192
feebdea2 1193 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
bc4a05c6
BP
1194 if (error) {
1195 return error;
1196 }
14608a15 1197
06f81620 1198 fat_rwlock_rdlock(&dp->cls.rwlock);
2c0ea78f 1199 netdev_flow = dp_netdev_find_flow(dp, &key);
06f81620 1200 fat_rwlock_unlock(&dp->cls.rwlock);
8a4e3a85 1201
1763b4b8 1202 if (netdev_flow) {
5279f8fd 1203 if (stats) {
1763b4b8 1204 get_dpif_flow_stats(netdev_flow, stats);
5279f8fd 1205 }
679ba04c 1206
5279f8fd 1207 if (actionsp) {
61e7deb1 1208 struct dp_netdev_actions *actions;
8a4e3a85 1209
61e7deb1 1210 actions = dp_netdev_flow_get_actions(netdev_flow);
8a4e3a85 1211 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
5279f8fd 1212 }
61e7deb1 1213 } else {
5279f8fd 1214 error = ENOENT;
72865317 1215 }
bc4a05c6 1216
5279f8fd 1217 return error;
72865317
BP
1218}
1219
72865317 1220static int
2c0ea78f
GS
1221dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1222 const struct flow_wildcards *wc,
1223 const struct nlattr *actions,
1224 size_t actions_len)
8a4e3a85 1225 OVS_REQUIRES(dp->flow_mutex)
72865317 1226{
1763b4b8 1227 struct dp_netdev_flow *netdev_flow;
2c0ea78f 1228 struct match match;
72865317 1229
1763b4b8 1230 netdev_flow = xzalloc(sizeof *netdev_flow);
8a4e3a85 1231 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
8a4e3a85
BP
1232
1233 ovs_mutex_init(&netdev_flow->mutex);
8a4e3a85 1234
679ba04c
BP
1235 ovsthread_stats_init(&netdev_flow->stats);
1236
61e7deb1
BP
1237 ovsrcu_set(&netdev_flow->actions,
1238 dp_netdev_actions_create(actions, actions_len));
2c0ea78f
GS
1239
1240 match_init(&match, flow, wc);
8a4e3a85
BP
1241 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1242 &match, NETDEV_RULE_PRIORITY);
06f81620 1243 fat_rwlock_wrlock(&dp->cls.rwlock);
8a4e3a85
BP
1244 classifier_insert(&dp->cls,
1245 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1246 hmap_insert(&dp->flow_table,
1247 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1248 flow_hash(flow, 0));
06f81620 1249 fat_rwlock_unlock(&dp->cls.rwlock);
72865317 1250
72865317
BP
1251 return 0;
1252}
1253
1254static void
1763b4b8 1255clear_stats(struct dp_netdev_flow *netdev_flow)
72865317 1256{
679ba04c
BP
1257 struct dp_netdev_flow_stats *bucket;
1258 size_t i;
1259
1260 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1261 ovs_mutex_lock(&bucket->mutex);
1262 bucket->used = 0;
1263 bucket->packet_count = 0;
1264 bucket->byte_count = 0;
1265 bucket->tcp_flags = 0;
1266 ovs_mutex_unlock(&bucket->mutex);
1267 }
72865317
BP
1268}
1269
1270static int
89625d1e 1271dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
72865317
BP
1272{
1273 struct dp_netdev *dp = get_dp_netdev(dpif);
1763b4b8 1274 struct dp_netdev_flow *netdev_flow;
2c0ea78f
GS
1275 struct flow flow;
1276 struct flow_wildcards wc;
36956a7d
BP
1277 int error;
1278
8c301900
JR
1279 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1280 if (error) {
1281 return error;
1282 }
1283 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1284 put->mask, put->mask_len,
1285 &flow, &wc.masks);
36956a7d
BP
1286 if (error) {
1287 return error;
1288 }
72865317 1289
8a4e3a85 1290 ovs_mutex_lock(&dp->flow_mutex);
2c0ea78f 1291 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1763b4b8 1292 if (!netdev_flow) {
89625d1e 1293 if (put->flags & DPIF_FP_CREATE) {
72865317 1294 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
89625d1e
BP
1295 if (put->stats) {
1296 memset(put->stats, 0, sizeof *put->stats);
feebdea2 1297 }
2c0ea78f 1298 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
5279f8fd 1299 put->actions_len);
72865317 1300 } else {
5279f8fd 1301 error = EFBIG;
72865317
BP
1302 }
1303 } else {
5279f8fd 1304 error = ENOENT;
72865317
BP
1305 }
1306 } else {
2c0ea78f
GS
1307 if (put->flags & DPIF_FP_MODIFY
1308 && flow_equal(&flow, &netdev_flow->flow)) {
8a4e3a85
BP
1309 struct dp_netdev_actions *new_actions;
1310 struct dp_netdev_actions *old_actions;
1311
1312 new_actions = dp_netdev_actions_create(put->actions,
1313 put->actions_len);
1314
61e7deb1
BP
1315 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1316 ovsrcu_set(&netdev_flow->actions, new_actions);
679ba04c 1317
a84cb64a
BP
1318 if (put->stats) {
1319 get_dpif_flow_stats(netdev_flow, put->stats);
1320 }
1321 if (put->flags & DPIF_FP_ZERO_STATS) {
1322 clear_stats(netdev_flow);
72865317 1323 }
8a4e3a85 1324
61e7deb1 1325 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
2c0ea78f 1326 } else if (put->flags & DPIF_FP_CREATE) {
5279f8fd 1327 error = EEXIST;
2c0ea78f
GS
1328 } else {
1329 /* Overlapping flow. */
1330 error = EINVAL;
72865317
BP
1331 }
1332 }
8a4e3a85 1333 ovs_mutex_unlock(&dp->flow_mutex);
5279f8fd
BP
1334
1335 return error;
72865317
BP
1336}
1337
72865317 1338static int
b99d3cee 1339dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
72865317
BP
1340{
1341 struct dp_netdev *dp = get_dp_netdev(dpif);
1763b4b8 1342 struct dp_netdev_flow *netdev_flow;
14608a15 1343 struct flow key;
36956a7d
BP
1344 int error;
1345
b99d3cee 1346 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
36956a7d
BP
1347 if (error) {
1348 return error;
1349 }
72865317 1350
8a4e3a85 1351 ovs_mutex_lock(&dp->flow_mutex);
06f81620 1352 fat_rwlock_wrlock(&dp->cls.rwlock);
2c0ea78f 1353 netdev_flow = dp_netdev_find_flow(dp, &key);
1763b4b8 1354 if (netdev_flow) {
b99d3cee 1355 if (del->stats) {
1763b4b8 1356 get_dpif_flow_stats(netdev_flow, del->stats);
feebdea2 1357 }
8a4e3a85 1358 dp_netdev_remove_flow(dp, netdev_flow);
72865317 1359 } else {
5279f8fd 1360 error = ENOENT;
72865317 1361 }
06f81620 1362 fat_rwlock_unlock(&dp->cls.rwlock);
8a4e3a85 1363 ovs_mutex_unlock(&dp->flow_mutex);
5279f8fd
BP
1364
1365 return error;
72865317
BP
1366}
1367
704a1e09 1368struct dp_netdev_flow_state {
a84cb64a 1369 struct dp_netdev_actions *actions;
19cf4069 1370 struct odputil_keybuf keybuf;
2c0ea78f 1371 struct odputil_keybuf maskbuf;
c97fb132 1372 struct dpif_flow_stats stats;
704a1e09
BP
1373};
1374
e723fd32
JS
1375struct dp_netdev_flow_iter {
1376 uint32_t bucket;
1377 uint32_t offset;
d2ad7ef1
JS
1378 int status;
1379 struct ovs_mutex mutex;
e723fd32
JS
1380};
1381
1382static void
1383dpif_netdev_flow_dump_state_init(void **statep)
72865317 1384{
feebdea2
BP
1385 struct dp_netdev_flow_state *state;
1386
1387 *statep = state = xmalloc(sizeof *state);
feebdea2 1388 state->actions = NULL;
e723fd32
JS
1389}
1390
1391static void
1392dpif_netdev_flow_dump_state_uninit(void *state_)
1393{
1394 struct dp_netdev_flow_state *state = state_;
1395
e723fd32
JS
1396 free(state);
1397}
1398
1399static int
1400dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1401{
1402 struct dp_netdev_flow_iter *iter;
1403
1404 *iterp = iter = xmalloc(sizeof *iter);
1405 iter->bucket = 0;
1406 iter->offset = 0;
d2ad7ef1
JS
1407 iter->status = 0;
1408 ovs_mutex_init(&iter->mutex);
704a1e09
BP
1409 return 0;
1410}
1411
61e7deb1 1412/* XXX the caller must use 'actions' without quiescing */
704a1e09 1413static int
d2ad7ef1 1414dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
feebdea2 1415 const struct nlattr **key, size_t *key_len,
e6cc0bab 1416 const struct nlattr **mask, size_t *mask_len,
feebdea2 1417 const struct nlattr **actions, size_t *actions_len,
c97fb132 1418 const struct dpif_flow_stats **stats)
704a1e09 1419{
e723fd32 1420 struct dp_netdev_flow_iter *iter = iter_;
d2ad7ef1 1421 struct dp_netdev_flow_state *state = state_;
72865317 1422 struct dp_netdev *dp = get_dp_netdev(dpif);
1763b4b8 1423 struct dp_netdev_flow *netdev_flow;
d2ad7ef1 1424 int error;
14608a15 1425
d2ad7ef1
JS
1426 ovs_mutex_lock(&iter->mutex);
1427 error = iter->status;
1428 if (!error) {
1429 struct hmap_node *node;
1430
1431 fat_rwlock_rdlock(&dp->cls.rwlock);
1432 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1433 if (node) {
1434 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
d2ad7ef1
JS
1435 }
1436 fat_rwlock_unlock(&dp->cls.rwlock);
1437 if (!node) {
1438 iter->status = error = EOF;
1439 }
8a4e3a85 1440 }
d2ad7ef1
JS
1441 ovs_mutex_unlock(&iter->mutex);
1442 if (error) {
1443 return error;
72865317 1444 }
704a1e09 1445
feebdea2
BP
1446 if (key) {
1447 struct ofpbuf buf;
1448
19cf4069 1449 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
2c0ea78f
GS
1450 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1451 netdev_flow->flow.in_port.odp_port);
36956a7d 1452
feebdea2
BP
1453 *key = buf.data;
1454 *key_len = buf.size;
1455 }
1456
2c0ea78f
GS
1457 if (key && mask) {
1458 struct ofpbuf buf;
1459 struct flow_wildcards wc;
1460
1461 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1462 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1463 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
8bfd0fda
BP
1464 odp_to_u32(wc.masks.in_port.odp_port),
1465 SIZE_MAX);
2c0ea78f
GS
1466
1467 *mask = buf.data;
1468 *mask_len = buf.size;
e6cc0bab
AZ
1469 }
1470
8a4e3a85 1471 if (actions || stats) {
8a4e3a85 1472 state->actions = NULL;
feebdea2 1473
8a4e3a85 1474 if (actions) {
61e7deb1 1475 state->actions = dp_netdev_flow_get_actions(netdev_flow);
8a4e3a85
BP
1476 *actions = state->actions->actions;
1477 *actions_len = state->actions->size;
1478 }
679ba04c 1479
8a4e3a85
BP
1480 if (stats) {
1481 get_dpif_flow_stats(netdev_flow, &state->stats);
1482 *stats = &state->stats;
1483 }
feebdea2 1484 }
704a1e09
BP
1485
1486 return 0;
1487}
1488
1489static int
e723fd32 1490dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
704a1e09 1491{
e723fd32 1492 struct dp_netdev_flow_iter *iter = iter_;
feebdea2 1493
d2ad7ef1 1494 ovs_mutex_destroy(&iter->mutex);
e723fd32 1495 free(iter);
704a1e09 1496 return 0;
72865317
BP
1497}
1498
1499static int
758c456d 1500dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
72865317
BP
1501{
1502 struct dp_netdev *dp = get_dp_netdev(dpif);
758c456d
JR
1503 struct pkt_metadata *md = &execute->md;
1504 struct flow key;
72865317 1505
89625d1e
BP
1506 if (execute->packet->size < ETH_HEADER_LEN ||
1507 execute->packet->size > UINT16_MAX) {
72865317
BP
1508 return EINVAL;
1509 }
1510
758c456d 1511 /* Extract flow key. */
b5e7e61a 1512 flow_extract(execute->packet, md, &key);
8a4e3a85
BP
1513
1514 ovs_rwlock_rdlock(&dp->port_rwlock);
df1e5a3b
PS
1515 dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1516 execute->actions, execute->actions_len);
8a4e3a85
BP
1517 ovs_rwlock_unlock(&dp->port_rwlock);
1518
758c456d 1519 return 0;
72865317
BP
1520}
1521
63be20be
AW
1522static void
1523dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1524 OVS_REQ_WRLOCK(dp->queue_rwlock)
1525{
1526 size_t i;
1527
1528 dp_netdev_purge_queues(dp);
1529
1530 for (i = 0; i < dp->n_handlers; i++) {
1531 struct dp_netdev_queue *q = &dp->handler_queues[i];
1532
1533 ovs_mutex_destroy(&q->mutex);
1534 seq_destroy(q->seq);
1535 }
1536 free(dp->handler_queues);
1537 dp->handler_queues = NULL;
1538 dp->n_handlers = 0;
1539}
1540
1541static void
1542dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1543 OVS_REQ_WRLOCK(dp->queue_rwlock)
1544{
1545 if (dp->n_handlers != n_handlers) {
1546 size_t i;
1547
1548 dp_netdev_destroy_all_queues(dp);
1549
1550 dp->n_handlers = n_handlers;
1551 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1552
1553 for (i = 0; i < n_handlers; i++) {
1554 struct dp_netdev_queue *q = &dp->handler_queues[i];
1555
1556 ovs_mutex_init(&q->mutex);
1557 q->seq = seq_create();
1558 }
1559 }
1560}
1561
72865317 1562static int
63be20be 1563dpif_netdev_recv_set(struct dpif *dpif, bool enable)
72865317 1564{
63be20be
AW
1565 struct dp_netdev *dp = get_dp_netdev(dpif);
1566
1567 if ((dp->handler_queues != NULL) == enable) {
1568 return 0;
1569 }
1570
1571 fat_rwlock_wrlock(&dp->queue_rwlock);
1572 if (!enable) {
1573 dp_netdev_destroy_all_queues(dp);
1574 } else {
1575 dp_netdev_refresh_queues(dp, 1);
1576 }
1577 fat_rwlock_unlock(&dp->queue_rwlock);
1578
82272ede 1579 return 0;
72865317
BP
1580}
1581
1954e6bb 1582static int
63be20be 1583dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1954e6bb 1584{
63be20be
AW
1585 struct dp_netdev *dp = get_dp_netdev(dpif);
1586
1587 fat_rwlock_wrlock(&dp->queue_rwlock);
1588 if (dp->handler_queues) {
1589 dp_netdev_refresh_queues(dp, n_handlers);
1590 }
1591 fat_rwlock_unlock(&dp->queue_rwlock);
1592
1954e6bb
AW
1593 return 0;
1594}
1595
5bf93d67
EJ
1596static int
1597dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1598 uint32_t queue_id, uint32_t *priority)
1599{
1600 *priority = queue_id;
1601 return 0;
1602}
1603
63be20be
AW
1604static bool
1605dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1606 OVS_REQ_RDLOCK(dp->queue_rwlock)
72865317 1607{
63be20be 1608 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
72865317 1609
63be20be
AW
1610 if (!dp->handler_queues) {
1611 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1612 return false;
72865317 1613 }
63be20be
AW
1614
1615 if (handler_id >= dp->n_handlers) {
1616 VLOG_WARN_RL(&rl, "handler index out of bound");
1617 return false;
1618 }
1619
1620 return true;
72865317
BP
1621}
1622
1623static int
63be20be 1624dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1954e6bb 1625 struct dpif_upcall *upcall, struct ofpbuf *buf)
72865317 1626{
f5126b57 1627 struct dp_netdev *dp = get_dp_netdev(dpif);
5279f8fd 1628 struct dp_netdev_queue *q;
63be20be
AW
1629 int error = 0;
1630
1631 fat_rwlock_rdlock(&dp->queue_rwlock);
5279f8fd 1632
63be20be
AW
1633 if (!dp_netdev_recv_check(dp, handler_id)) {
1634 error = EAGAIN;
1635 goto out;
1636 }
1637
1638 q = &dp->handler_queues[handler_id];
1639 ovs_mutex_lock(&q->mutex);
1640 if (q->head != q->tail) {
d88b629b
BP
1641 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1642
1643 *upcall = u->upcall;
b3907fbc 1644
90a7c55e 1645 ofpbuf_uninit(buf);
d88b629b 1646 *buf = u->buf;
72865317 1647 } else {
5279f8fd 1648 error = EAGAIN;
72865317 1649 }
63be20be
AW
1650 ovs_mutex_unlock(&q->mutex);
1651
1652out:
1653 fat_rwlock_unlock(&dp->queue_rwlock);
5279f8fd
BP
1654
1655 return error;
72865317
BP
1656}
1657
1658static void
63be20be 1659dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
72865317 1660{
d33ed218 1661 struct dp_netdev *dp = get_dp_netdev(dpif);
63be20be 1662 struct dp_netdev_queue *q;
d33ed218 1663 uint64_t seq;
5279f8fd 1664
63be20be
AW
1665 fat_rwlock_rdlock(&dp->queue_rwlock);
1666
1667 if (!dp_netdev_recv_check(dp, handler_id)) {
1668 goto out;
1669 }
1670
1671 q = &dp->handler_queues[handler_id];
1672 ovs_mutex_lock(&q->mutex);
1673 seq = seq_read(q->seq);
1674 if (q->head != q->tail) {
72865317 1675 poll_immediate_wake();
d33ed218 1676 } else {
63be20be 1677 seq_wait(q->seq, seq);
72865317 1678 }
63be20be
AW
1679
1680 ovs_mutex_unlock(&q->mutex);
1681
1682out:
1683 fat_rwlock_unlock(&dp->queue_rwlock);
72865317 1684}
1ba530f4
BP
1685
1686static void
1687dpif_netdev_recv_purge(struct dpif *dpif)
1688{
1689 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
f5126b57 1690
63be20be 1691 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1ba530f4 1692 dp_netdev_purge_queues(dpif_netdev->dp);
63be20be 1693 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1ba530f4 1694}
72865317 1695\f
a84cb64a
BP
1696/* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1697 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1698 * 'ofpacts'. */
1699struct dp_netdev_actions *
1700dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1701{
1702 struct dp_netdev_actions *netdev_actions;
1703
1704 netdev_actions = xmalloc(sizeof *netdev_actions);
a84cb64a
BP
1705 netdev_actions->actions = xmemdup(actions, size);
1706 netdev_actions->size = size;
1707
1708 return netdev_actions;
1709}
1710
a84cb64a 1711struct dp_netdev_actions *
61e7deb1 1712dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
a84cb64a 1713{
61e7deb1 1714 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
a84cb64a
BP
1715}
1716
61e7deb1
BP
1717static void
1718dp_netdev_actions_free(struct dp_netdev_actions *actions)
a84cb64a 1719{
61e7deb1
BP
1720 free(actions->actions);
1721 free(actions);
a84cb64a
BP
1722}
1723\f
e4cfed38
PS
1724
1725inline static void
1726dp_netdev_process_rx_port(struct dp_netdev *dp,
1727 struct dp_netdev_port *port,
1728 struct netdev_rx *queue)
1729{
1730 struct ofpbuf *packet[NETDEV_MAX_RX_BATCH];
1731 int error, c;
1732
1733 error = netdev_rx_recv(queue, packet, &c);
1734 if (!error) {
1735 struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1736 int i;
1737
1738 for (i = 0; i < c; i++) {
1739 dp_netdev_port_input(dp, packet[i], &md);
1740 }
1741 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1742 static struct vlog_rate_limit rl
1743 = VLOG_RATE_LIMIT_INIT(1, 5);
1744
1745 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1746 netdev_get_name(port->netdev),
1747 ovs_strerror(error));
1748 }
1749}
1750
1751static void
1752dpif_netdev_run(struct dpif *dpif)
1753{
1754 struct dp_netdev_port *port;
1755 struct dp_netdev *dp = get_dp_netdev(dpif);
1756
1757 ovs_rwlock_rdlock(&dp->port_rwlock);
1758
1759 HMAP_FOR_EACH (port, node, &dp->ports) {
1760 if (port->rx && !netdev_is_pmd(port->netdev)) {
1761 dp_netdev_process_rx_port(dp, port, port->rx);
1762 }
1763 }
1764
1765 ovs_rwlock_unlock(&dp->port_rwlock);
1766}
1767
1768static void
1769dpif_netdev_wait(struct dpif *dpif)
1770{
1771 struct dp_netdev_port *port;
1772 struct dp_netdev *dp = get_dp_netdev(dpif);
1773
1774 ovs_rwlock_rdlock(&dp->port_rwlock);
1775
1776 HMAP_FOR_EACH (port, node, &dp->ports) {
1777 if (port->rx && !netdev_is_pmd(port->netdev)) {
1778 netdev_rx_wait(port->rx);
1779 }
1780 }
1781 ovs_rwlock_unlock(&dp->port_rwlock);
1782}
1783
1784struct rx_poll {
1785 struct dp_netdev_port *port;
1786};
1787
1788static int
1789pmd_load_queues(struct pmd_thread *f,
1790 struct rx_poll **ppoll_list, int poll_cnt)
1791{
1792 struct dp_netdev *dp = f->dp;
1793 struct rx_poll *poll_list = *ppoll_list;
1794 struct dp_netdev_port *port;
1795 int id = f->id;
1796 int index;
1797 int i;
1798
1799 /* Simple scheduler for netdev rx polling. */
1800 ovs_rwlock_rdlock(&dp->port_rwlock);
1801 for (i = 0; i < poll_cnt; i++) {
1802 port_unref(poll_list[i].port);
1803 }
1804
1805 poll_cnt = 0;
1806 index = 0;
1807
1808 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1809 if (netdev_is_pmd(port->netdev)) {
1810 if ((index % dp->n_pmd_threads) == id) {
1811 poll_list = xrealloc(poll_list, sizeof *poll_list * (poll_cnt + 1));
1812
1813 port_ref(port);
1814 poll_list[poll_cnt++].port = port;
1815 }
1816 index++;
1817 }
1818 }
1819
1820 ovs_rwlock_unlock(&dp->port_rwlock);
1821 *ppoll_list = poll_list;
1822 return poll_cnt;
1823}
1824
6c3eee82 1825static void *
e4cfed38 1826pmd_thread_main(void *f_)
6c3eee82 1827{
e4cfed38 1828 struct pmd_thread *f = f_;
6c3eee82 1829 struct dp_netdev *dp = f->dp;
e4cfed38
PS
1830 unsigned int lc = 0;
1831 struct rx_poll *poll_list;
1832 unsigned int port_seq;
1833 int poll_cnt;
1834 int i;
6c3eee82 1835
e4cfed38 1836 f->name = xasprintf("pmd_%u", ovsthread_id_self());
6c3eee82 1837 set_subprogram_name("%s", f->name);
e4cfed38
PS
1838 poll_cnt = 0;
1839 poll_list = NULL;
1840
1841reload:
1842 poll_cnt = pmd_load_queues(f, &poll_list, poll_cnt);
1843 atomic_read(&f->change_seq, &port_seq);
6c3eee82 1844
e4cfed38
PS
1845 for (;;) {
1846 unsigned int c_port_seq;
6c3eee82
BP
1847 int i;
1848
e4cfed38
PS
1849 for (i = 0; i < poll_cnt; i++) {
1850 dp_netdev_process_rx_port(dp, poll_list[i].port, poll_list[i].port->rx);
1851 }
1852
1853 if (lc++ > 1024) {
1854 ovsrcu_quiesce();
6c3eee82 1855
e4cfed38
PS
1856 /* TODO: need completely userspace based signaling method.
1857 * to keep this thread entirely in userspace.
1858 * For now using atomic counter. */
1859 lc = 0;
1860 atomic_read_explicit(&f->change_seq, &c_port_seq, memory_order_consume);
1861 if (c_port_seq != port_seq) {
6c3eee82
BP
1862 break;
1863 }
1864 }
e4cfed38 1865 }
6c3eee82 1866
e4cfed38
PS
1867 if (!latch_is_set(&f->dp->exit_latch)){
1868 goto reload;
1869 }
6c3eee82 1870
e4cfed38
PS
1871 for (i = 0; i < poll_cnt; i++) {
1872 port_unref(poll_list[i].port);
6c3eee82 1873 }
6c3eee82 1874
e4cfed38 1875 free(poll_list);
6c3eee82 1876 free(f->name);
6c3eee82
BP
1877 return NULL;
1878}
1879
1880static void
e4cfed38 1881dp_netdev_set_pmd_threads(struct dp_netdev *dp, int n)
6c3eee82
BP
1882{
1883 int i;
1884
e4cfed38 1885 if (n == dp->n_pmd_threads) {
6c3eee82
BP
1886 return;
1887 }
1888
1889 /* Stop existing threads. */
1890 latch_set(&dp->exit_latch);
e4cfed38
PS
1891 dp_netdev_reload_pmd_threads(dp);
1892 for (i = 0; i < dp->n_pmd_threads; i++) {
1893 struct pmd_thread *f = &dp->pmd_threads[i];
6c3eee82
BP
1894
1895 xpthread_join(f->thread, NULL);
1896 }
1897 latch_poll(&dp->exit_latch);
e4cfed38 1898 free(dp->pmd_threads);
6c3eee82
BP
1899
1900 /* Start new threads. */
e4cfed38
PS
1901 dp->pmd_threads = xmalloc(n * sizeof *dp->pmd_threads);
1902 dp->n_pmd_threads = n;
1903
6c3eee82 1904 for (i = 0; i < n; i++) {
e4cfed38 1905 struct pmd_thread *f = &dp->pmd_threads[i];
6c3eee82
BP
1906
1907 f->dp = dp;
e4cfed38
PS
1908 f->id = i;
1909 atomic_store(&f->change_seq, 1);
1910
1911 /* Each thread will distribute all devices rx-queues among
1912 * themselves. */
1913 xpthread_create(&f->thread, NULL, pmd_thread_main, f);
6c3eee82
BP
1914 }
1915}
e4cfed38 1916
6c3eee82 1917\f
679ba04c
BP
1918static void *
1919dp_netdev_flow_stats_new_cb(void)
1920{
1921 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1922 ovs_mutex_init(&bucket->mutex);
1923 return bucket;
1924}
1925
72865317 1926static void
1763b4b8 1927dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
855dd13c
JR
1928 const struct ofpbuf *packet,
1929 const struct flow *key)
72865317 1930{
e0eecb1c 1931 uint16_t tcp_flags = ntohs(key->tcp_flags);
679ba04c
BP
1932 long long int now = time_msec();
1933 struct dp_netdev_flow_stats *bucket;
1934
1935 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1936 dp_netdev_flow_stats_new_cb);
1937
1938 ovs_mutex_lock(&bucket->mutex);
1939 bucket->used = MAX(now, bucket->used);
1940 bucket->packet_count++;
1941 bucket->byte_count += packet->size;
1942 bucket->tcp_flags |= tcp_flags;
1943 ovs_mutex_unlock(&bucket->mutex);
72865317
BP
1944}
1945
51852a57
BP
1946static void *
1947dp_netdev_stats_new_cb(void)
1948{
1949 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1950 ovs_mutex_init(&bucket->mutex);
1951 return bucket;
1952}
1953
1954static void
1955dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1956{
1957 struct dp_netdev_stats *bucket;
1958
1959 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1960 ovs_mutex_lock(&bucket->mutex);
1961 bucket->n[type]++;
1962 ovs_mutex_unlock(&bucket->mutex);
1963}
1964
72865317 1965static void
758c456d
JR
1966dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1967 struct pkt_metadata *md)
72865317 1968{
1763b4b8 1969 struct dp_netdev_flow *netdev_flow;
14608a15 1970 struct flow key;
72865317 1971
1805876e 1972 if (packet->size < ETH_HEADER_LEN) {
df1e5a3b 1973 ofpbuf_delete(packet);
1805876e
BP
1974 return;
1975 }
b5e7e61a 1976 flow_extract(packet, md, &key);
1763b4b8
GS
1977 netdev_flow = dp_netdev_lookup_flow(dp, &key);
1978 if (netdev_flow) {
a84cb64a
BP
1979 struct dp_netdev_actions *actions;
1980
855dd13c 1981 dp_netdev_flow_used(netdev_flow, packet, &key);
679ba04c 1982
61e7deb1 1983 actions = dp_netdev_flow_get_actions(netdev_flow);
df1e5a3b 1984 dp_netdev_execute_actions(dp, &key, packet, true, md,
a84cb64a 1985 actions->actions, actions->size);
51852a57 1986 dp_netdev_count_packet(dp, DP_STAT_HIT);
63be20be 1987 } else if (dp->handler_queues) {
51852a57 1988 dp_netdev_count_packet(dp, DP_STAT_MISS);
63be20be
AW
1989 dp_netdev_output_userspace(dp, packet,
1990 flow_hash_5tuple(&key, 0) % dp->n_handlers,
1991 DPIF_UC_MISS, &key, NULL);
df1e5a3b 1992 ofpbuf_delete(packet);
72865317
BP
1993 }
1994}
1995
72865317 1996static int
da546e07 1997dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
63be20be 1998 int queue_no, int type, const struct flow *flow,
e995e3df 1999 const struct nlattr *userdata)
72865317 2000{
63be20be 2001 struct dp_netdev_queue *q;
f5126b57
BP
2002 int error;
2003
63be20be
AW
2004 fat_rwlock_rdlock(&dp->queue_rwlock);
2005 q = &dp->handler_queues[queue_no];
2006 ovs_mutex_lock(&q->mutex);
e995e3df
BP
2007 if (q->head - q->tail < MAX_QUEUE_LEN) {
2008 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
2009 struct dpif_upcall *upcall = &u->upcall;
2010 struct ofpbuf *buf = &u->buf;
2011 size_t buf_size;
2012
63be20be 2013 upcall->type = type;
e995e3df
BP
2014
2015 /* Allocate buffer big enough for everything. */
da546e07 2016 buf_size = ODPUTIL_FLOW_KEY_BYTES;
e995e3df
BP
2017 if (userdata) {
2018 buf_size += NLA_ALIGN(userdata->nla_len);
2019 }
df1e5a3b 2020 buf_size += packet->size;
e995e3df 2021 ofpbuf_init(buf, buf_size);
72865317 2022
e995e3df 2023 /* Put ODP flow. */
4e022ec0 2024 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
e995e3df
BP
2025 upcall->key = buf->data;
2026 upcall->key_len = buf->size;
d88b629b 2027
e995e3df
BP
2028 /* Put userdata. */
2029 if (userdata) {
2030 upcall->userdata = ofpbuf_put(buf, userdata,
2031 NLA_ALIGN(userdata->nla_len));
2032 }
856081f6 2033
df1e5a3b
PS
2034 upcall->packet.data = ofpbuf_put(buf, packet->data, packet->size);
2035 upcall->packet.size = packet->size;
856081f6 2036
63be20be 2037 seq_change(q->seq);
d33ed218 2038
f5126b57 2039 error = 0;
e995e3df 2040 } else {
51852a57 2041 dp_netdev_count_packet(dp, DP_STAT_LOST);
f5126b57 2042 error = ENOBUFS;
e995e3df 2043 }
63be20be
AW
2044 ovs_mutex_unlock(&q->mutex);
2045 fat_rwlock_unlock(&dp->queue_rwlock);
f5126b57
BP
2046
2047 return error;
72865317
BP
2048}
2049
9080a111
JR
2050struct dp_netdev_execute_aux {
2051 struct dp_netdev *dp;
2052 const struct flow *key;
2053};
2054
2055static void
758c456d
JR
2056dp_execute_cb(void *aux_, struct ofpbuf *packet,
2057 const struct pkt_metadata *md OVS_UNUSED,
09f9da0b 2058 const struct nlattr *a, bool may_steal)
8a4e3a85 2059 OVS_NO_THREAD_SAFETY_ANALYSIS
9080a111
JR
2060{
2061 struct dp_netdev_execute_aux *aux = aux_;
09f9da0b 2062 int type = nl_attr_type(a);
8a4e3a85 2063 struct dp_netdev_port *p;
9080a111 2064
09f9da0b
JR
2065 switch ((enum ovs_action_attr)type) {
2066 case OVS_ACTION_ATTR_OUTPUT:
8a4e3a85
BP
2067 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
2068 if (p) {
40d26f04 2069 netdev_send(p->netdev, packet, may_steal);
8a4e3a85 2070 }
09f9da0b
JR
2071 break;
2072
2073 case OVS_ACTION_ATTR_USERSPACE: {
2074 const struct nlattr *userdata;
4fc65926 2075
09f9da0b 2076 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
da546e07 2077
63be20be
AW
2078 dp_netdev_output_userspace(aux->dp, packet,
2079 flow_hash_5tuple(aux->key, 0)
2080 % aux->dp->n_handlers,
2081 DPIF_UC_ACTION, aux->key,
09f9da0b 2082 userdata);
40d26f04
PS
2083
2084 if (may_steal) {
2085 ofpbuf_delete(packet);
2086 }
09f9da0b 2087 break;
da546e07 2088 }
09f9da0b
JR
2089 case OVS_ACTION_ATTR_PUSH_VLAN:
2090 case OVS_ACTION_ATTR_POP_VLAN:
2091 case OVS_ACTION_ATTR_PUSH_MPLS:
2092 case OVS_ACTION_ATTR_POP_MPLS:
2093 case OVS_ACTION_ATTR_SET:
2094 case OVS_ACTION_ATTR_SAMPLE:
2095 case OVS_ACTION_ATTR_UNSPEC:
2096 case __OVS_ACTION_ATTR_MAX:
2097 OVS_NOT_REACHED();
da546e07 2098 }
df1e5a3b 2099
98403001
BP
2100}
2101
4edb9ae9 2102static void
9080a111 2103dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
df1e5a3b
PS
2104 struct ofpbuf *packet, bool may_steal,
2105 struct pkt_metadata *md,
9080a111 2106 const struct nlattr *actions, size_t actions_len)
72865317 2107{
9080a111 2108 struct dp_netdev_execute_aux aux = {dp, key};
9080a111 2109
df1e5a3b
PS
2110 odp_execute_actions(&aux, packet, may_steal, md,
2111 actions, actions_len, dp_execute_cb);
72865317
BP
2112}
2113
2114const struct dpif_class dpif_netdev_class = {
72865317 2115 "netdev",
2197d7ab 2116 dpif_netdev_enumerate,
0aeaabc8 2117 dpif_netdev_port_open_type,
72865317
BP
2118 dpif_netdev_open,
2119 dpif_netdev_close,
7dab847a 2120 dpif_netdev_destroy,
e4cfed38
PS
2121 dpif_netdev_run,
2122 dpif_netdev_wait,
72865317 2123 dpif_netdev_get_stats,
72865317
BP
2124 dpif_netdev_port_add,
2125 dpif_netdev_port_del,
2126 dpif_netdev_port_query_by_number,
2127 dpif_netdev_port_query_by_name,
98403001 2128 NULL, /* port_get_pid */
b0ec0f27
BP
2129 dpif_netdev_port_dump_start,
2130 dpif_netdev_port_dump_next,
2131 dpif_netdev_port_dump_done,
72865317
BP
2132 dpif_netdev_port_poll,
2133 dpif_netdev_port_poll_wait,
72865317
BP
2134 dpif_netdev_flow_get,
2135 dpif_netdev_flow_put,
2136 dpif_netdev_flow_del,
2137 dpif_netdev_flow_flush,
e723fd32 2138 dpif_netdev_flow_dump_state_init,
704a1e09
BP
2139 dpif_netdev_flow_dump_start,
2140 dpif_netdev_flow_dump_next,
bdeadfdd 2141 NULL,
704a1e09 2142 dpif_netdev_flow_dump_done,
e723fd32 2143 dpif_netdev_flow_dump_state_uninit,
72865317 2144 dpif_netdev_execute,
6bc60024 2145 NULL, /* operate */
a12b3ead 2146 dpif_netdev_recv_set,
1954e6bb 2147 dpif_netdev_handlers_set,
5bf93d67 2148 dpif_netdev_queue_to_priority,
72865317
BP
2149 dpif_netdev_recv,
2150 dpif_netdev_recv_wait,
1ba530f4 2151 dpif_netdev_recv_purge,
72865317 2152};
614c4892 2153
74cc3969
BP
2154static void
2155dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2156 const char *argv[], void *aux OVS_UNUSED)
2157{
2158 struct dp_netdev_port *port;
2159 struct dp_netdev *dp;
ff073a71 2160 odp_port_t port_no;
74cc3969 2161
8a4e3a85 2162 ovs_mutex_lock(&dp_netdev_mutex);
74cc3969
BP
2163 dp = shash_find_data(&dp_netdevs, argv[1]);
2164 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
8a4e3a85 2165 ovs_mutex_unlock(&dp_netdev_mutex);
74cc3969
BP
2166 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2167 return;
2168 }
8a4e3a85
BP
2169 ovs_refcount_ref(&dp->ref_cnt);
2170 ovs_mutex_unlock(&dp_netdev_mutex);
74cc3969 2171
8a4e3a85 2172 ovs_rwlock_wrlock(&dp->port_rwlock);
74cc3969
BP
2173 if (get_port_by_name(dp, argv[2], &port)) {
2174 unixctl_command_reply_error(conn, "unknown port");
8a4e3a85 2175 goto exit;
74cc3969
BP
2176 }
2177
ff073a71
BP
2178 port_no = u32_to_odp(atoi(argv[3]));
2179 if (!port_no || port_no == ODPP_NONE) {
74cc3969 2180 unixctl_command_reply_error(conn, "bad port number");
8a4e3a85 2181 goto exit;
74cc3969 2182 }
ff073a71 2183 if (dp_netdev_lookup_port(dp, port_no)) {
74cc3969 2184 unixctl_command_reply_error(conn, "port number already in use");
8a4e3a85 2185 goto exit;
74cc3969 2186 }
ff073a71
BP
2187 hmap_remove(&dp->ports, &port->node);
2188 port->port_no = port_no;
2189 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
d33ed218 2190 seq_change(dp->port_seq);
74cc3969 2191 unixctl_command_reply(conn, NULL);
8a4e3a85
BP
2192
2193exit:
2194 ovs_rwlock_unlock(&dp->port_rwlock);
2195 dp_netdev_unref(dp);
74cc3969
BP
2196}
2197
0cbfe35d
BP
2198static void
2199dpif_dummy_register__(const char *type)
2200{
2201 struct dpif_class *class;
2202
2203 class = xmalloc(sizeof *class);
2204 *class = dpif_netdev_class;
2205 class->type = xstrdup(type);
2206 dp_register_provider(class);
2207}
2208
614c4892 2209void
0cbfe35d 2210dpif_dummy_register(bool override)
614c4892 2211{
0cbfe35d
BP
2212 if (override) {
2213 struct sset types;
2214 const char *type;
2215
2216 sset_init(&types);
2217 dp_enumerate_types(&types);
2218 SSET_FOR_EACH (type, &types) {
2219 if (!dp_unregister_provider(type)) {
2220 dpif_dummy_register__(type);
2221 }
2222 }
2223 sset_destroy(&types);
614c4892 2224 }
0cbfe35d
BP
2225
2226 dpif_dummy_register__("dummy");
74cc3969
BP
2227
2228 unixctl_command_register("dpif-dummy/change-port-number",
2229 "DP PORT NEW-NUMBER",
2230 3, 3, dpif_dummy_change_port_number, NULL);
614c4892 2231}