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