]>
Commit | Line | Data |
---|---|---|
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 */ | |
d62a17ae | 52 | static void map_slaves_to_bridge(struct interface *br_if, int link) |
6675513d | 53 | { |
d62a17ae | 54 | struct vrf *vrf; |
d62a17ae | 55 | struct interface *ifp; |
56 | ||
a2addae8 | 57 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { |
451fda4f | 58 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 59 | struct zebra_if *zif; |
60 | struct zebra_l2info_brslave *br_slave; | |
61 | ||
62 | if (ifp->ifindex == IFINDEX_INTERNAL || !ifp->info) | |
63 | continue; | |
64 | if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) | |
65 | continue; | |
66 | ||
67 | /* NOTE: This assumes 'zebra_l2info_brslave' is the | |
68 | * first field | |
69 | * for any L2 interface. | |
70 | */ | |
71 | zif = (struct zebra_if *)ifp->info; | |
72 | br_slave = &zif->brslave_info; | |
73 | ||
74 | if (link) { | |
75 | if (br_slave->bridge_ifindex == br_if->ifindex) | |
76 | br_slave->br_if = br_if; | |
77 | } else { | |
78 | if (br_slave->br_if == br_if) | |
79 | br_slave->br_if = NULL; | |
80 | } | |
81 | } | |
82 | } | |
6675513d | 83 | } |
84 | ||
85 | /* Public functions */ | |
d62a17ae | 86 | void zebra_l2_map_slave_to_bridge(struct zebra_l2info_brslave *br_slave) |
6675513d | 87 | { |
d62a17ae | 88 | struct interface *br_if; |
6675513d | 89 | |
d62a17ae | 90 | /* TODO: Handle change of master */ |
91 | br_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), | |
92 | br_slave->bridge_ifindex); | |
93 | if (br_if) | |
94 | br_slave->br_if = br_if; | |
6675513d | 95 | } |
96 | ||
d62a17ae | 97 | void zebra_l2_unmap_slave_from_bridge(struct zebra_l2info_brslave *br_slave) |
6675513d | 98 | { |
d62a17ae | 99 | br_slave->br_if = NULL; |
6675513d | 100 | } |
101 | ||
b9368db9 DD |
102 | void zebra_l2_map_slave_to_bond(struct zebra_l2info_bondslave *bond_slave) |
103 | { | |
104 | struct interface *bond_if; | |
105 | ||
106 | /* TODO: Handle change of master */ | |
107 | bond_if = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), | |
108 | bond_slave->bond_ifindex); | |
109 | if (bond_if) | |
110 | bond_slave->bond_if = bond_if; | |
111 | } | |
112 | ||
113 | void zebra_l2_unmap_slave_from_bond(struct zebra_l2info_bondslave *bond_slave) | |
114 | { | |
115 | if (bond_slave != NULL) | |
116 | bond_slave->bond_if = NULL; | |
117 | } | |
118 | ||
6675513d | 119 | /* |
120 | * Handle Bridge interface add or update. Update relevant info, | |
121 | * map slaves (if any) to the bridge. | |
122 | */ | |
d62a17ae | 123 | void zebra_l2_bridge_add_update(struct interface *ifp, |
124 | struct zebra_l2info_bridge *bridge_info, | |
125 | int add) | |
6675513d | 126 | { |
d62a17ae | 127 | struct zebra_if *zif; |
6675513d | 128 | |
d62a17ae | 129 | zif = ifp->info; |
130 | assert(zif); | |
6675513d | 131 | |
d62a17ae | 132 | /* Copy over the L2 information. */ |
133 | memcpy(&zif->l2info.br, bridge_info, sizeof(*bridge_info)); | |
6675513d | 134 | |
d62a17ae | 135 | /* Link all slaves to this bridge */ |
136 | map_slaves_to_bridge(ifp, 1); | |
6675513d | 137 | } |
138 | ||
139 | /* | |
140 | * Handle Bridge interface delete. | |
141 | */ | |
d62a17ae | 142 | void zebra_l2_bridge_del(struct interface *ifp) |
6675513d | 143 | { |
d62a17ae | 144 | /* Unlink all slaves to this bridge */ |
145 | map_slaves_to_bridge(ifp, 0); | |
6675513d | 146 | } |
147 | ||
148 | /* | |
149 | * Update L2 info for a VLAN interface. Only relevant parameter is the | |
150 | * VLAN Id and this cannot change. | |
151 | */ | |
d62a17ae | 152 | void zebra_l2_vlanif_update(struct interface *ifp, |
153 | struct zebra_l2info_vlan *vlan_info) | |
6675513d | 154 | { |
d62a17ae | 155 | struct zebra_if *zif; |
6675513d | 156 | |
d62a17ae | 157 | zif = ifp->info; |
158 | assert(zif); | |
6675513d | 159 | |
d62a17ae | 160 | /* Copy over the L2 information. */ |
161 | memcpy(&zif->l2info.vl, vlan_info, sizeof(*vlan_info)); | |
6675513d | 162 | } |
163 | ||
164 | /* | |
165 | * Update L2 info for a VxLAN interface. This is called upon interface | |
166 | * addition as well as update. Upon add, need to invoke the VNI create | |
167 | * function. Upon update, the params of interest are the local tunnel | |
168 | * IP and VLAN mapping, but the latter is handled separately. | |
169 | */ | |
d62a17ae | 170 | void zebra_l2_vxlanif_add_update(struct interface *ifp, |
171 | struct zebra_l2info_vxlan *vxlan_info, int add) | |
6675513d | 172 | { |
d62a17ae | 173 | struct zebra_if *zif; |
174 | struct in_addr old_vtep_ip; | |
6675513d | 175 | |
d62a17ae | 176 | zif = ifp->info; |
177 | assert(zif); | |
6675513d | 178 | |
d62a17ae | 179 | if (add) { |
180 | memcpy(&zif->l2info.vxl, vxlan_info, sizeof(*vxlan_info)); | |
181 | zebra_vxlan_if_add(ifp); | |
182 | return; | |
183 | } | |
6675513d | 184 | |
d62a17ae | 185 | old_vtep_ip = zif->l2info.vxl.vtep_ip; |
186 | if (IPV4_ADDR_SAME(&old_vtep_ip, &vxlan_info->vtep_ip)) | |
187 | return; | |
6675513d | 188 | |
d62a17ae | 189 | zif->l2info.vxl.vtep_ip = vxlan_info->vtep_ip; |
190 | zebra_vxlan_if_update(ifp, ZEBRA_VXLIF_LOCAL_IP_CHANGE); | |
6675513d | 191 | } |
192 | ||
193 | /* | |
194 | * Handle change to VLAN to VNI mapping. | |
195 | */ | |
d62a17ae | 196 | void zebra_l2_vxlanif_update_access_vlan(struct interface *ifp, |
197 | vlanid_t access_vlan) | |
6675513d | 198 | { |
d62a17ae | 199 | struct zebra_if *zif; |
200 | vlanid_t old_access_vlan; | |
6675513d | 201 | |
d62a17ae | 202 | zif = ifp->info; |
203 | assert(zif); | |
6675513d | 204 | |
d62a17ae | 205 | old_access_vlan = zif->l2info.vxl.access_vlan; |
206 | if (old_access_vlan == access_vlan) | |
207 | return; | |
13d60d35 | 208 | |
d62a17ae | 209 | zif->l2info.vxl.access_vlan = access_vlan; |
210 | zebra_vxlan_if_update(ifp, ZEBRA_VXLIF_VLAN_CHANGE); | |
6675513d | 211 | } |
212 | ||
213 | /* | |
214 | * Handle VxLAN interface delete. | |
215 | */ | |
d62a17ae | 216 | void zebra_l2_vxlanif_del(struct interface *ifp) |
6675513d | 217 | { |
d62a17ae | 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 | */ | |
d62a17ae | 226 | void zebra_l2if_update_bridge_slave(struct interface *ifp, |
227 | ifindex_t bridge_ifindex) | |
6675513d | 228 | { |
d62a17ae | 229 | struct zebra_if *zif; |
230 | ifindex_t old_bridge_ifindex; | |
6675513d | 231 | |
d62a17ae | 232 | zif = ifp->info; |
233 | assert(zif); | |
6675513d | 234 | |
d62a17ae | 235 | old_bridge_ifindex = zif->brslave_info.bridge_ifindex; |
236 | if (old_bridge_ifindex == bridge_ifindex) | |
237 | return; | |
6675513d | 238 | |
d62a17ae | 239 | zif->brslave_info.bridge_ifindex = bridge_ifindex; |
6675513d | 240 | |
d62a17ae | 241 | /* Set up or remove link with master */ |
af026ae4 | 242 | if (bridge_ifindex != IFINDEX_INTERNAL) { |
b682f6de | 243 | zebra_l2_map_slave_to_bridge(&zif->brslave_info); |
af026ae4 | 244 | /* In the case of VxLAN, invoke the handler for EVPN. */ |
245 | if (zif->zif_type == ZEBRA_IF_VXLAN) | |
b682f6de | 246 | zebra_vxlan_if_update(ifp, ZEBRA_VXLIF_MASTER_CHANGE); |
af026ae4 | 247 | } else if (old_bridge_ifindex != IFINDEX_INTERNAL) { |
57f7feb6 MK |
248 | /* |
249 | * In the case of VxLAN, invoke the handler for EVPN. | |
250 | * Note that this should be done *prior* | |
251 | * to unmapping the interface from the bridge. | |
af026ae4 | 252 | */ |
253 | if (zif->zif_type == ZEBRA_IF_VXLAN) | |
b682f6de | 254 | zebra_vxlan_if_update(ifp, ZEBRA_VXLIF_MASTER_CHANGE); |
255 | zebra_l2_unmap_slave_from_bridge(&zif->brslave_info); | |
af026ae4 | 256 | } |
6675513d | 257 | } |
b9368db9 DD |
258 | |
259 | void zebra_l2if_update_bond_slave(struct interface *ifp, ifindex_t bond_ifindex) | |
260 | { | |
261 | struct zebra_if *zif; | |
262 | ifindex_t old_bond_ifindex; | |
263 | ||
264 | zif = ifp->info; | |
265 | assert(zif); | |
266 | ||
267 | old_bond_ifindex = zif->bondslave_info.bond_ifindex; | |
268 | if (old_bond_ifindex == bond_ifindex) | |
269 | return; | |
270 | ||
271 | zif->bondslave_info.bond_ifindex = bond_ifindex; | |
272 | ||
273 | /* Set up or remove link with master */ | |
274 | if (bond_ifindex != IFINDEX_INTERNAL) | |
275 | zebra_l2_map_slave_to_bond(&zif->bondslave_info); | |
276 | else if (old_bond_ifindex != IFINDEX_INTERNAL) | |
277 | zebra_l2_unmap_slave_from_bond(&zif->bondslave_info); | |
278 | } |