openvswitch: Userspace tunneling.
[cascardo/ovs.git] / lib / tnl-ports.c
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <stddef.h>
19 #include <stdint.h>
20
21 #include "classifier.h"
22 #include "dynamic-string.h"
23 #include "hash.h"
24 #include "ofpbuf.h"
25 #include "ovs-thread.h"
26 #include "odp-util.h"
27 #include "tnl-arp-cache.h"
28 #include "tnl-ports.h"
29 #include "unixctl.h"
30 #include "util.h"
31
32 static struct classifier cls;   /* Tunnel ports. */
33
34 struct tnl_port_in {
35     struct cls_rule cr;
36     odp_port_t portno;
37     struct ovs_refcount ref_cnt;
38     char dev_name[IFNAMSIZ];
39 };
40
41 static struct tnl_port_in *
42 tnl_port_cast(const struct cls_rule *cr)
43 {
44     BUILD_ASSERT_DECL(offsetof(struct tnl_port_in, cr) == 0);
45
46     return CONTAINER_OF(cr, struct tnl_port_in, cr);
47 }
48
49 static void
50 tnl_port_free(struct tnl_port_in *p)
51 {
52     cls_rule_destroy(&p->cr);
53     free(p);
54 }
55
56 static void
57 tnl_port_init_flow(struct flow *flow, ovs_be32 ip_dst, ovs_be16 udp_port)
58 {
59     memset(flow, 0, sizeof *flow);
60     flow->dl_type = htons(ETH_TYPE_IP);
61     if (udp_port) {
62         flow->nw_proto = IPPROTO_UDP;
63     } else {
64         flow->nw_proto = IPPROTO_GRE;
65     }
66     flow->tp_dst = udp_port;
67     /* When matching on incoming flow from remove tnl end point,
68      * our dst ip address is source ip for them. */
69     flow->nw_src = ip_dst;
70 }
71
72 void
73 tnl_port_map_insert(odp_port_t port, ovs_be32 ip_dst, ovs_be16 udp_port,
74                     const char dev_name[])
75 {
76     const struct cls_rule *cr;
77     struct tnl_port_in *p;
78     struct match match;
79
80     memset(&match, 0, sizeof match);
81     tnl_port_init_flow(&match.flow, ip_dst, udp_port);
82
83     do {
84         cr = classifier_lookup(&cls, &match.flow, NULL);
85         p = tnl_port_cast(cr);
86         /* Try again if the rule was released before we get the reference. */
87     } while (p && !ovs_refcount_try_ref_rcu(&p->ref_cnt));
88
89     if (p) {
90         return; /* Added refcount of an existing port. */
91     }
92
93     p = xzalloc(sizeof *p);
94     p->portno = port;
95
96     match.wc.masks.dl_type = OVS_BE16_MAX;
97     match.wc.masks.nw_proto = 0xff;
98     match.wc.masks.nw_frag = 0xff;      /* XXX: No fragments support. */
99     match.wc.masks.tp_dst = OVS_BE16_MAX;
100     match.wc.masks.nw_src = OVS_BE32_MAX;
101
102     cls_rule_init(&p->cr, &match, 0);   /* Priority == 0. */
103     ovs_refcount_init(&p->ref_cnt);
104     strncpy(p->dev_name, dev_name, IFNAMSIZ);
105
106     classifier_insert(&cls, &p->cr);
107 }
108
109 static void
110 tnl_port_unref(const struct cls_rule *cr)
111 {
112     struct tnl_port_in *p = tnl_port_cast(cr);
113
114     if (cr && ovs_refcount_unref_relaxed(&p->ref_cnt) == 1) {
115         if (classifier_remove(&cls, cr)) {
116             ovsrcu_postpone(tnl_port_free, p);
117         }
118     }
119 }
120
121 void
122 tnl_port_map_delete(ovs_be32 ip_dst, ovs_be16 udp_port)
123 {
124     const struct cls_rule *cr;
125     struct flow flow;
126
127     tnl_port_init_flow(&flow, ip_dst, udp_port);
128
129     cr = classifier_lookup(&cls, &flow, NULL);
130     tnl_port_unref(cr);
131 }
132
133 odp_port_t
134 tnl_port_map_lookup(const struct flow *flow, struct flow_wildcards *wc)
135 {
136     const struct cls_rule *cr = classifier_lookup(&cls, flow, wc);
137
138     return (cr) ? tnl_port_cast(cr)->portno : ODPP_NONE;
139 }
140
141 static void
142 tnl_port_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
143               const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
144 {
145     struct ds ds = DS_EMPTY_INITIALIZER;
146     const struct tnl_port_in *p;
147
148     ds_put_format(&ds, "Listening ports:\n");
149     CLS_FOR_EACH(p, cr, &cls) {
150         struct odputil_keybuf keybuf;
151         struct odputil_keybuf maskbuf;
152         struct flow flow;
153         const struct nlattr *key, *mask;
154         size_t key_len, mask_len;
155         struct flow_wildcards wc;
156         struct ofpbuf buf;
157
158         ds_put_format(&ds, "%s (%"PRIu32") : ", p->dev_name, p->portno);
159         minimask_expand(&p->cr.match.mask, &wc);
160         miniflow_expand(&p->cr.match.flow, &flow);
161
162         /* Key. */
163         ofpbuf_use_stack(&buf, &keybuf, sizeof keybuf);
164         odp_flow_key_from_flow(&buf, &flow, &wc.masks,
165                                flow.in_port.odp_port, true);
166         key = ofpbuf_data(&buf);
167         key_len = ofpbuf_size(&buf);
168         /* mask*/
169         ofpbuf_use_stack(&buf, &maskbuf, sizeof maskbuf);
170         odp_flow_key_from_mask(&buf, &wc.masks, &flow,
171                                odp_to_u32(wc.masks.in_port.odp_port),
172                                SIZE_MAX, false);
173         mask = ofpbuf_data(&buf);
174         mask_len = ofpbuf_size(&buf);
175
176         /* build string. */
177         odp_flow_format(key, key_len, mask, mask_len, NULL, &ds, false);
178         ds_put_format(&ds, "\n");
179     }
180     unixctl_command_reply(conn, ds_cstr(&ds));
181     ds_destroy(&ds);
182 }
183
184 void
185 tnl_port_map_init(void)
186 {
187     classifier_init(&cls, flow_segment_u32s);
188     unixctl_command_register("tnl/ports/show", "", 0, 0, tnl_port_show, NULL);
189 }