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 Subnet natIpSubnet = resolveSubnetForIpv4Address(ctx.getTenant(natL3Ep.getTenant()),
97 Preconditions.checkNotNull(natIpAddress.getIpv4Address(),
98 "Endpoint {} does not have IPv4 address in NatAddress augmentation.", natL3Ep.getKey()));
99 if (natIpSubnet != null && natIpSubnet.getParent() != null) {
100 L2FloodDomain natEpl2Fd = ctx.getTenant(natL3Ep.getTenant()).resolveL2FloodDomain(
101 natIpSubnet.getParent());
102 if (natEpl2Fd.getAugmentation(Segmentation.class) != null) {
103 Integer vlanId = natEpl2Fd.getAugmentation(Segmentation.class).getSegmentationId();
104 ofWriter.writeFlow(nodeId, TABLE_ID,
105 buildPushVlanFlow(natIpAddress.getIpv4Address(), vlanId, 222));
112 * Tagging should be also considered when traffic is routed or switched to external domain.
114 * If the L2FloodDomain of Endpoint contains Segmentation augmentation, a Flow
115 * for applying VLAN tag is generated. The flow matches against REG5 holding
116 * the L2FloodDomain and REG7 holding value of an external interface.
118 for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
119 L2FloodDomain l2Fd = ctx.getTenant(ep.getTenant()).resolveL2FloodDomain(ep.getNetworkContainment());
120 Segmentation segmentation = l2Fd.getAugmentation(Segmentation.class);
121 if (segmentation == null) {
124 Integer vlanId = segmentation.getSegmentationId();
125 for (Flow flow : buildPushVlanFlow(nodeId, OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, ep).getFdId(),
127 ofWriter.writeFlow(nodeId, TABLE_ID, flow);
132 * Default Egress flow. Other methods may write to this table to augment egress
133 * functionality, such as bypassing/utilising the NAT table, or ServiceFunctionChaining
135 ofWriter.writeFlow(nodeId, TABLE_ID, defaultFlow());
139 * Generates a {@link Flow} for tagging VLAN traffic based on given arguments.
141 * @param ipv4Address source IPv4 address
142 * @param vlanId ID of VLAN tag to apply
143 * @param priority priority of the flow in the table
144 * @return {@link Flow} matching IPv4 source address, IPv4 ether-type and VLAN not set.
146 private Flow buildPushVlanFlow(Ipv4Address ipv4Address, Integer vlanId, int priority) {
147 // It is not needed here to match against external interfaces because
148 // we only use NAT when going to external networks.
149 Ipv4Prefix natIp = new Ipv4Prefix(ipv4Address.getValue() + "/32");
150 Match match = new MatchBuilder()
151 .setEthernetMatch(FlowUtils.ethernetMatch(null, null, Long.valueOf(FlowUtils.IPv4)))
152 .setLayer3Match(new Ipv4MatchBuilder().setIpv4Source(natIp).build())
153 .setVlanMatch(FlowUtils.vlanMatch(0, false))
155 List<ActionBuilder> pushVlanActions = new ArrayList<>();
156 pushVlanActions.addAll(FlowUtils.pushVlanActions(vlanId));
157 pushVlanActions.add(new ActionBuilder().setOrder(0).setAction(nxOutputRegAction(NxmNxReg7.class)));
158 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "external_nat_push_vlan", match);
159 return base().setPriority(priority)
162 .setInstructions(FlowUtils.instructions(applyActionIns(pushVlanActions)))
166 public static Subnet resolveSubnetForIpv4Address(IndexedTenant t, Ipv4Address ipv4Addr) {
167 Preconditions.checkNotNull(ipv4Addr);
168 if (t == null || t.getTenant() == null || t.getTenant().getForwardingContext() == null) {
171 List<Subnet> subnets = t.getTenant().getForwardingContext().getSubnet();
172 if (subnets != null) {
173 for (Subnet subnet : subnets) {
174 if (belongsToSubnet(ipv4Addr, subnet.getIpPrefix().getIpv4Prefix())) {
182 private static boolean belongsToSubnet(Ipv4Address ipv4Address, Ipv4Prefix subnetPrefix) {
183 SubnetUtils su = new SubnetUtils(subnetPrefix.getValue());
184 SubnetInfo si = su.getInfo();
185 return si.isInRange(ipv4Address.getValue());
189 * Generates a {@link Flow} for tagging VLAN traffic based on given arguments.
191 * @param nodeId of {@link Node} from which external interfaces are resolved
192 * @param fdId {@link L2FloodDomain} ordinal to match against
193 * @param vlanId applied to the traffic
194 * @param priority of flow in the table
195 * @return {@link List} of {@link Flow} matching {@link L2FloodDomain} in REG5,
196 * external interfaces of {@link Node} in REG7 and VLAN not set.
198 private List<Flow> buildPushVlanFlow(NodeId nodeId, int fdId, Integer vlanId, int priority) {
199 List<Flow> flows = new ArrayList<>();
200 for (Long portNum : ctx.getSwitchManager().getExternalPortNumbers(nodeId)) {
201 MatchBuilder mb = new MatchBuilder().setVlanMatch(FlowUtils.vlanMatch(0, false));
202 addNxRegMatch(mb, RegMatch.of(NxmNxReg7.class, BigInteger.valueOf(portNum).longValue()),
203 RegMatch.of(NxmNxReg5.class, Long.valueOf(fdId)));
204 Match match = mb.build();
205 List<ActionBuilder> pushVlanActions = new ArrayList<>();
206 pushVlanActions.addAll(FlowUtils.pushVlanActions(vlanId));
207 pushVlanActions.add(new ActionBuilder().setOrder(0).setAction(nxOutputRegAction(NxmNxReg7.class)));
208 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "external_push_vlan", match);
209 flows.add(base().setPriority(priority)
212 .setInstructions(FlowUtils.instructions(applyActionIns(pushVlanActions)))
218 private Flow defaultFlow() {
219 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "defaultExternalFlow", null);
220 Flow flow = base().setPriority(100)
222 .setInstructions(instructions(applyActionIns(nxOutputRegAction(NxmNxReg7.class))))