0

I am running an application in port 80 in a container. Now I want to call it by using port 90 and xdp will change the port from 90 to 80. But for some reason, I am getting any response or server is not getting any call either. Here is my ebf code:

static inline unsigned short checksum(unsigned short *buf, int bufsz) {
    unsigned long sum = 0;

    while (bufsz > 1) {
        sum += *buf;
        buf++;
        bufsz -= 2;
    }

    if (bufsz == 1) {
        sum += *(unsigned char *)buf;
    }

    sum = (sum & 0xffff) + (sum >> 16);
    sum = (sum & 0xffff) + (sum >> 16);

    return ~sum;
}

int tcpfilter(struct xdp_md *ctx) {
  bpf_trace_printk("got a packet\n");
  void *data = (void *)(long)ctx->data;
  void *data_end = (void *)(long)ctx->data_end;
  struct ethhdr *eth = data;
  if ((void*)eth + sizeof(*eth) <= data_end) {
    struct iphdr *ip = data + sizeof(*eth);
    if ((void*)ip + sizeof(*ip) <= data_end) {
      if (ip->protocol == IPPROTO_TCP) {
        struct tcphdr *tcp = (void*)ip + sizeof(*ip);
        if ((void*)tcp + sizeof(*tcp) <= data_end) {
          if (tcp->dest == ntohs(90)) {
            bpf_trace_printk("tcp  port 90\n");
            tcp->dest = ntohs(80);
            tcp->check=0;
            tcp->check = checksum((unsigned short *)tcp, sizeof(struct tcphdr));
            }
        }
      }
    }
  }
  return XDP_PASS;
}

FYI I am using bcc library and created an issue there as well. Any suggestions would be greatly appreciated. https://github.com/iovisor/bcc/issues/3829

Note: I am using loopback lo interface.

Omar Faroque Anik
  • 2,531
  • 1
  • 29
  • 42
  • Is your XDP program correctly loaded and attached? (`bpftool prog`, `bpftool net` should show it, `ip link show dev lo` too). Does it receive the packet and is it running? Check [program stats](https://qmonnet.github.io/whirl-offload/2021/09/23/bpftool-features-thread/#programs-statistics), or check the output from `bpf_trace_prink()`. Any output at all? If not, did you try adding more `bpf_trace_prink()` earlier in your program to debug the flow and see where it returns (if your program runs at all)? Did you run `tcpdump` in your container to see what you're receiving (port/checksum)? – Qeole Jan 29 '22 at 23:44
  • Yes program load correctly. Yes ran tcpdump but seems like handshake does not happen properly with application sever. I doubt when server tries to establish the connection, it is not aware that port has changed. Server tries to establish connection on port 80 but client calls for port 90. So May be that’s the problem. What do you think? I am very new to the space. Thanks a lot. – Omar Faroque Anik Jan 30 '22 at 00:05
  • `seems like handshake does not happen properly with application sever` -> What does this mean? Does your server receive TCP packets on port 80 (meaning that your XDP program does the job), or does it receive them on port 90 (something went wrong), or does it not see them at all (something wrong also)? Do the checksums look correct? – Qeole Jan 30 '22 at 23:05

0 Answers0