2 * Copyright (C) 2013 Red Hat, Inc.
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
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.mdsalopenflow13;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
15 import org.opendaylight.ovsdb.openstack.netvirt.providers.OF13Provider;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxmNxNspKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import com.google.common.collect.ImmutableList;
70 public class OF13MdSalMatch extends OF13Provider {
71 private static final Logger logger = LoggerFactory.getLogger(OF13MdSalMatch.class);
74 * Create Ingress Port Match dpidLong, inPort
76 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
77 * @param dpidLong Long the datapath ID of a switch/node
78 * @param inPort Long ingress port on a switch
79 * @return matchBuilder Map MatchBuilder Object with a match
81 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
83 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
84 logger.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
85 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
86 matchBuilder.setInPort(ncid);
92 * Create EtherType Match
94 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
95 * @param etherType Long EtherType
96 * @return matchBuilder Map MatchBuilder Object with a match
98 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
100 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
101 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
102 ethTypeBuilder.setType(new EtherType(etherType));
103 ethernetMatch.setEthernetType(ethTypeBuilder.build());
104 matchBuilder.setEthernetMatch(ethernetMatch.build());
110 * Create Ethernet Source Match
112 * @param matchBuilder MatchBuilder Object without a match yet
113 * @param sMacAddr String representing a source MAC
114 * @return matchBuilder Map MatchBuilder Object with a match
116 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
118 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
119 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
120 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
121 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
122 matchBuilder.setEthernetMatch(ethernetMatch.build());
128 * Create Ethernet Destination Match
130 * @param matchBuilder MatchBuilder Object without a match yet
131 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
132 * @return matchBuilder Map MatchBuilder Object with a match
134 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId) {
135 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
136 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
137 vlanIdBuilder.setVlanId(new VlanId(vlanId));
138 vlanIdBuilder.setVlanIdPresent(true);
139 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
140 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
146 * Create Ethernet Destination Match
148 * @param matchBuilder MatchBuilder Object without a match yet
149 * @param dMacAddr String representing a destination MAC
150 * @return matchBuilder Map MatchBuilder Object with a match
152 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
154 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
155 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
156 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
158 ethDestinationBuilder.setMask(mask);
160 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
161 matchBuilder.setEthernetMatch(ethernetMatch.build());
167 * Tunnel ID Match Builder
169 * @param matchBuilder MatchBuilder Object without a match yet
170 * @param tunnelId BigInteger representing a tunnel ID
171 * @return matchBuilder Map MatchBuilder Object with a match
173 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
175 TunnelBuilder tunnelBuilder = new TunnelBuilder();
176 tunnelBuilder.setTunnelId(tunnelId);
177 matchBuilder.setTunnel(tunnelBuilder.build());
183 * Match ICMP code and type
185 * @param matchBuilder MatchBuilder Object without a match yet
186 * @param type short representing an ICMP type
187 * @param code short representing an ICMP code
188 * @return matchBuilder Map MatchBuilder Object with a match
190 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
192 EthernetMatchBuilder eth = new EthernetMatchBuilder();
193 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
194 ethTypeBuilder.setType(new EtherType(0x0800L));
195 eth.setEthernetType(ethTypeBuilder.build());
196 matchBuilder.setEthernetMatch(eth.build());
198 // Build the IPv4 Match requied per OVS Syntax
199 IpMatchBuilder ipmatch = new IpMatchBuilder();
200 ipmatch.setIpProtocol((short) 1);
201 matchBuilder.setIpMatch(ipmatch.build());
203 // Build the ICMPv4 Match
204 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
205 icmpv4match.setIcmpv4Type(type);
206 icmpv4match.setIcmpv4Code(code);
207 matchBuilder.setIcmpv4Match(icmpv4match.build());
213 * @param matchBuilder MatchBuilder Object without a match yet
214 * @param dstip String containing an IPv4 prefix
215 * @return matchBuilder Map Object with a match
217 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
219 EthernetMatchBuilder eth = new EthernetMatchBuilder();
220 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
221 ethTypeBuilder.setType(new EtherType(0x0800L));
222 eth.setEthernetType(ethTypeBuilder.build());
223 matchBuilder.setEthernetMatch(eth.build());
225 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
226 ipv4match.setIpv4Destination(dstip);
228 matchBuilder.setLayer3Match(ipv4match.build());
235 * @param matchBuilder MatchBuilder Object without a match yet
236 * @param srcip String containing an IPv4 prefix
237 * @return matchBuilder Map Object with a match
239 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
241 EthernetMatchBuilder eth = new EthernetMatchBuilder();
242 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
243 ethTypeBuilder.setType(new EtherType(0x0800L));
244 eth.setEthernetType(ethTypeBuilder.build());
245 matchBuilder.setEthernetMatch(eth.build());
247 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
248 ipv4match.setIpv4Source(srcip);
249 matchBuilder.setLayer3Match(ipv4match.build());
256 * Create Source TCP Port Match
258 * @param matchBuilder @param matchbuilder MatchBuilder Object without a match yet
259 * @param tcpport Integer representing a source TCP port
260 * @return matchBuilder Map MatchBuilder Object with a match
262 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
264 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
265 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
266 ethTypeBuilder.setType(new EtherType(0x0800L));
267 ethType.setEthernetType(ethTypeBuilder.build());
268 matchBuilder.setEthernetMatch(ethType.build());
270 IpMatchBuilder ipmatch = new IpMatchBuilder();
271 ipmatch.setIpProtocol((short) 6);
272 matchBuilder.setIpMatch(ipmatch.build());
274 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
275 tcpmatch.setTcpSourcePort(tcpport);
276 matchBuilder.setLayer4Match(tcpmatch.build());
283 * Create Destination TCP Port Match
285 * @param matchBuilder MatchBuilder Object without a match yet
286 * @param tcpDstPort Integer representing a destination TCP port
287 * @return matchBuilder Map MatchBuilder Object with a match
289 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
291 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
292 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
293 ethTypeBuilder.setType(new EtherType(0x0800L));
294 ethType.setEthernetType(ethTypeBuilder.build());
295 matchBuilder.setEthernetMatch(ethType.build());
297 IpMatchBuilder ipmatch = new IpMatchBuilder();
298 ipmatch.setIpProtocol((short) 6);
299 matchBuilder.setIpMatch(ipmatch.build());
301 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
302 tcpmatch.setTcpDestinationPort(tcpDstPort);
303 matchBuilder.setLayer4Match(tcpmatch.build());
309 * Test match for TCP_Flags
311 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
313 * Defined TCP Flag values in OVS v2.1+
314 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
315 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
316 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
318 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, int tcpFlag) {
321 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
322 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
323 ethTypeBuilder.setType(new EtherType(0x0800L));
324 ethernetType.setEthernetType(ethTypeBuilder.build());
325 matchBuilder.setEthernetMatch(ethernetType.build());
327 // TCP Protocol Match
328 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
329 ipMatch.setIpProtocol((short) 6);
330 matchBuilder.setIpMatch(ipMatch.build());
333 PortNumber dstPort = new PortNumber(80);
334 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
335 tcpMatch.setTcpDestinationPort(dstPort);
336 matchBuilder.setLayer4Match(tcpMatch.build());
338 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
339 tcpFlagMatch.setTcpFlag(0x002);
340 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
345 public static class RegMatch {
346 final Class<? extends NxmNxReg> reg;
348 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
353 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
354 return new RegMatch(reg, value);
358 public static void addNxRegMatch(MatchBuilder match,
359 RegMatch... matches) {
360 ArrayList<ExtensionList> extensions = new ArrayList<>();
361 for (RegMatch rm : matches) {
362 Class<? extends ExtensionKey> key;
363 if (NxmNxReg0.class.equals(rm.reg)) {
364 key = NxmNxReg0Key.class;
365 } else if (NxmNxReg1.class.equals(rm.reg)) {
366 key = NxmNxReg1Key.class;
367 } else if (NxmNxReg2.class.equals(rm.reg)) {
368 key = NxmNxReg2Key.class;
369 } else if (NxmNxReg3.class.equals(rm.reg)) {
370 key = NxmNxReg3Key.class;
371 } else if (NxmNxReg4.class.equals(rm.reg)) {
372 key = NxmNxReg4Key.class;
373 } else if (NxmNxReg5.class.equals(rm.reg)) {
374 key = NxmNxReg5Key.class;
375 } else if (NxmNxReg6.class.equals(rm.reg)) {
376 key = NxmNxReg6Key.class;
378 key = NxmNxReg7Key.class;
380 NxAugMatchNodesNodeTableFlow am =
381 new NxAugMatchNodesNodeTableFlowBuilder()
382 .setNxmNxReg(new NxmNxRegBuilder()
387 extensions.add(new ExtensionListBuilder()
388 .setExtensionKey(key)
389 .setExtension(new ExtensionBuilder()
390 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
394 GeneralAugMatchNodesNodeTableFlow m =
395 new GeneralAugMatchNodesNodeTableFlowBuilder()
396 .setExtensionList(extensions)
398 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
401 public static void addNxTunIdMatch(MatchBuilder match,
403 NxAugMatchNodesNodeTableFlow am =
404 new NxAugMatchNodesNodeTableFlowBuilder()
405 .setNxmNxTunId(new NxmNxTunIdBuilder()
406 .setValue(BigInteger.valueOf(tunId))
409 GeneralAugMatchNodesNodeTableFlow m =
410 new GeneralAugMatchNodesNodeTableFlowBuilder()
411 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
412 .setExtensionKey(NxmNxTunIdKey.class)
413 .setExtension(new ExtensionBuilder()
414 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
418 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
421 public static void addNxNsp(MatchBuilder match, long nsp) {
422 org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am =
423 new org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
424 .setNxmNxNsp(new NxmNxNspBuilder()
429 GeneralAugMatchNodesNodeTableFlow m =
430 new GeneralAugMatchNodesNodeTableFlowBuilder()
431 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
432 .setExtensionKey(NxmNxNspKey.class)
433 .setExtension(new ExtensionBuilder()
434 .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
438 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
441 public static EthernetMatch ethernetMatch(MacAddress srcMac,
444 EthernetMatchBuilder emb = new EthernetMatchBuilder();
446 emb.setEthernetSource(new EthernetSourceBuilder()
450 emb.setEthernetDestination(new EthernetDestinationBuilder()
453 if (etherType != null)
454 emb.setEthernetType(new EthernetTypeBuilder()
455 .setType(new EtherType(etherType))