private static NodeConnectorId inPortMatch(Match sourceMatch) { MatchField inPort = sourceMatch.getField(MatchType.IN_PORT); if(inPort != null && inPort.getValue() != null && (inPort.getValue() instanceof NodeConnector)) { NodeConnector port = (NodeConnector)inPort.getValue(); return (NodeConnectorId)MDFlowMapping.toUri(port); } return null; }
private static Integer transportPort(final Match sourceMatch, final MatchType matchType) { MatchField transportPort = sourceMatch.getField(matchType); if (transportPort != null && transportPort.getValue() != null && transportPort.getValue().getClass().equals(Short.class)) { return NetUtils.getUnsignedShort((short) transportPort.getValue()); } return null; }
private static IpMatch ipMatch(final Match sourceMatch) { IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder(); MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS); if (networkTos != null && networkTos.getValue() != null) { Dscp dscp = new Dscp( (short) (NetUtils.getUnsignedByte((Byte) networkTos .getValue()))); targetIpMatchBuild.setIpDscp(dscp); } MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO); if (protocol != null && protocol.getValue() != null) { targetIpMatchBuild.setIpProtocol((short) ((byte) protocol .getValue())); } if((networkTos != null && networkTos.getValue() != null) || (protocol != null && protocol.getValue() != null)) { return targetIpMatchBuild.build(); } return null; }
private static Layer3Match layer3Match(final Match sourceMatch) { InetAddress inetSourceAddress = null; MatchField netSource = sourceMatch.getField(MatchType.NW_SRC); if (netSource != null && netSource.getValue() != null) { inetSourceAddress = (InetAddress) (netSource.getValue()); if (netDest != null && netDest.getValue() != null) { inetDestAddress = (InetAddress) (netDest.getValue()); MatchField dataLinkType = sourceMatch.getField(DL_TYPE); Short dLType = null; if (dataLinkType != null && dataLinkType.getValue() != null) { dLType = (Short) (dataLinkType.getValue());
private static Layer4Match layer4Match(final Match sourceMatch) { MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO); Short nwProtocolSource = null; if (nwProto != null && nwProto.getValue() != null) { nwProtocolSource = (short) ((byte) nwProto.getValue()); switch (nwProtocolSource) { case TCP: return Layer4MatchAsTcp(sourceMatch); case UDP: return Layer4MatchAsUdp(sourceMatch); case CRUDP: return Layer4MatchAsSctp(sourceMatch); } } return null; }
private static VlanMatch vlanMatch(final Match sourceMatch) { VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder(); MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN); if (vlan != null && vlan.getValue() != null) { VlanIdBuilder vlanIDBuilder = new VlanIdBuilder(); short vid = (short)vlan.getValue(); boolean present = (vid != MatchType.DL_VLAN_NONE); vlanIDBuilder.setVlanId(new VlanId((NetUtils .getUnsignedShort(vid)))); vlanIDBuilder.setVlanIdPresent(present); vlanMatchBuild.setVlanId(vlanIDBuilder.build()); } MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR); if (vlanPriority != null && vlanPriority.getValue() != null) { vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority .getValue()))); } if((vlan != null && vlan.getValue() != null) || (vlanPriority != null && vlanPriority.getValue() != null)) { return vlanMatchBuild.build(); } return null; }
/** * Returns whether this match is for an IPv6 flow */ public boolean isIPv6() { return (isPresent(MatchType.DL_TYPE) && ((Short) getField(MatchType.DL_TYPE).getValue()) .equals(EtherTypes.IPv6.shortValue()) || isPresent(MatchType.NW_PROTO) && ((Byte) getField(MatchType.NW_PROTO).getValue()) .equals(IPProtocols.IPV6ICMP.byteValue()) || isPresent(MatchType.NW_SRC) && getField(MatchType.NW_SRC).getValue() instanceof Inet6Address || isPresent(MatchType.NW_DST) && getField(MatchType.NW_DST).getValue() instanceof Inet6Address); }
InetAddress srcIp = (InetAddress) f.getMatch().getField(MatchType.NW_SRC).getValue(); InetAddress dstIp = (InetAddress) f.getMatch().getField(MatchType.NW_DST).getValue(); flowName = "[" + groupName + ":" + srcIp + ":" + dstIp + "]"; List<Action> actions = calcForwardingActions(node, srcIp, dstIp, attribs.get(groupName));
public void setStatsFromFlow(FlowOnNode flow) { MatchField protocolField = flow.getFlow().getMatch().getField(MatchType.NW_PROTO); Byte protocolNumber; if (protocolField == null) protocolNumber = IPProtocols.ANY.byteValue(); else protocolNumber = (Byte) protocolField.getValue(); // Prevent stats from getting overwritten by zero-byte flows. Long currentByteCount = this.byteCounts.get(protocolNumber); Long thisByteCount = flow.getByteCount(); Long thisPacketCount = flow.getPacketCount(); if (thisByteCount > 0 && (currentByteCount == null || currentByteCount <= thisByteCount)) { this.byteCounts.put(protocolNumber, thisByteCount); this.packetCounts.put(protocolNumber, thisPacketCount); this.durations.put(protocolNumber, flow.getDurationSeconds() + .000000001 * flow.getDurationNanoseconds()); } } }
protected Host getSourceHostFromFlow(Flow flow, Set<HostNodeConnector> hosts) { Host srcHost = null; Match match = flow.getMatch(); // Flow must have IN_PORT field (DL_SRC rarely (never?) // exists). if (match.isPresent(MatchType.IN_PORT)) { MatchField inPort = match.getField(MatchType.IN_PORT); // Check cache Host cacheHit = this.sourceHostCache.get(inPort); if (cacheHit != null) return cacheHit; // Find the source host by comparing the NodeConnectors NodeConnector inPortNc = (NodeConnector) inPort.getValue(); for (HostNodeConnector h : hosts) { NodeConnector hostNc = h.getnodeConnector(); if (hostNc.equals(inPortNc)) { srcHost = h; this.sourceHostCache.put(inPort, h); // Add to cache break; } } } return srcHost; }
private void handleFlowRemovedMessage(ISwitch sw, OFFlowRemoved msg) { Node node = NodeCreator.createOFNode(sw.getId()); Flow flow = new FlowConverter(msg.getMatch(), new ArrayList<OFAction>(0)).getFlow(node); flow.setPriority(msg.getPriority()); flow.setIdleTimeout(msg.getIdleTimeout()); flow.setId(msg.getCookie()); Match match = flow.getMatch(); NodeConnector inPort = match.isPresent(MatchType.IN_PORT) ? (NodeConnector) match .getField(MatchType.IN_PORT).getValue() : null; for (Map.Entry<String, IFlowProgrammerNotifier> containerNotifier : flowProgrammerNotifiers .entrySet()) { String container = containerNotifier.getKey(); IFlowProgrammerNotifier notifier = containerNotifier.getValue(); /* * Switch only provide us with the match information. For now let's * try to identify the container membership only from the input port * match field. In any case, upper layer consumers can derive * whether the notification was not for them. More sophisticated * filtering can be added later on. */ if (inPort == null || container.equals(GlobalConstants.DEFAULT.toString()) || (containerToNc.containsKey(container) && containerToNc.get(container).contains(inPort))) { notifier.flowRemoved(node, flow); } } }
/** * Returns a reversed version of this match * For example, in the reversed version the network source and destination * addresses will be exchanged. Non symmetric match field will not be * copied over into the reversed match version, like input port. * * @return */ public Match reverse() { // Copy over all fields Match reverse = this.clone(); // Flip symmetric fields for (Map.Entry<MatchType, MatchType> entry : Match.reversableMatches.entrySet()) { MatchType from = entry.getKey(); MatchType to = entry.getValue(); if (this.isPresent(from)) { reverse.setField(to, this.getField(from).getValue(), this.getField(from).getMask()); if (!this.isPresent(to)) { reverse.clearField(from); } } } // Reset asymmetric fields reverse.clearField(MatchType.IN_PORT); return reverse; }
private static EthernetMatch ethernetMatch(final Match sourceMatch) { final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder(); if(sourceMatch.getField(DL_SRC) != null && sourceMatch.getField(DL_SRC).getValue() != null) { EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder() .setAddress(ethernetSourceAddress(sourceMatch)); targetEthMatchBuild.setEthernetSource(ethSourBuild.build()); } if(sourceMatch.getField(DL_DST) != null && sourceMatch.getField(DL_DST).getValue() != null) { EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder() .setAddress(ethernetDestAddress(sourceMatch)); targetEthMatchBuild.setEthernetDestination(ethDestBuild.build()); } final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE); if (dataLinkType != null && dataLinkType.getValue() != null) { EtherType etherType = new EtherType((long) NetUtils.getUnsignedShort((Short) dataLinkType.getValue())); EthernetTypeBuilder ethType = new EthernetTypeBuilder() .setType(etherType); targetEthMatchBuild.setEthernetType(ethType.build()); } if((sourceMatch.getField(DL_SRC) != null && sourceMatch.getField(DL_SRC).getValue() != null) || (sourceMatch.getField(DL_DST) != null && sourceMatch.getField(DL_DST).getValue() != null)|| dataLinkType != null ) { return targetEthMatchBuild.build(); } return null; }
private Status validateFlow(Flow flow) { Match m = flow.getMatch(); boolean isIPEthertypeSet = m.isPresent(MatchType.DL_TYPE) && (m.getField(MatchType.DL_TYPE).getValue().equals(EtherTypes.IPv4.shortValue()) || m .getField(MatchType.DL_TYPE).getValue().equals(EtherTypes.IPv6.shortValue())); // network address check if ((m.isPresent(MatchType.NW_SRC) || m.isPresent(MatchType.NW_DST)) && !isIPEthertypeSet) { return new Status(StatusCode.NOTACCEPTABLE, "The match on network source or destination address cannot be accepted if the match " + "on proper ethertype is missing"); } // transport protocol check if (m.isPresent(MatchType.NW_PROTO) && !isIPEthertypeSet) { return new Status(StatusCode.NOTACCEPTABLE, "The match on network protocol cannot be accepted if the match on proper ethertype is missing"); } // transport ports check if ((m.isPresent(MatchType.TP_SRC) || m.isPresent(MatchType.TP_DST)) && (!isIPEthertypeSet || m.isAny(MatchType.NW_PROTO))) { return new Status( StatusCode.NOTACCEPTABLE, "The match on transport source or destination port cannot be accepted if the match on network protocol and match on IP ethertype are missing"); } return new Status(StatusCode.SUCCESS); }
private boolean doesFlowContainNodeConnector(Flow flow, NodeConnector nc) { if (nc == null) { return false; } Match match = flow.getMatch(); if (match.isPresent(MatchType.IN_PORT)) { NodeConnector matchPort = (NodeConnector) match.getField(MatchType.IN_PORT).getValue(); if (matchPort.equals(nc)) { return true; } } List<Action> actionsList = flow.getActions(); if (actionsList != null) { for (Action action : actionsList) { if (action instanceof Output) { NodeConnector actionPort = ((Output) action).getPort(); if (actionPort.equals(nc)) { return true; } } } } return false; }
/** * Check whether the ports in the flow match and flow actions for * the specified node belong to the container * * @param container * @param node * @param flow * @return */ private boolean flowPortsBelongToContainer(String container, Node node, Flow flow) { Match m = flow.getMatch(); if (m.isPresent(MatchType.IN_PORT)) { NodeConnector inPort = (NodeConnector) m.getField(MatchType.IN_PORT).getValue(); // If the incoming port is specified, check if it belongs to if (!containerOwnsNodeConnector(container, inPort)) { return false; } } // If an outgoing port is specified, it must belong to this container for (Action action : flow.getActions()) { if (action.getType() == ActionType.OUTPUT) { NodeConnector outPort = ((Output) action).getPort(); if (!containerOwnsNodeConnector(container, outPort)) { return false; } } } return true; }
if (m.isPresent(MatchType.IN_PORT)) { NodeConnector inPort = (NodeConnector) m .getField(MatchType.IN_PORT).getValue();