2 * Copyright (c) 2014, 2015 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.
21 #include "classifier.h"
22 #include "dynamic-string.h"
25 #include "ovs-thread.h"
27 #include "tnl-arp-cache.h"
28 #include "tnl-ports.h"
29 #include "ovs-thread.h"
33 static struct ovs_mutex mutex
= OVS_MUTEX_INITIALIZER
;
34 static struct classifier cls
; /* Tunnel ports. */
39 struct ovs_refcount ref_cnt
;
40 char dev_name
[IFNAMSIZ
];
43 static struct tnl_port_in
*
44 tnl_port_cast(const struct cls_rule
*cr
)
46 BUILD_ASSERT_DECL(offsetof(struct tnl_port_in
, cr
) == 0);
48 return CONTAINER_OF(cr
, struct tnl_port_in
, cr
);
52 tnl_port_free(struct tnl_port_in
*p
)
54 cls_rule_destroy(&p
->cr
);
59 tnl_port_init_flow(struct flow
*flow
, ovs_be16 udp_port
)
61 memset(flow
, 0, sizeof *flow
);
62 flow
->dl_type
= htons(ETH_TYPE_IP
);
64 flow
->nw_proto
= IPPROTO_UDP
;
66 flow
->nw_proto
= IPPROTO_GRE
;
68 flow
->tp_dst
= udp_port
;
72 tnl_port_map_insert(odp_port_t port
, ovs_be16 udp_port
, const char dev_name
[])
74 const struct cls_rule
*cr
;
75 struct tnl_port_in
*p
;
78 memset(&match
, 0, sizeof match
);
79 tnl_port_init_flow(&match
.flow
, udp_port
);
81 ovs_mutex_lock(&mutex
);
83 cr
= classifier_lookup(&cls
, CLS_MAX_VERSION
, &match
.flow
, NULL
);
84 p
= tnl_port_cast(cr
);
85 /* Try again if the rule was released before we get the reference. */
86 } while (p
&& !ovs_refcount_try_ref_rcu(&p
->ref_cnt
));
89 p
= xzalloc(sizeof *p
);
92 match
.wc
.masks
.dl_type
= OVS_BE16_MAX
;
93 match
.wc
.masks
.nw_proto
= 0xff;
94 match
.wc
.masks
.nw_frag
= 0xff; /* XXX: No fragments support. */
95 match
.wc
.masks
.tp_dst
= OVS_BE16_MAX
;
97 cls_rule_init(&p
->cr
, &match
, 0); /* Priority == 0. */
98 ovs_refcount_init(&p
->ref_cnt
);
99 ovs_strlcpy(p
->dev_name
, dev_name
, sizeof p
->dev_name
);
101 classifier_insert(&cls
, &p
->cr
, CLS_MIN_VERSION
, NULL
, 0);
103 ovs_mutex_unlock(&mutex
);
107 tnl_port_unref(const struct cls_rule
*cr
)
109 struct tnl_port_in
*p
= tnl_port_cast(cr
);
111 if (cr
&& ovs_refcount_unref_relaxed(&p
->ref_cnt
) == 1) {
112 ovs_mutex_lock(&mutex
);
113 if (classifier_remove(&cls
, cr
)) {
114 ovsrcu_postpone(tnl_port_free
, p
);
116 ovs_mutex_unlock(&mutex
);
121 tnl_port_map_delete(ovs_be16 udp_port
)
123 const struct cls_rule
*cr
;
126 tnl_port_init_flow(&flow
, udp_port
);
128 cr
= classifier_lookup(&cls
, CLS_MAX_VERSION
, &flow
, NULL
);
132 /* 'flow' is non-const to allow for temporary modifications during the lookup.
133 * Any changes are restored before returning. */
135 tnl_port_map_lookup(struct flow
*flow
, struct flow_wildcards
*wc
)
137 const struct cls_rule
*cr
= classifier_lookup(&cls
, CLS_MAX_VERSION
, flow
,
140 return (cr
) ? tnl_port_cast(cr
)->portno
: ODPP_NONE
;
144 tnl_port_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
145 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
147 struct ds ds
= DS_EMPTY_INITIALIZER
;
148 const struct tnl_port_in
*p
;
150 ds_put_format(&ds
, "Listening ports:\n");
151 CLS_FOR_EACH(p
, cr
, &cls
) {
152 struct odputil_keybuf keybuf
;
153 struct odputil_keybuf maskbuf
;
155 const struct nlattr
*key
, *mask
;
156 size_t key_len
, mask_len
;
157 struct flow_wildcards wc
;
159 struct odp_flow_key_parms odp_parms
= {
164 ds_put_format(&ds
, "%s (%"PRIu32
") : ", p
->dev_name
, p
->portno
);
165 minimask_expand(p
->cr
.match
.mask
, &wc
);
166 miniflow_expand(p
->cr
.match
.flow
, &flow
);
169 odp_parms
.odp_in_port
= flow
.in_port
.odp_port
;
170 odp_parms
.support
.recirc
= true;
171 ofpbuf_use_stack(&buf
, &keybuf
, sizeof keybuf
);
172 odp_flow_key_from_flow(&odp_parms
, &buf
);
177 odp_parms
.odp_in_port
= wc
.masks
.in_port
.odp_port
;
178 odp_parms
.support
.recirc
= false;
179 ofpbuf_use_stack(&buf
, &maskbuf
, sizeof maskbuf
);
180 odp_flow_key_from_mask(&odp_parms
, &buf
);
185 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &ds
, false);
186 ds_put_format(&ds
, "\n");
188 unixctl_command_reply(conn
, ds_cstr(&ds
));
193 tnl_port_map_init(void)
195 classifier_init(&cls
, flow_segment_u64s
);
196 unixctl_command_register("tnl/ports/show", "", 0, 0, tnl_port_show
, NULL
);