/*
- * Copyright (C) 2013 Red Hat, Inc.
+ * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
- *
*/
package org.opendaylight.ovsdb.utils.mdsal.openflow;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
import com.google.common.collect.Lists;
public class MatchUtils {
- private static final Logger logger = LoggerFactory.getLogger(MatchUtils.class);
+ private static final Logger LOG = LoggerFactory.getLogger(MatchUtils.class);
public static final short ICMP_SHORT = 1;
public static final short TCP_SHORT = 6;
public static final short UDP_SHORT = 17;
public static final String TCP = "tcp";
public static final String UDP = "udp";
private static final int TCP_SYN = 0x0002;
+ public static final String ICMP = "icmp";
/**
* Create Ingress Port Match dpidLong, inPort
public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
- logger.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
+ LOG.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
matchBuilder.setInPort(ncid);
/**
* Match ICMP code and type
*
- * @param matchBuilder MatchBuilder Object without a match yet
+ * @param matchBuilder MatchBuilder Object
* @param type short representing an ICMP type
* @param code short representing an ICMP code
* @return matchBuilder Map MatchBuilder Object with a match
*/
public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
- EthernetMatchBuilder eth = new EthernetMatchBuilder();
- EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
- ethTypeBuilder.setType(new EtherType(0x0800L));
- eth.setEthernetType(ethTypeBuilder.build());
- matchBuilder.setEthernetMatch(eth.build());
-
// Build the IPv4 Match requied per OVS Syntax
IpMatchBuilder ipmatch = new IpMatchBuilder();
ipmatch.setIpProtocol((short) 1);
*/
public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
String attachedMac, Integer tcpFlag, String tunnelID) {
+ return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
+ }
+
+ /**
+ * Create dmac ipSa match.
+ *
+ * @param matchBuilder the match builder
+ * @param attachedMac the attached mac
+ * @param ipPrefix the src ipPrefix
+ * @param tunnelID the tunnel iD
+ * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
+ */
+ public static MatchBuilder createDmacIpSaMatch(
+ MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
+ return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
+ }
+
+ /**
+ * Create dmac tcp port ipSa with flag match.
+ *
+ * @param matchBuilder the match builder
+ * @param attachedMac the attached mac
+ * @param tcpFlag the tcp flag
+ * @param ipPrefix the src ipPrefix
+ * @param tunnelID the tunnel iD
+ * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
+ */
+ public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
+ MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
ethTypeBuilder.setType(new EtherType(0x0800L));
ethernetMatch.setEthernetType(ethTypeBuilder.build());
- EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
- ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
- ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
- matchBuilder.setEthernetMatch(ethernetMatch.build());
+ if (attachedMac != null) {
+ EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
+ ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
+ ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
+ matchBuilder.setEthernetMatch(ethernetMatch.build());
+ }
- // TCP Protocol Match
- IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
- ipMatch.setIpProtocol(TCP_SHORT);
- matchBuilder.setIpMatch(ipMatch.build());
+ if (tcpFlag != null) {
+ // TCP Protocol Match
+ IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
+ ipMatch.setIpProtocol(TCP_SHORT);
+ matchBuilder.setIpMatch(ipMatch.build());
- TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
- tcpFlagMatch.setTcpFlag(tcpFlag);
- matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
+ TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
+ tcpFlagMatch.setTcpFlag(tcpFlag);
+ matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
+ }
- TunnelBuilder tunnelBuilder = new TunnelBuilder();
- tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
- matchBuilder.setTunnel(tunnelBuilder.build());
+ if (tunnelID != null) {
+ TunnelBuilder tunnelBuilder = new TunnelBuilder();
+ tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
+ matchBuilder.setTunnel(tunnelBuilder.build());
+ }
+
+ if (ipPrefix != null) {
+ Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
+ ipv4match.setIpv4Source(ipPrefix);
+ matchBuilder.setLayer3Match(ipv4match.build());
+ }
return matchBuilder;
}
return matchBuilder;
}
+ /**
+ * Create a DHCP match with pot provided.
+ *
+ * @param matchBuilder the match builder
+ * @param srcPort the source port
+ * @param dstPort the destination port
+ * @return the DHCP match
+ */
+ public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
+ int srcPort, int dstPort) {
+
+ EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
+ EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
+ ethTypeBuilder.setType(new EtherType(0x0800L));
+ ethernetMatch.setEthernetType(ethTypeBuilder.build());
+ matchBuilder.setEthernetMatch(ethernetMatch.build());
+
+ IpMatchBuilder ipmatch = new IpMatchBuilder();
+ ipmatch.setIpProtocol(UDP_SHORT);
+ matchBuilder.setIpMatch(ipmatch.build());
+
+ UdpMatchBuilder udpmatch = new UdpMatchBuilder();
+ udpmatch.setUdpSourcePort(new PortNumber(srcPort));
+ udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
+ matchBuilder.setLayer4Match(udpmatch.build());
+
+ return matchBuilder;
+
+ }
+
+ /**
+ * Creates DHCP server packet match with DHCP mac address and port.
+ *
+ * @param matchBuilder the matchbuilder
+ * @param dhcpServerMac MAc address of the DHCP server of the subnet
+ * @param srcPort the source port
+ * @param dstPort the destination port
+ * @return the DHCP server match
+ */
+ public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
+ int dstPort) {
+
+ EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
+ EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
+ ethTypeBuilder.setType(new EtherType(0x0800L));
+ ethernetMatch.setEthernetType(ethTypeBuilder.build());
+ matchBuilder.setEthernetMatch(ethernetMatch.build());
+
+ EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
+ ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
+ ethernetMatch.setEthernetSource(ethSourceBuilder.build());
+ matchBuilder.setEthernetMatch(ethernetMatch.build());
+
+ IpMatchBuilder ipmatch = new IpMatchBuilder();
+ ipmatch.setIpProtocol(UDP_SHORT);
+ matchBuilder.setIpMatch(ipmatch.build());
+
+ UdpMatchBuilder udpmatch = new UdpMatchBuilder();
+ udpmatch.setUdpSourcePort(new PortNumber(srcPort));
+ udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
+ matchBuilder.setLayer4Match(udpmatch.build());
+
+ return matchBuilder;
+
+ }
+
+ /**
+ * Creates a Match with src ip address mac address set.
+ * @param matchBuilder MatchBuilder Object
+ * @param srcip String containing an IPv4 prefix
+ * @param srcMac The source macAddress
+ * @return matchBuilder Map Object with a match
+ */
+ public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
+
+ Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
+ ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
+ EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
+ ethTypeBuilder.setType(new EtherType(0x0800L));
+ EthernetMatchBuilder eth = new EthernetMatchBuilder();
+ eth.setEthernetType(ethTypeBuilder.build());
+ eth.setEthernetSource(new EthernetSourceBuilder()
+ .setAddress(srcMac)
+ .build());
+
+ matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
+ matchBuilder.setEthernetMatch(eth.build());
+ return matchBuilder;
+
+ }
+
+ /**
+ * Creates a ether net match with ether type set to 0x0800L.
+ * @param matchBuilder MatchBuilder Object
+ * @param srcMac The source macAddress
+ * @param dstMac The destination mac address
+ * @return matchBuilder Map Object with a match
+ */
+ public static MatchBuilder createEtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
+ {
+ EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
+ ethTypeBuilder.setType(new EtherType(0x0800L));
+ EthernetMatchBuilder eth = new EthernetMatchBuilder();
+ eth.setEthernetType(ethTypeBuilder.build());
+ if (null != srcMac) {
+ eth.setEthernetSource(new EthernetSourceBuilder()
+ .setAddress(new MacAddress(srcMac)).build());
+ }
+ if (null != dstMac) {
+ eth.setEthernetDestination(new EthernetDestinationBuilder()
+ .setAddress(new MacAddress(dstMac)).build());
+ }
+ matchBuilder.setEthernetMatch(eth.build());
+ return matchBuilder;
+ }
+ /**
+ * Adds remote Ip prefix to existing match.
+ * @param matchBuilder The match builder
+ * @param sourceIpPrefix The source IP prefix
+ * @param destIpPrefix The destination IP prefix
+ * @return matchBuilder Map Object with a match
+ */
+ public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
+ Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
+ Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
+ if (null != sourceIpPrefix) {
+ ipv4match.setIpv4Source(sourceIpPrefix);
+ }
+ if (null != destIpPrefix) {
+ ipv4match.setIpv4Destination(destIpPrefix);
+ }
+ matchBuilder.setLayer3Match(ipv4match.build());
+
+ return matchBuilder;
+ }
+ /**
+ * Add a layer4 match to an existing match
+ *
+ * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
+ * @param protocol The layer4 protocol
+ * @param srcPort The src port
+ * @param destPort The destination port
+ * @return matchBuilder Map Object with a match
+ */
+ public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
+ int protocol, int srcPort, int destPort) {
+ IpMatchBuilder ipmatch = new IpMatchBuilder();
+ if (TCP_SHORT == protocol) {
+ ipmatch.setIpProtocol(TCP_SHORT);
+ TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
+ if (0 != srcPort) {
+ tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
+ }
+ if (0 != destPort) {
+ tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
+ }
+ matchBuilder.setLayer4Match(tcpmatch.build());
+ } else if (UDP_SHORT == protocol) {
+ ipmatch.setIpProtocol(UDP_SHORT);
+ UdpMatchBuilder udpMatch = new UdpMatchBuilder();
+ if (0 != srcPort) {
+ udpMatch.setUdpSourcePort(new PortNumber(srcPort));
+ }
+ if (0 != destPort) {
+ udpMatch.setUdpDestinationPort(new PortNumber(destPort));
+ }
+ matchBuilder.setLayer4Match(udpMatch.build());
+ }
+ matchBuilder.setIpMatch(ipmatch.build());
+
+ return matchBuilder;
+ }
+
+
public static class RegMatch {
final Class<? extends NxmNxReg> reg;
final Long value;
public static void addNxRegMatch(MatchBuilder match,
RegMatch... matches) {
- ArrayList<ExtensionList> extensions = new ArrayList<>();
+ List<ExtensionList> extensions = new ArrayList<>();
for (RegMatch rm : matches) {
Class<? extends ExtensionKey> key;
if (NxmNxReg0.class.equals(rm.reg)) {
MacAddress dstMac,
Long etherType) {
EthernetMatchBuilder emb = new EthernetMatchBuilder();
- if (srcMac != null)
+ if (srcMac != null) {
emb.setEthernetSource(new EthernetSourceBuilder()
.setAddress(srcMac)
.build());
- if (dstMac != null)
+ }
+ if (dstMac != null) {
emb.setEthernetDestination(new EthernetDestinationBuilder()
.setAddress(dstMac)
.build());
- if (etherType != null)
+ }
+ if (etherType != null) {
emb.setEthernetType(new EthernetTypeBuilder()
.setType(new EtherType(etherType))
.build());
+ }
return emb.build();
}