Ejemplo n.º 1
0
  @Override
  public Status programIpRewriteExclusion(
      Long dpid, String segmentationId, String excludedCidr, Action action) {
    String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;

    MatchBuilder matchBuilder = new MatchBuilder();
    NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);

    // Instructions List Stores Individual Instructions
    InstructionsBuilder isb = new InstructionsBuilder();
    List<Instruction> instructions = Lists.newArrayList();
    InstructionBuilder ib;

    MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
    String ipAddress = excludedCidr.substring(0, excludedCidr.indexOf("/"));
    InetAddress inetAddress;
    try {
      inetAddress = InetAddress.getByName(ipAddress);
    } catch (UnknownHostException e) {
      return new Status(StatusCode.BADREQUEST);
    }
    if (inetAddress instanceof Inet6Address) {
      // WORKAROUND: For now ipv6 is not supported
      // TODO: implement ipv6 cidr case
      LOG.debug("ipv6 cidr is not implemented yet. cidr {}", excludedCidr);
      return new Status(StatusCode.NOTIMPLEMENTED);
    }
    MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));

    // Goto Next Table
    ib = getMutablePipelineInstructionBuilder();
    ib.setOrder(0);
    ib.setKey(new InstructionKey(0));
    instructions.add(ib.build());

    FlowBuilder flowBuilder = new FlowBuilder();
    flowBuilder.setMatch(matchBuilder.build());
    flowBuilder.setInstructions(isb.setInstruction(instructions).build());

    String flowId = "InboundNATExclusion_" + segmentationId + "_" + excludedCidr;
    flowBuilder.setId(new FlowId(flowId));
    FlowKey key = new FlowKey(new FlowId(flowId));
    flowBuilder.setBarrier(true);
    flowBuilder.setTableId(this.getTable());
    flowBuilder.setKey(key);
    flowBuilder.setPriority(1024);
    flowBuilder.setFlowName(flowId);
    flowBuilder.setHardTimeout(0);
    flowBuilder.setIdleTimeout(0);

    if (action.equals(Action.ADD)) {
      writeFlow(flowBuilder, nodeBuilder);
    } else {
      removeFlow(flowBuilder, nodeBuilder);
    }

    // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
    return new Status(StatusCode.SUCCESS);
  }
Ejemplo n.º 2
0
 /**
  * Write a drop flow for the given ethertype at the given priority. If the ethertype is null, then
  * drop all traffic
  */
 public Flow dropFlow(Integer priority, Long etherType, Short tableId) {
   FlowId flowid;
   FlowBuilder flowb = base().setPriority(priority).setInstructions(FlowUtils.dropInstructions());
   if (etherType != null) {
     MatchBuilder mb =
         new MatchBuilder().setEthernetMatch(FlowUtils.ethernetMatch(null, null, etherType));
     Match match = mb.build();
     flowid = FlowIdUtils.newFlowId(tableId, "drop", match);
     flowb.setMatch(match);
   } else {
     flowid = FlowIdUtils.newFlowId("dropAll");
   }
   flowb.setId(flowid);
   return flowb.build();
 }
Ejemplo n.º 3
0
  @Override
  public Status programIpRewriteRule(
      Long dpid,
      Long inPort,
      String destSegId,
      InetAddress matchAddress,
      InetAddress rewriteAddress,
      Action action) {
    String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;

    MatchBuilder matchBuilder = new MatchBuilder();
    NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);

    // Instructions List Stores Individual Instructions
    InstructionsBuilder isb = new InstructionsBuilder();
    List<Instruction> instructions = Lists.newArrayList();
    InstructionBuilder ib = new InstructionBuilder();

    MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
    MatchUtils.createDstL3IPv4Match(
        matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));

    // Set register to indicate that rewrite took place
    ActionBuilder actionBuilder = new ActionBuilder();
    actionBuilder.setAction(
        ActionUtils.nxLoadRegAction(
            new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(), new BigInteger(destSegId)));

    // Set Dest IP address and set REG_FIELD
    InstructionUtils.createNwDstInstructions(
        ib, MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()), actionBuilder);
    ib.setOrder(0);
    ib.setKey(new InstructionKey(0));
    instructions.add(ib.build());

    // Goto Next Table
    ib = getMutablePipelineInstructionBuilder();
    ib.setOrder(1);
    ib.setKey(new InstructionKey(1));
    instructions.add(ib.build());

    FlowBuilder flowBuilder = new FlowBuilder();
    flowBuilder.setMatch(matchBuilder.build());
    flowBuilder.setInstructions(isb.setInstruction(instructions).build());

    String flowId = "InboundNAT_" + inPort + "_" + destSegId + "_" + matchAddress.getHostAddress();
    flowBuilder.setId(new FlowId(flowId));
    FlowKey key = new FlowKey(new FlowId(flowId));
    flowBuilder.setBarrier(true);
    flowBuilder.setTableId(this.getTable());
    flowBuilder.setKey(key);
    flowBuilder.setPriority(1024);
    flowBuilder.setFlowName(flowId);
    flowBuilder.setHardTimeout(0);
    flowBuilder.setIdleTimeout(0);

    if (action.equals(Action.ADD)) {
      writeFlow(flowBuilder, nodeBuilder);
    } else {
      removeFlow(flowBuilder, nodeBuilder);
    }

    // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
    return new Status(StatusCode.SUCCESS);
  }
  /**
   * Test {@link MultipartReplyTranslator#translate(SwitchConnectionDistinguisher, SessionContext,
   * OfHeader)} with experimenter MultipartReply message
   */
  @Test
  public void testFlowCase() {
    MultipartReplyMessageBuilder mpBuilder = new MultipartReplyMessageBuilder();
    mpBuilder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
    mpBuilder.setXid(123L);
    mpBuilder.setFlags(new MultipartRequestFlags(false));
    mpBuilder.setType(MultipartType.OFPMPFLOW);

    MultipartReplyFlowCaseBuilder caseBuilder = new MultipartReplyFlowCaseBuilder();
    MultipartReplyFlowBuilder flowBuilder = new MultipartReplyFlowBuilder();
    List<FlowStats> flowStats = new ArrayList<>();
    FlowStatsBuilder statsBuilder = new FlowStatsBuilder();
    statsBuilder.setTableId((short) 1);
    statsBuilder.setDurationSec(2L);
    statsBuilder.setDurationNsec(3L);
    statsBuilder.setPriority(4);
    statsBuilder.setIdleTimeout(5);
    statsBuilder.setHardTimeout(6);
    FlowModFlags flags = new FlowModFlags(true, false, true, false, true);
    statsBuilder.setFlags(flags);
    statsBuilder.setCookie(new BigInteger("7"));
    statsBuilder.setPacketCount(new BigInteger("8"));
    statsBuilder.setByteCount(new BigInteger("9"));
    MatchBuilder matchBuilder = new MatchBuilder();
    matchBuilder.setType(OxmMatchType.class);
    matchBuilder.setMatchEntry(new ArrayList<MatchEntry>());
    statsBuilder.setMatch(matchBuilder.build());
    statsBuilder.setInstruction(new ArrayList<Instruction>());
    flowStats.add(statsBuilder.build());
    statsBuilder = new FlowStatsBuilder();
    statsBuilder.setTableId((short) 10);
    statsBuilder.setDurationSec(20L);
    statsBuilder.setDurationNsec(30L);
    statsBuilder.setPriority(40);
    statsBuilder.setIdleTimeout(50);
    statsBuilder.setHardTimeout(60);
    flags = new FlowModFlags(false, true, false, true, false);
    statsBuilder.setFlags(flags);
    statsBuilder.setCookie(new BigInteger("70"));
    statsBuilder.setPacketCount(new BigInteger("80"));
    statsBuilder.setByteCount(new BigInteger("90"));
    matchBuilder = new MatchBuilder();
    matchBuilder.setType(OxmMatchType.class);
    matchBuilder.setMatchEntry(new ArrayList<MatchEntry>());
    statsBuilder.setMatch(matchBuilder.build());
    statsBuilder.setInstruction(new ArrayList<Instruction>());
    flowStats.add(statsBuilder.build());
    flowBuilder.setFlowStats(flowStats);
    caseBuilder.setMultipartReplyFlow(flowBuilder.build());
    mpBuilder.setMultipartReplyBody(caseBuilder.build());
    MultipartReplyMessage message = mpBuilder.build();

    List<DataObject> list = translator.translate(cookie, sc, message);

    Assert.assertEquals("Wrong list size", 1, list.size());
    FlowsStatisticsUpdate flowUpdate = (FlowsStatisticsUpdate) list.get(0);
    Assert.assertEquals("Wrong node-id", "openflow:42", flowUpdate.getId().getValue());
    Assert.assertEquals("Wrong more-replies", false, flowUpdate.isMoreReplies());
    Assert.assertEquals(
        "Wrong transaction-id", 123, flowUpdate.getTransactionId().getValue().intValue());
    List<FlowAndStatisticsMapList> mapList = flowUpdate.getFlowAndStatisticsMapList();
    Assert.assertEquals("Wrong flow stats size", 2, mapList.size());
    FlowAndStatisticsMapList stat = mapList.get(0);
    Assert.assertEquals("Wrong table-id", 1, stat.getTableId().intValue());
    Assert.assertEquals(
        "Wrong duration sec", 2, stat.getDuration().getSecond().getValue().intValue());
    Assert.assertEquals(
        "Wrong duration n sec", 3, stat.getDuration().getNanosecond().getValue().intValue());
    Assert.assertEquals("Wrong priority", 4, stat.getPriority().intValue());
    Assert.assertEquals("Wrong idle-timeout", 5, stat.getIdleTimeout().intValue());
    Assert.assertEquals("Wrong hard-timeout", 6, stat.getHardTimeout().intValue());
    org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags expectedFlags =
        new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags(
            !flags.isOFPFFCHECKOVERLAP(),
            !flags.isOFPFFNOBYTCOUNTS(),
            !flags.isOFPFFNOPKTCOUNTS(),
            !flags.isOFPFFRESETCOUNTS(),
            !flags.isOFPFFSENDFLOWREM());
    Assert.assertEquals("Wrong flags", expectedFlags, stat.getFlags());
    Assert.assertEquals("Wrong cookie", 7, stat.getCookie().getValue().intValue());
    Assert.assertEquals("Wrong packet count", 8, stat.getPacketCount().getValue().intValue());
    Assert.assertEquals("Wrong byte count", 9, stat.getByteCount().getValue().intValue());
    org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder
        emptyMatchBuilder =
            new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow
                .MatchBuilder();
    Match emptyMatch = emptyMatchBuilder.build();
    Assert.assertEquals("Wrong match", emptyMatch, stat.getMatch());
    Assert.assertEquals("Wrong instructions", 0, stat.getInstructions().getInstruction().size());
    stat = mapList.get(1);
    Assert.assertEquals("Wrong table-id", 10, stat.getTableId().intValue());
    Assert.assertEquals(
        "Wrong duration sec", 20, stat.getDuration().getSecond().getValue().intValue());
    Assert.assertEquals(
        "Wrong duration n sec", 30, stat.getDuration().getNanosecond().getValue().intValue());
    Assert.assertEquals("Wrong priority", 40, stat.getPriority().intValue());
    Assert.assertEquals("Wrong idle-timeout", 50, stat.getIdleTimeout().intValue());
    Assert.assertEquals("Wrong hard-timeout", 60, stat.getHardTimeout().intValue());
    expectedFlags =
        new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags(
            flags.isOFPFFCHECKOVERLAP(),
            flags.isOFPFFNOBYTCOUNTS(),
            flags.isOFPFFNOPKTCOUNTS(),
            flags.isOFPFFRESETCOUNTS(),
            flags.isOFPFFSENDFLOWREM());
    Assert.assertEquals("Wrong flags", expectedFlags, stat.getFlags());
    Assert.assertEquals("Wrong cookie", 70, stat.getCookie().getValue().intValue());
    Assert.assertEquals("Wrong packet count", 80, stat.getPacketCount().getValue().intValue());
    Assert.assertEquals("Wrong byte count", 90, stat.getByteCount().getValue().intValue());
    Assert.assertEquals("Wrong match", emptyMatch, stat.getMatch());
    Assert.assertEquals("Wrong instructions", 0, stat.getInstructions().getInstruction().size());
  }