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.infra.ManagedNewTransactionRunner;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
24 import org.opendaylight.genius.mdsalutil.ActionInfo;
25 import org.opendaylight.genius.mdsalutil.BucketInfo;
26 import org.opendaylight.genius.mdsalutil.FlowEntity;
27 import org.opendaylight.genius.mdsalutil.InstructionInfo;
28 import org.opendaylight.genius.mdsalutil.MDSALUtil;
29 import org.opendaylight.genius.mdsalutil.MatchInfo;
30 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
31 import org.opendaylight.genius.mdsalutil.NwConstants;
32 import org.opendaylight.genius.mdsalutil.actions.ActionDrop;
33 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
34 import org.opendaylight.genius.mdsalutil.actions.ActionLearn;
35 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
36 import org.opendaylight.genius.mdsalutil.actions.ActionPuntToController;
37 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
38 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
39 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
40 import org.opendaylight.genius.mdsalutil.matches.MatchArpOp;
41 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
42 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
43 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchRegister;
44 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
45 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderConstant;
46 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
47 import org.opendaylight.netvirt.elan.utils.ElanConstants;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 public class ElanNodeListener extends AsyncDataTreeChangeListenerBase<Node, ElanNodeListener> {
62 private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
63 private static final int LEARN_MATCH_REG4_VALUE = 1;
65 private final DataBroker broker;
66 private final ManagedNewTransactionRunner txRunner;
67 private final IMdsalApiManager mdsalManager;
68 private final IdManagerService idManagerService;
69 private final int tempSmacLearnTimeout;
70 private final boolean puntLldpToController;
71 private final JobCoordinator jobCoordinator;
74 public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager, ElanConfig elanConfig,
75 IdManagerService idManagerService, JobCoordinator jobCoordinator) {
76 this.broker = dataBroker;
77 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
78 this.mdsalManager = mdsalManager;
79 this.tempSmacLearnTimeout = elanConfig.getTempSmacLearnTimeout();
80 this.puntLldpToController = elanConfig.isPuntLldpToController();
81 this.idManagerService = idManagerService;
82 this.jobCoordinator = jobCoordinator;
88 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
92 protected InstanceIdentifier<Node> getWildCardPath() {
93 return InstanceIdentifier.create(Nodes.class).child(Node.class);
97 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
101 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
105 protected void add(InstanceIdentifier<Node> identifier, Node add) {
106 NodeId nodeId = add.getId();
107 String[] node = nodeId.getValue().split(":");
108 if (node.length < 2) {
109 LOG.warn("Unexpected nodeId {}", nodeId.getValue());
112 BigInteger dpId = new BigInteger(node[1]);
113 createTableMissEntry(dpId);
114 createMulticastFlows(dpId);
115 createArpDefaultFlowsForArpCheckTable(dpId);
118 private void createArpDefaultFlowsForArpCheckTable(BigInteger dpId) {
119 jobCoordinator.enqueueJob("ARP_CHECK_TABLE-" + dpId.toString(),
120 () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
121 LOG.debug("Received notification to install Arp Check Default entries for dpn {} ", dpId);
122 createArpRequestMatchFlows(dpId, tx);
123 createArpResponseMatchFlows(dpId, tx);
127 public void createTableMissEntry(BigInteger dpnId) {
128 setupTableMissSmacFlow(dpnId);
129 setupTableMissDmacFlow(dpnId);
130 setupTableMissArpCheckFlow(dpnId);
131 setupTableMissApResponderFlow(dpnId);
132 setupExternalL2vniTableMissFlow(dpnId);
135 private void createMulticastFlows(BigInteger dpId) {
136 createL2ControlProtocolDropFlows(dpId);
137 createMulticastPuntFlows(dpId);
140 private void createMulticastPuntFlows(BigInteger dpId) {
141 if (puntLldpToController) {
142 createLldpFlows(dpId);
146 private void createLldpFlows(BigInteger dpId) {
147 createLldpFlow(dpId, ElanConstants.LLDP_DST_1, "LLDP dMac Table Flow 1");
148 createLldpFlow(dpId, ElanConstants.LLDP_DST_2, "LLDP dMac Table Flow 2");
149 createLldpFlow(dpId, ElanConstants.LLDP_DST_3, "LLDP dMac Table Flow 3");
152 private void createLldpFlow(BigInteger dpId, String dstMac, String flowName) {
153 List<MatchInfo> mkMatches = new ArrayList<>();
154 mkMatches.add(new MatchEthernetType(ElanConstants.LLDP_ETH_TYPE));
155 mkMatches.add(new MatchEthernetDestination(new MacAddress(dstMac)));
157 List<ActionInfo> listActionInfo = new ArrayList<>();
158 listActionInfo.add(new ActionPuntToController());
160 List<InstructionInfo> mkInstructions = new ArrayList<>();
161 mkInstructions.add(new InstructionApplyActions(listActionInfo));
163 String flowId = dpId.toString() + NwConstants.ELAN_DMAC_TABLE + "lldp" + ElanConstants.LLDP_ETH_TYPE + dstMac;
164 FlowEntity lldpFlow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 16, flowName, 0, 0,
165 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
167 mdsalManager.installFlow(lldpFlow);
170 private void setupTableMissSmacFlow(BigInteger dpId) {
171 List<ActionInfo> actionsInfos = new ArrayList<>();
172 actionsInfos.add(new ActionPuntToController());
173 actionsInfos.add(new ActionLearn(0, tempSmacLearnTimeout, 0, ElanConstants.COOKIE_ELAN_LEARNED_SMAC, 0,
174 NwConstants.ELAN_SMAC_LEARNED_TABLE, 0, 0,
176 new ActionLearn.MatchFromField(NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getType(),
177 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getType(),
178 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getFlowModHeaderLenInt()),
179 new ActionLearn.MatchFromField(NwConstants.NxmOfFieldType.NXM_NX_REG1.getType(),
180 NwConstants.NxmOfFieldType.NXM_NX_REG1.getType(), ElanConstants.INTERFACE_TAG_LENGTH),
181 new ActionLearn.CopyFromValue(LEARN_MATCH_REG4_VALUE,
182 NwConstants.NxmOfFieldType.NXM_NX_REG4.getType(), 8))));
184 List<InstructionInfo> mkInstructions = new ArrayList<>();
185 mkInstructions.add(new InstructionApplyActions(actionsInfos));
186 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
188 List<MatchInfo> mkMatches = new ArrayList<>();
189 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
190 getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
191 ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
192 mdsalManager.installFlow(flowEntity);
194 addSmacBaseTableFlow(dpId);
195 addSmacLearnedTableFlow(dpId);
198 private void addSmacBaseTableFlow(BigInteger dpId) {
199 // T48 - resubmit to T49 & T50
200 List<ActionInfo> actionsInfo = new ArrayList<>();
201 actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_LEARNED_TABLE));
202 actionsInfo.add(new ActionNxResubmit(NwConstants.ELAN_SMAC_TABLE));
203 List<InstructionInfo> mkInstruct = new ArrayList<>();
204 mkInstruct.add(new InstructionApplyActions(actionsInfo));
205 List<MatchInfo> mkMatch = new ArrayList<>();
206 FlowEntity doubleResubmitTable = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_BASE_TABLE,
207 getTableMissFlowRef(NwConstants.ELAN_BASE_TABLE), 0, "Elan sMac resubmit table", 0, 0,
208 ElanConstants.COOKIE_ELAN_BASE_SMAC, mkMatch, mkInstruct);
209 mdsalManager.installFlow(doubleResubmitTable);
212 private void addSmacLearnedTableFlow(BigInteger dpId) {
213 // T50 - match on Reg4 and goto T51
214 List<MatchInfoBase> mkMatches = new ArrayList<>();
215 mkMatches.add(new NxMatchRegister(NxmNxReg4.class, LEARN_MATCH_REG4_VALUE));
216 List<InstructionInfo> mkInstructions = new ArrayList<>();
217 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
218 String flowRef = new StringBuffer().append(NwConstants.ELAN_SMAC_TABLE).append(NwConstants.FLOWID_SEPARATOR)
219 .append(LEARN_MATCH_REG4_VALUE).toString();
220 FlowEntity flowEntity =
221 MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE, flowRef, 10, "ELAN sMac Table Reg4 Flow",
222 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(BigInteger.valueOf(LEARN_MATCH_REG4_VALUE)),
223 mkMatches, mkInstructions);
224 mdsalManager.installFlow(flowEntity);
227 private void setupTableMissDmacFlow(BigInteger dpId) {
228 List<MatchInfo> mkMatches = new ArrayList<>();
230 List<InstructionInfo> mkInstructions = new ArrayList<>();
231 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_UNKNOWN_DMAC_TABLE));
233 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
234 getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
235 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
237 mdsalManager.installFlow(flowEntity);
240 private void setupExternalL2vniTableMissFlow(BigInteger dpnId) {
241 List<MatchInfo> matches = new ArrayList<>();
242 List<ActionInfo> actionsInfos = Collections.singletonList(new ActionNxResubmit(NwConstants
243 .LPORT_DISPATCHER_TABLE));
244 List<InstructionInfo> instructions = Collections.singletonList(new InstructionApplyActions(actionsInfos));
245 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L2VNI_EXTERNAL_TUNNEL_DEMUX_TABLE,
246 getTableMissFlowRef(NwConstants.L2VNI_EXTERNAL_TUNNEL_DEMUX_TABLE), 0,
247 "External L2VNI Table Miss Flow", 0, 0,
248 ElanConstants.COOKIE_L2VNI_DEMUX, matches, instructions);
249 mdsalManager.installFlow(flowEntity);
253 private void createL2ControlProtocolDropFlows(BigInteger dpId) {
254 List<MatchInfo> mkMatches = new ArrayList<>();
255 MatchEthernetDestination matchEthDst =
256 new MatchEthernetDestination(new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC),
257 new MacAddress(ElanConstants.L2_CONTROL_PACKETS_DMAC_MASK));
259 mkMatches.add(matchEthDst);
261 List<ActionInfo> listActionInfo = new ArrayList<>();
262 listActionInfo.add(new ActionDrop());
264 List<InstructionInfo> mkInstructions = new ArrayList<>();
265 mkInstructions.add(new InstructionApplyActions(listActionInfo));
267 String flowId = dpId.toString() + NwConstants.ELAN_DMAC_TABLE + "l2control"
268 + ElanConstants.L2_CONTROL_PACKETS_DMAC + ElanConstants.L2_CONTROL_PACKETS_DMAC_MASK;
269 FlowEntity flow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 15,
270 "L2 control packets dMac Table Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches,
273 mdsalManager.installFlow(flow);
276 private String getTableMissFlowRef(long tableId) {
277 return new StringBuffer().append(tableId).toString();
281 protected ElanNodeListener getDataTreeChangeListener() {
282 return ElanNodeListener.this;
285 private void setupTableMissApResponderFlow(final BigInteger dpnId) {
286 mdsalManager.installFlow(dpnId, ArpResponderUtil.getArpResponderTableMissFlow(dpnId));
289 private void setupTableMissArpCheckFlow(BigInteger dpnId) {
290 mdsalManager.installFlow(dpnId,
291 MDSALUtil.buildFlowEntity(dpnId, NwConstants.ARP_CHECK_TABLE,
292 String.valueOf("L2.ELAN." + NwConstants.ARP_CHECK_TABLE), NwConstants.TABLE_MISS_PRIORITY,
293 ArpResponderConstant.DROP_FLOW_NAME.value(), 0, 0, NwConstants.COOKIE_ARP_RESPONDER,
294 new ArrayList<MatchInfo>(),
295 Collections.singletonList(new InstructionGotoTable(NwConstants.ELAN_BASE_TABLE))));
298 private void createArpRequestMatchFlows(BigInteger dpId, WriteTransaction writeFlowTx) {
300 long arpRequestGroupId = ArpResponderUtil.retrieveStandardArpResponderGroupId(idManagerService);
301 List<BucketInfo> buckets = ArpResponderUtil.getDefaultBucketInfos(NwConstants.ELAN_BASE_TABLE,
302 NwConstants.ARP_RESPONDER_TABLE);
303 ArpResponderUtil.installGroup(mdsalManager, dpId, arpRequestGroupId,
304 ArpResponderConstant.GROUP_FLOW_NAME.value(), buckets);
306 FlowEntity arpReqArpCheckTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.ARP_CHECK_TABLE,
307 NwConstants.ARP_REQUEST, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REQUEST),
308 () -> Collections.singletonList(new ActionGroup(arpRequestGroupId)));
309 LOG.trace("Invoking MDSAL to install Arp Rquest Match Flow for table {}", NwConstants.ARP_CHECK_TABLE);
310 mdsalManager.addFlowToTx(arpReqArpCheckTbl, writeFlowTx);
314 private void createArpResponseMatchFlows(BigInteger dpId, WriteTransaction writeFlowTx) {
315 FlowEntity arpRepArpCheckTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.ARP_CHECK_TABLE,
316 NwConstants.ARP_REPLY, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REPLY),
317 () -> Arrays.asList(new ActionPuntToController(), new ActionNxResubmit(NwConstants.ELAN_BASE_TABLE)));
318 LOG.trace("Invoking MDSAL to install Arp Reply Match Flow for Table {} ", NwConstants.ARP_CHECK_TABLE);
319 mdsalManager.addFlowToTx(arpRepArpCheckTbl, writeFlowTx);