2 * Copyright (c) 2014, 2015, 2017 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "tnl-ports.h"
25 #include "classifier.h"
26 #include "openvswitch/dynamic-string.h"
28 #include "openvswitch/list.h"
30 #include "openvswitch/ofpbuf.h"
31 #include "ovs-thread.h"
33 #include "ovs-thread.h"
37 static struct ovs_mutex mutex
= OVS_MUTEX_INITIALIZER
;
38 static struct classifier cls
; /* Tunnel ports. */
43 struct in6_addr
*addr
;
47 char dev_name
[IFNAMSIZ
];
50 static struct ovs_list addr_list
;
54 struct ovs_refcount ref_cnt
;
57 char dev_name
[IFNAMSIZ
];
61 static struct ovs_list port_list
;
66 struct ovs_refcount ref_cnt
;
67 char dev_name
[IFNAMSIZ
];
70 static struct tnl_port_in
*
71 tnl_port_cast(const struct cls_rule
*cr
)
73 BUILD_ASSERT_DECL(offsetof(struct tnl_port_in
, cr
) == 0);
75 return CONTAINER_OF(cr
, struct tnl_port_in
, cr
);
79 tnl_port_free(struct tnl_port_in
*p
)
81 cls_rule_destroy(&p
->cr
);
86 tnl_port_init_flow(struct flow
*flow
, struct eth_addr mac
,
87 struct in6_addr
*addr
, uint8_t nw_proto
, ovs_be16 tp_port
)
89 memset(flow
, 0, sizeof *flow
);
92 if (IN6_IS_ADDR_V4MAPPED(addr
)) {
93 flow
->dl_type
= htons(ETH_TYPE_IP
);
94 flow
->nw_dst
= in6_addr_get_mapped_ipv4(addr
);
96 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
97 flow
->ipv6_dst
= *addr
;
100 flow
->nw_proto
= nw_proto
;
101 flow
->tp_dst
= tp_port
;
105 map_insert(odp_port_t port
, struct eth_addr mac
, struct in6_addr
*addr
,
106 uint8_t nw_proto
, ovs_be16 tp_port
, const char dev_name
[])
108 const struct cls_rule
*cr
;
109 struct tnl_port_in
*p
;
112 memset(&match
, 0, sizeof match
);
113 tnl_port_init_flow(&match
.flow
, mac
, addr
, nw_proto
, tp_port
);
116 cr
= classifier_lookup(&cls
, OVS_VERSION_MAX
, &match
.flow
, NULL
);
117 p
= tnl_port_cast(cr
);
118 /* Try again if the rule was released before we get the reference. */
119 } while (p
&& !ovs_refcount_try_ref_rcu(&p
->ref_cnt
));
122 p
= xzalloc(sizeof *p
);
125 match
.wc
.masks
.dl_type
= OVS_BE16_MAX
;
126 match
.wc
.masks
.nw_proto
= 0xff;
127 /* XXX: No fragments support. */
128 match
.wc
.masks
.nw_frag
= FLOW_NW_FRAG_MASK
;
130 /* 'tp_port' is zero for GRE tunnels. In this case it
131 * doesn't make sense to match on UDP port numbers. */
133 match
.wc
.masks
.tp_dst
= OVS_BE16_MAX
;
135 if (IN6_IS_ADDR_V4MAPPED(addr
)) {
136 match
.wc
.masks
.nw_dst
= OVS_BE32_MAX
;
138 match
.wc
.masks
.ipv6_dst
= in6addr_exact
;
140 match
.wc
.masks
.vlans
[0].tci
= OVS_BE16_MAX
;
141 memset(&match
.wc
.masks
.dl_dst
, 0xff, sizeof (struct eth_addr
));
143 cls_rule_init(&p
->cr
, &match
, 0); /* Priority == 0. */
144 ovs_refcount_init(&p
->ref_cnt
);
145 ovs_strlcpy(p
->dev_name
, dev_name
, sizeof p
->dev_name
);
147 classifier_insert(&cls
, &p
->cr
, OVS_VERSION_MIN
, NULL
, 0);
152 map_insert_ipdev__(struct ip_device
*ip_dev
, char dev_name
[],
153 odp_port_t port
, uint8_t nw_proto
, ovs_be16 tp_port
)
155 if (ip_dev
->n_addr
) {
158 for (i
= 0; i
< ip_dev
->n_addr
; i
++) {
159 map_insert(port
, ip_dev
->mac
, &ip_dev
->addr
[i
],
160 nw_proto
, tp_port
, dev_name
);
166 tnl_type_to_nw_proto(const char type
[])
168 if (!strcmp(type
, "geneve")) {
171 if (!strcmp(type
, "stt")) {
174 if (!strcmp(type
, "gre")) {
177 if (!strcmp(type
, "vxlan")) {
184 tnl_port_map_insert(odp_port_t port
, ovs_be16 tp_port
,
185 const char dev_name
[], const char type
[])
188 struct ip_device
*ip_dev
;
191 nw_proto
= tnl_type_to_nw_proto(type
);
196 ovs_mutex_lock(&mutex
);
197 LIST_FOR_EACH(p
, node
, &port_list
) {
198 if (p
->port
== port
&& p
->nw_proto
== nw_proto
) {
199 ovs_refcount_ref(&p
->ref_cnt
);
204 p
= xzalloc(sizeof *p
);
206 p
->tp_port
= tp_port
;
207 p
->nw_proto
= nw_proto
;
208 ovs_strlcpy(p
->dev_name
, dev_name
, sizeof p
->dev_name
);
209 ovs_refcount_init(&p
->ref_cnt
);
210 ovs_list_insert(&port_list
, &p
->node
);
212 LIST_FOR_EACH(ip_dev
, node
, &addr_list
) {
213 map_insert_ipdev__(ip_dev
, p
->dev_name
, p
->port
, p
->nw_proto
, p
->tp_port
);
217 ovs_mutex_unlock(&mutex
);
221 tnl_port_unref(const struct cls_rule
*cr
)
223 struct tnl_port_in
*p
= tnl_port_cast(cr
);
225 if (cr
&& ovs_refcount_unref_relaxed(&p
->ref_cnt
) == 1) {
226 if (classifier_remove(&cls
, cr
)) {
227 ovsrcu_postpone(tnl_port_free
, p
);
233 map_delete(struct eth_addr mac
, struct in6_addr
*addr
,
234 ovs_be16 tp_port
, uint8_t nw_proto
)
236 const struct cls_rule
*cr
;
239 tnl_port_init_flow(&flow
, mac
, addr
, nw_proto
, tp_port
);
241 cr
= classifier_lookup(&cls
, OVS_VERSION_MAX
, &flow
, NULL
);
246 ipdev_map_delete(struct ip_device
*ip_dev
, ovs_be16 tp_port
, uint8_t nw_proto
)
248 if (ip_dev
->n_addr
) {
251 for (i
= 0; i
< ip_dev
->n_addr
; i
++) {
252 map_delete(ip_dev
->mac
, &ip_dev
->addr
[i
], tp_port
, nw_proto
);
258 tnl_port_map_delete(odp_port_t port
, const char type
[])
260 struct tnl_port
*p
, *next
;
261 struct ip_device
*ip_dev
;
264 nw_proto
= tnl_type_to_nw_proto(type
);
266 ovs_mutex_lock(&mutex
);
267 LIST_FOR_EACH_SAFE(p
, next
, node
, &port_list
) {
268 if (p
->port
== port
&& p
->nw_proto
== nw_proto
&&
269 ovs_refcount_unref_relaxed(&p
->ref_cnt
) == 1) {
270 ovs_list_remove(&p
->node
);
271 LIST_FOR_EACH(ip_dev
, node
, &addr_list
) {
272 ipdev_map_delete(ip_dev
, p
->tp_port
, p
->nw_proto
);
278 ovs_mutex_unlock(&mutex
);
281 /* 'flow' is non-const to allow for temporary modifications during the lookup.
282 * Any changes are restored before returning. */
284 tnl_port_map_lookup(struct flow
*flow
, struct flow_wildcards
*wc
)
286 const struct cls_rule
*cr
= classifier_lookup(&cls
, OVS_VERSION_MAX
, flow
,
289 return (cr
) ? tnl_port_cast(cr
)->portno
: ODPP_NONE
;
293 tnl_port_show_v(struct ds
*ds
)
295 const struct tnl_port_in
*p
;
297 CLS_FOR_EACH(p
, cr
, &cls
) {
298 struct odputil_keybuf keybuf
;
299 struct odputil_keybuf maskbuf
;
301 const struct nlattr
*key
, *mask
;
302 size_t key_len
, mask_len
;
303 struct flow_wildcards wc
;
305 struct odp_flow_key_parms odp_parms
= {
310 ds_put_format(ds
, "%s (%"PRIu32
") : ", p
->dev_name
, p
->portno
);
311 minimask_expand(p
->cr
.match
.mask
, &wc
);
312 miniflow_expand(p
->cr
.match
.flow
, &flow
);
315 odp_parms
.support
.recirc
= true;
316 ofpbuf_use_stack(&buf
, &keybuf
, sizeof keybuf
);
317 odp_flow_key_from_flow(&odp_parms
, &buf
);
322 odp_parms
.support
.recirc
= false;
323 ofpbuf_use_stack(&buf
, &maskbuf
, sizeof maskbuf
);
324 odp_flow_key_from_mask(&odp_parms
, &buf
);
329 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, ds
, false);
330 ds_put_format(ds
, "\n");
335 tnl_port_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
336 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
338 struct ds ds
= DS_EMPTY_INITIALIZER
;
341 ds_put_format(&ds
, "Listening ports:\n");
342 ovs_mutex_lock(&mutex
);
344 if (!strcasecmp(argv
[1], "-v")) {
345 tnl_port_show_v(&ds
);
350 LIST_FOR_EACH(p
, node
, &port_list
) {
351 ds_put_format(&ds
, "%s (%"PRIu32
") ref_cnt=%u\n", p
->dev_name
, p
->port
,
352 ovs_refcount_read(&p
->ref_cnt
));
356 ovs_mutex_unlock(&mutex
);
357 unixctl_command_reply(conn
, ds_cstr(&ds
));
362 map_insert_ipdev(struct ip_device
*ip_dev
)
366 LIST_FOR_EACH(p
, node
, &port_list
) {
367 map_insert_ipdev__(ip_dev
, p
->dev_name
, p
->port
, p
->nw_proto
, p
->tp_port
);
372 insert_ipdev__(struct netdev
*dev
,
373 struct in6_addr
*addr
, int n_addr
)
375 struct ip_device
*ip_dev
;
376 enum netdev_flags flags
;
379 error
= netdev_get_flags(dev
, &flags
);
380 if (error
|| (flags
& NETDEV_LOOPBACK
)) {
384 ip_dev
= xzalloc(sizeof *ip_dev
);
385 ip_dev
->dev
= netdev_ref(dev
);
386 ip_dev
->change_seq
= netdev_get_change_seq(dev
);
387 error
= netdev_get_etheraddr(ip_dev
->dev
, &ip_dev
->mac
);
392 ip_dev
->n_addr
= n_addr
;
393 ovs_strlcpy(ip_dev
->dev_name
, netdev_get_name(dev
), sizeof ip_dev
->dev_name
);
394 ovs_list_insert(&addr_list
, &ip_dev
->node
);
395 map_insert_ipdev(ip_dev
);
399 netdev_close(ip_dev
->dev
);
406 insert_ipdev(const char dev_name
[])
408 struct in6_addr
*addr
, *mask
;
412 error
= netdev_open(dev_name
, netdev_get_type_from_name(dev_name
), &dev
);
417 error
= netdev_get_addr_list(dev
, &addr
, &mask
, &n_in6
);
423 insert_ipdev__(dev
, addr
, n_in6
);
428 delete_ipdev(struct ip_device
*ip_dev
)
432 LIST_FOR_EACH(p
, node
, &port_list
) {
433 ipdev_map_delete(ip_dev
, p
->tp_port
, p
->nw_proto
);
436 ovs_list_remove(&ip_dev
->node
);
437 netdev_close(ip_dev
->dev
);
443 tnl_port_map_insert_ipdev(const char dev_name
[])
445 struct ip_device
*ip_dev
, *next
;
447 ovs_mutex_lock(&mutex
);
449 LIST_FOR_EACH_SAFE(ip_dev
, next
, node
, &addr_list
) {
450 if (!strcmp(netdev_get_name(ip_dev
->dev
), dev_name
)) {
451 if (ip_dev
->change_seq
== netdev_get_change_seq(ip_dev
->dev
)) {
454 /* Address changed. */
455 delete_ipdev(ip_dev
);
458 insert_ipdev(dev_name
);
461 ovs_mutex_unlock(&mutex
);
465 tnl_port_map_delete_ipdev(const char dev_name
[])
467 struct ip_device
*ip_dev
, *next
;
469 ovs_mutex_lock(&mutex
);
470 LIST_FOR_EACH_SAFE(ip_dev
, next
, node
, &addr_list
) {
471 if (!strcmp(netdev_get_name(ip_dev
->dev
), dev_name
)) {
472 delete_ipdev(ip_dev
);
475 ovs_mutex_unlock(&mutex
);
479 tnl_port_map_run(void)
481 struct ip_device
*ip_dev
, *next
;
483 ovs_mutex_lock(&mutex
);
484 LIST_FOR_EACH_SAFE(ip_dev
, next
, node
, &addr_list
) {
485 char dev_name
[IFNAMSIZ
];
487 if (ip_dev
->change_seq
== netdev_get_change_seq(ip_dev
->dev
)) {
491 /* Address changed. */
492 ovs_strlcpy_arrays(dev_name
, ip_dev
->dev_name
);
493 delete_ipdev(ip_dev
);
494 insert_ipdev(dev_name
);
496 ovs_mutex_unlock(&mutex
);
500 tnl_port_map_init(void)
502 classifier_init(&cls
, flow_segment_u64s
);
503 ovs_list_init(&addr_list
);
504 ovs_list_init(&port_list
);
505 unixctl_command_register("tnl/ports/show", "-v", 0, 1, tnl_port_show
, NULL
);