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!\n");
29 int sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
30 if (unlikely(sock
< 0))
31 panic("Creation of PF socket failed!\n");
36 void set_sock_prio(int fd
, int prio
)
40 ret
= setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &val
, sizeof(val
));
42 panic("Cannot set socket priority!\n");
45 void set_nonblocking(int fd
)
47 int ret
= fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFD
, 0) | O_NONBLOCK
);
48 if (unlikely(ret
< 0))
49 panic("Cannot fcntl!\n");
52 int set_nonblocking_sloppy(int fd
)
54 return fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFD
, 0) | O_NONBLOCK
);
57 void set_socket_keepalive(int fd
)
61 ret
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &one
, sizeof(one
));
63 panic("Cannot set TCP keepalive!\n");
66 void set_tcp_nodelay(int fd
)
70 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &one
, sizeof(one
));
72 panic("Cannot set TCP nodelay!\n");
75 int set_ipv6_only(int fd
)
78 return setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &one
, sizeof(one
));
81 int set_reuseaddr(int fd
)
85 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
86 if (unlikely(ret
< 0))
87 panic("Cannot reuse addr!\n");
92 void set_mtu_disc_dont(int fd
)
94 int mtu
= IP_PMTUDISC_DONT
, ret
;
96 ret
= setsockopt(fd
, SOL_IP
, IP_MTU_DISCOVER
, &mtu
, sizeof(mtu
));
98 panic("Cannot set MTU discovery options!\n");
108 #define SMEM_SUG_MAX 104857600
109 #define SMEM_SUG_DEF 4194304
111 static const char *const sock_mem
[] = {
112 [sock_rmem_max
] = "/proc/sys/net/core/rmem_max",
113 [sock_rmem_def
] = "/proc/sys/net/core/rmem_default",
114 [sock_wmem_max
] = "/proc/sys/net/core/wmem_max",
115 [sock_wmem_def
] = "/proc/sys/net/core/wmem_default",
118 static int get_system_socket_mem(int which
)
122 const char *file
= sock_mem
[which
];
125 fd
= open(file
, O_RDONLY
);
129 ret
= read(fd
, buff
, sizeof(buff
));
137 static void set_system_socket_mem(int which
, int val
)
140 const char *file
= sock_mem
[which
];
144 fd
= open(file
, O_WRONLY
);
148 memset(buff
, 0, sizeof(buff
));
149 slprintf(buff
, sizeof(buff
), "%d", val
);
151 ret
= write(fd
, buff
, strlen(buff
));
157 void set_system_socket_memory(int *vals
, size_t len
)
161 if ((vals
[0] = get_system_socket_mem(sock_rmem_max
)) < SMEM_SUG_MAX
)
162 set_system_socket_mem(sock_rmem_max
, SMEM_SUG_MAX
);
163 if ((vals
[1] = get_system_socket_mem(sock_rmem_def
)) < SMEM_SUG_DEF
)
164 set_system_socket_mem(sock_rmem_def
, SMEM_SUG_DEF
);
165 if ((vals
[2] = get_system_socket_mem(sock_wmem_max
)) < SMEM_SUG_MAX
)
166 set_system_socket_mem(sock_wmem_max
, SMEM_SUG_MAX
);
167 if ((vals
[3] = get_system_socket_mem(sock_wmem_def
)) < SMEM_SUG_DEF
)
168 set_system_socket_mem(sock_wmem_def
, SMEM_SUG_DEF
);
171 void reset_system_socket_memory(int *vals
, size_t len
)
175 set_system_socket_mem(sock_rmem_max
, vals
[0]);
176 set_system_socket_mem(sock_rmem_def
, vals
[1]);
177 set_system_socket_mem(sock_wmem_max
, vals
[2]);
178 set_system_socket_mem(sock_wmem_def
, vals
[3]);