]>
git.proxmox.com Git - ovs.git/blob - ofproto/tunnel.c
1 /* Copyright (c) 2013, 2014 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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. */
20 #include "byte-order.h"
21 #include "connectivity.h"
22 #include "dynamic-string.h"
30 #include "socket-util.h"
34 VLOG_DEFINE_THIS_MODULE(tunnel
);
36 /* skb mark used for IPsec tunnel packets */
51 struct hmap_node ofport_node
;
52 struct hmap_node match_node
;
54 const struct ofport_dpif
*ofport
;
55 unsigned int change_seq
;
56 struct netdev
*netdev
;
58 struct tnl_match match
;
61 static struct ovs_rwlock rwlock
= OVS_RWLOCK_INITIALIZER
;
65 * This module maps packets received over tunnel protocols to vports. The
66 * tunnel protocol and, for some protocols, tunnel-specific information (e.g.,
67 * for VXLAN, the UDP destination port number) are always use as part of the
68 * mapping. Which other fields are used for the mapping depends on the vports
69 * themselves (the parenthesized notations refer to "struct tnl_match" fields):
71 * - in_key: A vport may match a specific tunnel ID (in_key_flow == false)
72 * or arrange for the tunnel ID to be matched as tunnel.tun_id in the
73 * OpenFlow flow (in_key_flow == true).
75 * - ip_dst: A vport may match a specific destination IP address
76 * (ip_dst_flow == false) or arrange for the destination IP to be matched
77 * as tunnel.ip_dst in the OpenFlow flow (ip_dst_flow == true).
79 * - ip_src: A vport may match a specific IP source address (ip_src_flow ==
80 * false, ip_src != 0), wildcard all source addresses (ip_src_flow ==
81 * false, ip_src == 0), or arrange for the IP source address to be
82 * handled in the OpenFlow flow table (ip_src_flow == true).
84 * Thus, there are 2 * 2 * 3 == 12 possible ways a vport can match against a
85 * tunnel packet. We number the possibilities for each field in increasing
86 * order as listed in each bullet above. We order the 12 overall combinations
87 * in lexicographic order considering in_key first, then ip_dst, then
89 #define N_MATCH_TYPES (2 * 2 * 3)
91 /* The three possibilities (see above) for vport ip_src matches. */
93 IP_SRC_CFG
, /* ip_src must equal configured address. */
94 IP_SRC_ANY
, /* Any ip_src is acceptable. */
95 IP_SRC_FLOW
/* ip_src is handled in flow table. */
98 /* Each hmap contains "struct tnl_port"s.
99 * The index is a combination of how each of the fields listed under "Tunnel
100 * matches" above matches, see the final paragraph for ordering. */
101 static struct hmap
*tnl_match_maps
[N_MATCH_TYPES
] OVS_GUARDED_BY(rwlock
);
102 static struct hmap
**tnl_match_map(const struct tnl_match
*);
104 static struct hmap ofport_map__
= HMAP_INITIALIZER(&ofport_map__
);
105 static struct hmap
*ofport_map
OVS_GUARDED_BY(rwlock
) = &ofport_map__
;
107 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
108 static struct vlog_rate_limit dbg_rl
= VLOG_RATE_LIMIT_INIT(60, 60);
110 static struct tnl_port
*tnl_find(const struct flow
*) OVS_REQ_RDLOCK(rwlock
);
111 static struct tnl_port
*tnl_find_exact(struct tnl_match
*, struct hmap
*)
112 OVS_REQ_RDLOCK(rwlock
);
113 static struct tnl_port
*tnl_find_ofport(const struct ofport_dpif
*)
114 OVS_REQ_RDLOCK(rwlock
);
116 static uint32_t tnl_hash(struct tnl_match
*);
117 static void tnl_match_fmt(const struct tnl_match
*, struct ds
*);
118 static char *tnl_port_fmt(const struct tnl_port
*) OVS_REQ_RDLOCK(rwlock
);
119 static void tnl_port_mod_log(const struct tnl_port
*, const char *action
)
120 OVS_REQ_RDLOCK(rwlock
);
121 static const char *tnl_port_get_name(const struct tnl_port
*)
122 OVS_REQ_RDLOCK(rwlock
);
123 static void tnl_port_del__(const struct ofport_dpif
*) OVS_REQ_WRLOCK(rwlock
);
126 tnl_port_add__(const struct ofport_dpif
*ofport
, const struct netdev
*netdev
,
127 odp_port_t odp_port
, bool warn
)
128 OVS_REQ_WRLOCK(rwlock
)
130 const struct netdev_tunnel_config
*cfg
;
131 struct tnl_port
*existing_port
;
132 struct tnl_port
*tnl_port
;
135 cfg
= netdev_get_tunnel_config(netdev
);
138 tnl_port
= xzalloc(sizeof *tnl_port
);
139 tnl_port
->ofport
= ofport
;
140 tnl_port
->netdev
= netdev_ref(netdev
);
141 tnl_port
->change_seq
= seq_read(connectivity_seq_get());
143 tnl_port
->match
.in_key
= cfg
->in_key
;
144 tnl_port
->match
.ip_src
= cfg
->ip_src
;
145 tnl_port
->match
.ip_dst
= cfg
->ip_dst
;
146 tnl_port
->match
.ip_src_flow
= cfg
->ip_src_flow
;
147 tnl_port
->match
.ip_dst_flow
= cfg
->ip_dst_flow
;
148 tnl_port
->match
.pkt_mark
= cfg
->ipsec
? IPSEC_MARK
: 0;
149 tnl_port
->match
.in_key_flow
= cfg
->in_key_flow
;
150 tnl_port
->match
.odp_port
= odp_port
;
152 map
= tnl_match_map(&tnl_port
->match
);
153 existing_port
= tnl_find_exact(&tnl_port
->match
, *map
);
156 struct ds ds
= DS_EMPTY_INITIALIZER
;
157 tnl_match_fmt(&tnl_port
->match
, &ds
);
158 VLOG_WARN("%s: attempting to add tunnel port with same config as "
159 "port '%s' (%s)", tnl_port_get_name(tnl_port
),
160 tnl_port_get_name(existing_port
), ds_cstr(&ds
));
163 netdev_close(tnl_port
->netdev
);
168 hmap_insert(ofport_map
, &tnl_port
->ofport_node
, hash_pointer(ofport
, 0));
171 *map
= xmalloc(sizeof **map
);
174 hmap_insert(*map
, &tnl_port
->match_node
, tnl_hash(&tnl_port
->match
));
175 tnl_port_mod_log(tnl_port
, "adding");
179 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
180 * must be added before they can be used by the module. 'ofport' must be a
183 tnl_port_add(const struct ofport_dpif
*ofport
, const struct netdev
*netdev
,
184 odp_port_t odp_port
) OVS_EXCLUDED(rwlock
)
186 ovs_rwlock_wrlock(&rwlock
);
187 tnl_port_add__(ofport
, netdev
, odp_port
, true);
188 ovs_rwlock_unlock(&rwlock
);
191 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
192 * in its netdev_tunnel_config. If it does, returns true. Otherwise, returns
193 * false. 'ofport' and 'odp_port' should be the same as would be passed to
196 tnl_port_reconfigure(const struct ofport_dpif
*ofport
,
197 const struct netdev
*netdev
, odp_port_t odp_port
)
200 struct tnl_port
*tnl_port
;
201 bool changed
= false;
203 ovs_rwlock_wrlock(&rwlock
);
204 tnl_port
= tnl_find_ofport(ofport
);
206 changed
= tnl_port_add__(ofport
, netdev
, odp_port
, false);
207 } else if (tnl_port
->netdev
!= netdev
208 || tnl_port
->match
.odp_port
!= odp_port
209 || tnl_port
->change_seq
!= seq_read(connectivity_seq_get())) {
210 VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port
));
211 tnl_port_del__(ofport
);
212 tnl_port_add__(ofport
, netdev
, odp_port
, true);
215 ovs_rwlock_unlock(&rwlock
);
220 tnl_port_del__(const struct ofport_dpif
*ofport
) OVS_REQ_WRLOCK(rwlock
)
222 struct tnl_port
*tnl_port
;
228 tnl_port
= tnl_find_ofport(ofport
);
232 tnl_port_mod_log(tnl_port
, "removing");
233 map
= tnl_match_map(&tnl_port
->match
);
234 hmap_remove(*map
, &tnl_port
->match_node
);
235 if (hmap_is_empty(*map
)) {
240 hmap_remove(ofport_map
, &tnl_port
->ofport_node
);
241 netdev_close(tnl_port
->netdev
);
246 /* Removes 'ofport' from the module. */
248 tnl_port_del(const struct ofport_dpif
*ofport
) OVS_EXCLUDED(rwlock
)
250 ovs_rwlock_wrlock(&rwlock
);
251 tnl_port_del__(ofport
);
252 ovs_rwlock_unlock(&rwlock
);
255 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
256 * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
259 * Callers should verify that 'flow' needs to be received by calling
260 * tnl_port_should_receive() before this function. */
261 const struct ofport_dpif
*
262 tnl_port_receive(const struct flow
*flow
) OVS_EXCLUDED(rwlock
)
264 char *pre_flow_str
= NULL
;
265 const struct ofport_dpif
*ofport
;
266 struct tnl_port
*tnl_port
;
268 ovs_rwlock_rdlock(&rwlock
);
269 tnl_port
= tnl_find(flow
);
270 ofport
= tnl_port
? tnl_port
->ofport
: NULL
;
272 char *flow_str
= flow_to_string(flow
);
274 VLOG_WARN_RL(&rl
, "receive tunnel port not found (%s)", flow_str
);
279 if (!VLOG_DROP_DBG(&dbg_rl
)) {
280 pre_flow_str
= flow_to_string(flow
);
284 char *post_flow_str
= flow_to_string(flow
);
285 char *tnl_str
= tnl_port_fmt(tnl_port
);
286 VLOG_DBG("flow received\n"
290 tnl_str
, pre_flow_str
, post_flow_str
);
297 ovs_rwlock_unlock(&rwlock
);
302 tnl_ecn_ok(const struct flow
*base_flow
, struct flow
*flow
,
303 struct flow_wildcards
*wc
)
305 if (is_ip_any(base_flow
)) {
306 if ((flow
->tunnel
.ip_tos
& IP_ECN_MASK
) == IP_ECN_CE
) {
307 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
308 if ((base_flow
->nw_tos
& IP_ECN_MASK
) == IP_ECN_NOT_ECT
) {
309 VLOG_WARN_RL(&rl
, "dropping tunnel packet marked ECN CE"
310 " but is not ECN capable");
313 /* Set the ECN CE value in the tunneled packet. */
314 flow
->nw_tos
|= IP_ECN_CE
;
322 /* Should be called at the beginning of action translation to initialize
323 * wildcards and perform any actions based on receiving on tunnel port.
325 * Returns false if the packet must be dropped. */
327 tnl_xlate_init(const struct flow
*base_flow
, struct flow
*flow
,
328 struct flow_wildcards
*wc
)
330 /* tnl_port_should_receive() examines the 'tunnel.ip_dst' field to
331 * determine the presence of the tunnel metadata. However, since tunnels'
332 * datapath port numbers are different from the non-tunnel ports, and we
333 * always unwildcard the 'in_port', we do not need to unwildcard
334 * the 'tunnel.ip_dst' for non-tunneled packets. */
335 if (tnl_port_should_receive(flow
)) {
336 wc
->masks
.tunnel
.tun_id
= OVS_BE64_MAX
;
337 wc
->masks
.tunnel
.ip_src
= OVS_BE32_MAX
;
338 wc
->masks
.tunnel
.ip_dst
= OVS_BE32_MAX
;
339 wc
->masks
.tunnel
.flags
= (FLOW_TNL_F_DONT_FRAGMENT
|
342 wc
->masks
.tunnel
.ip_tos
= UINT8_MAX
;
343 wc
->masks
.tunnel
.ip_ttl
= UINT8_MAX
;
344 /* The tp_src and tp_dst members in flow_tnl are set to be always
345 * wildcarded, not to unwildcard them here. */
346 wc
->masks
.tunnel
.tp_src
= 0;
347 wc
->masks
.tunnel
.tp_dst
= 0;
349 memset(&wc
->masks
.pkt_mark
, 0xff, sizeof wc
->masks
.pkt_mark
);
351 if (!tnl_ecn_ok(base_flow
, flow
, wc
)) {
355 flow
->pkt_mark
&= ~IPSEC_MARK
;
361 /* Given that 'flow' should be output to the ofport corresponding to
362 * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
363 * port that the output should happen on. May return ODPP_NONE if the output
364 * shouldn't occur. */
366 tnl_port_send(const struct ofport_dpif
*ofport
, struct flow
*flow
,
367 struct flow_wildcards
*wc
) OVS_EXCLUDED(rwlock
)
369 const struct netdev_tunnel_config
*cfg
;
370 struct tnl_port
*tnl_port
;
371 char *pre_flow_str
= NULL
;
374 ovs_rwlock_rdlock(&rwlock
);
375 tnl_port
= tnl_find_ofport(ofport
);
376 out_port
= tnl_port
? tnl_port
->match
.odp_port
: ODPP_NONE
;
381 cfg
= netdev_get_tunnel_config(tnl_port
->netdev
);
384 if (!VLOG_DROP_DBG(&dbg_rl
)) {
385 pre_flow_str
= flow_to_string(flow
);
388 if (!cfg
->ip_src_flow
) {
389 flow
->tunnel
.ip_src
= tnl_port
->match
.ip_src
;
391 if (!cfg
->ip_dst_flow
) {
392 flow
->tunnel
.ip_dst
= tnl_port
->match
.ip_dst
;
394 flow
->pkt_mark
= tnl_port
->match
.pkt_mark
;
396 if (!cfg
->out_key_flow
) {
397 flow
->tunnel
.tun_id
= cfg
->out_key
;
400 if (cfg
->ttl_inherit
&& is_ip_any(flow
)) {
401 wc
->masks
.nw_ttl
= 0xff;
402 flow
->tunnel
.ip_ttl
= flow
->nw_ttl
;
404 flow
->tunnel
.ip_ttl
= cfg
->ttl
;
407 if (cfg
->tos_inherit
&& is_ip_any(flow
)) {
408 wc
->masks
.nw_tos
= IP_DSCP_MASK
;
409 flow
->tunnel
.ip_tos
= flow
->nw_tos
& IP_DSCP_MASK
;
411 flow
->tunnel
.ip_tos
= cfg
->tos
;
414 /* ECN fields are always inherited. */
415 if (is_ip_any(flow
)) {
416 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
418 if ((flow
->nw_tos
& IP_ECN_MASK
) == IP_ECN_CE
) {
419 flow
->tunnel
.ip_tos
|= IP_ECN_ECT_0
;
421 flow
->tunnel
.ip_tos
|= flow
->nw_tos
& IP_ECN_MASK
;
425 flow
->tunnel
.flags
= (cfg
->dont_fragment
? FLOW_TNL_F_DONT_FRAGMENT
: 0)
426 | (cfg
->csum
? FLOW_TNL_F_CSUM
: 0)
427 | (cfg
->out_key_present
? FLOW_TNL_F_KEY
: 0);
430 char *post_flow_str
= flow_to_string(flow
);
431 char *tnl_str
= tnl_port_fmt(tnl_port
);
432 VLOG_DBG("flow sent\n"
436 tnl_str
, pre_flow_str
, post_flow_str
);
443 ovs_rwlock_unlock(&rwlock
);
448 tnl_hash(struct tnl_match
*match
)
450 BUILD_ASSERT_DECL(sizeof *match
% sizeof(uint32_t) == 0);
451 return hash_words((uint32_t *) match
, sizeof *match
/ sizeof(uint32_t), 0);
454 static struct tnl_port
*
455 tnl_find_ofport(const struct ofport_dpif
*ofport
) OVS_REQ_RDLOCK(rwlock
)
457 struct tnl_port
*tnl_port
;
459 HMAP_FOR_EACH_IN_BUCKET (tnl_port
, ofport_node
, hash_pointer(ofport
, 0),
461 if (tnl_port
->ofport
== ofport
) {
468 static struct tnl_port
*
469 tnl_find_exact(struct tnl_match
*match
, struct hmap
*map
)
470 OVS_REQ_RDLOCK(rwlock
)
473 struct tnl_port
*tnl_port
;
475 HMAP_FOR_EACH_WITH_HASH (tnl_port
, match_node
, tnl_hash(match
), map
) {
476 if (!memcmp(match
, &tnl_port
->match
, sizeof *match
)) {
484 /* Returns the tnl_port that is the best match for the tunnel data in 'flow',
485 * or NULL if no tnl_port matches 'flow'. */
486 static struct tnl_port
*
487 tnl_find(const struct flow
*flow
) OVS_REQ_RDLOCK(rwlock
)
489 enum ip_src_type ip_src
;
495 for (in_key_flow
= 0; in_key_flow
< 2; in_key_flow
++) {
496 for (ip_dst_flow
= 0; ip_dst_flow
< 2; ip_dst_flow
++) {
497 for (ip_src
= 0; ip_src
< 3; ip_src
++) {
498 struct hmap
*map
= tnl_match_maps
[i
];
501 struct tnl_port
*tnl_port
;
502 struct tnl_match match
;
504 memset(&match
, 0, sizeof match
);
506 /* The apparent mix-up of 'ip_dst' and 'ip_src' below is
507 * correct, because "struct tnl_match" is expressed in
508 * terms of packets being sent out, but we are using it
509 * here as a description of how to treat received
511 match
.in_key
= in_key_flow
? 0 : flow
->tunnel
.tun_id
;
512 match
.ip_src
= (ip_src
== IP_SRC_CFG
513 ? flow
->tunnel
.ip_dst
515 match
.ip_dst
= ip_dst_flow
? 0 : flow
->tunnel
.ip_src
;
516 match
.odp_port
= flow
->in_port
.odp_port
;
517 match
.pkt_mark
= flow
->pkt_mark
;
518 match
.in_key_flow
= in_key_flow
;
519 match
.ip_dst_flow
= ip_dst_flow
;
520 match
.ip_src_flow
= ip_src
== IP_SRC_FLOW
;
522 tnl_port
= tnl_find_exact(&match
, map
);
536 /* Returns a pointer to the 'tnl_match_maps' element corresponding to 'm''s
537 * matching criteria. */
538 static struct hmap
**
539 tnl_match_map(const struct tnl_match
*m
)
541 enum ip_src_type ip_src
;
543 ip_src
= (m
->ip_src_flow
? IP_SRC_FLOW
544 : m
->ip_src
? IP_SRC_CFG
547 return &tnl_match_maps
[6 * m
->in_key_flow
+ 3 * m
->ip_dst_flow
+ ip_src
];
551 tnl_match_fmt(const struct tnl_match
*match
, struct ds
*ds
)
552 OVS_REQ_RDLOCK(rwlock
)
554 if (!match
->ip_dst_flow
) {
555 ds_put_format(ds
, IP_FMT
"->"IP_FMT
, IP_ARGS(match
->ip_src
),
556 IP_ARGS(match
->ip_dst
));
557 } else if (!match
->ip_src_flow
) {
558 ds_put_format(ds
, IP_FMT
"->flow", IP_ARGS(match
->ip_src
));
560 ds_put_cstr(ds
, "flow->flow");
563 if (match
->in_key_flow
) {
564 ds_put_cstr(ds
, ", key=flow");
566 ds_put_format(ds
, ", key=%#"PRIx64
, ntohll(match
->in_key
));
569 ds_put_format(ds
, ", dp port=%"PRIu32
, match
->odp_port
);
570 ds_put_format(ds
, ", pkt mark=%"PRIu32
, match
->pkt_mark
);
574 tnl_port_mod_log(const struct tnl_port
*tnl_port
, const char *action
)
575 OVS_REQ_RDLOCK(rwlock
)
577 if (VLOG_IS_DBG_ENABLED()) {
578 struct ds ds
= DS_EMPTY_INITIALIZER
;
580 tnl_match_fmt(&tnl_port
->match
, &ds
);
581 VLOG_INFO("%s tunnel port %s (%s)", action
,
582 tnl_port_get_name(tnl_port
), ds_cstr(&ds
));
588 tnl_port_fmt(const struct tnl_port
*tnl_port
) OVS_REQ_RDLOCK(rwlock
)
590 const struct netdev_tunnel_config
*cfg
=
591 netdev_get_tunnel_config(tnl_port
->netdev
);
592 struct ds ds
= DS_EMPTY_INITIALIZER
;
594 ds_put_format(&ds
, "port %"PRIu32
": %s (%s: ", tnl_port
->match
.odp_port
,
595 tnl_port_get_name(tnl_port
),
596 netdev_get_type(tnl_port
->netdev
));
597 tnl_match_fmt(&tnl_port
->match
, &ds
);
599 if (cfg
->out_key
!= cfg
->in_key
||
600 cfg
->out_key_present
!= cfg
->in_key_present
||
601 cfg
->out_key_flow
!= cfg
->in_key_flow
) {
602 ds_put_cstr(&ds
, ", out_key=");
603 if (!cfg
->out_key_present
) {
604 ds_put_cstr(&ds
, "none");
605 } else if (cfg
->out_key_flow
) {
606 ds_put_cstr(&ds
, "flow");
608 ds_put_format(&ds
, "%#"PRIx64
, ntohll(cfg
->out_key
));
612 if (cfg
->ttl_inherit
) {
613 ds_put_cstr(&ds
, ", ttl=inherit");
615 ds_put_format(&ds
, ", ttl=%"PRIu8
, cfg
->ttl
);
618 if (cfg
->tos_inherit
) {
619 ds_put_cstr(&ds
, ", tos=inherit");
620 } else if (cfg
->tos
) {
621 ds_put_format(&ds
, ", tos=%#"PRIx8
, cfg
->tos
);
624 if (!cfg
->dont_fragment
) {
625 ds_put_cstr(&ds
, ", df=false");
629 ds_put_cstr(&ds
, ", csum=true");
632 ds_put_cstr(&ds
, ")\n");
634 return ds_steal_cstr(&ds
);
638 tnl_port_get_name(const struct tnl_port
*tnl_port
) OVS_REQ_RDLOCK(rwlock
)
640 return netdev_get_name(tnl_port
->netdev
);