Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MPLS push module #590

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bessctl/conf/samples/mpls_push.bess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scapy.all as scapy

eth = scapy.Ether(src='02:1e:67:9f:4d:ae', dst='06:16:3e:1b:72:32')
ip = scapy.IP(src='192.168.0.1', dst='10.0.0.1')
udp = scapy.UDP(sport=10001, dport=10002)
test_packet = bytes(eth/ip/udp)

mpls_push::MPLSPush()
mpls_push.set(label=7, ttl=64, tc=0, is_bottom_of_stack=True)

Source() -> Rewrite(templates=[test_packet]) -> mpls_push -> Sink()
52 changes: 52 additions & 0 deletions core/modules/mpls_push.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "mpls_push.h"

#include "../utils/endian.h"
#include "../utils/ether.h"
#include "../utils/mpls.h"

using bess::utils::Ethernet;
using bess::utils::be16_t;
using bess::utils::Mpls;

const Commands MPLSPush::cmds = {{"set", "MplsPushArg",
MODULE_CMD_FUNC(&MPLSPush::CommandSet),
Command::THREAD_UNSAFE}};

MPLSPush::MPLSPush() : label_(0), ttl_(64), tc_(0), is_bottom_of_stack_(true) {}


CommandResponse MPLSPush::Init(const bess::pb::MplsPushArg &arg) {
return CommandSet(arg);
}

void MPLSPush::ProcessBatch(bess::PacketBatch *batch) {
int cnt = batch->cnt();
for (int i = 0; i < cnt; i++) {
bess::Packet *pkt = batch->pkts()[i];
Ethernet *eth = pkt->head_data<Ethernet *>();

Ethernet::Address src_addr = eth->src_addr;
Ethernet::Address dst_addr = eth->dst_addr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh, sorry. Ignore my comment below. I didn't notice these lines.


pkt->prepend(4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All code south of here should be guarded by a check that this call to Packet::prepend() didn't fail.

eth = pkt->head_data<Ethernet *>();
eth->src_addr = src_addr;
eth->dst_addr = dst_addr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To the end of keeping the scope of this module narrow, might you just copy the original ethernet header here and change the ethertype? A sequence of SetMetadata and EtherEncap modules downstream of this module can take care of changing the addresses if needed.

eth->ether_type = be16_t(Ethernet::Type::kMpls);

Mpls *mpls_hdr = reinterpret_cast<Mpls *>(eth + 1);
mpls_hdr->SetEntry(label_, ttl_, tc_, is_bottom_of_stack_);
}

RunNextModule(batch);
}

CommandResponse MPLSPush::CommandSet(const bess::pb::MplsPushArg &arg) {
label_ = arg.label();
ttl_ = arg.ttl();
is_bottom_of_stack_ = arg.is_bottom_of_stack();
tc_ = arg.tc();
return CommandSuccess();
}

ADD_MODULE(MPLSPush, "mpls_push", "Push MPLS label")
29 changes: 29 additions & 0 deletions core/modules/mpls_push.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef BESS_MODULES_MPLSPUSH_H_
#define BESS_MODULES_MPLSPUSH_H_

#include "../module.h"
#include "../module_msg.pb.h"

class MPLSPush final : public Module {
public:
static const gate_idx_t kNumIGates = 1;
static const gate_idx_t kNumOGates = 1;

static const Commands cmds;

MPLSPush(); // constructor

CommandResponse Init(const bess::pb::MplsPushArg &arg);

void ProcessBatch(bess::PacketBatch *batch) override;

CommandResponse CommandSet(const bess::pb::MplsPushArg &arg);

private:
uint32_t label_;
uint8_t ttl_;
uint8_t tc_;
bool is_bottom_of_stack_;
};

#endif // BESS_MODULES_MPLSPUSH_H_
13 changes: 13 additions & 0 deletions protobuf/module_msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,16 @@ message MplsPopArg {
bool remove_eth_header = 1; // Remove ETH header with the pop
uint32 next_eth_type = 2; /// The next ETH type to set
}

/**
* The MPLS push module adds MPLS label
*
* __Input Gates__: 1
* __Output Gates__: 1
*/
message MplsPushArg {
uint32 label = 1;
uint32 ttl = 2;
uint32 tc = 3;
bool is_bottom_of_stack = 4;
}