2 * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
11 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.addNxTunIdMatch;
12 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;
13 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.ethernetMatch;
14 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.gotoTableIns;
15 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
16 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadRegAction;
17 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadTunIdAction;
19 import java.math.BigInteger;
20 import java.util.Collections;
21 import java.util.HashSet;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
27 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
28 import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
29 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.google.common.collect.Sets;
54 * Manage the table that assigns source endpoint group, bridge domain, and
55 * router domain to registers to be used by other tables.
57 public class SourceMapper extends FlowTable {
59 protected static final Logger LOG = LoggerFactory.getLogger(SourceMapper.class);
61 // TODO Li alagalah Improve UT coverage for this class.
62 public static short TABLE_ID;
64 public SourceMapper(OfContext ctx, short tableId) {
70 public short getTableId() {
75 public void sync(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
77 ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
79 // Handle case where packets from from External
80 for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
81 OfOverlayContext ofc = ep.getAugmentation(OfOverlayContext.class);
83 IndexedTenant tenant = ctx.getPolicyResolver().getTenant(ep.getTenant());
87 EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, ep);
88 if (epFwdCtxOrds == null) {
89 LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", ep);
93 createRemoteTunnels(ofWriter, nodeId, ep, policyInfo, epFwdCtxOrds);
95 if (ep.getTenant() == null || (ep.getEndpointGroup() == null && ep.getEndpointGroups() == null)) {
99 if (ofc != null && ofc.getNodeConnectorId() != null
100 && (ofc.getLocationType() == null || LocationType.Internal.equals(ofc.getLocationType()))) {
102 * Sync the local EP information.
104 syncEP(ofWriter, nodeId, ep, ofc.getNodeConnectorId(), epFwdCtxOrds);
109 private void createRemoteTunnels(OfWriter ofWriter, NodeId nodeId, Endpoint ep, PolicyInfo policyInfo,
110 EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
111 Set<EgKey> epgs = new HashSet<>();
113 // Get EPGs and add to Set to remove duplicates
114 // TODO alagalah Li: test EndpointManager.getEgKeys
115 if (ep.getEndpointGroup() != null) {
116 epgs.add(new EgKey(ep.getTenant(), ep.getEndpointGroup()));
118 if (ep.getEndpointGroups() != null) {
119 for (EndpointGroupId epgId : ep.getEndpointGroups()) {
120 epgs.add(new EgKey(ep.getTenant(), epgId));
124 // Create tunnels on remote Nodes that may talk to us.
125 for (EgKey epg : epgs) {
126 Set<EgKey> peers = Sets.union(Collections.singleton(epg), policyInfo.getPeers(epg));
127 for (EgKey peer : peers) {
128 for (NodeId remoteNodeId : ctx.getEndpointManager().getNodesForGroup(peer)) {
130 // Please do not check for remote v local nodeID, we need local to local tunnels
131 // in the case of chaining - The Great Dr Sunal.
132 NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(remoteNodeId, TunnelTypeVxlan.class);
133 if (tunPort == null) {
134 LOG.trace("No tunnel port for tunnel in SourceMapper between local:{} and remote:{}",
135 nodeId.getValue(), remoteNodeId.getValue());
138 ofWriter.writeFlow(remoteNodeId, TABLE_ID, createTunnelFlow(tunPort, epFwdCtxOrds));
139 ofWriter.writeFlow(remoteNodeId, TABLE_ID, createBroadcastFlow(tunPort, epFwdCtxOrds));
145 private Flow createBroadcastFlow(NodeConnectorId tunPort, EndpointFwdCtxOrdinals epFwdCtxOrds) {
147 int fdId = epFwdCtxOrds.getFdId();
149 MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
150 addNxTunIdMatch(mb, fdId);
152 // set condition group register to all ones to
154 // policy enforcement
156 * TODO: This breaks distributed policy enforcement
157 * especially wrt multi-action. BAD. Must be addressed
158 * (this is why we can't have nice things).
159 * This can be fixed with new tunnelId ordinal in
163 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
165 Match match = mb.build();
166 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "tunnelFdId", match);
167 FlowBuilder flowb = base().setId(flowid)
168 .setPriority(Integer.valueOf(150))
170 .setInstructions(instructions(applyActionIns(fdReg), gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
171 return flowb.build();
174 private Flow createTunnelFlow(NodeConnectorId tunPort, EndpointFwdCtxOrdinals epFwdCtxOrds) {
175 // ... this is a remote node.
177 int egId = epFwdCtxOrds.getEpgId();
178 int bdId = epFwdCtxOrds.getBdId();
179 int fdId = epFwdCtxOrds.getFdId();
180 int l3Id = epFwdCtxOrds.getL3Id();
181 int tunnelId = epFwdCtxOrds.getTunnelId();
183 MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
184 addNxTunIdMatch(mb, tunnelId);
186 Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
187 // set condition group register to all ones to
189 // policy enforcement
191 * TODO: This breaks distributed policy enforcement
192 * especially wrt multi-action. BAD. Must be addressed
193 * (this is why we can't have nice things).
194 * This can be fixed with new tunnelId ordinal in
197 Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0xffffff));
198 Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
199 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
200 Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
201 Match match = mb.build();
202 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "tunnel", match);
203 FlowBuilder flowb = base().setId(flowid)
204 .setPriority(Integer.valueOf(150))
207 instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg),
208 gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
209 return flowb.build();
212 private void syncEP(OfWriter ofWriter, NodeId nodeId, Endpoint ep, NodeConnectorId ncId, EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
214 // TODO alagalah Li/Be: We should also match on EndpointL3 with the appropriate
215 // network containment. This would solve a lot of problems and prepare for EndpointL3 RPC.
217 int egId = epFwdCtxOrds.getEpgId();
218 int bdId = epFwdCtxOrds.getBdId();
219 int fdId = epFwdCtxOrds.getFdId();
220 int l3Id = epFwdCtxOrds.getL3Id();
221 int cgId = epFwdCtxOrds.getCgId();
222 int tunnelId = epFwdCtxOrds.getTunnelId();
224 Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
225 Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
226 Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
227 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
228 Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
229 Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
231 Match match = new MatchBuilder().setEthernetMatch(ethernetMatch(ep.getMacAddress(), null, null))
234 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "ep", match);
235 FlowBuilder flowb = base().setPriority(Integer.valueOf(100))
239 instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg,tunIdAction),
240 gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
241 ofWriter.writeFlow(nodeId, TABLE_ID, flowb.build());