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;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
27 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
28 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
29 import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
30 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.google.common.collect.Sets;
57 * Manage the table that assigns source endpoint group, bridge domain, and
58 * router domain to registers to be used by other tables.
60 public class SourceMapper extends FlowTable {
62 protected static final Logger LOG = LoggerFactory.getLogger(SourceMapper.class);
64 // TODO Li alagalah Improve UT coverage for this class.
65 public static short TABLE_ID;
67 public SourceMapper(OfContext ctx, short tableId) {
73 public short getTableId() {
78 public void sync(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap) throws Exception {
80 flowMap.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
82 // Handle case where packets from from External
83 Map<EndpointKey, EndpointL3> l3EpWithNatByL2Key = ctx.getEndpointManager().getL3EpWithNatByL2Key();
84 for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
85 OfOverlayContext ofc = ep.getAugmentation(OfOverlayContext.class);
87 IndexedTenant tenant = ctx.getPolicyResolver().getTenant(ep.getTenant());
91 EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, ep);
92 EgKey sepg = new EgKey(ep.getTenant(), ep.getEndpointGroup());
94 createRemoteTunnels(flowMap, nodeId, ep, policyInfo, epFwdCtxOrds);
96 if (ep.getTenant() == null || (ep.getEndpointGroup() == null && ep.getEndpointGroups() == null)) {
100 if (ofc != null && ofc.getNodeConnectorId() != null
101 && (ofc.getLocationType() == null || LocationType.Internal.equals(ofc.getLocationType()))) {
103 * Sync the local EP information.
105 syncEP(flowMap, nodeId, ep, ofc.getNodeConnectorId(), epFwdCtxOrds);
108 // if (l3EpWithNatByL2Key.containsKey(ep.getKey())) {
109 // Set<NodeConnectorId> external = ctx.getSwitchManager().getExternalPorts(nodeId);
110 // for (NodeConnectorId ncId : external) {
111 // // TODO Bug 3546 - Difficult: External port is unrelated to Tenant, L3C, L2BD..
112 // syncEP(flowMap, nodeId, ep, ncId, epFwdCtxOrds);
118 private void createRemoteTunnels(FlowMap flowMap, NodeId nodeId, Endpoint ep, PolicyInfo policyInfo,
119 EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
120 Set<EgKey> epgs = new HashSet<>();
122 // Get EPGs and add to Set to remove duplicates
123 // TODO alagalah Li: test EndpointManager.getEgKeys
124 if (ep.getEndpointGroup() != null) {
125 epgs.add(new EgKey(ep.getTenant(), ep.getEndpointGroup()));
127 if (ep.getEndpointGroups() != null) {
128 for (EndpointGroupId epgId : ep.getEndpointGroups()) {
129 epgs.add(new EgKey(ep.getTenant(), epgId));
133 // Create tunnels on remote Nodes that may talk to us.
134 for (EgKey epg : epgs) {
135 Set<EgKey> peers = Sets.union(Collections.singleton(epg), policyInfo.getPeers(epg));
136 for (EgKey peer : peers) {
137 for (NodeId remoteNodeId : ctx.getEndpointManager().getNodesForGroup(peer)) {
139 // Please do not check for remote v local nodeID, we need local to local tunnels
140 // in the case of chaining - The Great Dr Sunal.
141 NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(remoteNodeId, TunnelTypeVxlan.class);
142 if (tunPort == null) {
143 LOG.trace("No tunnel port for tunnel in SourceMapper between local:{} and remote:{}",
144 nodeId.getValue(), remoteNodeId.getValue());
147 flowMap.writeFlow(remoteNodeId, TABLE_ID, createTunnelFlow(tunPort, epFwdCtxOrds));
148 flowMap.writeFlow(remoteNodeId, TABLE_ID, createBroadcastFlow(tunPort, epFwdCtxOrds));
154 private Flow createBroadcastFlow(NodeConnectorId tunPort, EndpointFwdCtxOrdinals epFwdCtxOrds) {
156 int fdId = epFwdCtxOrds.getFdId();
158 MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
159 addNxTunIdMatch(mb, fdId);
161 // set condition group register to all ones to
163 // policy enforcement
165 * TODO: This breaks distributed policy enforcement
166 * especially wrt multi-action. BAD. Must be addressed
167 * (this is why we can't have nice things).
168 * This can be fixed with new tunnelId ordinal in
172 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
174 Match match = mb.build();
175 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "tunnelFdId", match);
176 FlowBuilder flowb = base().setId(flowid)
177 .setPriority(Integer.valueOf(150))
179 .setInstructions(instructions(applyActionIns(fdReg), gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
180 return flowb.build();
183 private Flow createTunnelFlow(NodeConnectorId tunPort, EndpointFwdCtxOrdinals epFwdCtxOrds) {
184 // ... this is a remote node.
186 int egId = epFwdCtxOrds.getEpgId();
187 int bdId = epFwdCtxOrds.getBdId();
188 int fdId = epFwdCtxOrds.getFdId();
189 int l3Id = epFwdCtxOrds.getL3Id();
190 int tunnelId = epFwdCtxOrds.getTunnelId();
192 MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
193 addNxTunIdMatch(mb, tunnelId);
195 Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
196 // set condition group register to all ones to
198 // policy enforcement
200 * TODO: This breaks distributed policy enforcement
201 * especially wrt multi-action. BAD. Must be addressed
202 * (this is why we can't have nice things).
203 * This can be fixed with new tunnelId ordinal in
206 Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0xffffff));
207 Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
208 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
209 Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
210 Match match = mb.build();
211 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "tunnel", match);
212 FlowBuilder flowb = base().setId(flowid)
213 .setPriority(Integer.valueOf(150))
216 instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg),
217 gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
218 return flowb.build();
221 private void syncEP(FlowMap flowMap, NodeId nodeId, Endpoint ep, NodeConnectorId ncId, EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
223 // TODO alagalah Li/Be: We should also match on EndpointL3 with the appropriate
224 // network containment. This would solve a lot of problems and prepare for EndpointL3 RPC.
226 int egId = epFwdCtxOrds.getEpgId();
227 int bdId = epFwdCtxOrds.getBdId();
228 int fdId = epFwdCtxOrds.getFdId();
229 int l3Id = epFwdCtxOrds.getL3Id();
230 int cgId = epFwdCtxOrds.getCgId();
231 int tunnelId = epFwdCtxOrds.getTunnelId();
233 Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
234 Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
235 Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
236 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
237 Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
238 Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
240 Match match = new MatchBuilder().setEthernetMatch(ethernetMatch(ep.getMacAddress(), null, null))
243 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "ep", match);
244 FlowBuilder flowb = base().setPriority(Integer.valueOf(100))
248 instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg,tunIdAction),
249 gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
250 flowMap.writeFlow(nodeId, TABLE_ID, flowb.build());