tunnel: test VXLAN receive and UDP checksum
[cascardo/ovs.git] / lib / tnl-neigh-cache.c
1 /*
2  * Copyright (c) 2014, 2015 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
19 #include "tnl-neigh-cache.h"
20
21 #include <inttypes.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <stdlib.h>
26
27 #include "bitmap.h"
28 #include "cmap.h"
29 #include "coverage.h"
30 #include "dpif-netdev.h"
31 #include "dynamic-string.h"
32 #include "errno.h"
33 #include "flow.h"
34 #include "netdev.h"
35 #include "ovs-thread.h"
36 #include "packets.h"
37 #include "poll-loop.h"
38 #include "seq.h"
39 #include "socket-util.h"
40 #include "timeval.h"
41 #include "unaligned.h"
42 #include "unixctl.h"
43 #include "util.h"
44 #include "openvswitch/vlog.h"
45
46
47 /* In seconds */
48 #define NEIGH_ENTRY_DEFAULT_IDLE_TIME  (15 * 60)
49
50 struct tnl_neigh_entry {
51     struct cmap_node cmap_node;
52     struct in6_addr ip;
53     struct eth_addr mac;
54     time_t expires;             /* Expiration time. */
55     char br_name[IFNAMSIZ];
56 };
57
58 static struct cmap table;
59 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
60
61 static uint32_t
62 tnl_neigh_hash(const struct in6_addr *ip)
63 {
64     return hash_bytes(ip->s6_addr, 16, 0);
65 }
66
67 static struct tnl_neigh_entry *
68 tnl_neigh_lookup__(const char br_name[IFNAMSIZ], const struct in6_addr *dst)
69 {
70     struct tnl_neigh_entry *neigh;
71     uint32_t hash;
72
73     hash = tnl_neigh_hash(dst);
74     CMAP_FOR_EACH_WITH_HASH (neigh, cmap_node, hash, &table) {
75         if (ipv6_addr_equals(&neigh->ip, dst) && !strcmp(neigh->br_name, br_name)) {
76             neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
77             return neigh;
78         }
79     }
80     return NULL;
81 }
82
83 int
84 tnl_neigh_lookup(const char br_name[IFNAMSIZ], const struct in6_addr *dst,
85                  struct eth_addr *mac)
86 {
87     struct tnl_neigh_entry *neigh;
88     int res = ENOENT;
89
90     neigh = tnl_neigh_lookup__(br_name, dst);
91     if (neigh) {
92         *mac = neigh->mac;
93         res = 0;
94     }
95     return res;
96 }
97
98 static void
99 neigh_entry_free(struct tnl_neigh_entry *neigh)
100 {
101     free(neigh);
102 }
103
104 static void
105 tnl_neigh_delete(struct tnl_neigh_entry *neigh)
106 {
107     uint32_t hash = tnl_neigh_hash(&neigh->ip);
108     cmap_remove(&table, &neigh->cmap_node, hash);
109     ovsrcu_postpone(neigh_entry_free, neigh);
110 }
111
112 static void
113 tnl_neigh_set__(const char name[IFNAMSIZ], const struct in6_addr *dst,
114               const struct eth_addr mac)
115 {
116     ovs_mutex_lock(&mutex);
117     struct tnl_neigh_entry *neigh = tnl_neigh_lookup__(name, dst);
118     if (neigh) {
119         if (eth_addr_equals(neigh->mac, mac)) {
120             neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
121             ovs_mutex_unlock(&mutex);
122             return;
123         }
124         tnl_neigh_delete(neigh);
125         seq_change(tnl_conf_seq);
126     }
127
128     neigh = xmalloc(sizeof *neigh);
129
130     neigh->ip = *dst;
131     neigh->mac = mac;
132     neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
133     ovs_strlcpy(neigh->br_name, name, sizeof neigh->br_name);
134     cmap_insert(&table, &neigh->cmap_node, tnl_neigh_hash(&neigh->ip));
135     ovs_mutex_unlock(&mutex);
136 }
137
138 static void
139 tnl_arp_set(const char name[IFNAMSIZ], ovs_be32 dst,
140             const struct eth_addr mac)
141 {
142     struct in6_addr dst6 = in6_addr_mapped_ipv4(dst);
143     tnl_neigh_set__(name, &dst6, mac);
144 }
145
146 static int
147 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
148               const char name[IFNAMSIZ])
149 {
150     if (flow->dl_type != htons(ETH_TYPE_ARP)) {
151         return EINVAL;
152     }
153
154     /* Exact Match on all ARP flows. */
155     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
156     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
157     memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
158
159     tnl_arp_set(name, flow->nw_src, flow->arp_sha);
160     return 0;
161 }
162
163 static int
164 tnl_nd_snoop(const struct flow *flow, struct flow_wildcards *wc,
165               const char name[IFNAMSIZ])
166 {
167     if (flow->dl_type != htons(ETH_TYPE_IPV6) ||
168         flow->nw_proto != IPPROTO_ICMPV6 ||
169         flow->tp_dst != htons(0) ||
170         flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
171         return EINVAL;
172     }
173
174     if (eth_addr_is_zero(flow->arp_tha)) {
175         return EINVAL;
176     }
177
178     memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
179     memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
180     memset(&wc->masks.nd_target, 0xff, sizeof wc->masks.nd_target);
181     memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
182
183     tnl_neigh_set__(name, &flow->nd_target, flow->arp_tha);
184     return 0;
185 }
186
187 int
188 tnl_neigh_snoop(const struct flow *flow, struct flow_wildcards *wc,
189                 const char name[IFNAMSIZ])
190 {
191     int res;
192     res = tnl_arp_snoop(flow, wc, name);
193     if (res != EINVAL) {
194         return res;
195     }
196     return tnl_nd_snoop(flow, wc, name);
197 }
198
199 void
200 tnl_neigh_cache_run(void)
201 {
202     struct tnl_neigh_entry *neigh;
203     bool changed = false;
204
205     ovs_mutex_lock(&mutex);
206     CMAP_FOR_EACH(neigh, cmap_node, &table) {
207         if (neigh->expires <= time_now()) {
208             tnl_neigh_delete(neigh);
209             changed = true;
210         }
211     }
212     ovs_mutex_unlock(&mutex);
213
214     if (changed) {
215         seq_change(tnl_conf_seq);
216     }
217 }
218
219 static void
220 tnl_neigh_cache_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
221                     const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
222 {
223     struct tnl_neigh_entry *neigh;
224     bool changed = false;
225
226     ovs_mutex_lock(&mutex);
227     CMAP_FOR_EACH(neigh, cmap_node, &table) {
228         tnl_neigh_delete(neigh);
229         changed = true;
230     }
231     ovs_mutex_unlock(&mutex);
232     if (changed) {
233         seq_change(tnl_conf_seq);
234     }
235     unixctl_command_reply(conn, "OK");
236 }
237
238 static int
239 lookup_any(const char *host_name, struct in6_addr *address)
240 {
241     if (addr_is_ipv6(host_name)) {
242         return lookup_ipv6(host_name, address);
243     } else {
244         int r;
245         struct in_addr ip;
246         r = lookup_ip(host_name, &ip);
247         if (r == 0) {
248             in6_addr_set_mapped_ipv4(address, ip.s_addr);
249         }
250         return r;
251     }
252     return ENOENT;
253 }
254
255 static void
256 tnl_neigh_cache_add(struct unixctl_conn *conn, int argc OVS_UNUSED,
257                     const char *argv[], void *aux OVS_UNUSED)
258 {
259     const char *br_name = argv[1];
260     struct eth_addr mac;
261     struct in6_addr ip6;
262
263     if (lookup_any(argv[2], &ip6) != 0) {
264         unixctl_command_reply_error(conn, "bad IP address");
265         return;
266     }
267
268     if (!eth_addr_from_string(argv[3], &mac)) {
269         unixctl_command_reply_error(conn, "bad MAC address");
270         return;
271     }
272
273     tnl_neigh_set__(br_name, &ip6, mac);
274     unixctl_command_reply(conn, "OK");
275 }
276
277 static void
278 tnl_neigh_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
279                      const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
280 {
281     struct ds ds = DS_EMPTY_INITIALIZER;
282     struct tnl_neigh_entry *neigh;
283
284     ds_put_cstr(&ds, "IP                                            MAC                 Bridge\n");
285     ds_put_cstr(&ds, "==========================================================================\n");
286     ovs_mutex_lock(&mutex);
287     CMAP_FOR_EACH(neigh, cmap_node, &table) {
288         int start_len, need_ws;
289
290         start_len = ds.length;
291         ipv6_format_mapped(&neigh->ip, &ds);
292
293         need_ws = INET6_ADDRSTRLEN - (ds.length - start_len);
294         ds_put_char_multiple(&ds, ' ', need_ws);
295
296         ds_put_format(&ds, ETH_ADDR_FMT"   %s\n",
297                       ETH_ADDR_ARGS(neigh->mac), neigh->br_name);
298
299     }
300     ovs_mutex_unlock(&mutex);
301     unixctl_command_reply(conn, ds_cstr(&ds));
302     ds_destroy(&ds);
303 }
304
305 void
306 tnl_neigh_cache_init(void)
307 {
308     cmap_init(&table);
309
310     unixctl_command_register("tnl/arp/show", "", 0, 0, tnl_neigh_cache_show, NULL);
311     unixctl_command_register("tnl/arp/set", "BRIDGE IP MAC", 3, 3, tnl_neigh_cache_add, NULL);
312     unixctl_command_register("tnl/arp/flush", "", 0, 0, tnl_neigh_cache_flush, NULL);
313     unixctl_command_register("tnl/neigh/show", "", 0, 0, tnl_neigh_cache_show, NULL);
314     unixctl_command_register("tnl/neigh/set", "BRIDGE IP MAC", 3, 3, tnl_neigh_cache_add, NULL);
315     unixctl_command_register("tnl/neigh/flush", "", 0, 0, tnl_neigh_cache_flush, NULL);
316 }