<commit_before>/* Copyright 2013-present Barefoot Networks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * <NAME> (<EMAIL>)
 *
 */

#ifndef SIMPLE_SWITCH_SIMPLE_SWITCH_H_
#define SIMPLE_SWITCH_SIMPLE_SWITCH_H_

#include <memory>
#include <chrono>
#include <thread>
#include <vector>

#include "bm_sim/queue.h"
#include "bm_sim/queueing.h"
#include "bm_sim/packet.h"
#include "bm_sim/switch.h"
#include "bm_sim/event_logger.h"
#include "bm_sim/simple_pre_lag.h"

// TODO(antonin)
// experimental support for priority queueing
// to enable it, uncomment this flag
// you can also choose the field from which the priority value will be read, as
// well as the number of priority queues per port
// PRIORITY 0 IS THE HIGHEST PRIORITY
// #define SSWITCH_PRIORITY_QUEUEING_ON

#ifdef SSWITCH_PRIORITY_QUEUEING_ON
#define SSWITCH_PRIORITY_QUEUEING_NB_QUEUES 2
#define SSWITCH_PRIORITY_QUEUEING_SRC "intrinsic_metadata.priority"
#endif

using ts_res = std::chrono::microseconds;
using std::chrono::duration_cast;
using ticks = std::chrono::nanoseconds;

using bm::Switch;
using bm::Queue;
using bm::Packet;
using bm::PHV;
using bm::Parser;
using bm::Deparser;
using bm::Pipeline;
using bm::McSimplePreLAG;
using bm::Field;
using bm::FieldList;
using bm::packet_id_t;
using bm::p4object_id_t;


class SimpleSwitch : public Switch {
 public:
  typedef int mirror_id_t;

 private:
  typedef std::chrono::high_resolution_clock clock;

 public:
  // by default, swapping is off
  explicit SimpleSwitch(int max_port = 256, bool enable_swap = false);

  int receive(int port_num, const char *buffer, int len) {
    static int pkt_id = 0;

    // this is a good place to call this, because blocking this thread will not
    // block the processing of existing packet instances, which is a requirement
    do_swap();

    auto packet = new_packet_ptr(port_num, pkt_id++, len,
                                 bm::PacketBuffer(2048, buffer, len));

    BMELOG(packet_in, *packet);

    PHV *phv = packet->get_phv();
    // many current P4 programs assume this
    // it is also part of the original P4 spec
    phv->reset_metadata();

    // setting standard metadata
    phv->get_field("standard_metadata.ingress_port").set(port_num);
    phv->get_field("standard_metadata.packet_length").set(len);
    Field &f_instance_type = phv->get_field("standard_metadata.instance_type");<commit_msg>removed the 2048 hard-coded limit on packet size for simple_switch<commit_after>/* Copyright 2013-present Barefoot Networks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * <NAME> (<EMAIL>)
 *
 */

#ifndef SIMPLE_SWITCH_SIMPLE_SWITCH_H_
#define SIMPLE_SWITCH_SIMPLE_SWITCH_H_

#include <memory>
#include <chrono>
#include <thread>
#include <vector>

#include "bm_sim/queue.h"
#include "bm_sim/queueing.h"
#include "bm_sim/packet.h"
#include "bm_sim/switch.h"
#include "bm_sim/event_logger.h"
#include "bm_sim/simple_pre_lag.h"

// TODO(antonin)
// experimental support for priority queueing
// to enable it, uncomment this flag
// you can also choose the field from which the priority value will be read, as
// well as the number of priority queues per port
// PRIORITY 0 IS THE HIGHEST PRIORITY
// #define SSWITCH_PRIORITY_QUEUEING_ON

#ifdef SSWITCH_PRIORITY_QUEUEING_ON
#define SSWITCH_PRIORITY_QUEUEING_NB_QUEUES 2
#define SSWITCH_PRIORITY_QUEUEING_SRC "intrinsic_metadata.priority"
#endif

using ts_res = std::chrono::microseconds;
using std::chrono::duration_cast;
using ticks = std::chrono::nanoseconds;

using bm::Switch;
using bm::Queue;
using bm::Packet;
using bm::PHV;
using bm::Parser;
using bm::Deparser;
using bm::Pipeline;
using bm::McSimplePreLAG;
using bm::Field;
using bm::FieldList;
using bm::packet_id_t;
using bm::p4object_id_t;


class SimpleSwitch : public Switch {
 public:
  typedef int mirror_id_t;

 private:
  typedef std::chrono::high_resolution_clock clock;

 public:
  // by default, swapping is off
  explicit SimpleSwitch(int max_port = 256, bool enable_swap = false);

  int receive(int port_num, const char *buffer, int len) {
    static int pkt_id = 0;

    // this is a good place to call this, because blocking this thread will not
    // block the processing of existing packet instances, which is a requirement
    do_swap();

    // we limit the packet buffer to original size + 512 bytes, which means we
    // cannot add more than 512 bytes of header data to the packet, which should
    // be more than enough
    auto packet = new_packet_ptr(port_num, pkt_id++, len,
                                 bm::PacketBuffer(len + 512, buffer, len));

    BMELOG(packet_in, *packet);

    PHV *phv = packet->get_phv();
    // many current P4 programs assume this
    // it is also part of the original P4 spec
    phv->reset_metadata();

    // setting standard metadata
    phv->get_field("standard_metadata.ingress_port").set(port_num);
    phv->get_field("standard_metadata.packet_length").set(len);
    Field &f_instance_type = phv->get_field("standard_metadata.instance_type");
{'fraction_non_alphanumeric': 0.09716934282403576, 'fraction_numerical': 0.008442310875682833, 'mean_word_length': 4.272251308900524, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5613338', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1897, 'n_words': 631}