1 year ago

#107277

test-img

Omar Faroque Anik

Update destination port with bcc and xdp

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.

c

bcc-bpf

xdp-bpf

xdp-ebpf

0 Answers

Your Answer

Accepted video resources