]>
Commit | Line | Data |
---|---|---|
72865317 | 1 | /* |
149f577a | 2 | * Copyright (c) 2009, 2010 Nicira Networks. |
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 | ||
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 | ||
1a6f1e2a | 199 | dpname = xasprintf("dp%d", dp->dp_idx); |
72865317 BP |
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. */ | |
ec6fde61 | 222 | dp_netdevs[dp_idx] = dp = xzalloc(sizeof *dp); |
72865317 BP |
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); | |
149f577a | 240 | return ENODEV; |
72865317 BP |
241 | } |
242 | ||
243 | *dpifp = create_dpif_netdev(dp); | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static int | |
1a6f1e2a | 248 | dpif_netdev_open(const char *name, const char *type UNUSED, bool create, |
72865317 BP |
249 | struct dpif **dpifp) |
250 | { | |
251 | if (create) { | |
1a6f1e2a | 252 | if (find_dp_netdev(name)) { |
72865317 BP |
253 | return EEXIST; |
254 | } else { | |
1a6f1e2a | 255 | int dp_idx = name_to_dp_idx(name); |
72865317 | 256 | if (dp_idx >= 0) { |
1a6f1e2a | 257 | return create_dp_netdev(name, dp_idx, dpifp); |
72865317 BP |
258 | } else { |
259 | /* Scan for unused dp_idx number. */ | |
260 | for (dp_idx = 0; dp_idx < N_DP_NETDEVS; dp_idx++) { | |
1a6f1e2a | 261 | int error = create_dp_netdev(name, dp_idx, dpifp); |
72865317 BP |
262 | if (error != EBUSY) { |
263 | return error; | |
264 | } | |
265 | } | |
266 | ||
267 | /* All datapath numbers in use. */ | |
268 | return ENOBUFS; | |
269 | } | |
270 | } | |
271 | } else { | |
1a6f1e2a | 272 | struct dp_netdev *dp = find_dp_netdev(name); |
72865317 BP |
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; | |
149f577a | 366 | struct netdev_options netdev_options; |
72865317 BP |
367 | struct netdev *netdev; |
368 | int mtu; | |
369 | int error; | |
370 | ||
371 | /* XXX reject devices already in some dp_netdev. */ | |
372 | ||
373 | /* Open and validate network device. */ | |
149f577a JG |
374 | memset(&netdev_options, 0, sizeof netdev_options); |
375 | netdev_options.name = devname; | |
376 | netdev_options.ethertype = NETDEV_ETH_TYPE_ANY; | |
377 | netdev_options.may_create = true; | |
378 | if (internal) { | |
379 | netdev_options.type = "tap"; | |
72865317 | 380 | } else { |
149f577a | 381 | netdev_options.may_open = true; |
72865317 | 382 | } |
149f577a JG |
383 | |
384 | error = netdev_open(&netdev_options, &netdev); | |
72865317 BP |
385 | if (error) { |
386 | return error; | |
387 | } | |
388 | /* XXX reject loopback devices */ | |
389 | /* XXX reject non-Ethernet devices */ | |
390 | ||
391 | error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false); | |
392 | if (error) { | |
393 | netdev_close(netdev); | |
394 | return error; | |
395 | } | |
396 | ||
397 | port = xmalloc(sizeof *port); | |
398 | port->port_no = port_no; | |
399 | port->netdev = netdev; | |
400 | port->internal = internal; | |
401 | ||
3d222126 | 402 | netdev_get_mtu(netdev, &mtu); |
72865317 BP |
403 | if (mtu > max_mtu) { |
404 | max_mtu = mtu; | |
405 | } | |
406 | ||
407 | list_push_back(&dp->port_list, &port->node); | |
408 | dp->ports[port_no] = port; | |
409 | dp->n_ports++; | |
410 | dp->serial++; | |
411 | ||
412 | return 0; | |
413 | } | |
414 | ||
415 | static int | |
416 | dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags, | |
417 | uint16_t *port_nop) | |
418 | { | |
419 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
420 | int port_no; | |
421 | ||
422 | for (port_no = 0; port_no < MAX_PORTS; port_no++) { | |
423 | if (!dp->ports[port_no]) { | |
424 | *port_nop = port_no; | |
425 | return do_add_port(dp, devname, flags, port_no); | |
426 | } | |
427 | } | |
3c71830a | 428 | return EFBIG; |
72865317 BP |
429 | } |
430 | ||
431 | static int | |
432 | dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no) | |
433 | { | |
434 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
435 | return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no); | |
436 | } | |
437 | ||
438 | static bool | |
439 | is_valid_port_number(uint16_t port_no) | |
440 | { | |
441 | return port_no < MAX_PORTS; | |
442 | } | |
443 | ||
444 | static int | |
445 | get_port_by_number(struct dp_netdev *dp, | |
446 | uint16_t port_no, struct dp_netdev_port **portp) | |
447 | { | |
448 | if (!is_valid_port_number(port_no)) { | |
449 | *portp = NULL; | |
450 | return EINVAL; | |
451 | } else { | |
452 | *portp = dp->ports[port_no]; | |
453 | return *portp ? 0 : ENOENT; | |
454 | } | |
455 | } | |
456 | ||
457 | static int | |
458 | get_port_by_name(struct dp_netdev *dp, | |
459 | const char *devname, struct dp_netdev_port **portp) | |
460 | { | |
461 | struct dp_netdev_port *port; | |
462 | ||
463 | LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) { | |
464 | if (!strcmp(netdev_get_name(port->netdev), devname)) { | |
465 | *portp = port; | |
466 | return 0; | |
467 | } | |
468 | } | |
469 | return ENOENT; | |
470 | } | |
471 | ||
472 | static int | |
473 | do_del_port(struct dp_netdev *dp, uint16_t port_no) | |
474 | { | |
475 | struct dp_netdev_port *port; | |
6c88d577 | 476 | char *name; |
72865317 BP |
477 | int error; |
478 | ||
479 | error = get_port_by_number(dp, port_no, &port); | |
480 | if (error) { | |
481 | return error; | |
482 | } | |
483 | ||
484 | list_remove(&port->node); | |
485 | dp->ports[port->port_no] = NULL; | |
486 | dp->n_ports--; | |
487 | dp->serial++; | |
488 | ||
6c88d577 | 489 | name = xstrdup(netdev_get_name(port->netdev)); |
72865317 | 490 | netdev_close(port->netdev); |
149f577a | 491 | |
6c88d577 | 492 | free(name); |
72865317 BP |
493 | free(port); |
494 | ||
495 | return 0; | |
496 | } | |
497 | ||
498 | static void | |
499 | answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port) | |
500 | { | |
501 | memset(odp_port, 0, sizeof *odp_port); | |
502 | ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev), | |
503 | sizeof odp_port->devname); | |
504 | odp_port->port = port->port_no; | |
505 | odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0; | |
506 | } | |
507 | ||
508 | static int | |
509 | dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no, | |
510 | struct odp_port *odp_port) | |
511 | { | |
512 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
513 | struct dp_netdev_port *port; | |
514 | int error; | |
515 | ||
516 | error = get_port_by_number(dp, port_no, &port); | |
517 | if (!error) { | |
518 | answer_port_query(port, odp_port); | |
519 | } | |
520 | return error; | |
521 | } | |
522 | ||
523 | static int | |
524 | dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname, | |
525 | struct odp_port *odp_port) | |
526 | { | |
527 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
528 | struct dp_netdev_port *port; | |
529 | int error; | |
530 | ||
531 | error = get_port_by_name(dp, devname, &port); | |
532 | if (!error) { | |
533 | answer_port_query(port, odp_port); | |
534 | } | |
535 | return error; | |
536 | } | |
537 | ||
538 | static void | |
539 | dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow) | |
540 | { | |
541 | hmap_remove(&dp->flow_table, &flow->node); | |
542 | free(flow->actions); | |
543 | free(flow); | |
544 | } | |
545 | ||
546 | static void | |
547 | dp_netdev_flow_flush(struct dp_netdev *dp) | |
548 | { | |
549 | struct dp_netdev_flow *flow, *next; | |
550 | ||
551 | HMAP_FOR_EACH_SAFE (flow, next, struct dp_netdev_flow, node, | |
552 | &dp->flow_table) { | |
553 | dp_netdev_free_flow(dp, flow); | |
554 | } | |
555 | } | |
556 | ||
557 | static int | |
558 | dpif_netdev_flow_flush(struct dpif *dpif) | |
559 | { | |
560 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
561 | dp_netdev_flow_flush(dp); | |
562 | return 0; | |
563 | } | |
564 | ||
565 | static int | |
566 | dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n) | |
567 | { | |
568 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
569 | struct dp_netdev_port *port; | |
570 | int i; | |
571 | ||
572 | i = 0; | |
573 | LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) { | |
574 | struct odp_port *odp_port = &ports[i]; | |
575 | if (i >= n) { | |
576 | break; | |
577 | } | |
578 | answer_port_query(port, odp_port); | |
579 | i++; | |
580 | } | |
581 | return dp->n_ports; | |
582 | } | |
583 | ||
584 | static int | |
585 | dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep UNUSED) | |
586 | { | |
587 | struct dpif_netdev *dpif = dpif_netdev_cast(dpif_); | |
588 | if (dpif->dp_serial != dpif->dp->serial) { | |
589 | dpif->dp_serial = dpif->dp->serial; | |
590 | return ENOBUFS; | |
591 | } else { | |
592 | return EAGAIN; | |
593 | } | |
594 | } | |
595 | ||
596 | static void | |
597 | dpif_netdev_port_poll_wait(const struct dpif *dpif_) | |
598 | { | |
599 | struct dpif_netdev *dpif = dpif_netdev_cast(dpif_); | |
600 | if (dpif->dp_serial != dpif->dp->serial) { | |
601 | poll_immediate_wake(); | |
602 | } | |
603 | } | |
604 | ||
605 | static int | |
606 | get_port_group(const struct dpif *dpif, int group_no, | |
607 | struct odp_port_group **groupp) | |
608 | { | |
609 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
610 | ||
611 | if (group_no >= 0 && group_no < N_GROUPS) { | |
612 | *groupp = &dp->groups[group_no]; | |
613 | return 0; | |
614 | } else { | |
615 | *groupp = NULL; | |
616 | return EINVAL; | |
617 | } | |
618 | } | |
619 | ||
620 | static int | |
621 | dpif_netdev_port_group_get(const struct dpif *dpif, int group_no, | |
622 | uint16_t ports[], int n) | |
623 | { | |
624 | struct odp_port_group *group; | |
625 | int error; | |
626 | ||
627 | if (n < 0) { | |
628 | return -EINVAL; | |
629 | } | |
630 | ||
631 | error = get_port_group(dpif, group_no, &group); | |
632 | if (!error) { | |
633 | memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports); | |
634 | return group->n_ports; | |
635 | } else { | |
636 | return -error; | |
637 | } | |
638 | } | |
639 | ||
640 | static int | |
641 | dpif_netdev_port_group_set(struct dpif *dpif, int group_no, | |
642 | const uint16_t ports[], int n) | |
643 | { | |
644 | struct odp_port_group *group; | |
645 | int error; | |
646 | ||
647 | if (n < 0 || n > MAX_PORTS) { | |
648 | return EINVAL; | |
649 | } | |
650 | ||
651 | error = get_port_group(dpif, group_no, &group); | |
652 | if (!error) { | |
653 | free(group->ports); | |
654 | group->ports = xmemdup(ports, n * sizeof *group->ports); | |
655 | group->n_ports = n; | |
656 | group->group = group_no; | |
657 | } | |
658 | return error; | |
659 | } | |
660 | ||
661 | static struct dp_netdev_flow * | |
662 | dp_netdev_lookup_flow(const struct dp_netdev *dp, const flow_t *key) | |
663 | { | |
664 | struct dp_netdev_flow *flow; | |
665 | ||
666 | assert(key->reserved == 0); | |
667 | HMAP_FOR_EACH_WITH_HASH (flow, struct dp_netdev_flow, node, | |
668 | flow_hash(key, 0), &dp->flow_table) { | |
669 | if (flow_equal(&flow->key, key)) { | |
670 | return flow; | |
671 | } | |
672 | } | |
673 | return NULL; | |
674 | } | |
675 | ||
676 | static void | |
fde05ade | 677 | answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags, |
72865317 BP |
678 | struct odp_flow *odp_flow) |
679 | { | |
680 | if (flow) { | |
681 | odp_flow->key = flow->key; | |
682 | odp_flow->stats.n_packets = flow->packet_count; | |
683 | odp_flow->stats.n_bytes = flow->byte_count; | |
684 | odp_flow->stats.used_sec = flow->used.tv_sec; | |
685 | odp_flow->stats.used_nsec = flow->used.tv_usec * 1000; | |
686 | odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl); | |
687 | odp_flow->stats.ip_tos = flow->ip_tos; | |
688 | odp_flow->stats.error = 0; | |
689 | if (odp_flow->n_actions > 0) { | |
690 | unsigned int n = MIN(odp_flow->n_actions, flow->n_actions); | |
691 | memcpy(odp_flow->actions, flow->actions, | |
692 | n * sizeof *odp_flow->actions); | |
693 | odp_flow->n_actions = flow->n_actions; | |
694 | } | |
fde05ade JG |
695 | |
696 | if (query_flags & ODPFF_ZERO_TCP_FLAGS) { | |
697 | flow->tcp_ctl = 0; | |
698 | } | |
699 | ||
72865317 BP |
700 | } else { |
701 | odp_flow->stats.error = ENOENT; | |
702 | } | |
703 | } | |
704 | ||
705 | static int | |
706 | dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n) | |
707 | { | |
708 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
709 | int i; | |
710 | ||
711 | for (i = 0; i < n; i++) { | |
712 | struct odp_flow *odp_flow = &flows[i]; | |
fde05ade JG |
713 | answer_flow_query(dp_netdev_lookup_flow(dp, &odp_flow->key), |
714 | odp_flow->flags, odp_flow); | |
72865317 BP |
715 | } |
716 | return 0; | |
717 | } | |
718 | ||
719 | static int | |
720 | dpif_netdev_validate_actions(const union odp_action *actions, int n_actions, | |
721 | bool *mutates) | |
722 | { | |
723 | unsigned int i; | |
724 | ||
725 | *mutates = false; | |
726 | for (i = 0; i < n_actions; i++) { | |
727 | const union odp_action *a = &actions[i]; | |
728 | switch (a->type) { | |
729 | case ODPAT_OUTPUT: | |
730 | if (a->output.port >= MAX_PORTS) { | |
731 | return EINVAL; | |
732 | } | |
733 | break; | |
734 | ||
735 | case ODPAT_OUTPUT_GROUP: | |
736 | *mutates = true; | |
737 | if (a->output_group.group >= N_GROUPS) { | |
738 | return EINVAL; | |
739 | } | |
740 | break; | |
741 | ||
742 | case ODPAT_CONTROLLER: | |
743 | break; | |
744 | ||
745 | case ODPAT_SET_VLAN_VID: | |
746 | *mutates = true; | |
747 | if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) { | |
748 | return EINVAL; | |
749 | } | |
750 | break; | |
751 | ||
752 | case ODPAT_SET_VLAN_PCP: | |
753 | *mutates = true; | |
754 | if (a->vlan_pcp.vlan_pcp & ~VLAN_PCP_MASK) { | |
755 | return EINVAL; | |
756 | } | |
757 | break; | |
758 | ||
759 | case ODPAT_STRIP_VLAN: | |
760 | case ODPAT_SET_DL_SRC: | |
761 | case ODPAT_SET_DL_DST: | |
762 | case ODPAT_SET_NW_SRC: | |
763 | case ODPAT_SET_NW_DST: | |
764 | case ODPAT_SET_TP_SRC: | |
765 | case ODPAT_SET_TP_DST: | |
766 | *mutates = true; | |
767 | break; | |
768 | ||
769 | default: | |
770 | return EOPNOTSUPP; | |
771 | } | |
772 | } | |
773 | return 0; | |
774 | } | |
775 | ||
776 | static int | |
777 | set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow) | |
778 | { | |
779 | size_t n_bytes; | |
780 | bool mutates; | |
781 | int error; | |
782 | ||
783 | if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) { | |
784 | return EINVAL; | |
785 | } | |
786 | error = dpif_netdev_validate_actions(odp_flow->actions, | |
787 | odp_flow->n_actions, &mutates); | |
788 | if (error) { | |
789 | return error; | |
790 | } | |
791 | ||
792 | n_bytes = odp_flow->n_actions * sizeof *flow->actions; | |
793 | flow->actions = xrealloc(flow->actions, n_bytes); | |
794 | flow->n_actions = odp_flow->n_actions; | |
795 | memcpy(flow->actions, odp_flow->actions, n_bytes); | |
796 | return 0; | |
797 | } | |
798 | ||
799 | static int | |
800 | add_flow(struct dpif *dpif, struct odp_flow *odp_flow) | |
801 | { | |
802 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
803 | struct dp_netdev_flow *flow; | |
804 | int error; | |
805 | ||
ec6fde61 | 806 | flow = xzalloc(sizeof *flow); |
72865317 BP |
807 | flow->key = odp_flow->key; |
808 | flow->key.reserved = 0; | |
809 | ||
810 | error = set_flow_actions(flow, odp_flow); | |
811 | if (error) { | |
812 | free(flow); | |
813 | return error; | |
814 | } | |
815 | ||
816 | hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0)); | |
817 | return 0; | |
818 | } | |
819 | ||
820 | static void | |
821 | clear_stats(struct dp_netdev_flow *flow) | |
822 | { | |
823 | flow->used.tv_sec = 0; | |
824 | flow->used.tv_usec = 0; | |
825 | flow->packet_count = 0; | |
826 | flow->byte_count = 0; | |
827 | flow->ip_tos = 0; | |
828 | flow->tcp_ctl = 0; | |
829 | } | |
830 | ||
831 | static int | |
832 | dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put) | |
833 | { | |
834 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
835 | struct dp_netdev_flow *flow; | |
836 | ||
837 | flow = dp_netdev_lookup_flow(dp, &put->flow.key); | |
838 | if (!flow) { | |
839 | if (put->flags & ODPPF_CREATE) { | |
840 | if (hmap_count(&dp->flow_table) < MAX_FLOWS) { | |
841 | return add_flow(dpif, &put->flow); | |
842 | } else { | |
3c71830a | 843 | return EFBIG; |
72865317 BP |
844 | } |
845 | } else { | |
846 | return ENOENT; | |
847 | } | |
848 | } else { | |
849 | if (put->flags & ODPPF_MODIFY) { | |
850 | int error = set_flow_actions(flow, &put->flow); | |
851 | if (!error && put->flags & ODPPF_ZERO_STATS) { | |
852 | clear_stats(flow); | |
853 | } | |
854 | return error; | |
855 | } else { | |
856 | return EEXIST; | |
857 | } | |
858 | } | |
859 | } | |
860 | ||
861 | ||
862 | static int | |
863 | dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow) | |
864 | { | |
865 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
866 | struct dp_netdev_flow *flow; | |
867 | ||
868 | flow = dp_netdev_lookup_flow(dp, &odp_flow->key); | |
869 | if (flow) { | |
fde05ade | 870 | answer_flow_query(flow, 0, odp_flow); |
72865317 BP |
871 | dp_netdev_free_flow(dp, flow); |
872 | return 0; | |
873 | } else { | |
874 | return ENOENT; | |
875 | } | |
876 | } | |
877 | ||
878 | static int | |
879 | dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n) | |
880 | { | |
881 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
882 | struct dp_netdev_flow *flow; | |
883 | int i; | |
884 | ||
885 | i = 0; | |
886 | HMAP_FOR_EACH (flow, struct dp_netdev_flow, node, &dp->flow_table) { | |
887 | if (i >= n) { | |
888 | break; | |
889 | } | |
fde05ade | 890 | answer_flow_query(flow, 0, &flows[i++]); |
72865317 BP |
891 | } |
892 | return hmap_count(&dp->flow_table); | |
893 | } | |
894 | ||
895 | static int | |
896 | dpif_netdev_execute(struct dpif *dpif, uint16_t in_port, | |
897 | const union odp_action actions[], int n_actions, | |
898 | const struct ofpbuf *packet) | |
899 | { | |
900 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
901 | struct ofpbuf copy; | |
902 | bool mutates; | |
903 | flow_t flow; | |
904 | int error; | |
905 | ||
3c71830a | 906 | if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) { |
72865317 BP |
907 | return EINVAL; |
908 | } | |
909 | ||
910 | error = dpif_netdev_validate_actions(actions, n_actions, &mutates); | |
911 | if (error) { | |
912 | return error; | |
913 | } | |
914 | ||
915 | if (mutates) { | |
916 | /* We need a deep copy of 'packet' since we're going to modify its | |
917 | * data. */ | |
918 | ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size); | |
919 | copy.data = (char*)copy.base + DP_NETDEV_HEADROOM; | |
920 | ofpbuf_put(©, packet->data, packet->size); | |
921 | } else { | |
922 | /* We still need a shallow copy of 'packet', even though we won't | |
923 | * modify its data, because flow_extract() modifies packet->l2, etc. | |
924 | * We could probably get away with modifying those but it's more polite | |
925 | * if we don't. */ | |
926 | copy = *packet; | |
927 | } | |
928 | flow_extract(©, in_port, &flow); | |
929 | error = dp_netdev_execute_actions(dp, ©, &flow, actions, n_actions); | |
930 | if (mutates) { | |
931 | ofpbuf_uninit(©); | |
932 | } | |
933 | return error; | |
934 | } | |
935 | ||
936 | static int | |
937 | dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask) | |
938 | { | |
939 | struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif); | |
940 | *listen_mask = dpif_netdev->listen_mask; | |
941 | return 0; | |
942 | } | |
943 | ||
944 | static int | |
945 | dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask) | |
946 | { | |
947 | struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif); | |
948 | if (!(listen_mask & ~ODPL_ALL)) { | |
949 | dpif_netdev->listen_mask = listen_mask; | |
950 | return 0; | |
951 | } else { | |
952 | return EINVAL; | |
953 | } | |
954 | } | |
955 | ||
956 | static struct ovs_queue * | |
957 | find_nonempty_queue(struct dpif *dpif) | |
958 | { | |
959 | struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif); | |
960 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
961 | int mask = dpif_netdev->listen_mask; | |
962 | int i; | |
963 | ||
964 | for (i = 0; i < N_QUEUES; i++) { | |
965 | struct ovs_queue *q = &dp->queues[i]; | |
966 | if (q->n && mask & (1u << i)) { | |
967 | return q; | |
968 | } | |
969 | } | |
970 | return NULL; | |
971 | } | |
972 | ||
973 | static int | |
974 | dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp) | |
975 | { | |
976 | struct ovs_queue *q = find_nonempty_queue(dpif); | |
977 | if (q) { | |
978 | *bufp = queue_pop_head(q); | |
979 | return 0; | |
980 | } else { | |
981 | return EAGAIN; | |
982 | } | |
983 | } | |
984 | ||
985 | static void | |
986 | dpif_netdev_recv_wait(struct dpif *dpif) | |
987 | { | |
988 | struct ovs_queue *q = find_nonempty_queue(dpif); | |
989 | if (q) { | |
990 | poll_immediate_wake(); | |
991 | } else { | |
992 | /* No messages ready to be received, and dp_wait() will ensure that we | |
993 | * wake up to queue new messages, so there is nothing to do. */ | |
994 | } | |
995 | } | |
996 | \f | |
997 | static void | |
998 | dp_netdev_flow_used(struct dp_netdev_flow *flow, const flow_t *key, | |
999 | const struct ofpbuf *packet) | |
1000 | { | |
1001 | time_timeval(&flow->used); | |
1002 | flow->packet_count++; | |
1003 | flow->byte_count += packet->size; | |
3c71830a | 1004 | if (key->dl_type == htons(ETH_TYPE_IP)) { |
72865317 BP |
1005 | struct ip_header *nh = packet->l3; |
1006 | flow->ip_tos = nh->ip_tos; | |
1007 | ||
1008 | if (key->nw_proto == IPPROTO_TCP) { | |
1009 | struct tcp_header *th = packet->l4; | |
1010 | flow->tcp_ctl |= th->tcp_ctl; | |
1011 | } | |
1012 | } | |
1013 | } | |
1014 | ||
1015 | static void | |
1016 | dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port, | |
1017 | struct ofpbuf *packet) | |
1018 | { | |
1019 | struct dp_netdev_flow *flow; | |
1020 | flow_t key; | |
1021 | ||
1022 | if (flow_extract(packet, port->port_no, &key) && dp->drop_frags) { | |
1023 | dp->n_frags++; | |
1024 | return; | |
1025 | } | |
1026 | ||
1027 | flow = dp_netdev_lookup_flow(dp, &key); | |
1028 | if (flow) { | |
1029 | dp_netdev_flow_used(flow, &key, packet); | |
1030 | dp_netdev_execute_actions(dp, packet, &key, | |
1031 | flow->actions, flow->n_actions); | |
1032 | dp->n_hit++; | |
1033 | } else { | |
1034 | dp->n_missed++; | |
1035 | dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0); | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | static void | |
1040 | dp_netdev_run(void) | |
1041 | { | |
1042 | struct ofpbuf packet; | |
1043 | struct dp_netdev *dp; | |
1044 | ||
1045 | ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu); | |
1046 | LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) { | |
1047 | struct dp_netdev_port *port; | |
1048 | ||
1049 | LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) { | |
1050 | int error; | |
1051 | ||
1052 | /* Reset packet contents. */ | |
1053 | packet.data = (char*)packet.base + DP_NETDEV_HEADROOM; | |
1054 | packet.size = 0; | |
1055 | ||
1056 | error = netdev_recv(port->netdev, &packet); | |
1057 | if (!error) { | |
1058 | dp_netdev_port_input(dp, port, &packet); | |
1059 | } else if (error != EAGAIN) { | |
1060 | struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
1061 | VLOG_ERR_RL(&rl, "error receiving data from %s: %s", | |
1062 | netdev_get_name(port->netdev), strerror(error)); | |
1063 | } | |
1064 | } | |
1065 | } | |
1066 | ofpbuf_uninit(&packet); | |
1067 | } | |
1068 | ||
1069 | static void | |
1070 | dp_netdev_wait(void) | |
1071 | { | |
1072 | struct dp_netdev *dp; | |
1073 | ||
1074 | LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) { | |
1075 | struct dp_netdev_port *port; | |
1076 | LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) { | |
1077 | netdev_recv_wait(port->netdev); | |
1078 | } | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | static void | |
1083 | dp_netdev_modify_vlan_tci(struct ofpbuf *packet, flow_t *key, | |
1084 | uint16_t tci, uint16_t mask) | |
1085 | { | |
1086 | struct vlan_eth_header *veh; | |
1087 | ||
1088 | if (key->dl_vlan != htons(ODP_VLAN_NONE)) { | |
1089 | /* Modify 'mask' bits, but maintain other TCI bits. */ | |
1090 | veh = packet->l2; | |
1091 | veh->veth_tci &= ~htons(mask); | |
1092 | veh->veth_tci |= htons(tci); | |
1093 | } else { | |
1094 | /* Insert new 802.1Q header. */ | |
1095 | struct eth_header *eh = packet->l2; | |
1096 | struct vlan_eth_header tmp; | |
1097 | memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN); | |
1098 | memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN); | |
1099 | tmp.veth_type = htons(ETH_TYPE_VLAN); | |
1100 | tmp.veth_tci = htons(tci); | |
1101 | tmp.veth_next_type = eh->eth_type; | |
1102 | ||
1103 | veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN); | |
1104 | memcpy(veh, &tmp, sizeof tmp); | |
1105 | packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN; | |
1106 | } | |
1107 | ||
1108 | key->dl_vlan = veh->veth_tci & htons(VLAN_VID_MASK); | |
1109 | } | |
1110 | ||
1111 | static void | |
1112 | dp_netdev_strip_vlan(struct ofpbuf *packet, flow_t *key) | |
1113 | { | |
1114 | struct vlan_eth_header *veh = packet->l2; | |
1115 | if (veh->veth_type == htons(ETH_TYPE_VLAN)) { | |
1116 | struct eth_header tmp; | |
1117 | ||
1118 | memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN); | |
1119 | memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN); | |
1120 | tmp.eth_type = veh->veth_next_type; | |
1121 | ||
1122 | packet->size -= VLAN_HEADER_LEN; | |
1123 | packet->data = (char*)packet->data + VLAN_HEADER_LEN; | |
1124 | packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN; | |
1125 | memcpy(packet->data, &tmp, sizeof tmp); | |
1126 | ||
1127 | key->dl_vlan = htons(ODP_VLAN_NONE); | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | static void | |
1132 | dp_netdev_set_dl_src(struct ofpbuf *packet, | |
1133 | const uint8_t dl_addr[ETH_ADDR_LEN]) | |
1134 | { | |
1135 | struct eth_header *eh = packet->l2; | |
1136 | memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src); | |
1137 | } | |
1138 | ||
1139 | static void | |
1140 | dp_netdev_set_dl_dst(struct ofpbuf *packet, | |
1141 | const uint8_t dl_addr[ETH_ADDR_LEN]) | |
1142 | { | |
1143 | struct eth_header *eh = packet->l2; | |
1144 | memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst); | |
1145 | } | |
1146 | ||
1147 | static void | |
1148 | dp_netdev_set_nw_addr(struct ofpbuf *packet, flow_t *key, | |
1149 | const struct odp_action_nw_addr *a) | |
1150 | { | |
1151 | if (key->dl_type == htons(ETH_TYPE_IP)) { | |
1152 | struct ip_header *nh = packet->l3; | |
1153 | uint32_t *field; | |
1154 | ||
1155 | field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst; | |
1156 | if (key->nw_proto == IP_TYPE_TCP) { | |
1157 | struct tcp_header *th = packet->l4; | |
1158 | th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr); | |
1159 | } else if (key->nw_proto == IP_TYPE_UDP) { | |
1160 | struct udp_header *uh = packet->l4; | |
1161 | if (uh->udp_csum) { | |
1162 | uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr); | |
1163 | if (!uh->udp_csum) { | |
1164 | uh->udp_csum = 0xffff; | |
1165 | } | |
1166 | } | |
1167 | } | |
1168 | nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr); | |
1169 | *field = a->nw_addr; | |
1170 | } | |
1171 | } | |
1172 | ||
1173 | static void | |
1174 | dp_netdev_set_tp_port(struct ofpbuf *packet, flow_t *key, | |
1175 | const struct odp_action_tp_port *a) | |
1176 | { | |
3c71830a | 1177 | if (key->dl_type == htons(ETH_TYPE_IP)) { |
72865317 BP |
1178 | uint16_t *field; |
1179 | if (key->nw_proto == IPPROTO_TCP) { | |
1180 | struct tcp_header *th = packet->l4; | |
1181 | field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst; | |
1182 | th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port); | |
1183 | *field = a->tp_port; | |
1184 | } else if (key->nw_proto == IPPROTO_UDP) { | |
1185 | struct udp_header *uh = packet->l4; | |
1186 | field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst; | |
1187 | uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port); | |
1188 | *field = a->tp_port; | |
1189 | } | |
1190 | } | |
1191 | } | |
1192 | ||
1193 | static void | |
1194 | dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet, | |
1195 | uint16_t out_port) | |
1196 | { | |
1197 | struct dp_netdev_port *p = dp->ports[out_port]; | |
1198 | if (p) { | |
1199 | netdev_send(p->netdev, packet); | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | static void | |
1204 | dp_netdev_output_group(struct dp_netdev *dp, uint16_t group, uint16_t in_port, | |
1205 | struct ofpbuf *packet) | |
1206 | { | |
1207 | struct odp_port_group *g = &dp->groups[group]; | |
1208 | int i; | |
1209 | ||
1210 | for (i = 0; i < g->n_ports; i++) { | |
1211 | uint16_t out_port = g->ports[i]; | |
1212 | if (out_port != in_port) { | |
1213 | dp_netdev_output_port(dp, packet, out_port); | |
1214 | } | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | static int | |
1219 | dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet, | |
1220 | int queue_no, int port_no, uint32_t arg) | |
1221 | { | |
1222 | struct ovs_queue *q = &dp->queues[queue_no]; | |
1223 | struct odp_msg *header; | |
1224 | struct ofpbuf *msg; | |
1225 | size_t msg_size; | |
1226 | ||
1227 | if (q->n >= MAX_QUEUE_LEN) { | |
1228 | dp->n_lost++; | |
1229 | return ENOBUFS; | |
1230 | } | |
1231 | ||
1232 | msg_size = sizeof *header + packet->size; | |
1233 | msg = ofpbuf_new(msg_size); | |
1234 | header = ofpbuf_put_uninit(msg, sizeof *header); | |
1235 | header->type = queue_no; | |
1236 | header->length = msg_size; | |
1237 | header->port = port_no; | |
1238 | header->arg = arg; | |
1239 | ofpbuf_put(msg, packet->data, packet->size); | |
1240 | queue_push_tail(q, msg); | |
1241 | ||
1242 | return 0; | |
1243 | } | |
1244 | ||
1245 | static int | |
1246 | dp_netdev_execute_actions(struct dp_netdev *dp, | |
1247 | struct ofpbuf *packet, flow_t *key, | |
1248 | const union odp_action *actions, int n_actions) | |
1249 | { | |
1250 | int i; | |
1251 | for (i = 0; i < n_actions; i++) { | |
1252 | const union odp_action *a = &actions[i]; | |
1253 | ||
1254 | switch (a->type) { | |
1255 | case ODPAT_OUTPUT: | |
1256 | dp_netdev_output_port(dp, packet, a->output.port); | |
1257 | break; | |
1258 | ||
1259 | case ODPAT_OUTPUT_GROUP: | |
1260 | dp_netdev_output_group(dp, a->output_group.group, key->in_port, | |
1261 | packet); | |
1262 | break; | |
1263 | ||
1264 | case ODPAT_CONTROLLER: | |
1265 | dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR, | |
1266 | key->in_port, a->controller.arg); | |
1267 | break; | |
1268 | ||
1269 | case ODPAT_SET_VLAN_VID: | |
1270 | dp_netdev_modify_vlan_tci(packet, key, ntohs(a->vlan_vid.vlan_vid), | |
1271 | VLAN_VID_MASK); | |
1272 | break; | |
1273 | ||
1274 | case ODPAT_SET_VLAN_PCP: | |
1275 | dp_netdev_modify_vlan_tci(packet, key, a->vlan_pcp.vlan_pcp << 13, | |
1276 | VLAN_PCP_MASK); | |
1277 | break; | |
1278 | ||
1279 | case ODPAT_STRIP_VLAN: | |
1280 | dp_netdev_strip_vlan(packet, key); | |
1281 | break; | |
1282 | ||
1283 | case ODPAT_SET_DL_SRC: | |
1284 | dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr); | |
1285 | break; | |
1286 | ||
1287 | case ODPAT_SET_DL_DST: | |
1288 | dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr); | |
1289 | break; | |
1290 | ||
1291 | case ODPAT_SET_NW_SRC: | |
1292 | case ODPAT_SET_NW_DST: | |
1293 | dp_netdev_set_nw_addr(packet, key, &a->nw_addr); | |
1294 | break; | |
1295 | ||
1296 | case ODPAT_SET_TP_SRC: | |
1297 | case ODPAT_SET_TP_DST: | |
1298 | dp_netdev_set_tp_port(packet, key, &a->tp_port); | |
1299 | break; | |
1300 | } | |
1301 | } | |
1302 | return 0; | |
1303 | } | |
1304 | ||
1305 | const struct dpif_class dpif_netdev_class = { | |
72865317 BP |
1306 | "netdev", |
1307 | dp_netdev_run, | |
1308 | dp_netdev_wait, | |
d3d22744 | 1309 | NULL, /* enumerate */ |
72865317 BP |
1310 | dpif_netdev_open, |
1311 | dpif_netdev_close, | |
d3d22744 | 1312 | NULL, /* get_all_names */ |
72865317 BP |
1313 | dpif_netdev_delete, |
1314 | dpif_netdev_get_stats, | |
1315 | dpif_netdev_get_drop_frags, | |
1316 | dpif_netdev_set_drop_frags, | |
1317 | dpif_netdev_port_add, | |
1318 | dpif_netdev_port_del, | |
1319 | dpif_netdev_port_query_by_number, | |
1320 | dpif_netdev_port_query_by_name, | |
1321 | dpif_netdev_port_list, | |
1322 | dpif_netdev_port_poll, | |
1323 | dpif_netdev_port_poll_wait, | |
1324 | dpif_netdev_port_group_get, | |
1325 | dpif_netdev_port_group_set, | |
1326 | dpif_netdev_flow_get, | |
1327 | dpif_netdev_flow_put, | |
1328 | dpif_netdev_flow_del, | |
1329 | dpif_netdev_flow_flush, | |
1330 | dpif_netdev_flow_list, | |
1331 | dpif_netdev_execute, | |
1332 | dpif_netdev_recv_get_mask, | |
1333 | dpif_netdev_recv_set_mask, | |
72b06300 BP |
1334 | NULL, /* get_sflow_probability */ |
1335 | NULL, /* set_sflow_probability */ | |
72865317 BP |
1336 | dpif_netdev_recv, |
1337 | dpif_netdev_recv_wait, | |
1338 | }; |