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