int bpf_iw(struct bpf_sock_ops *skops) { int bufsize = 1500000; int rwnd_init = 40; int iw = 40; int rv = 0; int op; /* For testing purposes, only execute rest of BPF program * if neither port numberis 55601 */ if (bpf_ntohl(skops->remote_port) != 55601 && skops->local_port != 55601) { skops->reply = -1; return 1; } op = (int) skops->op; #ifdef DEBUG bpf_printk("BPF command: %d\n", op); #endif /* Usually there would be a check to insure the hosts are far * from each other so it makes sense to increase buffer sizes */ switch (op) { case BPF_SOCK_OPS_RWND_INIT: rv = rwnd_init; break; case BPF_SOCK_OPS_TCP_CONNECT_CB: /* Set sndbuf and rcvbuf of active connections */ rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)); rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize)); break; case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: rv = bpf_setsockopt(skops, SOL_TCP, TCP_BPF_IW, &iw, sizeof(iw)); break; case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: /* Set sndbuf and rcvbuf of passive connections */ rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)); rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize)); break; default: rv = -1; } #ifdef DEBUG bpf_printk("Returning %d\n", rv); #endif skops->reply = rv; return 1; }
int bpf_sockmap(struct bpf_sock_ops *skops) { __u32 lport, rport; int op, err = 0, index, key, ret; op = (int) skops->op; switch (op) { case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: lport = skops->local_port; rport = skops->remote_port; if (lport == 10000) { ret = 1; err = bpf_sock_map_update(skops, &sock_map, &ret, BPF_NOEXIST); bpf_printk("passive(%i -> %i) map ctx update err: %d\n", lport, bpf_ntohl(rport), err); } break; case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: lport = skops->local_port; rport = skops->remote_port; if (bpf_ntohl(rport) == 10001) { ret = 10; err = bpf_sock_map_update(skops, &sock_map, &ret, BPF_NOEXIST); bpf_printk("active(%i -> %i) map ctx update err: %d\n", lport, bpf_ntohl(rport), err); } break; default: break; } return 0; }
int bpf_synrto(struct bpf_sock_ops *skops) { int rv = -1; int op; /* For testing purposes, only execute rest of BPF program * if neither port numberis 55601 */ if (bpf_ntohl(skops->remote_port) != 55601 && skops->local_port != 55601) { skops->reply = -1; return 1; } op = (int) skops->op; #ifdef DEBUG bpf_printk("BPF command: %d\n", op); #endif /* Check for TIMEOUT_INIT operation and IPv6 addresses */ if (op == BPF_SOCK_OPS_TIMEOUT_INIT && skops->family == AF_INET6) { /* If the first 5.5 bytes of the IPv6 address are the same * then both hosts are in the same datacenter * so use an RTO of 10ms */ if (skops->local_ip6[0] == skops->remote_ip6[0] && (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) == (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) rv = 10; } #ifdef DEBUG bpf_printk("Returning %d\n", rv); #endif skops->reply = rv; return 1; }
int bpf_prog2(struct __sk_buff *skb) { __u32 lport = skb->local_port; __u32 rport = skb->remote_port; int ret = 0; if (lport == 10000) ret = 10; else ret = 1; bpf_printk("sockmap: %d -> %d @ %d\n", lport, bpf_ntohl(rport), ret); return bpf_sk_redirect_map(&sock_map, ret, 0); }
int bpf_testcb(struct bpf_sock_ops *skops) { int rv = -1; int op; op = (int) skops->op; if (bpf_ntohl(skops->remote_port) != TESTPORT) { skops->reply = -1; return 0; } switch (op) { case BPF_SOCK_OPS_TIMEOUT_INIT: case BPF_SOCK_OPS_RWND_INIT: case BPF_SOCK_OPS_NEEDS_ECN: case BPF_SOCK_OPS_BASE_RTT: case BPF_SOCK_OPS_RTO_CB: rv = 1; break; case BPF_SOCK_OPS_TCP_CONNECT_CB: case BPF_SOCK_OPS_TCP_LISTEN_CB: case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: bpf_sock_ops_cb_flags_set(skops, (BPF_SOCK_OPS_RETRANS_CB_FLAG| BPF_SOCK_OPS_RTO_CB_FLAG)); rv = 1; break; case BPF_SOCK_OPS_RETRANS_CB: { __u32 key = 0; struct tcpnotify_globals g, *gp; struct tcp_notifier msg = { .type = 0xde, .subtype = 0xad, .source = 0xbe, .hash = 0xef, }; rv = 1; /* Update results */ gp = bpf_map_lookup_elem(&global_map, &key); if (!gp) break; g = *gp; g.total_retrans = skops->total_retrans; g.ncalls++; bpf_map_update_elem(&global_map, &key, &g, BPF_ANY); bpf_perf_event_output(skops, &perf_event_map, BPF_F_CURRENT_CPU, &msg, sizeof(msg)); } break; default: rv = -1; } skops->reply = rv; return 1; } char _license[] SEC("license") = "GPL";