]> git.proxmox.com Git - ovs.git/blob - ofproto/tunnel.c
tnl-neigh-cache: Purge learnt neighbors when port/bridge is deleted
[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 (!cfg->erspan_ver_flow) {
478 flow->tunnel.erspan_ver = cfg->erspan_ver;
479 }
480
481 if (!cfg->erspan_idx_flow) {
482 flow->tunnel.erspan_idx = cfg->erspan_idx;
483 }
484
485 if (!cfg->erspan_dir_flow) {
486 flow->tunnel.erspan_dir = cfg->erspan_dir;
487 }
488
489 if (!cfg->erspan_hwid_flow) {
490 flow->tunnel.erspan_hwid = cfg->erspan_hwid;
491 }
492
493 if (pre_flow_str) {
494 char *post_flow_str = flow_to_string(flow, NULL);
495 char *tnl_str = tnl_port_to_string(tnl_port);
496 VLOG_DBG("flow sent\n"
497 "%s"
498 " pre: %s\n"
499 "post: %s",
500 tnl_str, pre_flow_str, post_flow_str);
501 free(tnl_str);
502 free(pre_flow_str);
503 free(post_flow_str);
504 }
505
506 out:
507 fat_rwlock_unlock(&rwlock);
508 return out_port;
509 }
510
511 static uint32_t
512 tnl_hash(struct tnl_match *match)
513 {
514 BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
515 return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
516 }
517
518 static struct tnl_port *
519 tnl_find_ofport(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
520 {
521 struct tnl_port *tnl_port;
522
523 HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
524 ofport_map) {
525 if (tnl_port->ofport == ofport) {
526 return tnl_port;
527 }
528 }
529 return NULL;
530 }
531
532 static struct tnl_port *
533 tnl_find_exact(struct tnl_match *match, struct hmap *map)
534 OVS_REQ_RDLOCK(rwlock)
535 {
536 if (map) {
537 struct tnl_port *tnl_port;
538
539 HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match), map) {
540 if (!memcmp(match, &tnl_port->match, sizeof *match)) {
541 return tnl_port;
542 }
543 }
544 }
545 return NULL;
546 }
547
548 /* Returns the tnl_port that is the best match for the tunnel data in 'flow',
549 * or NULL if no tnl_port matches 'flow'. */
550 static struct tnl_port *
551 tnl_find(const struct flow *flow) OVS_REQ_RDLOCK(rwlock)
552 {
553 enum ip_src_type ip_src;
554 int in_key_flow;
555 int ip_dst_flow;
556 int i;
557
558 i = 0;
559 for (in_key_flow = 0; in_key_flow < 2; in_key_flow++) {
560 for (ip_dst_flow = 0; ip_dst_flow < 2; ip_dst_flow++) {
561 for (ip_src = 0; ip_src < 3; ip_src++) {
562 struct hmap *map = tnl_match_maps[i];
563
564 if (map) {
565 struct tnl_port *tnl_port;
566 struct tnl_match match;
567
568 memset(&match, 0, sizeof match);
569
570 /* The apparent mix-up of 'ip_dst' and 'ip_src' below is
571 * correct, because "struct tnl_match" is expressed in
572 * terms of packets being sent out, but we are using it
573 * here as a description of how to treat received
574 * packets. */
575 match.in_key = in_key_flow ? 0 : flow->tunnel.tun_id;
576 if (ip_src == IP_SRC_CFG) {
577 match.ipv6_src = flow_tnl_dst(&flow->tunnel);
578 }
579 if (!ip_dst_flow) {
580 match.ipv6_dst = flow_tnl_src(&flow->tunnel);
581 }
582 match.odp_port = flow->in_port.odp_port;
583 match.in_key_flow = in_key_flow;
584 match.ip_dst_flow = ip_dst_flow;
585 match.ip_src_flow = ip_src == IP_SRC_FLOW;
586
587 /* Look for a legacy L2 or L3 tunnel port first. */
588 if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE) {
589 match.pt_mode = NETDEV_PT_LEGACY_L3;
590 } else {
591 match.pt_mode = NETDEV_PT_LEGACY_L2;
592 }
593 tnl_port = tnl_find_exact(&match, map);
594 if (tnl_port) {
595 return tnl_port;
596 }
597
598 /* Then check for a packet type aware port. */
599 match.pt_mode = NETDEV_PT_AWARE;
600 tnl_port = tnl_find_exact(&match, map);
601 if (tnl_port) {
602 return tnl_port;
603 }
604 }
605
606 i++;
607 }
608 }
609 }
610
611 return NULL;
612 }
613
614 /* Returns a pointer to the 'tnl_match_maps' element corresponding to 'm''s
615 * matching criteria. */
616 static struct hmap **
617 tnl_match_map(const struct tnl_match *m)
618 {
619 enum ip_src_type ip_src;
620
621 ip_src = (m->ip_src_flow ? IP_SRC_FLOW
622 : ipv6_addr_is_set(&m->ipv6_src) ? IP_SRC_CFG
623 : IP_SRC_ANY);
624
625 return &tnl_match_maps[6 * m->in_key_flow + 3 * m->ip_dst_flow + ip_src];
626 }
627
628 static void
629 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
630 OVS_REQ_RDLOCK(rwlock)
631 {
632 if (!match->ip_dst_flow) {
633 ipv6_format_mapped(&match->ipv6_src, ds);
634 ds_put_cstr(ds, "->");
635 ipv6_format_mapped(&match->ipv6_dst, ds);
636 } else if (!match->ip_src_flow) {
637 ipv6_format_mapped(&match->ipv6_src, ds);
638 ds_put_cstr(ds, "->flow");
639 } else {
640 ds_put_cstr(ds, "flow->flow");
641 }
642
643 if (match->in_key_flow) {
644 ds_put_cstr(ds, ", key=flow");
645 } else {
646 ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
647 }
648
649 const char *pt_mode
650 = (match->pt_mode == NETDEV_PT_LEGACY_L2 ? "legacy_l2"
651 : match->pt_mode == NETDEV_PT_LEGACY_L3 ? "legacy_l3"
652 : "ptap");
653 ds_put_format(ds, ", %s, dp port=%"PRIu32, pt_mode, match->odp_port);
654 }
655
656 static void
657 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
658 OVS_REQ_RDLOCK(rwlock)
659 {
660 if (VLOG_IS_DBG_ENABLED()) {
661 struct ds ds = DS_EMPTY_INITIALIZER;
662
663 tnl_match_fmt(&tnl_port->match, &ds);
664 VLOG_INFO("%s tunnel port %s (%s)", action,
665 tnl_port_get_name(tnl_port), ds_cstr(&ds));
666 ds_destroy(&ds);
667 }
668 }
669
670 static void OVS_REQ_RDLOCK(rwlock)
671 tnl_port_format(const struct tnl_port *tnl_port, struct ds *ds)
672 {
673 const struct netdev_tunnel_config *cfg =
674 netdev_get_tunnel_config(tnl_port->netdev);
675
676 ds_put_format(ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
677 tnl_port_get_name(tnl_port),
678 netdev_get_type(tnl_port->netdev));
679 tnl_match_fmt(&tnl_port->match, ds);
680
681 if (cfg->out_key != cfg->in_key ||
682 cfg->out_key_present != cfg->in_key_present ||
683 cfg->out_key_flow != cfg->in_key_flow) {
684 ds_put_cstr(ds, ", out_key=");
685 if (!cfg->out_key_present) {
686 ds_put_cstr(ds, "none");
687 } else if (cfg->out_key_flow) {
688 ds_put_cstr(ds, "flow");
689 } else {
690 ds_put_format(ds, "%#"PRIx64, ntohll(cfg->out_key));
691 }
692 }
693
694 if (cfg->ttl_inherit) {
695 ds_put_cstr(ds, ", ttl=inherit");
696 } else {
697 ds_put_format(ds, ", ttl=%"PRIu8, cfg->ttl);
698 }
699
700 if (cfg->tos_inherit) {
701 ds_put_cstr(ds, ", tos=inherit");
702 } else if (cfg->tos) {
703 ds_put_format(ds, ", tos=%#"PRIx8, cfg->tos);
704 }
705
706 if (!cfg->dont_fragment) {
707 ds_put_cstr(ds, ", df=false");
708 }
709
710 if (cfg->csum) {
711 ds_put_cstr(ds, ", csum=true");
712 }
713
714 ds_put_cstr(ds, ")\n");
715 }
716
717 static char * OVS_REQ_RDLOCK(rwlock)
718 tnl_port_to_string(const struct tnl_port *tnl_port)
719 {
720 struct ds ds = DS_EMPTY_INITIALIZER;
721 tnl_port_format(tnl_port, &ds);
722 return ds_steal_cstr(&ds);
723 }
724
725 static const char *
726 tnl_port_get_name(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
727 {
728 return netdev_get_name(tnl_port->netdev);
729 }
730
731 const char *
732 tnl_port_get_type(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
733 {
734 struct tnl_port *tnl_port;
735
736 tnl_port = tnl_find_ofport(ofport);
737 return !tnl_port ? NULL :
738 netdev_get_type(tnl_port->netdev);
739 }
740
741 int
742 tnl_port_build_header(const struct ofport_dpif *ofport,
743 struct ovs_action_push_tnl *data,
744 const struct netdev_tnl_build_header_params *params)
745 {
746 struct tnl_port *tnl_port;
747 int res;
748
749 fat_rwlock_rdlock(&rwlock);
750 tnl_port = tnl_find_ofport(ofport);
751 ovs_assert(tnl_port);
752 res = netdev_build_header(tnl_port->netdev, data, params);
753 fat_rwlock_unlock(&rwlock);
754
755 return res;
756 }
757
758 static void
759 tnl_unixctl_list(struct unixctl_conn *conn,
760 int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
761 void *aux OVS_UNUSED)
762 {
763 struct ds reply = DS_EMPTY_INITIALIZER;
764
765 fat_rwlock_rdlock(&rwlock);
766 for (int i = 0; i < N_MATCH_TYPES; i++) {
767 struct hmap *map = tnl_match_maps[i];
768 if (map) {
769 struct tnl_port *tnl_port;
770 HMAP_FOR_EACH (tnl_port, match_node, map) {
771 tnl_port_format(tnl_port, &reply);
772 }
773 }
774 }
775 fat_rwlock_unlock(&rwlock);
776
777 unixctl_command_reply(conn, ds_cstr(&reply));
778 ds_destroy(&reply);
779 }