]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_l2.c
zebra: VNI and VTEP handling
[mirror_frr.git] / zebra / zebra_l2.c
CommitLineData
6675513d 1/*
2 * Zebra Layer-2 interface handling code
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
4 *
5 * This file is part of FRR.
6 *
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * FRR is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "if.h"
26#include "prefix.h"
27#include "table.h"
28#include "memory.h"
29#include "log.h"
30#include "linklist.h"
31#include "stream.h"
32#include "hash.h"
33#include "jhash.h"
34
35#include "zebra/rib.h"
36#include "zebra/rt.h"
37#include "zebra/zebra_ns.h"
38#include "zebra/zserv.h"
39#include "zebra/debug.h"
40#include "zebra/interface.h"
41#include "zebra/zebra_memory.h"
42#include "zebra/zebra_vrf.h"
43#include "zebra/rt_netlink.h"
44#include "zebra/zebra_l2.h"
13d60d35 45#include "zebra/zebra_vxlan.h"
6675513d 46
47/* definitions */
48
49/* static function declarations */
50
51/* Private functions */
52static void
53map_slaves_to_bridge (struct interface *br_if, int link)
54{
55 struct vrf *vrf;
56 struct listnode *node;
57 struct interface *ifp;
58
59 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
60 {
61 for (ALL_LIST_ELEMENTS_RO (vrf->iflist, node, ifp))
62 {
63 struct zebra_if *zif;
64 struct zebra_l2info_brslave *br_slave;
65
66 if (ifp->ifindex == IFINDEX_INTERNAL ||
67 !ifp->info)
68 continue;
69 if (!IS_ZEBRA_IF_BRIDGE_SLAVE (ifp))
70 continue;
71
72 /* NOTE: This assumes 'zebra_l2info_brslave' is the first field
73 * for any L2 interface.
74 */
75 zif = (struct zebra_if *) ifp->info;
76 br_slave = &zif->brslave_info;
77
78 if (link)
79 {
80 if (br_slave->bridge_ifindex == br_if->ifindex)
81 br_slave->br_if = br_if;
82 }
83 else
84 {
85 if (br_slave->br_if == br_if)
86 br_slave->br_if = NULL;
87 }
88 }
89 }
90}
91
92/* Public functions */
93void
94zebra_l2_map_slave_to_bridge (struct zebra_l2info_brslave *br_slave)
95{
96 struct interface *br_if;
97
98 /* TODO: Handle change of master */
99 br_if = if_lookup_by_index_per_ns (zebra_ns_lookup (NS_DEFAULT),
100 br_slave->bridge_ifindex);
101 if (br_if)
102 br_slave->br_if = br_if;
103}
104
105void
106zebra_l2_unmap_slave_from_bridge (struct zebra_l2info_brslave *br_slave)
107{
108 br_slave->br_if = NULL;
109}
110
111/*
112 * Handle Bridge interface add or update. Update relevant info,
113 * map slaves (if any) to the bridge.
114 */
115void
116zebra_l2_bridge_add_update (struct interface *ifp,
117 struct zebra_l2info_bridge *bridge_info,
118 int add)
119{
120 struct zebra_if *zif;
121
122 zif = ifp->info;
123 assert(zif);
124
125 /* Copy over the L2 information. */
126 memcpy (&zif->l2info.br, bridge_info, sizeof (*bridge_info));
127
128 /* Link all slaves to this bridge */
129 map_slaves_to_bridge (ifp, 1);
130}
131
132/*
133 * Handle Bridge interface delete.
134 */
135void
136zebra_l2_bridge_del (struct interface *ifp)
137{
138 /* Unlink all slaves to this bridge */
139 map_slaves_to_bridge (ifp, 0);
140}
141
142/*
143 * Update L2 info for a VLAN interface. Only relevant parameter is the
144 * VLAN Id and this cannot change.
145 */
146void
147zebra_l2_vlanif_update (struct interface *ifp,
148 struct zebra_l2info_vlan *vlan_info)
149{
150 struct zebra_if *zif;
151
152 zif = ifp->info;
153 assert(zif);
154
155 /* Copy over the L2 information. */
156 memcpy (&zif->l2info.vl, vlan_info, sizeof (*vlan_info));
157}
158
159/*
160 * Update L2 info for a VxLAN interface. This is called upon interface
161 * addition as well as update. Upon add, need to invoke the VNI create
162 * function. Upon update, the params of interest are the local tunnel
163 * IP and VLAN mapping, but the latter is handled separately.
164 */
165void
166zebra_l2_vxlanif_add_update (struct interface *ifp,
167 struct zebra_l2info_vxlan *vxlan_info,
168 int add)
169{
170 struct zebra_if *zif;
171 struct in_addr old_vtep_ip;
172
173 zif = ifp->info;
174 assert(zif);
175
176 if (add)
177 {
178 memcpy (&zif->l2info.vxl, vxlan_info, sizeof (*vxlan_info));
13d60d35 179 zebra_vxlan_if_add (ifp);
6675513d 180 return;
181 }
182
183 old_vtep_ip = zif->l2info.vxl.vtep_ip;
184 if (IPV4_ADDR_SAME(&old_vtep_ip, &vxlan_info->vtep_ip))
185 return;
186
187 zif->l2info.vxl.vtep_ip = vxlan_info->vtep_ip;
13d60d35 188 zebra_vxlan_if_update (ifp, ZEBRA_VXLIF_LOCAL_IP_CHANGE);
6675513d 189}
190
191/*
192 * Handle change to VLAN to VNI mapping.
193 */
194void
195zebra_l2_vxlanif_update_access_vlan (struct interface *ifp,
196 vlanid_t access_vlan)
197{
198 struct zebra_if *zif;
13d60d35 199 vlanid_t old_access_vlan;
6675513d 200
201 zif = ifp->info;
202 assert(zif);
203
13d60d35 204 old_access_vlan = zif->l2info.vxl.access_vlan;
205 if (old_access_vlan == access_vlan)
206 return;
207
6675513d 208 zif->l2info.vxl.access_vlan = access_vlan;
13d60d35 209 zebra_vxlan_if_update (ifp, ZEBRA_VXLIF_VLAN_CHANGE);
6675513d 210}
211
212/*
213 * Handle VxLAN interface delete.
214 */
215void
216zebra_l2_vxlanif_del (struct interface *ifp)
217{
13d60d35 218 zebra_vxlan_if_del (ifp);
6675513d 219}
220
221/*
222 * Map or unmap interface from bridge.
223 * NOTE: It is currently assumped that an interface has to be unmapped
224 * from a bridge before it can be mapped to another bridge.
225 */
226void
227zebra_l2if_update_bridge_slave (struct interface *ifp,
228 ifindex_t bridge_ifindex)
229{
230 struct zebra_if *zif;
231 ifindex_t old_bridge_ifindex;
232
233 zif = ifp->info;
234 assert(zif);
235
236 old_bridge_ifindex = zif->brslave_info.bridge_ifindex;
237 if (old_bridge_ifindex == bridge_ifindex)
238 return;
239
240 zif->brslave_info.bridge_ifindex = bridge_ifindex;
241
242 /* Set up or remove link with master */
243 if (bridge_ifindex != IFINDEX_INTERNAL)
244 zebra_l2_map_slave_to_bridge (&zif->brslave_info);
245 else if (old_bridge_ifindex != IFINDEX_INTERNAL)
246 zebra_l2_unmap_slave_from_bridge (&zif->brslave_info);
13d60d35 247
248 /* In the case of VxLAN, invoke the handler for EVPN. */
249 if (zif->zif_type == ZEBRA_IF_VXLAN)
250 zebra_vxlan_if_update (ifp, ZEBRA_VXLIF_MASTER_CHANGE);
6675513d 251}