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.addNxRegMatch;
12 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;
13 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
14 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxOutputRegAction;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
21 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
22 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
23 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.RegMatch;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.Segmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;
43 import org.apache.commons.net.util.SubnetUtils;
44 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import com.google.common.base.Preconditions;
51 * Manage the table that assigns source endpoint group, bridge domain, and
52 * router domain to registers to be used by other tables.
54 public class ExternalMapper extends FlowTable {
56 protected static final Logger LOG = LoggerFactory.getLogger(ExternalMapper.class);
58 public static short TABLE_ID;
60 public ExternalMapper(OfContext ctx, short tableId) {
66 public short getTableId() {
71 public void sync(NodeId nodeId, OfWriter ofWriter) throws Exception {
74 ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
77 * When source address was translated to NAT address, it has to be figured out
78 * whether or not the traffic should be tagged since external interfaces are
79 * considered as trunk ports.
81 * Subnet to which NAT address belong have to be found so that
82 * corresponding L2FloodDomain can be resolved.
84 * If the L2FloodDomain contains Segmentation augmentation, a Flow is generated
85 * for applying VLAN tag against traffic with NAT IP as source address.
87 * Note: NetworkContainment of NAT EndpointL3 point's to subnet of original address.
88 * This is why subnet of NAT IP is resolved here.
90 Collection<EndpointL3> natL3Endpoints = ctx.getEndpointManager().getL3EndpointsWithNat();
91 if (natL3Endpoints != null) {
92 for (EndpointL3 natL3Ep : natL3Endpoints) {
93 IpAddress natIpAddress = Preconditions.checkNotNull(natL3Ep.getAugmentation(NatAddress.class),
94 "NAT address augmentation is missing for NAT endpoint: [{}].", natL3Ep.getKey())
96 L2FloodDomain natEpl2Fd = resolveL2FloodDomainForIpv4Address(ctx.getTenant(natL3Ep.getTenant()),
97 Preconditions.checkNotNull(natIpAddress.getIpv4Address(),
98 "Endpoint {} does not have IPv4 address in NatAddress augmentation.", natL3Ep.getKey()));
99 if (natEpl2Fd != null && natEpl2Fd.getAugmentation(Segmentation.class) != null) {
100 Integer vlanId = natEpl2Fd.getAugmentation(Segmentation.class).getSegmentationId();
101 ofWriter.writeFlow(nodeId, TABLE_ID, buildPushVlanFlow(natIpAddress.getIpv4Address(), vlanId, 222));
107 * Tagging should be also considered when traffic is routed or switched to external domain.
109 * If the L2FloodDomain of Endpoint contains Segmentation augmentation, a Flow
110 * for applying VLAN tag is generated. The flow matches against REG5 holding
111 * the L2FloodDomain and REG7 holding value of an external interface.
113 for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
114 L2FloodDomain l2Fd = ctx.getTenant(ep.getTenant()).resolveL2FloodDomain(ep.getNetworkContainment());
115 Segmentation segmentation = l2Fd.getAugmentation(Segmentation.class);
116 if (segmentation == null) {
119 Integer vlanId = segmentation.getSegmentationId();
120 for (Flow flow : buildPushVlanFlow(nodeId, OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, ep).getFdId(),
122 ofWriter.writeFlow(nodeId, TABLE_ID, flow);
127 * Default Egress flow. Other methods may write to this table to augment egress
128 * functionality, such as bypassing/utilising the NAT table, or ServiceFunctionChaining
130 ofWriter.writeFlow(nodeId, TABLE_ID, defaultFlow());
133 static L2FloodDomain resolveL2FloodDomainForIpv4Address(IndexedTenant t, Ipv4Address ipv4Addr) {
134 Preconditions.checkNotNull(ipv4Addr);
135 if (t == null || t.getTenant() == null || t.getTenant().getForwardingContext() == null) {
138 List<Subnet> subnets = t.getTenant().getForwardingContext().getSubnet();
139 if (subnets != null) {
140 for (Subnet subnet : subnets) {
141 if (belongsToSubnet(ipv4Addr, subnet.getIpPrefix().getIpv4Prefix())) {
142 return t.resolveL2FloodDomain(subnet.getParent());
147 "No subnet for IPv4 address {} found in tenant {}!",
148 ipv4Addr.getValue(), t.getTenant().getId());
152 static boolean belongsToSubnet(Ipv4Address ipv4Address, Ipv4Prefix subnetPrefix) {
153 SubnetUtils su = new SubnetUtils(subnetPrefix.getValue());
154 SubnetInfo si = su.getInfo();
155 return si.isInRange(ipv4Address.getValue());
159 * Generates a {@link Flow} for tagging VLAN traffic based on given arguments.
161 * @param ipv4Address source IPv4 address
162 * @param vlanId ID of VLAN tag to apply
163 * @param priority priority of the flow in the table
164 * @return {@link Flow} matching IPv4 source address, IPv4 ether-type and VLAN not set.
166 private Flow buildPushVlanFlow(Ipv4Address ipv4Address, Integer vlanId, int priority) {
167 // It is not needed here to match against external interfaces because
168 // we only use NAT when going to external networks.
169 Ipv4Prefix natIp = new Ipv4Prefix(ipv4Address.getValue() + "/32");
170 Match match = new MatchBuilder()
171 .setEthernetMatch(FlowUtils.ethernetMatch(null, null, Long.valueOf(FlowUtils.IPv4)))
172 .setLayer3Match(new Ipv4MatchBuilder().setIpv4Source(natIp).build())
173 .setVlanMatch(FlowUtils.vlanMatch(0, false))
175 List<ActionBuilder> pushVlanActions = new ArrayList<>();
176 pushVlanActions.addAll(FlowUtils.pushVlanActions(vlanId));
177 pushVlanActions.add(new ActionBuilder().setOrder(0).setAction(nxOutputRegAction(NxmNxReg7.class)));
178 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "external_nat_push_vlan", match);
179 return base().setPriority(priority)
182 .setInstructions(FlowUtils.instructions(applyActionIns(pushVlanActions)))
187 * Generates a {@link Flow} for tagging VLAN traffic based on given arguments.
189 * @param nodeId of {@link Node} from which external interfaces are resolved
190 * @param fdId {@link L2FloodDomain} ordinal to match against
191 * @param vlanId applied to the traffic
192 * @param priority of flow in the table
193 * @return {@link List} of {@link Flow} matching {@link L2FloodDomain} in REG5,
194 * external interfaces of {@link Node} in REG7 and VLAN not set.
196 private List<Flow> buildPushVlanFlow(NodeId nodeId, int fdId, Integer vlanId, int priority) {
197 List<Flow> flows = new ArrayList<>();
198 for (Long portNum : ctx.getSwitchManager().getExternalPortNumbers(nodeId)) {
199 MatchBuilder mb = new MatchBuilder().setVlanMatch(FlowUtils.vlanMatch(0, false));
200 addNxRegMatch(mb, RegMatch.of(NxmNxReg7.class, BigInteger.valueOf(portNum).longValue()),
201 RegMatch.of(NxmNxReg5.class, Long.valueOf(fdId)));
202 Match match = mb.build();
203 List<ActionBuilder> pushVlanActions = new ArrayList<>();
204 pushVlanActions.addAll(FlowUtils.pushVlanActions(vlanId));
205 pushVlanActions.add(new ActionBuilder().setOrder(0).setAction(nxOutputRegAction(NxmNxReg7.class)));
206 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "external_push_vlan", match);
207 flows.add(base().setPriority(priority)
210 .setInstructions(FlowUtils.instructions(applyActionIns(pushVlanActions)))
216 private Flow defaultFlow() {
217 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "defaultExternalFlow", null);
218 Flow flow = base().setPriority(100)
220 .setInstructions(instructions(applyActionIns(nxOutputRegAction(NxmNxReg7.class))))