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.
19 #include "tnl-arp-cache.h"
22 #include <netinet/icmp6.h>
28 #include "dpif-netdev.h"
29 #include "dynamic-string.h"
33 #include "ovs-thread.h"
35 #include "poll-loop.h"
37 #include "socket-util.h"
39 #include "unaligned.h"
42 #include "openvswitch/vlog.h"
46 #define ARP_ENTRY_DEFAULT_IDLE_TIME (15 * 60)
48 struct tnl_arp_entry {
49 struct cmap_node cmap_node;
52 time_t expires; /* Expiration time. */
53 char br_name[IFNAMSIZ];
56 static struct cmap table;
57 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
60 tnl_arp_hash(const struct in6_addr *ip)
62 return hash_bytes(ip->s6_addr, 16, 0);
65 static struct tnl_arp_entry *
66 tnl_arp_lookup__(const char br_name[IFNAMSIZ], const struct in6_addr *dst)
68 struct tnl_arp_entry *arp;
71 hash = tnl_arp_hash(dst);
72 CMAP_FOR_EACH_WITH_HASH (arp, cmap_node, hash, &table) {
73 if (ipv6_addr_equals(&arp->ip, dst) && !strcmp(arp->br_name, br_name)) {
74 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
82 tnl_arp_lookup(const char br_name[IFNAMSIZ], ovs_be32 dst,
85 struct tnl_arp_entry *arp;
89 in6_addr_set_mapped_ipv4(&dst6, dst);
91 arp = tnl_arp_lookup__(br_name, &dst6);
101 tnl_nd_lookup(const char br_name[IFNAMSIZ], const struct in6_addr *dst,
102 struct eth_addr *mac)
104 struct tnl_arp_entry *arp;
107 arp = tnl_arp_lookup__(br_name, dst);
116 arp_entry_free(struct tnl_arp_entry *arp)
122 tnl_arp_delete(struct tnl_arp_entry *arp)
124 uint32_t hash = tnl_arp_hash(&arp->ip);
125 cmap_remove(&table, &arp->cmap_node, hash);
126 ovsrcu_postpone(arp_entry_free, arp);
130 tnl_arp_set__(const char name[IFNAMSIZ], const struct in6_addr *dst,
131 const struct eth_addr mac)
133 ovs_mutex_lock(&mutex);
134 struct tnl_arp_entry *arp = tnl_arp_lookup__(name, dst);
136 if (eth_addr_equals(arp->mac, mac)) {
137 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
138 ovs_mutex_unlock(&mutex);
142 seq_change(tnl_conf_seq);
145 arp = xmalloc(sizeof *arp);
149 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
150 ovs_strlcpy(arp->br_name, name, sizeof arp->br_name);
151 cmap_insert(&table, &arp->cmap_node, tnl_arp_hash(&arp->ip));
152 ovs_mutex_unlock(&mutex);
156 tnl_arp_set(const char name[IFNAMSIZ], ovs_be32 dst,
157 const struct eth_addr mac)
159 struct in6_addr dst6;
161 in6_addr_set_mapped_ipv4(&dst6, dst);
162 tnl_arp_set__(name, &dst6, mac);
166 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
167 const char name[IFNAMSIZ])
169 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
173 /* Exact Match on all ARP flows. */
174 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
175 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
176 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
178 tnl_arp_set(name, flow->nw_src, flow->arp_sha);
183 tnl_nd_snoop(const struct flow *flow, struct flow_wildcards *wc,
184 const char name[IFNAMSIZ])
186 if (flow->dl_type != htons(ETH_TYPE_IPV6) ||
187 flow->nw_proto != IPPROTO_ICMPV6 ||
188 flow->tp_dst != htons(0) ||
189 flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
193 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
194 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
195 memset(&wc->masks.nd_target, 0xff, sizeof wc->masks.nd_target);
196 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
198 tnl_arp_set__(name, &flow->nd_target, flow->arp_tha);
203 tnl_arp_cache_run(void)
205 struct tnl_arp_entry *arp;
206 bool changed = false;
208 ovs_mutex_lock(&mutex);
209 CMAP_FOR_EACH(arp, cmap_node, &table) {
210 if (arp->expires <= time_now()) {
215 ovs_mutex_unlock(&mutex);
218 seq_change(tnl_conf_seq);
223 tnl_arp_cache_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
224 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
226 struct tnl_arp_entry *arp;
227 bool changed = false;
229 ovs_mutex_lock(&mutex);
230 CMAP_FOR_EACH(arp, cmap_node, &table) {
234 ovs_mutex_unlock(&mutex);
236 seq_change(tnl_conf_seq);
238 unixctl_command_reply(conn, "OK");
242 lookup_any(const char *host_name, struct in6_addr *address)
244 if (addr_is_ipv6(host_name)) {
245 return lookup_ipv6(host_name, address);
249 r = lookup_ip(host_name, &ip);
251 in6_addr_set_mapped_ipv4(address, ip.s_addr);
259 tnl_arp_cache_add(struct unixctl_conn *conn, int argc OVS_UNUSED,
260 const char *argv[], void *aux OVS_UNUSED)
262 const char *br_name = argv[1];
266 if (lookup_any(argv[2], &ip6) != 0) {
267 unixctl_command_reply_error(conn, "bad IP address");
271 if (!eth_addr_from_string(argv[3], &mac)) {
272 unixctl_command_reply_error(conn, "bad MAC address");
276 tnl_arp_set__(br_name, &ip6, mac);
277 unixctl_command_reply(conn, "OK");
281 tnl_arp_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
282 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
284 struct ds ds = DS_EMPTY_INITIALIZER;
285 struct tnl_arp_entry *arp;
287 ds_put_cstr(&ds, "IP MAC Bridge\n");
288 ds_put_cstr(&ds, "==========================================================================\n");
289 ovs_mutex_lock(&mutex);
290 CMAP_FOR_EACH(arp, cmap_node, &table) {
291 int start_len, need_ws;
293 start_len = ds.length;
294 print_ipv6_mapped(&ds, &arp->ip);
296 need_ws = INET6_ADDRSTRLEN - (ds.length - start_len);
297 ds_put_char_multiple(&ds, ' ', need_ws);
299 ds_put_format(&ds, ETH_ADDR_FMT" %s\n",
300 ETH_ADDR_ARGS(arp->mac), arp->br_name);
303 ovs_mutex_unlock(&mutex);
304 unixctl_command_reply(conn, ds_cstr(&ds));
309 tnl_arp_cache_init(void)
313 unixctl_command_register("tnl/arp/show", "", 0, 0, tnl_arp_cache_show, NULL);
314 unixctl_command_register("tnl/arp/set", "BRIDGE IP MAC", 3, 3, tnl_arp_cache_add, NULL);
315 unixctl_command_register("tnl/arp/flush", "", 0, 0, tnl_arp_cache_flush, NULL);