]>
Commit | Line | Data |
---|---|---|
72865317 BP |
1 | /* |
2 | * Copyright (c) 2009 Nicira Networks. | |
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 | ||
20 | #include <assert.h> | |
21 | #include <ctype.h> | |
22 | #include <errno.h> | |
23 | #include <fcntl.h> | |
24 | #include <inttypes.h> | |
25 | #include <net/if.h> | |
72865317 BP |
26 | #include <netinet/in.h> |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <sys/ioctl.h> | |
30 | #include <sys/stat.h> | |
72865317 BP |
31 | #include <unistd.h> |
32 | ||
33 | #include "csum.h" | |
34 | #include "dpif-provider.h" | |
35 | #include "flow.h" | |
36 | #include "hmap.h" | |
37 | #include "list.h" | |
38 | #include "netdev.h" | |
39 | #include "odp-util.h" | |
40 | #include "ofp-print.h" | |
41 | #include "ofpbuf.h" | |
42 | #include "packets.h" | |
43 | #include "poll-loop.h" | |
44 | #include "queue.h" | |
45 | #include "timeval.h" | |
46 | #include "util.h" | |
47 | ||
48 | #include "vlog.h" | |
49 | #define THIS_MODULE VLM_dpif_netdev | |
50 | ||
51 | /* Configuration parameters. */ | |
52 | enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */ | |
53 | enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */ | |
54 | enum { N_GROUPS = 16 }; /* Number of port groups. */ | |
55 | enum { MAX_PORTS = 256 }; /* Maximum number of ports. */ | |
56 | enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */ | |
57 | ||
58 | /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP | |
59 | * headers to be aligned on a 4-byte boundary. */ | |
60 | enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN }; | |
61 | ||
62 | /* Datapath based on the network device interface from netdev.h. */ | |
63 | struct dp_netdev { | |
64 | struct list node; | |
65 | int dp_idx; | |
66 | int open_cnt; | |
67 | bool deleted; | |
68 | ||
69 | bool drop_frags; /* Drop all IP fragments, if true. */ | |
70 | struct ovs_queue queues[N_QUEUES]; /* Messages queued for dpif_recv(). */ | |
71 | struct hmap flow_table; /* Flow table. */ | |
72 | struct odp_port_group groups[N_GROUPS]; | |
73 | ||
74 | /* Statistics. */ | |
75 | long long int n_frags; /* Number of dropped IP fragments. */ | |
76 | long long int n_hit; /* Number of flow table matches. */ | |
77 | long long int n_missed; /* Number of flow table misses. */ | |
78 | long long int n_lost; /* Number of misses not passed to client. */ | |
79 | ||
80 | /* Ports. */ | |
81 | int n_ports; | |
82 | struct dp_netdev_port *ports[MAX_PORTS]; | |
83 | struct list port_list; | |
84 | unsigned int serial; | |
85 | }; | |
86 | ||
87 | /* A port in a netdev-based datapath. */ | |
88 | struct dp_netdev_port { | |
89 | int port_no; /* Index into dp_netdev's 'ports'. */ | |
90 | struct list node; /* Element in dp_netdev's 'port_list'. */ | |
91 | struct netdev *netdev; | |
92 | bool internal; /* Internal port (as ODP_PORT_INTERNAL)? */ | |
93 | }; | |
94 | ||
95 | /* A flow in dp_netdev's 'flow_table'. */ | |
96 | struct dp_netdev_flow { | |
97 | struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */ | |
98 | flow_t key; | |
99 | ||
100 | /* Statistics. */ | |
101 | struct timeval used; /* Last used time, in milliseconds. */ | |
102 | long long int packet_count; /* Number of packets matched. */ | |
103 | long long int byte_count; /* Number of bytes matched. */ | |
104 | uint8_t ip_tos; /* IP TOS value. */ | |
105 | uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */ | |
106 | ||
107 | /* Actions. */ | |
108 | union odp_action *actions; | |
109 | unsigned int n_actions; | |
110 | }; | |
111 | ||
112 | /* Interface to netdev-based datapath. */ | |
113 | struct dpif_netdev { | |
114 | struct dpif dpif; | |
115 | struct dp_netdev *dp; | |
116 | int listen_mask; | |
117 | unsigned int dp_serial; | |
118 | }; | |
119 | ||
120 | /* All netdev-based datapaths. */ | |
121 | static struct dp_netdev *dp_netdevs[256]; | |
122 | struct list dp_netdev_list = LIST_INITIALIZER(&dp_netdev_list); | |
123 | enum { N_DP_NETDEVS = ARRAY_SIZE(dp_netdevs) }; | |
124 | ||
125 | /* Maximum port MTU seen so far. */ | |
126 | static int max_mtu = ETH_PAYLOAD_MAX; | |
127 | ||
128 | static int get_port_by_number(struct dp_netdev *, uint16_t port_no, | |
129 | struct dp_netdev_port **portp); | |
130 | static int get_port_by_name(struct dp_netdev *, const char *devname, | |
131 | struct dp_netdev_port **portp); | |
132 | static void dp_netdev_free(struct dp_netdev *); | |
133 | static void dp_netdev_flow_flush(struct dp_netdev *); | |
134 | static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags, | |
135 | uint16_t port_no); | |
136 | static int do_del_port(struct dp_netdev *, uint16_t port_no); | |
137 | static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *, | |
138 | int queue_no, int port_no, uint32_t arg); | |
139 | static int dp_netdev_execute_actions(struct dp_netdev *, | |
140 | struct ofpbuf *, flow_t *, | |
141 | const union odp_action *, int n); | |
142 | ||
143 | static struct dpif_netdev * | |
144 | dpif_netdev_cast(const struct dpif *dpif) | |
145 | { | |
146 | dpif_assert_class(dpif, &dpif_netdev_class); | |
147 | return CONTAINER_OF(dpif, struct dpif_netdev, dpif); | |
148 | } | |
149 | ||
150 | static struct dp_netdev * | |
151 | get_dp_netdev(const struct dpif *dpif) | |
152 | { | |
153 | return dpif_netdev_cast(dpif)->dp; | |
154 | } | |
155 | ||
156 | static int | |
157 | name_to_dp_idx(const char *name) | |
158 | { | |
be2c418b | 159 | if (!strncmp(name, "dp", 2) && isdigit((unsigned char)name[2])) { |
72865317 BP |
160 | int dp_idx = atoi(name + 2); |
161 | if (dp_idx >= 0 && dp_idx < N_DP_NETDEVS) { | |
162 | return dp_idx; | |
163 | } | |
164 | } | |
165 | return -1; | |
166 | } | |
167 | ||
168 | static struct dp_netdev * | |
169 | find_dp_netdev(const char *name) | |
170 | { | |
171 | int dp_idx; | |
172 | size_t i; | |
173 | ||
174 | dp_idx = name_to_dp_idx(name); | |
175 | if (dp_idx >= 0) { | |
176 | return dp_netdevs[dp_idx]; | |
177 | } | |
178 | ||
179 | for (i = 0; i < N_DP_NETDEVS; i++) { | |
180 | struct dp_netdev *dp = dp_netdevs[i]; | |
181 | if (dp) { | |
182 | struct dp_netdev_port *port; | |
183 | if (!get_port_by_name(dp, name, &port)) { | |
184 | return dp; | |
185 | } | |
186 | } | |
187 | } | |
188 | return NULL; | |
189 | } | |
190 | ||
191 | static struct dpif * | |
192 | create_dpif_netdev(struct dp_netdev *dp) | |
193 | { | |
194 | struct dpif_netdev *dpif; | |
195 | char *dpname; | |
196 | ||
197 | dp->open_cnt++; | |
198 | ||
199 | dpname = xasprintf("netdev:dp%d", dp->dp_idx); | |
200 | dpif = xmalloc(sizeof *dpif); | |
201 | dpif_init(&dpif->dpif, &dpif_netdev_class, dpname, dp->dp_idx, dp->dp_idx); | |
202 | dpif->dp = dp; | |
203 | dpif->listen_mask = 0; | |
204 | dpif->dp_serial = dp->serial; | |
205 | free(dpname); | |
206 | ||
207 | return &dpif->dpif; | |
208 | } | |
209 | ||
210 | static int | |
211 | create_dp_netdev(const char *name, int dp_idx, struct dpif **dpifp) | |
212 | { | |
213 | struct dp_netdev *dp; | |
214 | int error; | |
215 | int i; | |
216 | ||
217 | if (dp_netdevs[dp_idx]) { | |
218 | return EBUSY; | |
219 | } | |
220 | ||
221 | /* Create datapath. */ | |
222 | dp_netdevs[dp_idx] = dp = xcalloc(1, sizeof *dp); | |
223 | list_push_back(&dp_netdev_list, &dp->node); | |
224 | dp->dp_idx = dp_idx; | |
225 | dp->open_cnt = 0; | |
226 | dp->drop_frags = false; | |
227 | for (i = 0; i < N_QUEUES; i++) { | |
228 | queue_init(&dp->queues[i]); | |
229 | } | |
230 | hmap_init(&dp->flow_table); | |
231 | for (i = 0; i < N_GROUPS; i++) { | |
232 | dp->groups[i].ports = NULL; | |
233 | dp->groups[i].n_ports = 0; | |
234 | dp->groups[i].group = i; | |
235 | } | |
236 | list_init(&dp->port_list); | |
237 | error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL); | |
238 | if (error) { | |
239 | dp_netdev_free(dp); | |
240 | return error; | |
241 | } | |
242 | ||
243 | *dpifp = create_dpif_netdev(dp); | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static int | |
248 | dpif_netdev_open(const char *name UNUSED, char *suffix, bool create, | |
249 | struct dpif **dpifp) | |
250 | { | |
251 | if (create) { | |
252 | if (find_dp_netdev(suffix)) { | |
253 | return EEXIST; | |
254 | } else { | |
255 | int dp_idx = name_to_dp_idx(suffix); | |
256 | if (dp_idx >= 0) { | |
257 | return create_dp_netdev(suffix, dp_idx, dpifp); | |
258 | } else { | |
259 | /* Scan for unused dp_idx number. */ | |
260 | for (dp_idx = 0; dp_idx < N_DP_NETDEVS; dp_idx++) { | |
261 | int error = create_dp_netdev(suffix, dp_idx, dpifp); | |
262 | if (error != EBUSY) { | |
263 | return error; | |
264 | } | |
265 | } | |
266 | ||
267 | /* All datapath numbers in use. */ | |
268 | return ENOBUFS; | |
269 | } | |
270 | } | |
271 | } else { | |
272 | struct dp_netdev *dp = find_dp_netdev(suffix); | |
273 | if (dp) { | |
274 | *dpifp = create_dpif_netdev(dp); | |
275 | return 0; | |
276 | } else { | |
277 | return ENODEV; | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | static void | |
283 | dp_netdev_free(struct dp_netdev *dp) | |
284 | { | |
285 | int i; | |
286 | ||
287 | dp_netdev_flow_flush(dp); | |
288 | while (dp->n_ports > 0) { | |
289 | struct dp_netdev_port *port = CONTAINER_OF( | |
290 | dp->port_list.next, struct dp_netdev_port, node); | |
291 | do_del_port(dp, port->port_no); | |
292 | } | |
293 | for (i = 0; i < N_QUEUES; i++) { | |
294 | queue_destroy(&dp->queues[i]); | |
295 | } | |
296 | hmap_destroy(&dp->flow_table); | |
297 | for (i = 0; i < N_GROUPS; i++) { | |
298 | free(dp->groups[i].ports); | |
299 | } | |
300 | dp_netdevs[dp->dp_idx] = NULL; | |
301 | list_remove(&dp->node); | |
302 | free(dp); | |
303 | } | |
304 | ||
305 | static void | |
306 | dpif_netdev_close(struct dpif *dpif) | |
307 | { | |
308 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
309 | assert(dp->open_cnt > 0); | |
310 | if (--dp->open_cnt == 0 && dp->deleted) { | |
311 | dp_netdev_free(dp); | |
312 | } | |
313 | free(dpif); | |
314 | } | |
315 | ||
316 | static int | |
317 | dpif_netdev_delete(struct dpif *dpif) | |
318 | { | |
319 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
320 | dp->deleted = true; | |
321 | return 0; | |
322 | } | |
323 | ||
324 | static int | |
325 | dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats) | |
326 | { | |
327 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
328 | memset(stats, 0, sizeof *stats); | |
329 | stats->n_flows = hmap_count(&dp->flow_table); | |
330 | stats->cur_capacity = hmap_capacity(&dp->flow_table); | |
331 | stats->max_capacity = MAX_FLOWS; | |
332 | stats->n_ports = dp->n_ports; | |
333 | stats->max_ports = MAX_PORTS; | |
334 | stats->max_groups = N_GROUPS; | |
335 | stats->n_frags = dp->n_frags; | |
336 | stats->n_hit = dp->n_hit; | |
337 | stats->n_missed = dp->n_missed; | |
338 | stats->n_lost = dp->n_lost; | |
339 | stats->max_miss_queue = MAX_QUEUE_LEN; | |
340 | stats->max_action_queue = MAX_QUEUE_LEN; | |
341 | return 0; | |
342 | } | |
343 | ||
344 | static int | |
345 | dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp) | |
346 | { | |
347 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
348 | *drop_fragsp = dp->drop_frags; | |
349 | return 0; | |
350 | } | |
351 | ||
352 | static int | |
353 | dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags) | |
354 | { | |
355 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
356 | dp->drop_frags = drop_frags; | |
357 | return 0; | |
358 | } | |
359 | ||
360 | static int | |
361 | do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags, | |
362 | uint16_t port_no) | |
363 | { | |
364 | bool internal = (flags & ODP_PORT_INTERNAL) != 0; | |
365 | struct dp_netdev_port *port; | |
366 | struct netdev *netdev; | |
367 | int mtu; | |
368 | int error; | |
369 | ||
370 | /* XXX reject devices already in some dp_netdev. */ | |
371 | ||
372 | /* Open and validate network device. */ | |
373 | if (!internal) { | |
374 | error = netdev_open(devname, NETDEV_ETH_TYPE_ANY, &netdev); | |
375 | } else { | |
e33e4d35 BP |
376 | char *tapname = xasprintf("tap:%s", devname); |
377 | error = netdev_open(tapname, NETDEV_ETH_TYPE_ANY, &netdev); | |
378 | free(tapname); | |
72865317 BP |
379 | } |
380 | if (error) { | |
381 | return error; | |
382 | } | |
383 | /* XXX reject loopback devices */ | |
384 | /* XXX reject non-Ethernet devices */ | |
385 | ||
386 | error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false); | |
387 | if (error) { | |
388 | netdev_close(netdev); | |
389 | return error; | |
390 | } | |
391 | ||
392 | port = xmalloc(sizeof *port); | |
393 | port->port_no = port_no; | |
394 | port->netdev = netdev; | |
395 | port->internal = internal; | |
396 | ||
3d222126 | 397 | netdev_get_mtu(netdev, &mtu); |
72865317 BP |
398 | if (mtu > max_mtu) { |
399 | max_mtu = mtu; | |
400 | } | |
401 | ||
402 | list_push_back(&dp->port_list, &port->node); | |
403 | dp->ports[port_no] = port; | |
404 | dp->n_ports++; | |
405 | dp->serial++; | |
406 | ||
407 | return 0; | |
408 | } | |
409 | ||
410 | static int | |
411 | dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags, | |
412 | uint16_t *port_nop) | |
413 | { | |
414 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
415 | int port_no; | |
416 | ||
417 | for (port_no = 0; port_no < MAX_PORTS; port_no++) { | |
418 | if (!dp->ports[port_no]) { | |
419 | *port_nop = port_no; | |
420 | return do_add_port(dp, devname, flags, port_no); | |
421 | } | |
422 | } | |
3c71830a | 423 | return EFBIG; |
72865317 BP |
424 | } |
425 | ||
426 | static int | |
427 | dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no) | |
428 | { | |
429 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
430 | return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no); | |
431 | } | |
432 | ||
433 | static bool | |
434 | is_valid_port_number(uint16_t port_no) | |
435 | { | |
436 | return port_no < MAX_PORTS; | |
437 | } | |
438 | ||
439 | static int | |
440 | get_port_by_number(struct dp_netdev *dp, | |
441 | uint16_t port_no, struct dp_netdev_port **portp) | |
442 | { | |
443 | if (!is_valid_port_number(port_no)) { | |
444 | *portp = NULL; | |
445 | return EINVAL; | |
446 | } else { | |
447 | *portp = dp->ports[port_no]; | |
448 | return *portp ? 0 : ENOENT; | |
449 | } | |
450 | } | |
451 | ||
452 | static int | |
453 | get_port_by_name(struct dp_netdev *dp, | |
454 | const char *devname, struct dp_netdev_port **portp) | |
455 | { | |
456 | struct dp_netdev_port *port; | |
457 | ||
458 | LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) { | |
459 | if (!strcmp(netdev_get_name(port->netdev), devname)) { | |
460 | *portp = port; | |
461 | return 0; | |
462 | } | |
463 | } | |
464 | return ENOENT; | |
465 | } | |
466 | ||
467 | static int | |
468 | do_del_port(struct dp_netdev *dp, uint16_t port_no) | |
469 | { | |
470 | struct dp_netdev_port *port; | |
471 | int error; | |
472 | ||
473 | error = get_port_by_number(dp, port_no, &port); | |
474 | if (error) { | |
475 | return error; | |
476 | } | |
477 | ||
478 | list_remove(&port->node); | |
479 | dp->ports[port->port_no] = NULL; | |
480 | dp->n_ports--; | |
481 | dp->serial++; | |
482 | ||
483 | netdev_close(port->netdev); | |
484 | free(port); | |
485 | ||
486 | return 0; | |
487 | } | |
488 | ||
489 | static void | |
490 | answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port) | |
491 | { | |
492 | memset(odp_port, 0, sizeof *odp_port); | |
493 | ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev), | |
494 | sizeof odp_port->devname); | |
495 | odp_port->port = port->port_no; | |
496 | odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0; | |
497 | } | |
498 | ||
499 | static int | |
500 | dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no, | |
501 | struct odp_port *odp_port) | |
502 | { | |
503 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
504 | struct dp_netdev_port *port; | |
505 | int error; | |
506 | ||
507 | error = get_port_by_number(dp, port_no, &port); | |
508 | if (!error) { | |
509 | answer_port_query(port, odp_port); | |
510 | } | |
511 | return error; | |
512 | } | |
513 | ||
514 | static int | |
515 | dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname, | |
516 | struct odp_port *odp_port) | |
517 | { | |
518 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
519 | struct dp_netdev_port *port; | |
520 | int error; | |
521 | ||
522 | error = get_port_by_name(dp, devname, &port); | |
523 | if (!error) { | |
524 | answer_port_query(port, odp_port); | |
525 | } | |
526 | return error; | |
527 | } | |
528 | ||
529 | static void | |
530 | dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow) | |
531 | { | |
532 | hmap_remove(&dp->flow_table, &flow->node); | |
533 | free(flow->actions); | |
534 | free(flow); | |
535 | } | |
536 | ||
537 | static void | |
538 | dp_netdev_flow_flush(struct dp_netdev *dp) | |
539 | { | |
540 | struct dp_netdev_flow *flow, *next; | |
541 | ||
542 | HMAP_FOR_EACH_SAFE (flow, next, struct dp_netdev_flow, node, | |
543 | &dp->flow_table) { | |
544 | dp_netdev_free_flow(dp, flow); | |
545 | } | |
546 | } | |
547 | ||
548 | static int | |
549 | dpif_netdev_flow_flush(struct dpif *dpif) | |
550 | { | |
551 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
552 | dp_netdev_flow_flush(dp); | |
553 | return 0; | |
554 | } | |
555 | ||
556 | static int | |
557 | dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n) | |
558 | { | |
559 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
560 | struct dp_netdev_port *port; | |
561 | int i; | |
562 | ||
563 | i = 0; | |
564 | LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) { | |
565 | struct odp_port *odp_port = &ports[i]; | |
566 | if (i >= n) { | |
567 | break; | |
568 | } | |
569 | answer_port_query(port, odp_port); | |
570 | i++; | |
571 | } | |
572 | return dp->n_ports; | |
573 | } | |
574 | ||
575 | static int | |
576 | dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep UNUSED) | |
577 | { | |
578 | struct dpif_netdev *dpif = dpif_netdev_cast(dpif_); | |
579 | if (dpif->dp_serial != dpif->dp->serial) { | |
580 | dpif->dp_serial = dpif->dp->serial; | |
581 | return ENOBUFS; | |
582 | } else { | |
583 | return EAGAIN; | |
584 | } | |
585 | } | |
586 | ||
587 | static void | |
588 | dpif_netdev_port_poll_wait(const struct dpif *dpif_) | |
589 | { | |
590 | struct dpif_netdev *dpif = dpif_netdev_cast(dpif_); | |
591 | if (dpif->dp_serial != dpif->dp->serial) { | |
592 | poll_immediate_wake(); | |
593 | } | |
594 | } | |
595 | ||
596 | static int | |
597 | get_port_group(const struct dpif *dpif, int group_no, | |
598 | struct odp_port_group **groupp) | |
599 | { | |
600 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
601 | ||
602 | if (group_no >= 0 && group_no < N_GROUPS) { | |
603 | *groupp = &dp->groups[group_no]; | |
604 | return 0; | |
605 | } else { | |
606 | *groupp = NULL; | |
607 | return EINVAL; | |
608 | } | |
609 | } | |
610 | ||
611 | static int | |
612 | dpif_netdev_port_group_get(const struct dpif *dpif, int group_no, | |
613 | uint16_t ports[], int n) | |
614 | { | |
615 | struct odp_port_group *group; | |
616 | int error; | |
617 | ||
618 | if (n < 0) { | |
619 | return -EINVAL; | |
620 | } | |
621 | ||
622 | error = get_port_group(dpif, group_no, &group); | |
623 | if (!error) { | |
624 | memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports); | |
625 | return group->n_ports; | |
626 | } else { | |
627 | return -error; | |
628 | } | |
629 | } | |
630 | ||
631 | static int | |
632 | dpif_netdev_port_group_set(struct dpif *dpif, int group_no, | |
633 | const uint16_t ports[], int n) | |
634 | { | |
635 | struct odp_port_group *group; | |
636 | int error; | |
637 | ||
638 | if (n < 0 || n > MAX_PORTS) { | |
639 | return EINVAL; | |
640 | } | |
641 | ||
642 | error = get_port_group(dpif, group_no, &group); | |
643 | if (!error) { | |
644 | free(group->ports); | |
645 | group->ports = xmemdup(ports, n * sizeof *group->ports); | |
646 | group->n_ports = n; | |
647 | group->group = group_no; | |
648 | } | |
649 | return error; | |
650 | } | |
651 | ||
652 | static struct dp_netdev_flow * | |
653 | dp_netdev_lookup_flow(const struct dp_netdev *dp, const flow_t *key) | |
654 | { | |
655 | struct dp_netdev_flow *flow; | |
656 | ||
657 | assert(key->reserved == 0); | |
658 | HMAP_FOR_EACH_WITH_HASH (flow, struct dp_netdev_flow, node, | |
659 | flow_hash(key, 0), &dp->flow_table) { | |
660 | if (flow_equal(&flow->key, key)) { | |
661 | return flow; | |
662 | } | |
663 | } | |
664 | return NULL; | |
665 | } | |
666 | ||
667 | static void | |
fde05ade | 668 | answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags, |
72865317 BP |
669 | struct odp_flow *odp_flow) |
670 | { | |
671 | if (flow) { | |
672 | odp_flow->key = flow->key; | |
673 | odp_flow->stats.n_packets = flow->packet_count; | |
674 | odp_flow->stats.n_bytes = flow->byte_count; | |
675 | odp_flow->stats.used_sec = flow->used.tv_sec; | |
676 | odp_flow->stats.used_nsec = flow->used.tv_usec * 1000; | |
677 | odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl); | |
678 | odp_flow->stats.ip_tos = flow->ip_tos; | |
679 | odp_flow->stats.error = 0; | |
680 | if (odp_flow->n_actions > 0) { | |
681 | unsigned int n = MIN(odp_flow->n_actions, flow->n_actions); | |
682 | memcpy(odp_flow->actions, flow->actions, | |
683 | n * sizeof *odp_flow->actions); | |
684 | odp_flow->n_actions = flow->n_actions; | |
685 | } | |
fde05ade JG |
686 | |
687 | if (query_flags & ODPFF_ZERO_TCP_FLAGS) { | |
688 | flow->tcp_ctl = 0; | |
689 | } | |
690 | ||
72865317 BP |
691 | } else { |
692 | odp_flow->stats.error = ENOENT; | |
693 | } | |
694 | } | |
695 | ||
696 | static int | |
697 | dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n) | |
698 | { | |
699 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
700 | int i; | |
701 | ||
702 | for (i = 0; i < n; i++) { | |
703 | struct odp_flow *odp_flow = &flows[i]; | |
fde05ade JG |
704 | answer_flow_query(dp_netdev_lookup_flow(dp, &odp_flow->key), |
705 | odp_flow->flags, odp_flow); | |
72865317 BP |
706 | } |
707 | return 0; | |
708 | } | |
709 | ||
710 | static int | |
711 | dpif_netdev_validate_actions(const union odp_action *actions, int n_actions, | |
712 | bool *mutates) | |
713 | { | |
714 | unsigned int i; | |
715 | ||
716 | *mutates = false; | |
717 | for (i = 0; i < n_actions; i++) { | |
718 | const union odp_action *a = &actions[i]; | |
719 | switch (a->type) { | |
720 | case ODPAT_OUTPUT: | |
721 | if (a->output.port >= MAX_PORTS) { | |
722 | return EINVAL; | |
723 | } | |
724 | break; | |
725 | ||
726 | case ODPAT_OUTPUT_GROUP: | |
727 | *mutates = true; | |
728 | if (a->output_group.group >= N_GROUPS) { | |
729 | return EINVAL; | |
730 | } | |
731 | break; | |
732 | ||
733 | case ODPAT_CONTROLLER: | |
734 | break; | |
735 | ||
736 | case ODPAT_SET_VLAN_VID: | |
737 | *mutates = true; | |
738 | if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) { | |
739 | return EINVAL; | |
740 | } | |
741 | break; | |
742 | ||
743 | case ODPAT_SET_VLAN_PCP: | |
744 | *mutates = true; | |
745 | if (a->vlan_pcp.vlan_pcp & ~VLAN_PCP_MASK) { | |
746 | return EINVAL; | |
747 | } | |
748 | break; | |
749 | ||
750 | case ODPAT_STRIP_VLAN: | |
751 | case ODPAT_SET_DL_SRC: | |
752 | case ODPAT_SET_DL_DST: | |
753 | case ODPAT_SET_NW_SRC: | |
754 | case ODPAT_SET_NW_DST: | |
755 | case ODPAT_SET_TP_SRC: | |
756 | case ODPAT_SET_TP_DST: | |
757 | *mutates = true; | |
758 | break; | |
759 | ||
760 | default: | |
761 | return EOPNOTSUPP; | |
762 | } | |
763 | } | |
764 | return 0; | |
765 | } | |
766 | ||
767 | static int | |
768 | set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow) | |
769 | { | |
770 | size_t n_bytes; | |
771 | bool mutates; | |
772 | int error; | |
773 | ||
774 | if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) { | |
775 | return EINVAL; | |
776 | } | |
777 | error = dpif_netdev_validate_actions(odp_flow->actions, | |
778 | odp_flow->n_actions, &mutates); | |
779 | if (error) { | |
780 | return error; | |
781 | } | |
782 | ||
783 | n_bytes = odp_flow->n_actions * sizeof *flow->actions; | |
784 | flow->actions = xrealloc(flow->actions, n_bytes); | |
785 | flow->n_actions = odp_flow->n_actions; | |
786 | memcpy(flow->actions, odp_flow->actions, n_bytes); | |
787 | return 0; | |
788 | } | |
789 | ||
790 | static int | |
791 | add_flow(struct dpif *dpif, struct odp_flow *odp_flow) | |
792 | { | |
793 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
794 | struct dp_netdev_flow *flow; | |
795 | int error; | |
796 | ||
797 | flow = xcalloc(1, sizeof *flow); | |
798 | flow->key = odp_flow->key; | |
799 | flow->key.reserved = 0; | |
800 | ||
801 | error = set_flow_actions(flow, odp_flow); | |
802 | if (error) { | |
803 | free(flow); | |
804 | return error; | |
805 | } | |
806 | ||
807 | hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0)); | |
808 | return 0; | |
809 | } | |
810 | ||
811 | static void | |
812 | clear_stats(struct dp_netdev_flow *flow) | |
813 | { | |
814 | flow->used.tv_sec = 0; | |
815 | flow->used.tv_usec = 0; | |
816 | flow->packet_count = 0; | |
817 | flow->byte_count = 0; | |
818 | flow->ip_tos = 0; | |
819 | flow->tcp_ctl = 0; | |
820 | } | |
821 | ||
822 | static int | |
823 | dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put) | |
824 | { | |
825 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
826 | struct dp_netdev_flow *flow; | |
827 | ||
828 | flow = dp_netdev_lookup_flow(dp, &put->flow.key); | |
829 | if (!flow) { | |
830 | if (put->flags & ODPPF_CREATE) { | |
831 | if (hmap_count(&dp->flow_table) < MAX_FLOWS) { | |
832 | return add_flow(dpif, &put->flow); | |
833 | } else { | |
3c71830a | 834 | return EFBIG; |
72865317 BP |
835 | } |
836 | } else { | |
837 | return ENOENT; | |
838 | } | |
839 | } else { | |
840 | if (put->flags & ODPPF_MODIFY) { | |
841 | int error = set_flow_actions(flow, &put->flow); | |
842 | if (!error && put->flags & ODPPF_ZERO_STATS) { | |
843 | clear_stats(flow); | |
844 | } | |
845 | return error; | |
846 | } else { | |
847 | return EEXIST; | |
848 | } | |
849 | } | |
850 | } | |
851 | ||
852 | ||
853 | static int | |
854 | dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow) | |
855 | { | |
856 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
857 | struct dp_netdev_flow *flow; | |
858 | ||
859 | flow = dp_netdev_lookup_flow(dp, &odp_flow->key); | |
860 | if (flow) { | |
fde05ade | 861 | answer_flow_query(flow, 0, odp_flow); |
72865317 BP |
862 | dp_netdev_free_flow(dp, flow); |
863 | return 0; | |
864 | } else { | |
865 | return ENOENT; | |
866 | } | |
867 | } | |
868 | ||
869 | static int | |
870 | dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n) | |
871 | { | |
872 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
873 | struct dp_netdev_flow *flow; | |
874 | int i; | |
875 | ||
876 | i = 0; | |
877 | HMAP_FOR_EACH (flow, struct dp_netdev_flow, node, &dp->flow_table) { | |
878 | if (i >= n) { | |
879 | break; | |
880 | } | |
fde05ade | 881 | answer_flow_query(flow, 0, &flows[i++]); |
72865317 BP |
882 | } |
883 | return hmap_count(&dp->flow_table); | |
884 | } | |
885 | ||
886 | static int | |
887 | dpif_netdev_execute(struct dpif *dpif, uint16_t in_port, | |
888 | const union odp_action actions[], int n_actions, | |
889 | const struct ofpbuf *packet) | |
890 | { | |
891 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
892 | struct ofpbuf copy; | |
893 | bool mutates; | |
894 | flow_t flow; | |
895 | int error; | |
896 | ||
3c71830a | 897 | if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) { |
72865317 BP |
898 | return EINVAL; |
899 | } | |
900 | ||
901 | error = dpif_netdev_validate_actions(actions, n_actions, &mutates); | |
902 | if (error) { | |
903 | return error; | |
904 | } | |
905 | ||
906 | if (mutates) { | |
907 | /* We need a deep copy of 'packet' since we're going to modify its | |
908 | * data. */ | |
909 | ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size); | |
910 | copy.data = (char*)copy.base + DP_NETDEV_HEADROOM; | |
911 | ofpbuf_put(©, packet->data, packet->size); | |
912 | } else { | |
913 | /* We still need a shallow copy of 'packet', even though we won't | |
914 | * modify its data, because flow_extract() modifies packet->l2, etc. | |
915 | * We could probably get away with modifying those but it's more polite | |
916 | * if we don't. */ | |
917 | copy = *packet; | |
918 | } | |
919 | flow_extract(©, in_port, &flow); | |
920 | error = dp_netdev_execute_actions(dp, ©, &flow, actions, n_actions); | |
921 | if (mutates) { | |
922 | ofpbuf_uninit(©); | |
923 | } | |
924 | return error; | |
925 | } | |
926 | ||
927 | static int | |
928 | dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask) | |
929 | { | |
930 | struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif); | |
931 | *listen_mask = dpif_netdev->listen_mask; | |
932 | return 0; | |
933 | } | |
934 | ||
935 | static int | |
936 | dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask) | |
937 | { | |
938 | struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif); | |
939 | if (!(listen_mask & ~ODPL_ALL)) { | |
940 | dpif_netdev->listen_mask = listen_mask; | |
941 | return 0; | |
942 | } else { | |
943 | return EINVAL; | |
944 | } | |
945 | } | |
946 | ||
947 | static struct ovs_queue * | |
948 | find_nonempty_queue(struct dpif *dpif) | |
949 | { | |
950 | struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif); | |
951 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
952 | int mask = dpif_netdev->listen_mask; | |
953 | int i; | |
954 | ||
955 | for (i = 0; i < N_QUEUES; i++) { | |
956 | struct ovs_queue *q = &dp->queues[i]; | |
957 | if (q->n && mask & (1u << i)) { | |
958 | return q; | |
959 | } | |
960 | } | |
961 | return NULL; | |
962 | } | |
963 | ||
964 | static int | |
965 | dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp) | |
966 | { | |
967 | struct ovs_queue *q = find_nonempty_queue(dpif); | |
968 | if (q) { | |
969 | *bufp = queue_pop_head(q); | |
970 | return 0; | |
971 | } else { | |
972 | return EAGAIN; | |
973 | } | |
974 | } | |
975 | ||
976 | static void | |
977 | dpif_netdev_recv_wait(struct dpif *dpif) | |
978 | { | |
979 | struct ovs_queue *q = find_nonempty_queue(dpif); | |
980 | if (q) { | |
981 | poll_immediate_wake(); | |
982 | } else { | |
983 | /* No messages ready to be received, and dp_wait() will ensure that we | |
984 | * wake up to queue new messages, so there is nothing to do. */ | |
985 | } | |
986 | } | |
987 | \f | |
988 | static void | |
989 | dp_netdev_flow_used(struct dp_netdev_flow *flow, const flow_t *key, | |
990 | const struct ofpbuf *packet) | |
991 | { | |
992 | time_timeval(&flow->used); | |
993 | flow->packet_count++; | |
994 | flow->byte_count += packet->size; | |
3c71830a | 995 | if (key->dl_type == htons(ETH_TYPE_IP)) { |
72865317 BP |
996 | struct ip_header *nh = packet->l3; |
997 | flow->ip_tos = nh->ip_tos; | |
998 | ||
999 | if (key->nw_proto == IPPROTO_TCP) { | |
1000 | struct tcp_header *th = packet->l4; | |
1001 | flow->tcp_ctl |= th->tcp_ctl; | |
1002 | } | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | static void | |
1007 | dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port, | |
1008 | struct ofpbuf *packet) | |
1009 | { | |
1010 | struct dp_netdev_flow *flow; | |
1011 | flow_t key; | |
1012 | ||
1013 | if (flow_extract(packet, port->port_no, &key) && dp->drop_frags) { | |
1014 | dp->n_frags++; | |
1015 | return; | |
1016 | } | |
1017 | ||
1018 | flow = dp_netdev_lookup_flow(dp, &key); | |
1019 | if (flow) { | |
1020 | dp_netdev_flow_used(flow, &key, packet); | |
1021 | dp_netdev_execute_actions(dp, packet, &key, | |
1022 | flow->actions, flow->n_actions); | |
1023 | dp->n_hit++; | |
1024 | } else { | |
1025 | dp->n_missed++; | |
1026 | dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0); | |
1027 | } | |
1028 | } | |
1029 | ||
1030 | static void | |
1031 | dp_netdev_run(void) | |
1032 | { | |
1033 | struct ofpbuf packet; | |
1034 | struct dp_netdev *dp; | |
1035 | ||
1036 | ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu); | |
1037 | LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) { | |
1038 | struct dp_netdev_port *port; | |
1039 | ||
1040 | LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) { | |
1041 | int error; | |
1042 | ||
1043 | /* Reset packet contents. */ | |
1044 | packet.data = (char*)packet.base + DP_NETDEV_HEADROOM; | |
1045 | packet.size = 0; | |
1046 | ||
1047 | error = netdev_recv(port->netdev, &packet); | |
1048 | if (!error) { | |
1049 | dp_netdev_port_input(dp, port, &packet); | |
1050 | } else if (error != EAGAIN) { | |
1051 | struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
1052 | VLOG_ERR_RL(&rl, "error receiving data from %s: %s", | |
1053 | netdev_get_name(port->netdev), strerror(error)); | |
1054 | } | |
1055 | } | |
1056 | } | |
1057 | ofpbuf_uninit(&packet); | |
1058 | } | |
1059 | ||
1060 | static void | |
1061 | dp_netdev_wait(void) | |
1062 | { | |
1063 | struct dp_netdev *dp; | |
1064 | ||
1065 | LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) { | |
1066 | struct dp_netdev_port *port; | |
1067 | LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) { | |
1068 | netdev_recv_wait(port->netdev); | |
1069 | } | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | static void | |
1074 | dp_netdev_modify_vlan_tci(struct ofpbuf *packet, flow_t *key, | |
1075 | uint16_t tci, uint16_t mask) | |
1076 | { | |
1077 | struct vlan_eth_header *veh; | |
1078 | ||
1079 | if (key->dl_vlan != htons(ODP_VLAN_NONE)) { | |
1080 | /* Modify 'mask' bits, but maintain other TCI bits. */ | |
1081 | veh = packet->l2; | |
1082 | veh->veth_tci &= ~htons(mask); | |
1083 | veh->veth_tci |= htons(tci); | |
1084 | } else { | |
1085 | /* Insert new 802.1Q header. */ | |
1086 | struct eth_header *eh = packet->l2; | |
1087 | struct vlan_eth_header tmp; | |
1088 | memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN); | |
1089 | memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN); | |
1090 | tmp.veth_type = htons(ETH_TYPE_VLAN); | |
1091 | tmp.veth_tci = htons(tci); | |
1092 | tmp.veth_next_type = eh->eth_type; | |
1093 | ||
1094 | veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN); | |
1095 | memcpy(veh, &tmp, sizeof tmp); | |
1096 | packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN; | |
1097 | } | |
1098 | ||
1099 | key->dl_vlan = veh->veth_tci & htons(VLAN_VID_MASK); | |
1100 | } | |
1101 | ||
1102 | static void | |
1103 | dp_netdev_strip_vlan(struct ofpbuf *packet, flow_t *key) | |
1104 | { | |
1105 | struct vlan_eth_header *veh = packet->l2; | |
1106 | if (veh->veth_type == htons(ETH_TYPE_VLAN)) { | |
1107 | struct eth_header tmp; | |
1108 | ||
1109 | memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN); | |
1110 | memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN); | |
1111 | tmp.eth_type = veh->veth_next_type; | |
1112 | ||
1113 | packet->size -= VLAN_HEADER_LEN; | |
1114 | packet->data = (char*)packet->data + VLAN_HEADER_LEN; | |
1115 | packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN; | |
1116 | memcpy(packet->data, &tmp, sizeof tmp); | |
1117 | ||
1118 | key->dl_vlan = htons(ODP_VLAN_NONE); | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | static void | |
1123 | dp_netdev_set_dl_src(struct ofpbuf *packet, | |
1124 | const uint8_t dl_addr[ETH_ADDR_LEN]) | |
1125 | { | |
1126 | struct eth_header *eh = packet->l2; | |
1127 | memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src); | |
1128 | } | |
1129 | ||
1130 | static void | |
1131 | dp_netdev_set_dl_dst(struct ofpbuf *packet, | |
1132 | const uint8_t dl_addr[ETH_ADDR_LEN]) | |
1133 | { | |
1134 | struct eth_header *eh = packet->l2; | |
1135 | memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst); | |
1136 | } | |
1137 | ||
1138 | static void | |
1139 | dp_netdev_set_nw_addr(struct ofpbuf *packet, flow_t *key, | |
1140 | const struct odp_action_nw_addr *a) | |
1141 | { | |
1142 | if (key->dl_type == htons(ETH_TYPE_IP)) { | |
1143 | struct ip_header *nh = packet->l3; | |
1144 | uint32_t *field; | |
1145 | ||
1146 | field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst; | |
1147 | if (key->nw_proto == IP_TYPE_TCP) { | |
1148 | struct tcp_header *th = packet->l4; | |
1149 | th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr); | |
1150 | } else if (key->nw_proto == IP_TYPE_UDP) { | |
1151 | struct udp_header *uh = packet->l4; | |
1152 | if (uh->udp_csum) { | |
1153 | uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr); | |
1154 | if (!uh->udp_csum) { | |
1155 | uh->udp_csum = 0xffff; | |
1156 | } | |
1157 | } | |
1158 | } | |
1159 | nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr); | |
1160 | *field = a->nw_addr; | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | static void | |
1165 | dp_netdev_set_tp_port(struct ofpbuf *packet, flow_t *key, | |
1166 | const struct odp_action_tp_port *a) | |
1167 | { | |
3c71830a | 1168 | if (key->dl_type == htons(ETH_TYPE_IP)) { |
72865317 BP |
1169 | uint16_t *field; |
1170 | if (key->nw_proto == IPPROTO_TCP) { | |
1171 | struct tcp_header *th = packet->l4; | |
1172 | field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst; | |
1173 | th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port); | |
1174 | *field = a->tp_port; | |
1175 | } else if (key->nw_proto == IPPROTO_UDP) { | |
1176 | struct udp_header *uh = packet->l4; | |
1177 | field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst; | |
1178 | uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port); | |
1179 | *field = a->tp_port; | |
1180 | } | |
1181 | } | |
1182 | } | |
1183 | ||
1184 | static void | |
1185 | dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet, | |
1186 | uint16_t out_port) | |
1187 | { | |
1188 | struct dp_netdev_port *p = dp->ports[out_port]; | |
1189 | if (p) { | |
1190 | netdev_send(p->netdev, packet); | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | static void | |
1195 | dp_netdev_output_group(struct dp_netdev *dp, uint16_t group, uint16_t in_port, | |
1196 | struct ofpbuf *packet) | |
1197 | { | |
1198 | struct odp_port_group *g = &dp->groups[group]; | |
1199 | int i; | |
1200 | ||
1201 | for (i = 0; i < g->n_ports; i++) { | |
1202 | uint16_t out_port = g->ports[i]; | |
1203 | if (out_port != in_port) { | |
1204 | dp_netdev_output_port(dp, packet, out_port); | |
1205 | } | |
1206 | } | |
1207 | } | |
1208 | ||
1209 | static int | |
1210 | dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet, | |
1211 | int queue_no, int port_no, uint32_t arg) | |
1212 | { | |
1213 | struct ovs_queue *q = &dp->queues[queue_no]; | |
1214 | struct odp_msg *header; | |
1215 | struct ofpbuf *msg; | |
1216 | size_t msg_size; | |
1217 | ||
1218 | if (q->n >= MAX_QUEUE_LEN) { | |
1219 | dp->n_lost++; | |
1220 | return ENOBUFS; | |
1221 | } | |
1222 | ||
1223 | msg_size = sizeof *header + packet->size; | |
1224 | msg = ofpbuf_new(msg_size); | |
1225 | header = ofpbuf_put_uninit(msg, sizeof *header); | |
1226 | header->type = queue_no; | |
1227 | header->length = msg_size; | |
1228 | header->port = port_no; | |
1229 | header->arg = arg; | |
1230 | ofpbuf_put(msg, packet->data, packet->size); | |
1231 | queue_push_tail(q, msg); | |
1232 | ||
1233 | return 0; | |
1234 | } | |
1235 | ||
1236 | static int | |
1237 | dp_netdev_execute_actions(struct dp_netdev *dp, | |
1238 | struct ofpbuf *packet, flow_t *key, | |
1239 | const union odp_action *actions, int n_actions) | |
1240 | { | |
1241 | int i; | |
1242 | for (i = 0; i < n_actions; i++) { | |
1243 | const union odp_action *a = &actions[i]; | |
1244 | ||
1245 | switch (a->type) { | |
1246 | case ODPAT_OUTPUT: | |
1247 | dp_netdev_output_port(dp, packet, a->output.port); | |
1248 | break; | |
1249 | ||
1250 | case ODPAT_OUTPUT_GROUP: | |
1251 | dp_netdev_output_group(dp, a->output_group.group, key->in_port, | |
1252 | packet); | |
1253 | break; | |
1254 | ||
1255 | case ODPAT_CONTROLLER: | |
1256 | dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR, | |
1257 | key->in_port, a->controller.arg); | |
1258 | break; | |
1259 | ||
1260 | case ODPAT_SET_VLAN_VID: | |
1261 | dp_netdev_modify_vlan_tci(packet, key, ntohs(a->vlan_vid.vlan_vid), | |
1262 | VLAN_VID_MASK); | |
1263 | break; | |
1264 | ||
1265 | case ODPAT_SET_VLAN_PCP: | |
1266 | dp_netdev_modify_vlan_tci(packet, key, a->vlan_pcp.vlan_pcp << 13, | |
1267 | VLAN_PCP_MASK); | |
1268 | break; | |
1269 | ||
1270 | case ODPAT_STRIP_VLAN: | |
1271 | dp_netdev_strip_vlan(packet, key); | |
1272 | break; | |
1273 | ||
1274 | case ODPAT_SET_DL_SRC: | |
1275 | dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr); | |
1276 | break; | |
1277 | ||
1278 | case ODPAT_SET_DL_DST: | |
1279 | dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr); | |
1280 | break; | |
1281 | ||
1282 | case ODPAT_SET_NW_SRC: | |
1283 | case ODPAT_SET_NW_DST: | |
1284 | dp_netdev_set_nw_addr(packet, key, &a->nw_addr); | |
1285 | break; | |
1286 | ||
1287 | case ODPAT_SET_TP_SRC: | |
1288 | case ODPAT_SET_TP_DST: | |
1289 | dp_netdev_set_tp_port(packet, key, &a->tp_port); | |
1290 | break; | |
1291 | } | |
1292 | } | |
1293 | return 0; | |
1294 | } | |
1295 | ||
1296 | const struct dpif_class dpif_netdev_class = { | |
1297 | "netdev", | |
1298 | "netdev", | |
1299 | dp_netdev_run, | |
1300 | dp_netdev_wait, | |
d3d22744 | 1301 | NULL, /* enumerate */ |
72865317 BP |
1302 | dpif_netdev_open, |
1303 | dpif_netdev_close, | |
d3d22744 | 1304 | NULL, /* get_all_names */ |
72865317 BP |
1305 | dpif_netdev_delete, |
1306 | dpif_netdev_get_stats, | |
1307 | dpif_netdev_get_drop_frags, | |
1308 | dpif_netdev_set_drop_frags, | |
1309 | dpif_netdev_port_add, | |
1310 | dpif_netdev_port_del, | |
1311 | dpif_netdev_port_query_by_number, | |
1312 | dpif_netdev_port_query_by_name, | |
1313 | dpif_netdev_port_list, | |
1314 | dpif_netdev_port_poll, | |
1315 | dpif_netdev_port_poll_wait, | |
1316 | dpif_netdev_port_group_get, | |
1317 | dpif_netdev_port_group_set, | |
1318 | dpif_netdev_flow_get, | |
1319 | dpif_netdev_flow_put, | |
1320 | dpif_netdev_flow_del, | |
1321 | dpif_netdev_flow_flush, | |
1322 | dpif_netdev_flow_list, | |
1323 | dpif_netdev_execute, | |
1324 | dpif_netdev_recv_get_mask, | |
1325 | dpif_netdev_recv_set_mask, | |
1326 | dpif_netdev_recv, | |
1327 | dpif_netdev_recv_wait, | |
1328 | }; |