private void setDstIp(PacketHeaderConstraints constraints, Flow.Builder builder) { String headerDstIp = constraints.getDstIps(); checkArgument( constraints.getDstIps() != null, "Cannot perform traceroute without a destination"); IpSpaceSpecifier dstIpSpecifier = IpSpaceSpecifierFactory.load(IP_SPECIFIER_FACTORY).buildIpSpaceSpecifier(headerDstIp); IpSpaceAssignment dstIps = dstIpSpecifier.resolve(ImmutableSet.of(), _specifierContext); checkArgument( dstIps.getEntries().size() == 1, "Specified destination: %s, resolves to more than one IP", headerDstIp); IpSpace space = dstIps.getEntries().iterator().next().getIpSpace(); Optional<Ip> dstIp = _ipSpaceRepresentative.getRepresentative(space); checkArgument(dstIp.isPresent(), "At least one destination IP is required"); builder.setDstIp(dstIp.get()); }
private void set(IpField field, Ip ip) { switch (field) { case DESTINATION: _flowBuilder.setDstIp(ip); break; case SOURCE: _flowBuilder.setSrcIp(ip); break; default: throw new IllegalArgumentException("unknown IpField " + field); } }
private void setDstIp(PacketHeaderConstraints constraints, Builder builder) { String headerDstIp = constraints.getDstIps(); if (headerDstIp != null) { IpSpaceSpecifier dstIpSpecifier = IpSpaceSpecifierFactory.load(IP_SPECIFIER_FACTORY).buildIpSpaceSpecifier(headerDstIp); IpSpaceAssignment dstIps = dstIpSpecifier.resolve(ImmutableSet.of(), _batfish.specifierContext()); // Filter out empty IP assignments ImmutableList<Entry> nonEmptyIpSpaces = dstIps.getEntries().stream() .filter(e -> !e.getIpSpace().equals(EmptyIpSpace.INSTANCE)) .collect(ImmutableList.toImmutableList()); checkArgument( nonEmptyIpSpaces.size() > 0, "At least one destination IP is required, could not resolve any"); checkArgument( nonEmptyIpSpaces.size() == 1, "Specified destination: %s, resolves to more than one IP", headerDstIp); IpSpace space = nonEmptyIpSpaces.iterator().next().getIpSpace(); Optional<Ip> dstIp = _ipSpaceRepresentative.getRepresentative(space); checkArgument(dstIp.isPresent(), "Specified destination: %s has no IPs", headerDstIp); builder.setDstIp(dstIp.get()); } else { builder.setDstIp(DEFAULT_IP_ADDRESS); } }
fb.setIngressVrf(initiator.getVrfName()); fb.setSrcIp(srcAddress); fb.setDstIp(dstAddress); fb.setSrcPort(NamedPort.EPHEMERAL_LOWEST.number()); fb.setDstPort(NamedPort.BGP.number()); fb.setIngressVrf(listener.getVrfName()); fb.setSrcIp(forwardFlow.getDstIp()); fb.setDstIp(forwardFlow.getSrcIp()); fb.setSrcPort(forwardFlow.getDstPort()); fb.setDstPort(forwardFlow.getSrcPort());
Flow.builder().setTag("tag").setIngressNode("node").setDstIp(Ip.parse("1.1.1.1")).build(); Environment environment = new Environment(null, null, null, null, null, null, null);
b.setIngressNode(routerName); b.setSrcIp(srcIp); b.setDstIp(dstIp); b.setSrcPort(srcPort); b.setDstPort(dstPort);
@Test public void testDiffFlowTracesToRowsMaxTraces() { Flow flow = Flow.builder().setTag("tag").setIngressNode("node").setDstIp(Ip.parse("1.1.1.1")).build();
@Test public void testDiffFlowTracesToRows() { Flow flow = Flow.builder().setTag("tag").setIngressNode("node").setDstIp(Ip.parse("1.1.1.1")).build();
@Test public void testFlowDiffs() { Ip dst1 = Ip.parse("1.1.1.1"); Ip src1 = Ip.parse("2.2.2.2"); Ip dst2 = Ip.parse("3.3.3.3"); Ip src2 = Ip.parse("4.4.4.4"); Flow orig = Flow.builder() .setIngressNode("ingressNode") .setTag("tag") .setDstIp(dst1) .setSrcIp(src1) .build(); Flow newDst = orig.toBuilder().setDstIp(dst2).build(); Flow newSrc = orig.toBuilder().setSrcIp(src2).build(); Flow newDstAndSrc = newDst.toBuilder().setSrcIp(src2).build(); assertThat(flowDiffs(null, orig), empty()); assertThat(flowDiffs(orig, null), empty()); assertThat(flowDiffs(orig, orig), empty()); FlowDiff dstDiff = new FlowDiff(PROP_DST_IP, dst1.toString(), dst2.toString()); FlowDiff srcDiff = new FlowDiff(PROP_SRC_IP, src1.toString(), src2.toString()); assertThat(flowDiffs(orig, newDst), contains(dstDiff)); assertThat(flowDiffs(orig, newSrc), contains(srcDiff)); assertThat(flowDiffs(orig, newDstAndSrc), containsInAnyOrder(dstDiff, srcDiff)); }
@Test public void testColumnComparatorFlowTrace() { String col = "col1"; ColumnMetadata columnMetadata = new ColumnMetadata(col, Schema.FLOW_TRACE, "colDesc"); Comparator<Row> comparator = _manager.columnComparator(columnMetadata); Row r1 = Row.of(col, new FlowTrace(FlowDisposition.ACCEPTED, ImmutableList.of(), "")); Row r2 = Row.of(col, new FlowTrace(FlowDisposition.DELIVERED_TO_SUBNET, ImmutableList.of(), "")); Row r3 = Row.of( col, new FlowTrace( FlowDisposition.ACCEPTED, ImmutableList.of( new FlowTraceHop( Edge.of("a", "a", "b", "b"), ImmutableSortedSet.of(), "a", "a", Flow.builder().setDstIp(Ip.ZERO).setIngressNode("a").setTag("a").build())), "")); assertThat(comparator.compare(r1, r2), lessThan(0)); assertThat(comparator.compare(r1, r3), lessThan(0)); assertThat(comparator.compare(r2, r3), lessThan(0)); }
public Flow.Builder getFlowFromAssignment(BDD satAssignment) { checkArgument(isAssignment(satAssignment)); Flow.Builder fb = Flow.builder(); fb.setDstIp(Ip.create(_dstIp.satAssignmentToLong(satAssignment))); fb.setSrcIp(Ip.create(_srcIp.satAssignmentToLong(satAssignment))); fb.setDstPort(_dstPort.satAssignmentToLong(satAssignment).intValue()); fb.setSrcPort(_srcPort.satAssignmentToLong(satAssignment).intValue()); fb.setIpProtocol( IpProtocol.fromNumber(_ipProtocol.satAssignmentToLong(satAssignment).intValue())); fb.setIcmpCode(_icmpCode.satAssignmentToLong(satAssignment).intValue()); fb.setIcmpType(_icmpType.satAssignmentToLong(satAssignment).intValue()); fb.setTcpFlagsAck(_tcpAck.and(satAssignment).isZero() ? 0 : 1); fb.setTcpFlagsCwr(_tcpCwr.and(satAssignment).isZero() ? 0 : 1); fb.setTcpFlagsEce(_tcpEce.and(satAssignment).isZero() ? 0 : 1); fb.setTcpFlagsFin(_tcpFin.and(satAssignment).isZero() ? 0 : 1); fb.setTcpFlagsPsh(_tcpPsh.and(satAssignment).isZero() ? 0 : 1); fb.setTcpFlagsRst(_tcpRst.and(satAssignment).isZero() ? 0 : 1); fb.setTcpFlagsSyn(_tcpSyn.and(satAssignment).isZero() ? 0 : 1); fb.setTcpFlagsUrg(_tcpUrg.and(satAssignment).isZero() ? 0 : 1); fb.setDscp(_dscp.satAssignmentToLong(satAssignment).intValue()); fb.setEcn(_ecn.satAssignmentToLong(satAssignment).intValue()); fb.setFragmentOffset(_fragmentOffset.satAssignmentToLong(satAssignment).intValue()); fb.setState(FlowState.fromNum(_state.satAssignmentToLong(satAssignment).intValue())); return fb; }
@Test public void flowHistoryToRow() { Set<FlowTrace> traces = ImmutableSet.of( new FlowTrace(FlowDisposition.ACCEPTED, ImmutableList.of(), "notes1"), new FlowTrace(FlowDisposition.DENIED_OUT, ImmutableList.of(), "notes2")); Flow flow = Flow.builder().setTag("tag").setIngressNode("node").setDstIp(Ip.parse("1.1.1.1")).build(); FlowHistoryInfo historyInfo = new FlowHistoryInfo( flow, ImmutableMap.of("env", new Environment(null, null, null, null, null, null, null)), ImmutableMap.of("env", traces)); Row row = TracerouteAnswerer.flowHistoryToRow(historyInfo); assertThat( row, equalTo( Row.of( TracerouteAnswerer.COL_FLOW, flow, TracerouteAnswerer.COL_TRACES, historyInfo.getPaths().values().stream().findAny()))); } }
/** * Creates a return {@link Flow} for the input {@param forwardFlow}. Swaps the source/destination * IPs/ports, and sets the ingress node/vrf/interface. */ static Flow returnFlow( Flow forwardFlow, String returnIngressNode, @Nullable String returnIngressVrf, @Nullable String returnIngressIface) { checkArgument( returnIngressVrf == null ^ returnIngressIface == null, "Either returnIngressVrf or returnIngressIface required, but not both"); return forwardFlow .toBuilder() .setDstIp(forwardFlow.getSrcIp()) .setDstPort(forwardFlow.getSrcPort()) .setSrcIp(forwardFlow.getDstIp()) .setSrcPort(forwardFlow.getDstPort()) .setIngressNode(returnIngressNode) .setIngressVrf(returnIngressVrf) .setIngressInterface(returnIngressIface) .build(); }
/** Compute the differences between two flows */ public static SortedSet<FlowDiff> flowDiffs(@Nullable Flow flow1, @Nullable Flow flow2) { if (flow1 == null || flow2 == null || flow1.equals(flow2)) { return ImmutableSortedSet.of(); } checkArgument( flow1 .toBuilder() .setDstIp(flow2.getDstIp()) .setSrcIp(flow2.getSrcIp()) .build() .equals(flow2), "flowDiff only supports differences of src or dst Ip"); ImmutableSortedSet.Builder<FlowDiff> diffs = ImmutableSortedSet.naturalOrder(); if (!flow1.getDstIp().equals(flow2.getDstIp())) { diffs.add( new FlowDiff(PROP_DST_IP, flow1.getDstIp().toString(), flow2.getDstIp().toString())); } if (!flow1.getSrcIp().equals(flow2.getSrcIp())) { diffs.add( new FlowDiff(PROP_SRC_IP, flow1.getSrcIp().toString(), flow2.getSrcIp().toString())); } return diffs.build(); }
@Test public void testFlowTracesToRowsMaxTraces() { Flow flow = Flow.builder().setTag("tag").setIngressNode("node").setDstIp(Ip.parse("1.1.1.1")).build(); SortedMap<Flow, List<Trace>> flowTraces = ImmutableSortedMap.of( flow, ImmutableList.of( new Trace(FlowDisposition.DENIED_OUT, ImmutableList.of()), new Trace(FlowDisposition.DENIED_IN, ImmutableList.of()))); Multiset<Row> rows = flowTracesToRows(flowTraces, 1); assertThat( rows.iterator().next(), allOf( hasColumn( COL_FLOW, allOf(hasDstIp(Ip.parse("1.1.1.1")), hasIngressNode("node"), hasTag("tag")), Schema.FLOW), hasColumn(COL_TRACES, hasSize(1), Schema.set(Schema.TRACE)), hasColumn(TracerouteAnswerer.COL_TRACE_COUNT, equalTo(2), Schema.INTEGER))); }
@Test public void testTestFilter() { String hostname = _config.getHostname(); Flow flow = Flow.builder().setIngressNode(hostname).setDstIp(IP2).setTag("tag").build(); assertThat( TestFiltersAnswerer.getRow(ACL, flow, _config), allOf( hasColumn(COL_ACTION, equalTo("DENY"), Schema.STRING), hasColumn(COL_FILTER_NAME, equalTo(ACL.getName()), Schema.STRING))); }
@Test public void testColumnComparatorFlow() { String col = "col1"; ColumnMetadata columnMetadata = new ColumnMetadata(col, Schema.FLOW, "colDesc"); Comparator<Row> comparator = _manager.columnComparator(columnMetadata); Row r1 = Row.of(col, Flow.builder().setDstIp(Ip.ZERO).setIngressNode("a").setTag("a").build()); Row r2 = Row.of(col, Flow.builder().setDstIp(Ip.MAX).setIngressNode("a").setTag("a").build()); assertThat(comparator.compare(r1, r2), lessThan(0)); }
@Before public void setup() { _flow = Flow.builder() .setIngressNode("ingress") .setTag("tag") .setDstIp(Ip.parse("1.1.1.1")) .build(); _describerNoNamesNorMetadata = new IpSpaceDescriber( new AclTracer(_flow, null, ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of())); }