2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netvirt.elan.internal;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.List;
15 import javax.annotation.PostConstruct;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
22 import org.opendaylight.genius.mdsalutil.ActionInfo;
23 import org.opendaylight.genius.mdsalutil.BucketInfo;
24 import org.opendaylight.genius.mdsalutil.FlowEntity;
25 import org.opendaylight.genius.mdsalutil.InstructionInfo;
26 import org.opendaylight.genius.mdsalutil.MDSALUtil;
27 import org.opendaylight.genius.mdsalutil.MatchInfo;
28 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
29 import org.opendaylight.genius.mdsalutil.NwConstants;
30 import org.opendaylight.genius.mdsalutil.actions.ActionDrop;
31 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
32 import org.opendaylight.genius.mdsalutil.actions.ActionLearn;
33 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
34 import org.opendaylight.genius.mdsalutil.actions.ActionPuntToController;
35 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
36 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
37 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
38 import org.opendaylight.genius.mdsalutil.matches.MatchArpOp;
39 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
40 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
41 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchRegister;
42 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
43 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderConstant;
44 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
45 import org.opendaylight.netvirt.elan.utils.ElanConstants;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, ElanNodeListener> {
60 private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
61 private static final int LEARN_MATCH_REG4_VALUE = 1;
63 private final DataBroker broker;
64 private final IMdsalApiManager mdsalManager;
65 private final IdManagerService idManagerService;
66 private final int tempSmacLearnTimeout;
67 private final boolean puntLldpToController;
68 private final JobCoordinator jobCoordinator;
71 public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager, ElanConfig elanConfig,
72 IdManagerService idManagerService, JobCoordinator jobCoordinator) {
73 this.broker = dataBroker;
74 this.mdsalManager = mdsalManager;
75 this.tempSmacLearnTimeout = elanConfig.getTempSmacLearnTimeout();
76 this.puntLldpToController = elanConfig.isPuntLldpToController();
77 this.idManagerService = idManagerService;
78 this.jobCoordinator = jobCoordinator;
84 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
88 protected InstanceIdentifier<Node> getWildCardPath() {
89 return InstanceIdentifier.create(Nodes.class).child(Node.class);
93 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
97 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
101 protected void add(InstanceIdentifier<Node> identifier, Node add) {
102 NodeId nodeId = add.getId();
103 String[] node = nodeId.getValue().split(":");
104 if (node.length < 2) {
105 LOG.warn("Unexpected nodeId {}", nodeId.getValue());
108 BigInteger dpId = new BigInteger(node[1]);
109 createTableMissEntry(dpId);
110 createMulticastFlows(dpId);
111 createArpDefaultFlowsForArpCheckTable(dpId);
114 private void createArpDefaultFlowsForArpCheckTable(BigInteger dpId) {
115 jobCoordinator.enqueueJob("ARP_CHECK_TABLE-" + dpId.toString(), () -> {
116 WriteTransaction writeFlowTx = broker.newWriteOnlyTransaction();
117 LOG.debug("Received notification to install Arp Check Default entries for dpn {} ", dpId);
118 createArpRequestMatchFlows(dpId, writeFlowTx);
119 createArpResponseMatchFlows(dpId, writeFlowTx);
120 return Arrays.asList(writeFlowTx.submit());
124 public void createTableMissEntry(BigInteger dpnId) {
125 setupTableMissSmacFlow(dpnId);
126 setupTableMissDmacFlow(dpnId);
127 setupTableMissArpCheckFlow(dpnId);
128 setupTableMissApResponderFlow(dpnId);
129 setupExternalL2vniTableMissFlow(dpnId);
132 private void createMulticastFlows(BigInteger dpId) {
133 createL2ControlProtocolDropFlows(dpId);
134 createMulticastPuntFlows(dpId);
137 private void createMulticastPuntFlows(BigInteger dpId) {
138 if (puntLldpToController) {
139 createLldpFlows(dpId);
143 private void createLldpFlows(BigInteger dpId) {
144 createLldpFlow(dpId, ElanConstants.LLDP_DST_1, "LLDP dMac Table Flow 1");
145 createLldpFlow(dpId, ElanConstants.LLDP_DST_2, "LLDP dMac Table Flow 2");
146 createLldpFlow(dpId, ElanConstants.LLDP_DST_3, "LLDP dMac Table Flow 3");
149 private void createLldpFlow(BigInteger dpId, String dstMac, String flowName) {
150 List<MatchInfo> mkMatches = new ArrayList<>();
151 mkMatches.add(new MatchEthernetType(ElanConstants.LLDP_ETH_TYPE));
152 mkMatches.add(new MatchEthernetDestination(new MacAddress(dstMac)));
154 List<ActionInfo> listActionInfo = new ArrayList<>();
155 listActionInfo.add(new ActionPuntToController());
157 List<InstructionInfo> mkInstructions = new ArrayList<>();
158 mkInstructions.add(new InstructionApplyActions(listActionInfo));
160 String flowId = dpId.toString() + NwConstants.ELAN_DMAC_TABLE + "lldp" + ElanConstants.LLDP_ETH_TYPE + dstMac;
161 FlowEntity lldpFlow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 16, flowName, 0, 0,
162 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
164 mdsalManager.installFlow(lldpFlow);
167 private void setupTableMissSmacFlow(BigInteger dpId) {
168 List<ActionInfo> actionsInfos = new ArrayList<>();
169 actionsInfos.add(new ActionPuntToController());
170 actionsInfos.add(new ActionLearn(0, tempSmacLearnTimeout, 0, ElanConstants.COOKIE_ELAN_LEARNED_SMAC, 0,
171 NwConstants.ELAN_SMAC_LEARNED_TABLE, 0, 0,
173 new ActionLearn.MatchFromField(NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getType(),
174 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getType(),
175 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getFlowModHeaderLenInt()),
176 new ActionLearn.MatchFromField(NwConstants.NxmOfFieldType.NXM_NX_REG1.getType(),
177 NwConstants.NxmOfFieldType.NXM_NX_REG1.getType(), ElanConstants.INTERFACE_TAG_LENGTH),
178 new ActionLearn.CopyFromValue(LEARN_MATCH_REG4_VALUE,
179 NwConstants.NxmOfFieldType.NXM_NX_REG4.getType(), 8))));
181 List<InstructionInfo> mkInstructions = new ArrayList<>();
182 mkInstructions.add(new InstructionApplyActions(actionsInfos));
183 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
185 List<MatchInfo> mkMatches = new ArrayList<>();
186 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
187 getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
188 ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
189 mdsalManager.installFlow(flowEntity);
191 addSmacBaseTableFlow(dpId);
192 addSmacLearnedTableFlow(dpId);
195 private void addSmacBaseTableFlow(BigInteger dpId) {
196 // T48 - resubmit to T49 & T50
197 List<ActionInfo> actionsInfo = new ArrayList<>();
198 actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_LEARNED_TABLE));
199 actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_TABLE));
200 List<InstructionInfo> mkInstruct = new ArrayList<>();
201 mkInstruct.add(new InstructionApplyActions(actionsInfo));
202 List<MatchInfo> mkMatch = new ArrayList<>();
203 FlowEntity doubleResubmitTable = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_BASE_TABLE,
204 getTableMissFlowRef(NwConstants.ELAN_BASE_TABLE), 0, "Elan sMac resubmit table", 0, 0,
205 ElanConstants.COOKIE_ELAN_BASE_SMAC, mkMatch, mkInstruct);
206 mdsalManager.installFlow(doubleResubmitTable);
209 private void addSmacLearnedTableFlow(BigInteger dpId) {
210 // T50 - match on Reg4 and goto T51
211 List<MatchInfoBase> mkMatches = new ArrayList<>();
212 mkMatches.add(new NxMatchRegister(NxmNxReg4.class, LEARN_MATCH_REG4_VALUE));
213 List<InstructionInfo> mkInstructions = new ArrayList<>();
214 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
215 String flowRef = new StringBuffer().append(NwConstants.ELAN_SMAC_TABLE).append(NwConstants.FLOWID_SEPARATOR)
216 .append(LEARN_MATCH_REG4_VALUE).toString();
217 FlowEntity flowEntity =
218 MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE, flowRef, 10, "ELAN sMac Table Reg4 Flow",
219 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(BigInteger.valueOf(LEARN_MATCH_REG4_VALUE)),
220 mkMatches, mkInstructions);
221 mdsalManager.installFlow(flowEntity);
224 private void setupTableMissDmacFlow(BigInteger dpId) {
225 List<MatchInfo> mkMatches = new ArrayList<>();
227 List<InstructionInfo> mkInstructions = new ArrayList<>();
228 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_UNKNOWN_DMAC_TABLE));
230 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
231 getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
232 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
234 mdsalManager.installFlow(flowEntity);
237 private void setupExternalL2vniTableMissFlow(BigInteger dpnId) {
238 List<MatchInfo> matches = new ArrayList<>();
239 List<ActionInfo> actionsInfos = Collections.singletonList(new ActionNxResubmit(NwConstants
240 .LPORT_DISPATCHER_TABLE));
241 List<InstructionInfo> instructions = Collections.singletonList(new InstructionApplyActions(actionsInfos));
242 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L2VNI_EXTERNAL_TUNNEL_DEMUX_TABLE,
243 getTableMissFlowRef(NwConstants.L2VNI_EXTERNAL_TUNNEL_DEMUX_TABLE), 0,
244 "External L2VNI Table Miss Flow", 0, 0,
245 ElanConstants.COOKIE_L2VNI_DEMUX, matches, instructions);
246 mdsalManager.installFlow(flowEntity);
250 private void createL2ControlProtocolDropFlows(BigInteger dpId) {
251 List<MatchInfo> mkMatches = new ArrayList<>();
252 MatchEthernetDestination matchEthDst =
253 new MatchEthernetDestination(new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC),
254 new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC_MASK));
256 mkMatches.add(matchEthDst);
258 List<ActionInfo> listActionInfo = new ArrayList<>();
259 listActionInfo.add(new ActionDrop());
261 List<InstructionInfo> mkInstructions = new ArrayList<>();
262 mkInstructions.add(new InstructionApplyActions(listActionInfo));
264 String flowId = dpId.toString() + NwConstants.ELAN_DMAC_TABLE + "l2control"
265 + ElanConstants.L2_CONTROL_PACKETS_DMAC + ElanConstants.L2_CONTROL_PACKETS_DMAC_MASK;
266 FlowEntity flow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 15,
267 "L2 control packets dMac Table Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches,
270 mdsalManager.installFlow(flow);
273 private String getTableMissFlowRef(long tableId) {
274 return new StringBuffer().append(tableId).toString();
278 protected ElanNodeListener getDataTreeChangeListener() {
279 return ElanNodeListener.this;
282 private void setupTableMissApResponderFlow(final BigInteger dpnId) {
283 mdsalManager.installFlow(dpnId, ArpResponderUtil.getArpResponderTableMissFlow(dpnId));
286 private void setupTableMissArpCheckFlow(BigInteger dpnId) {
287 mdsalManager.installFlow(dpnId,
288 MDSALUtil.buildFlowEntity(dpnId, NwConstants.ARP_CHECK_TABLE,
289 String.valueOf("L2.ELAN." + NwConstants.ARP_CHECK_TABLE), NwConstants.TABLE_MISS_PRIORITY,
290 ArpResponderConstant.DROP_FLOW_NAME.value(), 0, 0, NwConstants.COOKIE_ARP_RESPONDER,
291 new ArrayList<MatchInfo>(),
292 Collections.singletonList(new InstructionGotoTable(NwConstants.ELAN_BASE_TABLE))));
295 private void createArpRequestMatchFlows(BigInteger dpId, WriteTransaction writeFlowTx) {
297 long arpRequestGroupId = ArpResponderUtil.retrieveStandardArpResponderGroupId(idManagerService);
298 List<BucketInfo> buckets = ArpResponderUtil.getDefaultBucketInfos(NwConstants.ELAN_BASE_TABLE,
299 NwConstants.ARP_RESPONDER_TABLE);
300 ArpResponderUtil.installGroup(mdsalManager, dpId, arpRequestGroupId,
301 ArpResponderConstant.GROUP_FLOW_NAME.value(), buckets);
303 FlowEntity arpReqArpCheckTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.ARP_CHECK_TABLE,
304 NwConstants.ARP_REQUEST, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REQUEST),
305 () -> Collections.singletonList(new ActionGroup(arpRequestGroupId)));
306 LOG.trace("Invoking MDSAL to install Arp Rquest Match Flow for table {}", NwConstants.ARP_CHECK_TABLE);
307 mdsalManager.addFlowToTx(arpReqArpCheckTbl, writeFlowTx);
311 private void createArpResponseMatchFlows(BigInteger dpId, WriteTransaction writeFlowTx) {
312 FlowEntity arpRepArpCheckTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.ARP_CHECK_TABLE,
313 NwConstants.ARP_REPLY, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REPLY),
314 () -> Arrays.asList(new ActionPuntToController(), new ActionNxResubmit(NwConstants.ELAN_BASE_TABLE)));
315 LOG.trace("Invoking MDSAL to install Arp Reply Match Flow for Table {} ", NwConstants.ARP_CHECK_TABLE);
316 mdsalManager.addFlowToTx(arpRepArpCheckTbl, writeFlowTx);