Skip to content

Commit

Permalink
second commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ami-GS committed Sep 20, 2024
1 parent 390597a commit e2a3ad4
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 61 deletions.
29 changes: 20 additions & 9 deletions src/platform/datapath_raw_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#pragma warning(disable:4116) // unnamed type definition in parentheses
#pragma warning(disable:4100) // unreferenced formal parameter

static int ClientPort;

uint32_t
CxPlatGetRawSocketSize(void) {
Expand Down Expand Up @@ -179,7 +180,15 @@ CxPlatDpRawParseUdp(
Packet->Reserved = L4_TYPE_UDP;

Packet->Route->RemoteAddress.Ipv4.sin_port = Udp->SourcePort;
Packet->Route->LocalAddress.Ipv4.sin_port = Udp->DestinationPort;
if (htons(55555) == Udp->SourcePort) {
fprintf(stderr, "Source port 55555 arrived\n");
}
if (htons(55555) == Udp->DestinationPort) {
fprintf(stderr, "Destination port 55555 arrived, rewrite to %d\n", ntohs(ClientPort));
Packet->Route->LocalAddress.Ipv4.sin_port = ClientPort;
} else {
Packet->Route->LocalAddress.Ipv4.sin_port = Udp->DestinationPort;
}

Packet->Buffer = (uint8_t*)Udp->Data;
Packet->BufferLength = QuicNetByteSwapShort(Udp->Length) - sizeof(UDP_HEADER);
Expand Down Expand Up @@ -737,23 +746,25 @@ CxPlatFramingWriteHeaders(
UDP = (UDP_HEADER*)(Buffer->Buffer - sizeof(UDP_HEADER));
UDP->DestinationPort = Route->RemoteAddress.Ipv4.sin_port;

if (true)
// if (false)
char* EnvPath = getenv("MSQUIC_FAKE_NAT_REBINDING");
if (EnvPath && EnvPath[0] == '1')
{
static BOOLEAN toggleSwitch = FALSE;
static BOOLEAN toggleRebinding = FALSE;
static uint64_t waitUntil = 1000;
static uint64_t count = 0;
if (FakeNatRebinding && !Socket->ServerOwned) {
if (toggleSwitch) {
// set to 55555
if (toggleRebinding) {
fprintf(stderr, "FakeNatRebinding. set to 55555 (%d)\n", htons(55555));
ClientPort = Route->LocalAddress.Ipv4.sin_port;
UDP->SourcePort = htons(55555);
} else {
fprintf(stderr, "No FakeNatRebinding. set to %d (%d)\n", ntohs(Route->LocalAddress.Ipv4.sin_port), Route->LocalAddress.Ipv4.sin_port);
UDP->SourcePort = Route->LocalAddress.Ipv4.sin_port;
}
count++;
if (count == 5) {
count = 0;
toggleSwitch = !toggleSwitch;
if (count > waitUntil && count % 2 == 0) {
count = waitUntil;
toggleRebinding = !toggleRebinding;
}
} else {
UDP->SourcePort = Route->LocalAddress.Ipv4.sin_port;
Expand Down
44 changes: 41 additions & 3 deletions src/platform/datapath_raw_xdp_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,15 @@ CxPlatDpRawPlumbRulesOnSocket(
XdpSetPortFails,
"[ xdp] Failed to set port %d on %s", port, Interface->IfName);
}
char* FakeNatRebinding = getenv("MSQUIC_FAKE_NAT_REBINDING");
if (FakeNatRebinding && FakeNatRebinding[0] == '1') {
port = htons(55555);
if (bpf_map_update_elem(bpf_map__fd(port_map), &port, &exist, BPF_ANY)) {
QuicTraceLogVerbose(
XdpSetPortFails,
"[ xdp] Failed to set port %d on %s", port, Interface->IfName);
}
}
} else {
if (bpf_map_delete_elem(bpf_map__fd(port_map), &port)) {
QuicTraceLogVerbose(
Expand Down Expand Up @@ -994,7 +1003,6 @@ CxPlatDpRawPlumbRulesOnSocket(
}
}


// Debug info
// TODO: set flag to enable dump in xdp program
struct bpf_map *ifname_map = bpf_object__find_map_by_name(xdp_program__bpf_obj(Interface->XdpProg), "ifname_map");
Expand Down Expand Up @@ -1031,11 +1039,41 @@ CxPlatDpRawPlumbRulesOnSocket(
struct bpf_map *feature_map = bpf_object__find_map_by_name(xdp_program__bpf_obj(Interface->XdpProg), "feature_map");
if (feature_map) {
uint8_t key = 0;
uint8_t val = 0x03; // CID_RSS 0x01, DROP_PATH_CHALLENGE 0x02
uint8_t val = 0b11100000; // TODO: set appropriate value
char* NormalDatapath = getenv("MSQUIC_XDP_NORMAL_DATAPATH");
if (NormalDatapath && NormalDatapath[0] == '1') {
fprintf(stderr, "Use normal datapath %c\n", NormalDatapath[0]);
val |= 0x04;
}
char* DropPathChallenge = getenv("MSQUIC_XDP_DROP_PATH_CHALLENGE");
if (DropPathChallenge && DropPathChallenge[0] == '1') {
fprintf(stderr, "Use drop path challenge %c\n", DropPathChallenge[0]);
val |= 0x02;
}
char* RssType = getenv("MSQUIC_XDP_RSS_TYPE");
if (RssType) {
fprintf(stderr, "RSS type %s\n", RssType);
val |= atoi(RssType); // 0x01: MAP, 0x08: HASH
}
if (bpf_map_update_elem(bpf_map__fd(feature_map), &key, &val, BPF_ANY)) {
fprintf(stderr, "Failed to set feature %d on %s\n", key, Interface->IfName);
} else {
fprintf(stderr, "Set feature %d on %s\n", key, Interface->IfName);
fprintf(stderr, "Set feature %02x on %s\n", val, Interface->IfName);
}
}

struct bpf_map *cpu_map = bpf_object__find_map_by_name(xdp_program__bpf_obj(Interface->XdpProg), "cpu_map");
if (cpu_map && IsCreated) {
struct bpf_cpumap_val value = {
.qsize = 2048,
.bpf_prog.fd = 0,
};
for (__u32 cpu = 0; cpu < 8; cpu++) {
if (bpf_map_update_elem(bpf_map__fd(cpu_map), &cpu, &value, BPF_ANY)) {
fprintf(stderr, "Failed to set CPUMAP %d on %s\n", cpu, Interface->IfName);
} else {
fprintf(stderr, "Set CPUMAP %d on %s\n", cpu, Interface->IfName);
}
}
}
}
Expand Down
Loading

0 comments on commit e2a3ad4

Please sign in to comment.