2925c8d7e75afcb5c06aa420a3e14a596a9f099d
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / netvirt / openstack / netvirt / providers / openflow13 / services / IcmpEchoResponderService.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.services;
9
10 import com.google.common.collect.Lists;
11 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
12 import org.opendaylight.netvirt.openstack.netvirt.api.IcmpEchoProvider;
13 import org.opendaylight.netvirt.openstack.netvirt.api.Status;
14 import org.opendaylight.netvirt.openstack.netvirt.api.StatusCode;
15 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
16 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
17 import org.opendaylight.netvirt.openstack.netvirt.providers.ConfigInterface;
18 import org.opendaylight.netvirt.utils.mdsal.openflow.ActionUtils;
19 import org.opendaylight.netvirt.utils.mdsal.openflow.FlowUtils;
20 import org.opendaylight.netvirt.utils.mdsal.openflow.MatchUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstOfEthDstCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxRegCaseBuilder;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import java.math.BigInteger;
46 import java.net.Inet6Address;
47 import java.net.InetAddress;
48 import java.util.List;
49
50 /**
51  * @author Josh Hershberg (jhershbe@redhat.com)
52  */
53 public class IcmpEchoResponderService extends AbstractServiceInstance implements IcmpEchoProvider, ConfigInterface {
54     private static final Logger LOG = LoggerFactory.getLogger(IcmpEchoResponderService.class);
55     public static final Class<? extends NxmNxReg> SRC_MAC_4_HIGH_BYTES_FIELD = NxmNxReg4.class;
56     public static final Class<? extends NxmNxReg> SRC_MAC_2_LOW_BYTES_FIELD = NxmNxReg5.class;
57
58     public IcmpEchoResponderService() {
59         super(Service.ICMP_ECHO);
60     }
61
62     public IcmpEchoResponderService(Service service) {
63         super(service);
64     }
65
66     @Override
67     public Status programIcmpEchoEntry(Long dpid, String segmentationId, String macAddressStr, InetAddress ipAddress, Action action) {
68
69         if (segmentationId == null) return new Status(StatusCode.BADREQUEST);
70
71         if (ipAddress instanceof Inet6Address) {
72             // WORKAROUND: For now ipv6 is not supported
73             // TODO: implement ipv6 case
74             LOG.debug("ipv6 address case is not implemented yet. dpid {} segmentationId {} macAddressStr, ipAddress {} action {}",
75                     dpid, segmentationId, macAddressStr, ipAddress, action);
76             return new Status(StatusCode.NOTIMPLEMENTED);
77         }
78
79         MacAddress macAddress = new MacAddress(macAddressStr);
80
81         programEntry(dpid, segmentationId, macAddress, ipAddress, true, action);
82         programEntry(dpid, segmentationId, macAddress, ipAddress, false, action);
83
84         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
85         return new Status(StatusCode.SUCCESS);
86     }
87
88     private Status programEntry(long dpid, String segmentationId, MacAddress macAddress, InetAddress ipAddress, boolean isRouted, Action action) {
89         FlowBuilder flowBuilder = new FlowBuilder();
90         String flowName = (isRouted ? "RoutedIcmpEchoResponder_" : "LanIcmpEchoResponder_")
91                 + segmentationId + "_" + ipAddress.getHostAddress();
92
93         //The non-routed flow has an extra match condition and it must therefor be of a higher
94         //prio to make sure we get a "best match" kind of logic between the two
95         FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(isRouted ? 2048 : 2049);
96
97         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
98
99         MatchBuilder matchBuilder = new MatchBuilder();
100
101         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
102
103         // Match ICMP echo requests, type=8, code=0
104         MatchUtils.createICMPv4Match(matchBuilder, (short) 8, (short) 0);
105         MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
106
107         if (!isRouted) {
108             //packets that have been "routed" in table 60 (DVR) will have their src MAC in nxm_nx_reg4 and nxm_nx_reg5
109             //here we check that nxm_nx_reg4 is empty to check whether the packet has *not* been router since its
110             //destination is on the same LAN
111             MatchUtils.addNxRegMatch(matchBuilder, new MatchUtils.RegMatch(SRC_MAC_4_HIGH_BYTES_FIELD, 0x0L));
112         }
113
114         flowBuilder.setMatch(matchBuilder.build());
115
116         if (action.equals(Action.ADD)) {
117             // Instructions List Stores Individual Instructions
118             InstructionsBuilder isb = new InstructionsBuilder();
119             List<Instruction> instructions = Lists.newArrayList();
120             InstructionBuilder ib = new InstructionBuilder();
121             ApplyActionsBuilder aab = new ApplyActionsBuilder();
122             ActionBuilder ab = new ActionBuilder();
123             List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = Lists.newArrayList();
124
125             if (isRouted) {
126                 ab.setAction(
127                             ActionUtils.nxMoveRegAction(
128                             new SrcNxRegCaseBuilder().setNxReg(SRC_MAC_4_HIGH_BYTES_FIELD).build(),
129                             new DstOfEthDstCaseBuilder().setOfEthDst(true).build(),
130                             0, 0, 31, false));
131                 ab.setOrder(actionList.size());
132                 ab.setKey(new ActionKey(actionList.size()));
133                 actionList.add(ab.build());
134
135                 ab.setAction(
136                             ActionUtils.nxMoveRegAction(
137                             new SrcNxRegCaseBuilder().setNxReg(SRC_MAC_2_LOW_BYTES_FIELD).build(),
138                             new DstOfEthDstCaseBuilder().setOfEthDst(true).build(),
139                             0, 32, 47, false));
140                 ab.setOrder(actionList.size());
141                 ab.setKey(new ActionKey(actionList.size()));
142                 actionList.add(ab.build());
143             } else {
144                 ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
145                 ab.setOrder(actionList.size());
146                 ab.setKey(new ActionKey(actionList.size()));
147                 actionList.add(ab.build());
148             }
149
150             // Set Eth Src
151             ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
152             ab.setOrder(actionList.size());
153             ab.setKey(new ActionKey(actionList.size()));
154             actionList.add(ab.build());
155
156             // Move Ip Src to Ip Dst
157             ab.setAction(ActionUtils.nxMoveIpSrcToIpDstAction());
158             ab.setOrder(actionList.size());
159             ab.setKey(new ActionKey(actionList.size()));
160             actionList.add(ab.build());
161
162             // Set Ip Src
163             ab.setAction(ActionUtils.setNwSrcAction(new Ipv4Builder().setIpv4Address(
164                     MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress())).build()));
165             ab.setOrder(actionList.size());
166             ab.setKey(new ActionKey(actionList.size()));
167             actionList.add(ab.build());
168
169             // Set the ICMP type to 0 (echo reply)
170             ab.setAction(ActionUtils.setIcmpTypeAction((byte)0));
171             ab.setOrder(actionList.size());
172             ab.setKey(new ActionKey(actionList.size()));
173             actionList.add(ab.build());
174
175             // Output of InPort
176             ab.setAction(ActionUtils.outputAction(new NodeConnectorId(
177                                                     nodeBuilder.getId().getValue() + ":INPORT")));
178             ab.setOrder(actionList.size());
179             ab.setKey(new ActionKey(actionList.size()));
180             actionList.add(ab.build());
181
182             // Create Apply Actions Instruction
183             aab.setAction(actionList);
184             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
185             ib.setOrder(0);
186             ib.setKey(new InstructionKey(0));
187             instructions.add(ib.build());
188
189             flowBuilder.setInstructions(isb.setInstruction(instructions).build());
190
191             writeFlow(flowBuilder, nodeBuilder);
192         } else {
193             removeFlow(flowBuilder, nodeBuilder);
194         }
195
196         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
197         return new Status(StatusCode.SUCCESS);
198     }
199
200     @Override
201     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
202         super.setDependencies(bundleContext.getServiceReference(IcmpEchoProvider.class.getName()), this);
203     }
204
205     @Override
206     public void setDependencies(Object impl) {}
207 }