/**
   * TODO This method must be moved to a layer below forwarding so that anyone can use it.
   *
   * @param packetData
   * @param sw
   * @param ports
   * @param cntx
   */
  @LogMessageDoc(
      level = "ERROR",
      message = "Failed to clear all flows on switch {switch}",
      explanation = "An I/O error occured while trying send " + "topology discovery packet",
      recommendation = LogMessageDoc.CHECK_SWITCH)
  public void doMultiActionPacketOut(
      byte[] packetData, IOFSwitch sw, Set<Short> ports, FloodlightContext cntx) {

    if (ports == null) return;
    if (packetData == null || packetData.length <= 0) return;

    OFPacketOut po =
        (OFPacketOut) floodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT);

    List<OFAction> actions = new ArrayList<OFAction>();
    for (short p : ports) {
      actions.add(new OFActionOutput(p, (short) 0));
    }

    // set actions
    po.setActions(actions);
    // set action length
    po.setActionsLength((short) (OFActionOutput.MINIMUM_LENGTH * ports.size()));
    // set buffer-id to BUFFER_ID_NONE
    po.setBufferId(OFPacketOut.BUFFER_ID_NONE);
    // set in-port to OFPP_NONE
    po.setInPort(OFPort.OFPP_NONE.getValue());

    // set packet data
    po.setPacketData(packetData);

    // compute and set packet length.
    short poLength =
        (short) (OFPacketOut.MINIMUM_LENGTH + po.getActionsLength() + packetData.length);

    po.setLength(poLength);

    try {
      // counterStore.updatePktOutFMCounterStore(sw, po);
      if (log.isTraceEnabled()) {
        log.trace(
            "write broadcast packet on switch-id={} " + "interaces={} packet-data={} packet-out={}",
            new Object[] {sw.getId(), ports, packetData, po});
      }
      sw.write(po, cntx);

    } catch (IOException e) {
      log.error("Failure writing packet out", e);
    }
  }
 static {
   FlowMod2 = new OFFlowMod();
   TestRule2 = new HashMap<String, Object>();
   TestRule2.put(COLUMN_NAME, "TestRule2");
   TestRule2.put(COLUMN_SWITCH, TestSwitch1DPID);
   // setup match
   OFMatch match = new OFMatch();
   TestRule2.put(COLUMN_NW_DST, "192.168.1.0/24");
   match.fromString("nw_dst=192.168.1.0/24");
   // setup actions
   List<OFAction> actions = new LinkedList<OFAction>();
   TestRule2.put(COLUMN_ACTIONS, "output=1");
   actions.add(new OFActionOutput((short) 1, (short) Short.MAX_VALUE));
   // done
   FlowMod2.setMatch(match);
   FlowMod2.setActions(actions);
   FlowMod2.setBufferId(-1);
   FlowMod2.setOutPort(OFPort.OFPP_NONE.getValue());
   FlowMod2.setPriority(Short.MAX_VALUE);
   FlowMod2.setLengthU(OFFlowMod.MINIMUM_LENGTH + 8); // 8 bytes of actions
 }
 static {
   FlowMod3 = new OFFlowMod();
   TestRule3 = new HashMap<String, Object>();
   TestRule3.put(COLUMN_NAME, "TestRule3");
   TestRule3.put(COLUMN_SWITCH, TestSwitch1DPID);
   // setup match
   OFMatch match = new OFMatch();
   TestRule3.put(COLUMN_DL_DST, "00:20:30:40:50:60");
   TestRule3.put(COLUMN_DL_VLAN, 4096);
   match.fromString("dl_dst=00:20:30:40:50:60,dl_vlan=4096");
   // setup actions
   TestRule3.put(COLUMN_ACTIONS, "output=controller");
   List<OFAction> actions = new LinkedList<OFAction>();
   actions.add(new OFActionOutput(OFPort.OFPP_CONTROLLER.getValue(), (short) Short.MAX_VALUE));
   // done
   FlowMod3.setMatch(match);
   FlowMod3.setActions(actions);
   FlowMod3.setBufferId(-1);
   FlowMod3.setOutPort(OFPort.OFPP_NONE.getValue());
   FlowMod3.setPriority(Short.MAX_VALUE);
   FlowMod3.setLengthU(OFFlowMod.MINIMUM_LENGTH + 8); // 8 bytes of actions
 }
Exemplo n.º 4
0
  /** Called periodically to retrieve all port statistics of all switches */
  @Override
  public synchronized void run() {
    // get a list of all the switches found
    Map<Long, IOFSwitch> switches = floodlightProvider.getSwitches();
    Collection<IOFSwitch> swList = switches.values();
    Iterator<IOFSwitch> it = swList.iterator();
    try {
      fw = new FileWriter("netstatslog.txt", true);
      bw = new BufferedWriter(fw);

      // for each switch, retrieve the statistics
      while (it.hasNext()) {
        IOFSwitch sw = it.next();
        List<OFStatistics> stats = new ArrayList<OFStatistics>();

        // make a statistics request
        OFStatisticsRequest req = new OFStatisticsRequest();
        req.setStatisticType(OFStatisticsType.PORT);
        req.setXid(sw.getNextTransactionId());

        // fill it with a specific request
        OFPortStatisticsRequest specReq = new OFPortStatisticsRequest();
        specReq.setPortNumber(OFPort.OFPP_NONE.getValue());
        stats.add((OFStatistics) specReq);
        req.setStatistics(stats);
        req.setLengthU(req.getLengthU() + specReq.getLength());

        // attempt to retrieve the statistics
        Future<List<OFStatistics>> future = null;
        List<OFStatistics> values = null;

        try {
          future = sw.getStatistics(req);
          values = future.get();
        } catch (Exception e) {
          System.err.println("Exception while retrieving statistics for switch: " + sw + " " + e);
        }

        // process the statistics
        if (!values.isEmpty()) {
          OFPortStatisticsReply reply = (OFPortStatisticsReply) values.get(0);

          // determine the new load
          long newload = 0;
          if (currentLoad.containsKey(sw.getId())) {
            // we've seen the switch before
            newload = reply.getTransmitBytes();
            loadHistory.put(sw.getId(), reply.getTransmitBytes());
          } else {
            // this is the first time we see the switch
            newload = reply.getTransmitBytes();
            loadHistory.put(sw.getId(), newload);
          }
          // System.out.println("Capa: " + sw.getCapabilities() * 1024);
          // System.out.println("Load: " + newload);
          currentLoad.put(sw.getId(), (sw.getCapabilities() * 1024) - newload);
          bw.write("\n id: " + sw.getId() + " ;load: " + ((sw.getCapabilities() * 1024) - newload));

          // System.out.println("Load for switch: " + currentLoad.get(sw.getId()));
        }
      }
      bw.write("\n******************");

      bw.close();
    } catch (Exception e) { // Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
Exemplo n.º 5
0
  @Override
  public void debug() {
    logger.debug("Nathan Debug called");

    // 从IFloodlightProviderService中获得连接的所有Switch
    Map<Long, IOFSwitch> switches = floodlightProvider.getSwitches();
    Collection<Long> sw_keys = switches.keySet();
    // sw_keys-所有switches的switch.getId()
    Iterator<Long> sw_key_iter = sw_keys.iterator();
    while (sw_key_iter.hasNext()) {
      Long sw_id = sw_key_iter.next();
      IOFSwitch sw = switches.get(sw_id);
      // 输出switch信息
      logger.debug("Got Switch: {}", sw);

      // 此处测试各类OFStatistics
      boolean ofp_desc_stats = true;
      boolean ofp_flow_stat = true;
      // ofp_desc_stat
      if (ofp_desc_stats) {
        OFStatisticsRequest request = new OFStatisticsRequest();
        request.setStatisticType(OFStatisticsType.DESC);
        try {
          logger.debug("request desc future");
          Future<List<OFStatistics>> future = sw.getStatistics(request);
          List<OFStatistics> list = future.get(10, TimeUnit.SECONDS);
          for (int i = 0; i < list.size(); i++) {
            OFDescriptionStatistics desc = (OFDescriptionStatistics) list.get(i);
            logger.info("Got Desc: {}", desc);
          }
        } catch (IOException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (ExecutionException e) {
          e.printStackTrace();
        } catch (TimeoutException e) {
          e.printStackTrace();
        }
      }
      // ofp_flow_stat
      if (ofp_flow_stat) {
        // flow request body
        OFFlowStatisticsRequest flow_request = new OFFlowStatisticsRequest();
        OFMatch match = new OFMatch();
        flow_request.setMatch(match);
        flow_request.setOutPort(OFPort.OFPP_NONE.getValue());
        flow_request.setTableId((byte) 0xFF);
        // statistics request
        OFStatisticsRequest request = new OFStatisticsRequest();
        request.setStatisticType(OFStatisticsType.FLOW);
        request.setStatistics(Collections.singletonList((OFStatistics) flow_request));
        // 这步非常重要,需要显示的增加request的长度,否则将出现越界错误
        int request_length = request.getLength() + flow_request.getLength();
        request.setLengthU(request_length);
        try {
          logger.debug("request flow future");
          Future<List<OFStatistics>> future = sw.getStatistics(request);
          List<OFStatistics> list = future.get(10, TimeUnit.SECONDS);
          for (int i = 0; i < list.size(); i++) {
            OFFlowStatisticsReply flow = (OFFlowStatisticsReply) list.get(i);
            logger.info("Got Flow: {}", flow);
          }
        } catch (IOException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (ExecutionException e) {
          e.printStackTrace();
        } catch (TimeoutException e) {
          e.printStackTrace();
        }
      }
    }

    // obtain all attached devices from IDeviceService
    Collection<? extends IDevice> devices = deviceService.getAllDevices();
    Iterator<? extends IDevice> dev_iter = devices.iterator();
    while (dev_iter.hasNext()) {
      IDevice dev = dev_iter.next();
      logger.info("Got Device: {}", dev);
    }
    // obtain all links between switches from ILinkDiscoveryService
    Map<Link, LinkInfo> link_map = linkService.getLinks();
    Collection<Link> links = link_map.keySet();
    // links contains all links, and it can be used as key to get its corresponding LinkInfo
    Iterator<Link> link_iter = links.iterator();
    while (link_iter.hasNext()) {
      Link link = link_iter.next();
      logger.info("Got Link: {}" + link);
    }
  }