Search in sources :

Example 46 with Interface

use of org.batfish.datamodel.Interface in project batfish by batfish.

the class Instance method toConfigurationNode.

public Configuration toConfigurationNode(AwsConfiguration awsVpcConfig, Region region, Warnings warnings) {
    String name = _tags.getOrDefault("Name", _instanceId);
    Configuration cfgNode = Utils.newAwsConfiguration(name, "aws");
    for (String interfaceId : _networkInterfaces) {
        NetworkInterface netInterface = region.getNetworkInterfaces().get(interfaceId);
        if (netInterface == null) {
            warnings.redFlag(String.format("Network interface \"%s\" for instance \"%s\" not found", interfaceId, _instanceId));
            continue;
        }
        ImmutableSortedSet.Builder<InterfaceAddress> ifaceAddressesBuilder = new ImmutableSortedSet.Builder<>(Comparator.naturalOrder());
        Subnet subnet = region.getSubnets().get(netInterface.getSubnetId());
        Prefix ifaceSubnet = subnet.getCidrBlock();
        Ip defaultGatewayAddress = subnet.computeInstancesIfaceIp();
        StaticRoute defaultRoute = StaticRoute.builder().setAdministrativeCost(Route.DEFAULT_STATIC_ROUTE_ADMIN).setMetric(Route.DEFAULT_STATIC_ROUTE_COST).setNextHopIp(defaultGatewayAddress).setNetwork(Prefix.ZERO).build();
        cfgNode.getDefaultVrf().getStaticRoutes().add(defaultRoute);
        for (Ip ip : netInterface.getIpAddressAssociations().keySet()) {
            if (!ifaceSubnet.containsIp(ip)) {
                warnings.pedantic(String.format("Instance subnet \"%s\" does not contain private ip: \"%s\"", ifaceSubnet, ip));
                continue;
            }
            if (ip.equals(ifaceSubnet.getEndIp())) {
                warnings.pedantic(String.format("Expected end address \"%s\" to be used by generated subnet node", ip));
                continue;
            }
            InterfaceAddress address = new InterfaceAddress(ip, ifaceSubnet.getPrefixLength());
            ifaceAddressesBuilder.add(address);
        }
        SortedSet<InterfaceAddress> ifaceAddresses = ifaceAddressesBuilder.build();
        Interface iface = Utils.newInterface(interfaceId, cfgNode, ifaceAddresses.first());
        iface.setAllAddresses(ifaceAddresses);
        cfgNode.getVendorFamily().getAws().setVpcId(_vpcId);
        cfgNode.getVendorFamily().getAws().setSubnetId(_subnetId);
        cfgNode.getVendorFamily().getAws().setRegion(region.getName());
    }
    Utils.processSecurityGroups(region, cfgNode, _securityGroups, warnings);
    return cfgNode;
}
Also used : StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Interface(org.batfish.datamodel.Interface)

Example 47 with Interface

use of org.batfish.datamodel.Interface in project batfish by batfish.

the class IptablesVendorConfiguration method applyAsOverlay.

public void applyAsOverlay(Configuration configuration, Warnings warnings) {
    IpAccessList prerouting = configuration.getIpAccessLists().remove("mangle::PREROUTING");
    IpAccessList postrouting = configuration.getIpAccessLists().remove("mangle::POSTROUTING");
    if (!configuration.getIpAccessLists().isEmpty()) {
        throw new BatfishException("Merging iptables rules for " + configuration.getName() + ": only mangle tables are supported");
    }
    if (prerouting != null) {
        for (Interface i : configuration.getInterfaces().values()) {
            String dbgName = configuration.getHostname() + ":" + i.getName();
            List<IpAccessListLine> newRules = prerouting.getLines().stream().filter(l -> {
                String iface = _lineInInterfaces.get(l);
                return iface == null || i.getName().equals(iface);
            }).collect(Collectors.toList());
            if (i.getIncomingFilter() != null) {
                throw new BatfishException(dbgName + " already has a filter," + " cannot combine with iptables rules!");
            }
            String aclName = "iptables_" + i.getName() + "_ingress";
            IpAccessList acl = new IpAccessList(aclName, newRules);
            if (configuration.getIpAccessLists().putIfAbsent(aclName, acl) != null) {
                throw new BatfishException(dbgName + " acl " + aclName + " already exists");
            }
            i.setIncomingFilter(acl);
        }
    }
    if (postrouting != null) {
        for (Interface i : configuration.getInterfaces().values()) {
            String dbgName = configuration.getHostname() + ":" + i.getName();
            List<IpAccessListLine> newRules = postrouting.getLines().stream().filter(l -> {
                String iface = _lineOutInterfaces.get(l);
                return iface == null || i.getName().equals(iface);
            }).collect(Collectors.toList());
            if (i.getOutgoingFilter() != null) {
                throw new BatfishException(dbgName + " already has a filter," + " cannot combine with iptables rules!");
            }
            String aclName = "iptables_" + i.getName() + "_egress";
            IpAccessList acl = new IpAccessList(aclName, newRules);
            if (configuration.getIpAccessLists().putIfAbsent(aclName, acl) != null) {
                throw new BatfishException(dbgName + " acl " + aclName + " already exists");
            }
            i.setOutgoingFilter(acl);
        }
    }
}
Also used : ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat) Iterables(com.google.common.collect.Iterables) IdentityHashMap(java.util.IdentityHashMap) SortedSet(java.util.SortedSet) VendorConfiguration(org.batfish.vendor.VendorConfiguration) Set(java.util.Set) BatfishException(org.batfish.common.BatfishException) IpAccessList(org.batfish.datamodel.IpAccessList) Collectors(java.util.stream.Collectors) Interface(org.batfish.datamodel.Interface) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) Warnings(org.batfish.common.Warnings) Map(java.util.Map) Entry(java.util.Map.Entry) Configuration(org.batfish.datamodel.Configuration) LineAction(org.batfish.datamodel.LineAction) Collections(java.util.Collections) VendorConversionException(org.batfish.common.VendorConversionException) BatfishException(org.batfish.common.BatfishException) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) IpAccessList(org.batfish.datamodel.IpAccessList) Interface(org.batfish.datamodel.Interface)

Example 48 with Interface

use of org.batfish.datamodel.Interface in project batfish by batfish.

the class HostInterface method toInterface.

public Interface toInterface(Configuration configuration, Warnings warnings) {
    String name = _canonicalName != null ? _canonicalName : _name;
    Interface.Builder iface = Interface.builder().setName(name).setOwner(configuration).setActive(true).setAddresses(_address, _otherAddresses).setBandwidth(_bandwidth).setDeclaredNames(ImmutableSortedSet.of(_name)).setProxyArp(false).setVrf(configuration.getDefaultVrf());
    if (_shared) {
        SourceNat sourceNat = new SourceNat();
        Ip publicIp = _address.getIp();
        sourceNat.setPoolIpFirst(publicIp);
        sourceNat.setPoolIpLast(publicIp);
        iface.setSourceNats(ImmutableList.of(sourceNat));
    }
    return iface.build();
}
Also used : SourceNat(org.batfish.datamodel.SourceNat) Ip(org.batfish.datamodel.Ip) Interface(org.batfish.datamodel.Interface)

Example 49 with Interface

use of org.batfish.datamodel.Interface in project batfish by batfish.

the class Encoder method initFailedLinkVariables.

/*
   * Initialize symbolic variables to represent link failures.
   */
private void initFailedLinkVariables() {
    for (List<GraphEdge> edges : _graph.getEdgeMap().values()) {
        for (GraphEdge ge : edges) {
            if (ge.getPeer() == null) {
                Interface i = ge.getStart();
                String name = getId() + "_FAILED-EDGE_" + ge.getRouter() + "_" + i.getName();
                ArithExpr var = getCtx().mkIntConst(name);
                _symbolicFailures.getFailedEdgeLinks().put(ge, var);
                _allVariables.put(var.toString(), var);
            }
        }
    }
    for (Entry<String, Set<String>> entry : _graph.getNeighbors().entrySet()) {
        String router = entry.getKey();
        Set<String> peers = entry.getValue();
        for (String peer : peers) {
            // sort names for unique
            String pair = (router.compareTo(peer) < 0 ? router + "_" + peer : peer + "_" + router);
            String name = getId() + "_FAILED-EDGE_" + pair;
            ArithExpr var = _ctx.mkIntConst(name);
            _symbolicFailures.getFailedInternalLinks().put(router, peer, var);
            _allVariables.put(var.toString(), var);
        }
    }
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) GraphEdge(org.batfish.symbolic.GraphEdge) Interface(org.batfish.datamodel.Interface)

Example 50 with Interface

use of org.batfish.datamodel.Interface in project batfish by batfish.

the class EncoderSlice method equalAreas.

/*
   * Creates a test to check for equal ospf areas
   * tags after accounting for null values introduced by optimizations
   */
private BoolExpr equalAreas(SymbolicRoute best, SymbolicRoute vars, @Nullable LogicalEdge e) {
    BoolExpr equalOspfArea;
    boolean hasBestArea = (best.getOspfArea() != null && best.getOspfArea().getBitVec() != null);
    boolean hasVarsArea = (vars.getOspfArea() != null && vars.getOspfArea().getBitVec() != null);
    if (e != null) {
        if (hasBestArea) {
            Interface iface = e.getEdge().getStart();
            if (hasVarsArea) {
                equalOspfArea = best.getOspfArea().mkEq(vars.getOspfArea());
            } else if (iface.getOspfAreaName() != null) {
                equalOspfArea = best.getOspfArea().checkIfValue(iface.getOspfAreaName());
            } else {
                equalOspfArea = best.getOspfArea().isDefaultValue();
            }
        } else {
            equalOspfArea = mkTrue();
        }
    } else {
        equalOspfArea = mkTrue();
    }
    return equalOspfArea;
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) Interface(org.batfish.datamodel.Interface)

Aggregations

Interface (org.batfish.datamodel.Interface)68 Configuration (org.batfish.datamodel.Configuration)42 Ip (org.batfish.datamodel.Ip)26 Edge (org.batfish.datamodel.Edge)21 Prefix (org.batfish.datamodel.Prefix)20 Test (org.junit.Test)19 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)18 Vrf (org.batfish.datamodel.Vrf)18 HashMap (java.util.HashMap)17 IpAccessList (org.batfish.datamodel.IpAccessList)16 Topology (org.batfish.datamodel.Topology)14 ArrayList (java.util.ArrayList)13 List (java.util.List)13 StaticRoute (org.batfish.datamodel.StaticRoute)13 HashSet (java.util.HashSet)12 Set (java.util.Set)12 BatfishException (org.batfish.common.BatfishException)12 Map (java.util.Map)11 TreeSet (java.util.TreeSet)10 SortedSet (java.util.SortedSet)9