2 #include <sys/socket.h>
5 #include <linux/if_ether.h>
17 if (unlikely(af
!= AF_INET
&& af
!= AF_INET6
))
18 panic("Wrong AF socket type!\n");
20 sock
= socket(af
, SOCK_DGRAM
, 0);
21 if (unlikely(sock
< 0))
22 panic("Creation AF socket failed: %s\n", strerror(errno
));
29 int sock
= socket(PF_PACKET
, SOCK_RAW
, 0);
30 if (unlikely(sock
< 0))
31 panic("Creation of PF socket failed: %s\n", strerror(errno
));
36 /* Avail in kernel >= 3.14
37 * in commit d346a3fae3 (packet: introduce PACKET_QDISC_BYPASS socket option)
39 void set_sock_qdisc_bypass(int fd
, int verbose
)
43 ret
= setsockopt(fd
, SOL_PACKET
, PACKET_QDISC_BYPASS
, &val
, sizeof(val
));
45 if (errno
== ENOPROTOOPT
) {
47 printf("No kernel support for PACKET_QDISC_BYPASS"
48 " (kernel < 3.14?)\n");
50 perror("Cannot set PACKET_QDISC_BYPASS");
52 if (verbose
) printf("Enabled kernel qdisc bypass\n");
55 void set_sock_prio(int fd
, int prio
)
59 ret
= setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &val
, sizeof(val
));
61 panic("Cannot set socket priority: %s\n", strerror(errno
));
64 void set_nonblocking(int fd
)
66 int ret
= fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFD
, 0) | O_NONBLOCK
);
67 if (unlikely(ret
< 0))
68 panic("Cannot fcntl: %s\n", strerror(errno
));
71 int set_nonblocking_sloppy(int fd
)
73 return fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFD
, 0) | O_NONBLOCK
);
76 void set_socket_keepalive(int fd
)
80 ret
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &one
, sizeof(one
));
82 panic("Cannot set TCP keepalive: %s\n", strerror(errno
));
85 void set_tcp_nodelay(int fd
)
89 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &one
, sizeof(one
));
91 panic("Cannot set TCP nodelay: %s\n", strerror(errno
));
94 int set_ipv6_only(int fd
)
97 return setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &one
, sizeof(one
));
100 int set_reuseaddr(int fd
)
104 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
105 if (unlikely(ret
< 0))
106 panic("Cannot reuse addr: %s\n", strerror(errno
));
111 void set_mtu_disc_dont(int fd
)
113 int mtu
= IP_PMTUDISC_DONT
, ret
;
115 ret
= setsockopt(fd
, SOL_IP
, IP_MTU_DISCOVER
, &mtu
, sizeof(mtu
));
117 panic("Cannot set MTU discovery options: %s\n", strerror(errno
));
127 #define SMEM_SUG_MAX 104857600
128 #define SMEM_SUG_DEF 4194304
130 static const char *const sock_mem
[] = {
131 [sock_rmem_max
] = "/proc/sys/net/core/rmem_max",
132 [sock_rmem_def
] = "/proc/sys/net/core/rmem_default",
133 [sock_wmem_max
] = "/proc/sys/net/core/wmem_max",
134 [sock_wmem_def
] = "/proc/sys/net/core/wmem_default",
137 static int get_system_socket_mem(int which
)
141 const char *file
= sock_mem
[which
];
144 fd
= open(file
, O_RDONLY
);
148 ret
= read(fd
, buff
, sizeof(buff
));
156 static void set_system_socket_mem(int which
, int val
)
159 const char *file
= sock_mem
[which
];
163 fd
= open(file
, O_WRONLY
);
167 memset(buff
, 0, sizeof(buff
));
168 slprintf(buff
, sizeof(buff
), "%d", val
);
170 ret
= write(fd
, buff
, strlen(buff
));
172 panic("Cannot set system socket memory!\n");
177 void set_system_socket_memory(int *vals
, size_t len
)
181 if ((vals
[0] = get_system_socket_mem(sock_rmem_max
)) < SMEM_SUG_MAX
)
182 set_system_socket_mem(sock_rmem_max
, SMEM_SUG_MAX
);
183 if ((vals
[1] = get_system_socket_mem(sock_rmem_def
)) < SMEM_SUG_DEF
)
184 set_system_socket_mem(sock_rmem_def
, SMEM_SUG_DEF
);
185 if ((vals
[2] = get_system_socket_mem(sock_wmem_max
)) < SMEM_SUG_MAX
)
186 set_system_socket_mem(sock_wmem_max
, SMEM_SUG_MAX
);
187 if ((vals
[3] = get_system_socket_mem(sock_wmem_def
)) < SMEM_SUG_DEF
)
188 set_system_socket_mem(sock_wmem_def
, SMEM_SUG_DEF
);
191 void reset_system_socket_memory(int *vals
, size_t len
)
195 set_system_socket_mem(sock_rmem_max
, vals
[0]);
196 set_system_socket_mem(sock_rmem_def
, vals
[1]);
197 set_system_socket_mem(sock_wmem_max
, vals
[2]);
198 set_system_socket_mem(sock_wmem_def
, vals
[3]);