]> git.proxmox.com Git - ovs.git/blob - ofproto/tunnel.c
Avoid crash in OvS while transmitting fragmented packets over tunnel.
[ovs.git] / ofproto / tunnel.c
1 /* Copyright (c) 2013, 2014, 2015, 2017 Nicira, Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
14
15 #include <config.h>
16 #include "tunnel.h"
17
18 #include <errno.h>
19
20 #include "byte-order.h"
21 #include "connectivity.h"
22 #include "csum.h"
23 #include "dpif.h"
24 #include "openvswitch/dynamic-string.h"
25 #include "fat-rwlock.h"
26 #include "hash.h"
27 #include "openvswitch/hmap.h"
28 #include "netdev.h"
29 #include "odp-util.h"
30 #include "openvswitch/ofpbuf.h"
31 #include "packets.h"
32 #include "route-table.h"
33 #include "seq.h"
34 #include "smap.h"
35 #include "socket-util.h"
36 #include "tnl-ports.h"
37 #include "tunnel.h"
38 #include "openvswitch/vlog.h"
39 #include "unaligned.h"
40 #include "unixctl.h"
41 #include "ofproto-dpif.h"
42 #include "netdev-vport.h"
43
44 VLOG_DEFINE_THIS_MODULE(tunnel);
45
46 struct tnl_match {
47 ovs_be64 in_key;
48 struct in6_addr ipv6_src;
49 struct in6_addr ipv6_dst;
50 odp_port_t odp_port;
51 bool in_key_flow;
52 bool ip_src_flow;
53 bool ip_dst_flow;
54 enum netdev_pt_mode pt_mode;
55 };
56
57 struct tnl_port {
58 struct hmap_node ofport_node;
59 struct hmap_node match_node;
60
61 const struct ofport_dpif *ofport;
62 uint64_t change_seq;
63 struct netdev *netdev;
64
65 struct tnl_match match;
66 };
67
68 static struct fat_rwlock rwlock;
69
70 /* Tunnel matches.
71 *
72 * This module maps packets received over tunnel protocols to vports. The
73 * tunnel protocol and, for some protocols, tunnel-specific information (e.g.,
74 * for VXLAN, the UDP destination port number) are always use as part of the
75 * mapping. Which other fields are used for the mapping depends on the vports
76 * themselves (the parenthesized notations refer to "struct tnl_match" fields):
77 *
78 * - in_key: A vport may match a specific tunnel ID (in_key_flow == false)
79 * or arrange for the tunnel ID to be matched as tunnel.tun_id in the
80 * OpenFlow flow (in_key_flow == true).
81 *
82 * - ip_dst: A vport may match a specific destination IP address
83 * (ip_dst_flow == false) or arrange for the destination IP to be matched
84 * as tunnel.ip_dst in the OpenFlow flow (ip_dst_flow == true).
85 *
86 * - ip_src: A vport may match a specific IP source address (ip_src_flow ==
87 * false, ip_src != 0), wildcard all source addresses (ip_src_flow ==
88 * false, ip_src == 0), or arrange for the IP source address to be
89 * handled in the OpenFlow flow table (ip_src_flow == true).
90 *
91 * Thus, there are 2 * 2 * 3 == 12 possible ways a vport can match against a
92 * tunnel packet. We number the possibilities for each field in increasing
93 * order as listed in each bullet above. We order the 12 overall combinations
94 * in lexicographic order considering in_key first, then ip_dst, then
95 * ip_src. */
96 #define N_MATCH_TYPES (2 * 2 * 3)
97
98 /* The three possibilities (see above) for vport ip_src matches. */
99 enum ip_src_type {
100 IP_SRC_CFG, /* ip_src must equal configured address. */
101 IP_SRC_ANY, /* Any ip_src is acceptable. */
102 IP_SRC_FLOW /* ip_src is handled in flow table. */
103 };
104
105 /* Each hmap contains "struct tnl_port"s.
106 * The index is a combination of how each of the fields listed under "Tunnel
107 * matches" above matches, see the final paragraph for ordering. */
108 static struct hmap *tnl_match_maps[N_MATCH_TYPES] OVS_GUARDED_BY(rwlock);
109 static struct hmap **tnl_match_map(const struct tnl_match *);
110
111 static struct hmap ofport_map__ = HMAP_INITIALIZER(&ofport_map__);
112 static struct hmap *ofport_map OVS_GUARDED_BY(rwlock) = &ofport_map__;
113
114 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
115 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
116
117 static struct tnl_port *tnl_find(const struct flow *) OVS_REQ_RDLOCK(rwlock);
118 static struct tnl_port *tnl_find_exact(struct tnl_match *, struct hmap *)
119 OVS_REQ_RDLOCK(rwlock);
120 static struct tnl_port *tnl_find_ofport(const struct ofport_dpif *)
121 OVS_REQ_RDLOCK(rwlock);
122
123 static uint32_t tnl_hash(struct tnl_match *);
124 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
125 static char *tnl_port_to_string(const struct tnl_port *)
126 OVS_REQ_RDLOCK(rwlock);
127 static void tnl_port_format(const struct tnl_port *, struct ds *)
128 OVS_REQ_RDLOCK(rwlock);
129 static void tnl_port_mod_log(const struct tnl_port *, const char *action)
130 OVS_REQ_RDLOCK(rwlock);
131 static const char *tnl_port_get_name(const struct tnl_port *)
132 OVS_REQ_RDLOCK(rwlock);
133 static void tnl_port_del__(const struct ofport_dpif *, odp_port_t)
134 OVS_REQ_WRLOCK(rwlock);
135
136 static unixctl_cb_func tnl_unixctl_list;
137
138 void
139 ofproto_tunnel_init(void)
140 {
141 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
142
143 if (ovsthread_once_start(&once)) {
144 fat_rwlock_init(&rwlock);
145 unixctl_command_register("ofproto/list-tunnels", "", 0, 0,
146 tnl_unixctl_list, NULL);
147 ovsthread_once_done(&once);
148 }
149 }
150
151 static bool
152 tnl_port_add__(const struct ofport_dpif *ofport, const struct netdev *netdev,
153 odp_port_t odp_port, bool warn, bool native_tnl, const char name[])
154 OVS_REQ_WRLOCK(rwlock)
155 {
156 const struct netdev_tunnel_config *cfg;
157 struct tnl_port *existing_port;
158 struct tnl_port *tnl_port;
159 struct hmap **map;
160
161 cfg = netdev_get_tunnel_config(netdev);
162 ovs_assert(cfg);
163
164 tnl_port = xzalloc(sizeof *tnl_port);
165 tnl_port->ofport = ofport;
166 tnl_port->netdev = netdev_ref(netdev);
167 tnl_port->change_seq = netdev_get_change_seq(tnl_port->netdev);
168
169 tnl_port->match.in_key = cfg->in_key;
170 tnl_port->match.ipv6_src = cfg->ipv6_src;
171 tnl_port->match.ipv6_dst = cfg->ipv6_dst;
172 tnl_port->match.ip_src_flow = cfg->ip_src_flow;
173 tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
174 tnl_port->match.in_key_flow = cfg->in_key_flow;
175 tnl_port->match.odp_port = odp_port;
176 tnl_port->match.pt_mode = netdev_get_pt_mode(netdev);
177
178 map = tnl_match_map(&tnl_port->match);
179 existing_port = tnl_find_exact(&tnl_port->match, *map);
180 if (existing_port) {
181 if (warn) {
182 struct ds ds = DS_EMPTY_INITIALIZER;
183 tnl_match_fmt(&tnl_port->match, &ds);
184 VLOG_WARN("%s: attempting to add tunnel port with same config as "
185 "port '%s' (%s)", tnl_port_get_name(tnl_port),
186 tnl_port_get_name(existing_port), ds_cstr(&ds));
187 ds_destroy(&ds);
188 }
189 netdev_close(tnl_port->netdev);
190 free(tnl_port);
191 return false;
192 }
193
194 hmap_insert(ofport_map, &tnl_port->ofport_node, hash_pointer(ofport, 0));
195
196 if (!*map) {
197 *map = xmalloc(sizeof **map);
198 hmap_init(*map);
199 }
200 hmap_insert(*map, &tnl_port->match_node, tnl_hash(&tnl_port->match));
201 tnl_port_mod_log(tnl_port, "adding");
202
203 if (native_tnl) {
204 const char *type;
205
206 type = netdev_get_type(netdev);
207 tnl_port_map_insert(odp_port, cfg->dst_port, name, type);
208
209 }
210 return true;
211 }
212
213 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
214 * must be added before they can be used by the module. 'ofport' must be a
215 * tunnel.
216 *
217 * Returns 0 if successful, otherwise a positive errno value. */
218 int
219 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
220 odp_port_t odp_port, bool native_tnl, const char name[])
221 OVS_EXCLUDED(rwlock)
222 {
223 bool ok;
224
225 fat_rwlock_wrlock(&rwlock);
226 ok = tnl_port_add__(ofport, netdev, odp_port, true, native_tnl, name);
227 fat_rwlock_unlock(&rwlock);
228
229 return ok ? 0 : EEXIST;
230 }
231
232 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
233 * in its netdev_tunnel_config. If it does, returns true. Otherwise, returns
234 * false. 'new_odp_port' should be the port number coming from 'ofport' that
235 * is passed to tnl_port_add__(). 'old_odp_port' should be the port number
236 * that is passed to tnl_port_del__(). */
237 bool
238 tnl_port_reconfigure(const struct ofport_dpif *ofport,
239 const struct netdev *netdev, odp_port_t new_odp_port,
240 odp_port_t old_odp_port, bool native_tnl,
241 const char name[])
242 OVS_EXCLUDED(rwlock)
243 {
244 struct tnl_port *tnl_port;
245 bool changed = false;
246
247 fat_rwlock_wrlock(&rwlock);
248 tnl_port = tnl_find_ofport(ofport);
249 if (!tnl_port) {
250 changed = tnl_port_add__(ofport, netdev, new_odp_port, false,
251 native_tnl, name);
252 } else if (tnl_port->netdev != netdev
253 || tnl_port->match.odp_port != new_odp_port
254 || tnl_port->change_seq != netdev_get_change_seq(tnl_port->netdev)) {
255 VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
256 tnl_port_del__(ofport, old_odp_port);
257 tnl_port_add__(ofport, netdev, new_odp_port, true, native_tnl, name);
258 changed = true;
259 }
260 fat_rwlock_unlock(&rwlock);
261 return changed;
262 }
263
264 static void
265 tnl_port_del__(const struct ofport_dpif *ofport, odp_port_t odp_port)
266 OVS_REQ_WRLOCK(rwlock)
267 {
268 struct tnl_port *tnl_port;
269
270 if (!ofport) {
271 return;
272 }
273
274 tnl_port = tnl_find_ofport(ofport);
275 if (tnl_port) {
276 struct hmap **map;
277
278 tnl_port_map_delete(odp_port, netdev_get_type(tnl_port->netdev));
279 tnl_port_mod_log(tnl_port, "removing");
280 map = tnl_match_map(&tnl_port->match);
281 hmap_remove(*map, &tnl_port->match_node);
282 if (hmap_is_empty(*map)) {
283 hmap_destroy(*map);
284 free(*map);
285 *map = NULL;
286 }
287 hmap_remove(ofport_map, &tnl_port->ofport_node);
288 netdev_close(tnl_port->netdev);
289 free(tnl_port);
290 }
291 }
292
293 /* Removes 'ofport' from the module. */
294 void
295 tnl_port_del(const struct ofport_dpif *ofport, odp_port_t odp_port)
296 OVS_EXCLUDED(rwlock)
297 {
298 fat_rwlock_wrlock(&rwlock);
299 tnl_port_del__(ofport, odp_port);
300 fat_rwlock_unlock(&rwlock);
301 }
302
303 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
304 * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
305 * none is found.
306 *
307 * Callers should verify that 'flow' needs to be received by calling
308 * tnl_port_should_receive() before this function. */
309 const struct ofport_dpif *
310 tnl_port_receive(const struct flow *flow) OVS_EXCLUDED(rwlock)
311 {
312 const struct ofport_dpif *ofport;
313 struct tnl_port *tnl_port;
314
315 fat_rwlock_rdlock(&rwlock);
316 tnl_port = tnl_find(flow);
317 ofport = tnl_port ? tnl_port->ofport : NULL;
318 if (!tnl_port) {
319 if (!VLOG_DROP_WARN(&rl)) {
320 char *flow_str = flow_to_string(flow, NULL);
321 VLOG_WARN("receive tunnel port not found (%s)", flow_str);
322 free(flow_str);
323 }
324 goto out;
325 }
326
327 if (!VLOG_DROP_DBG(&dbg_rl)) {
328 char *flow_str = flow_to_string(flow, NULL);
329 char *tnl_str = tnl_port_to_string(tnl_port);
330 VLOG_DBG("tunnel port %s receive from flow %s", tnl_str, flow_str);
331 free(tnl_str);
332 free(flow_str);
333 }
334
335 out:
336 fat_rwlock_unlock(&rwlock);
337 return ofport;
338 }
339
340 /* Should be called at the beginning of action translation to initialize
341 * wildcards and perform any actions based on receiving on tunnel port.
342 *
343 * Returns false if the packet must be dropped. */
344 bool
345 tnl_process_ecn(struct flow *flow)
346 {
347 if (!tnl_port_should_receive(flow)) {
348 return true;
349 }
350
351 if (is_ip_any(flow) && IP_ECN_is_ce(flow->tunnel.ip_tos)) {
352 if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
353 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
354 " but is not ECN capable");
355 return false;
356 }
357
358 /* Set the ECN CE value in the tunneled packet. */
359 flow->nw_tos |= IP_ECN_CE;
360 }
361
362 return true;
363 }
364
365 void
366 tnl_wc_init(struct flow *flow, struct flow_wildcards *wc)
367 {
368 if (tnl_port_should_receive(flow)) {
369 wc->masks.tunnel.tun_id = OVS_BE64_MAX;
370 if (flow->tunnel.ip_dst) {
371 wc->masks.tunnel.ip_src = OVS_BE32_MAX;
372 wc->masks.tunnel.ip_dst = OVS_BE32_MAX;
373 } else {
374 wc->masks.tunnel.ipv6_src = in6addr_exact;
375 wc->masks.tunnel.ipv6_dst = in6addr_exact;
376 }
377 wc->masks.tunnel.flags = (FLOW_TNL_F_DONT_FRAGMENT |
378 FLOW_TNL_F_CSUM |
379 FLOW_TNL_F_KEY);
380 wc->masks.tunnel.ip_tos = UINT8_MAX;
381 wc->masks.tunnel.ip_ttl = 0;
382 /* The tp_src and tp_dst members in flow_tnl are set to be always
383 * wildcarded, not to unwildcard them here. */
384 wc->masks.tunnel.tp_src = 0;
385 wc->masks.tunnel.tp_dst = 0;
386
387 if (is_ip_any(flow)
388 && IP_ECN_is_ce(flow->tunnel.ip_tos)) {
389 wc->masks.nw_tos |= IP_ECN_MASK;
390 }
391 }
392 }
393
394 /* Given that 'flow' should be output to the ofport corresponding to
395 * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
396 * port that the output should happen on. May return ODPP_NONE if the output
397 * shouldn't occur. */
398 odp_port_t
399 tnl_port_send(const struct ofport_dpif *ofport, struct flow *flow,
400 struct flow_wildcards *wc) OVS_EXCLUDED(rwlock)
401 {
402 const struct netdev_tunnel_config *cfg;
403 struct tnl_port *tnl_port;
404 char *pre_flow_str = NULL;
405 odp_port_t out_port;
406
407 fat_rwlock_rdlock(&rwlock);
408 tnl_port = tnl_find_ofport(ofport);
409 out_port = tnl_port ? tnl_port->match.odp_port : ODPP_NONE;
410 if (!tnl_port) {
411 goto out;
412 }
413
414 cfg = netdev_get_tunnel_config(tnl_port->netdev);
415 ovs_assert(cfg);
416
417 if (!VLOG_DROP_DBG(&dbg_rl)) {
418 pre_flow_str = flow_to_string(flow, NULL);
419 }
420
421 if (!cfg->ip_src_flow) {
422 flow->tunnel.ip_src = in6_addr_get_mapped_ipv4(&tnl_port->match.ipv6_src);
423 if (!flow->tunnel.ip_src) {
424 flow->tunnel.ipv6_src = tnl_port->match.ipv6_src;
425 } else {
426 flow->tunnel.ipv6_src = in6addr_any;
427 }
428 }
429 if (!cfg->ip_dst_flow) {
430 flow->tunnel.ip_dst = in6_addr_get_mapped_ipv4(&tnl_port->match.ipv6_dst);
431 if (!flow->tunnel.ip_dst) {
432 flow->tunnel.ipv6_dst = tnl_port->match.ipv6_dst;
433 } else {
434 flow->tunnel.ipv6_dst = in6addr_any;
435 }
436 }
437 flow->tunnel.tp_dst = cfg->dst_port;
438 if (!cfg->out_key_flow) {
439 flow->tunnel.tun_id = cfg->out_key;
440 }
441
442 if (cfg->ttl_inherit && is_ip_any(flow)) {
443 wc->masks.nw_ttl = 0xff;
444 flow->tunnel.ip_ttl = flow->nw_ttl;
445 } else {
446 flow->tunnel.ip_ttl = cfg->ttl;
447 }
448
449 if (cfg->tos_inherit && is_ip_any(flow)) {
450 wc->masks.nw_tos |= IP_DSCP_MASK;
451 flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
452 } else {
453 flow->tunnel.ip_tos = cfg->tos;
454 }
455
456 /* ECN fields are always inherited. */
457 if (is_ip_any(flow)) {
458 wc->masks.nw_tos |= IP_ECN_MASK;
459
460 if (IP_ECN_is_ce(flow->nw_tos)) {
461 flow->tunnel.ip_tos |= IP_ECN_ECT_0;
462 } else {
463 flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
464 }
465 }
466
467 flow->tunnel.flags &= ~(FLOW_TNL_F_MASK & ~FLOW_TNL_PUB_F_MASK);
468 flow->tunnel.flags |= (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
469 | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
470 | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
471
472 if (cfg->set_egress_pkt_mark) {
473 flow->pkt_mark = cfg->egress_pkt_mark;
474 wc->masks.pkt_mark = UINT32_MAX;
475 }
476
477 if (pre_flow_str) {
478 char *post_flow_str = flow_to_string(flow, NULL);
479 char *tnl_str = tnl_port_to_string(tnl_port);
480 VLOG_DBG("flow sent\n"
481 "%s"
482 " pre: %s\n"
483 "post: %s",
484 tnl_str, pre_flow_str, post_flow_str);
485 free(tnl_str);
486 free(pre_flow_str);
487 free(post_flow_str);
488 }
489
490 out:
491 fat_rwlock_unlock(&rwlock);
492 return out_port;
493 }
494
495 static uint32_t
496 tnl_hash(struct tnl_match *match)
497 {
498 BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
499 return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
500 }
501
502 static struct tnl_port *
503 tnl_find_ofport(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
504 {
505 struct tnl_port *tnl_port;
506
507 HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
508 ofport_map) {
509 if (tnl_port->ofport == ofport) {
510 return tnl_port;
511 }
512 }
513 return NULL;
514 }
515
516 static struct tnl_port *
517 tnl_find_exact(struct tnl_match *match, struct hmap *map)
518 OVS_REQ_RDLOCK(rwlock)
519 {
520 if (map) {
521 struct tnl_port *tnl_port;
522
523 HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match), map) {
524 if (!memcmp(match, &tnl_port->match, sizeof *match)) {
525 return tnl_port;
526 }
527 }
528 }
529 return NULL;
530 }
531
532 /* Returns the tnl_port that is the best match for the tunnel data in 'flow',
533 * or NULL if no tnl_port matches 'flow'. */
534 static struct tnl_port *
535 tnl_find(const struct flow *flow) OVS_REQ_RDLOCK(rwlock)
536 {
537 enum ip_src_type ip_src;
538 int in_key_flow;
539 int ip_dst_flow;
540 int i;
541
542 i = 0;
543 for (in_key_flow = 0; in_key_flow < 2; in_key_flow++) {
544 for (ip_dst_flow = 0; ip_dst_flow < 2; ip_dst_flow++) {
545 for (ip_src = 0; ip_src < 3; ip_src++) {
546 struct hmap *map = tnl_match_maps[i];
547
548 if (map) {
549 struct tnl_port *tnl_port;
550 struct tnl_match match;
551
552 memset(&match, 0, sizeof match);
553
554 /* The apparent mix-up of 'ip_dst' and 'ip_src' below is
555 * correct, because "struct tnl_match" is expressed in
556 * terms of packets being sent out, but we are using it
557 * here as a description of how to treat received
558 * packets. */
559 match.in_key = in_key_flow ? 0 : flow->tunnel.tun_id;
560 if (ip_src == IP_SRC_CFG) {
561 match.ipv6_src = flow_tnl_dst(&flow->tunnel);
562 }
563 if (!ip_dst_flow) {
564 match.ipv6_dst = flow_tnl_src(&flow->tunnel);
565 }
566 match.odp_port = flow->in_port.odp_port;
567 match.in_key_flow = in_key_flow;
568 match.ip_dst_flow = ip_dst_flow;
569 match.ip_src_flow = ip_src == IP_SRC_FLOW;
570
571 /* Look for a legacy L2 or L3 tunnel port first. */
572 if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE) {
573 match.pt_mode = NETDEV_PT_LEGACY_L3;
574 } else {
575 match.pt_mode = NETDEV_PT_LEGACY_L2;
576 }
577 tnl_port = tnl_find_exact(&match, map);
578 if (tnl_port) {
579 return tnl_port;
580 }
581
582 /* Then check for a packet type aware port. */
583 match.pt_mode = NETDEV_PT_AWARE;
584 tnl_port = tnl_find_exact(&match, map);
585 if (tnl_port) {
586 return tnl_port;
587 }
588 }
589
590 i++;
591 }
592 }
593 }
594
595 return NULL;
596 }
597
598 /* Returns a pointer to the 'tnl_match_maps' element corresponding to 'm''s
599 * matching criteria. */
600 static struct hmap **
601 tnl_match_map(const struct tnl_match *m)
602 {
603 enum ip_src_type ip_src;
604
605 ip_src = (m->ip_src_flow ? IP_SRC_FLOW
606 : ipv6_addr_is_set(&m->ipv6_src) ? IP_SRC_CFG
607 : IP_SRC_ANY);
608
609 return &tnl_match_maps[6 * m->in_key_flow + 3 * m->ip_dst_flow + ip_src];
610 }
611
612 static void
613 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
614 OVS_REQ_RDLOCK(rwlock)
615 {
616 if (!match->ip_dst_flow) {
617 ipv6_format_mapped(&match->ipv6_src, ds);
618 ds_put_cstr(ds, "->");
619 ipv6_format_mapped(&match->ipv6_dst, ds);
620 } else if (!match->ip_src_flow) {
621 ipv6_format_mapped(&match->ipv6_src, ds);
622 ds_put_cstr(ds, "->flow");
623 } else {
624 ds_put_cstr(ds, "flow->flow");
625 }
626
627 if (match->in_key_flow) {
628 ds_put_cstr(ds, ", key=flow");
629 } else {
630 ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
631 }
632
633 const char *pt_mode
634 = (match->pt_mode == NETDEV_PT_LEGACY_L2 ? "legacy_l2"
635 : match->pt_mode == NETDEV_PT_LEGACY_L3 ? "legacy_l3"
636 : "ptap");
637 ds_put_format(ds, ", %s, dp port=%"PRIu32, pt_mode, match->odp_port);
638 }
639
640 static void
641 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
642 OVS_REQ_RDLOCK(rwlock)
643 {
644 if (VLOG_IS_DBG_ENABLED()) {
645 struct ds ds = DS_EMPTY_INITIALIZER;
646
647 tnl_match_fmt(&tnl_port->match, &ds);
648 VLOG_INFO("%s tunnel port %s (%s)", action,
649 tnl_port_get_name(tnl_port), ds_cstr(&ds));
650 ds_destroy(&ds);
651 }
652 }
653
654 static void OVS_REQ_RDLOCK(rwlock)
655 tnl_port_format(const struct tnl_port *tnl_port, struct ds *ds)
656 {
657 const struct netdev_tunnel_config *cfg =
658 netdev_get_tunnel_config(tnl_port->netdev);
659
660 ds_put_format(ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
661 tnl_port_get_name(tnl_port),
662 netdev_get_type(tnl_port->netdev));
663 tnl_match_fmt(&tnl_port->match, ds);
664
665 if (cfg->out_key != cfg->in_key ||
666 cfg->out_key_present != cfg->in_key_present ||
667 cfg->out_key_flow != cfg->in_key_flow) {
668 ds_put_cstr(ds, ", out_key=");
669 if (!cfg->out_key_present) {
670 ds_put_cstr(ds, "none");
671 } else if (cfg->out_key_flow) {
672 ds_put_cstr(ds, "flow");
673 } else {
674 ds_put_format(ds, "%#"PRIx64, ntohll(cfg->out_key));
675 }
676 }
677
678 if (cfg->ttl_inherit) {
679 ds_put_cstr(ds, ", ttl=inherit");
680 } else {
681 ds_put_format(ds, ", ttl=%"PRIu8, cfg->ttl);
682 }
683
684 if (cfg->tos_inherit) {
685 ds_put_cstr(ds, ", tos=inherit");
686 } else if (cfg->tos) {
687 ds_put_format(ds, ", tos=%#"PRIx8, cfg->tos);
688 }
689
690 if (!cfg->dont_fragment) {
691 ds_put_cstr(ds, ", df=false");
692 }
693
694 if (cfg->csum) {
695 ds_put_cstr(ds, ", csum=true");
696 }
697
698 ds_put_cstr(ds, ")\n");
699 }
700
701 static char * OVS_REQ_RDLOCK(rwlock)
702 tnl_port_to_string(const struct tnl_port *tnl_port)
703 {
704 struct ds ds = DS_EMPTY_INITIALIZER;
705 tnl_port_format(tnl_port, &ds);
706 return ds_steal_cstr(&ds);
707 }
708
709 static const char *
710 tnl_port_get_name(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
711 {
712 return netdev_get_name(tnl_port->netdev);
713 }
714
715 int
716 tnl_port_build_header(const struct ofport_dpif *ofport,
717 struct ovs_action_push_tnl *data,
718 const struct netdev_tnl_build_header_params *params)
719 {
720 struct tnl_port *tnl_port;
721 int res;
722
723 fat_rwlock_rdlock(&rwlock);
724 tnl_port = tnl_find_ofport(ofport);
725 ovs_assert(tnl_port);
726 res = netdev_build_header(tnl_port->netdev, data, params);
727 fat_rwlock_unlock(&rwlock);
728
729 return res;
730 }
731
732 static void
733 tnl_unixctl_list(struct unixctl_conn *conn,
734 int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
735 void *aux OVS_UNUSED)
736 {
737 struct ds reply = DS_EMPTY_INITIALIZER;
738
739 fat_rwlock_rdlock(&rwlock);
740 for (int i = 0; i < N_MATCH_TYPES; i++) {
741 struct hmap *map = tnl_match_maps[i];
742 if (map) {
743 struct tnl_port *tnl_port;
744 HMAP_FOR_EACH (tnl_port, match_node, map) {
745 tnl_port_format(tnl_port, &reply);
746 }
747 }
748 }
749 fat_rwlock_unlock(&rwlock);
750
751 unixctl_command_reply(conn, ds_cstr(&reply));
752 ds_destroy(&reply);
753 }