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
)
69 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &one
, sizeof(one
));
72 int set_ipv6_only(int fd
)
75 return setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &one
, sizeof(one
));
78 int set_reuseaddr(int fd
)
82 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
83 if (unlikely(ret
< 0))
84 panic("Cannot reuse addr!\n");
89 void set_mtu_disc_dont(int fd
)
91 int mtu
= IP_PMTUDISC_DONT
, ret
;
93 ret
= setsockopt(fd
, SOL_IP
, IP_MTU_DISCOVER
, &mtu
, sizeof(mtu
));
95 panic("Cannot set MTU discovery options!\n");
105 #define SMEM_SUG_MAX 104857600
106 #define SMEM_SUG_DEF 4194304
108 static const char *const sock_mem
[] = {
109 [sock_rmem_max
] = "/proc/sys/net/core/rmem_max",
110 [sock_rmem_def
] = "/proc/sys/net/core/rmem_default",
111 [sock_wmem_max
] = "/proc/sys/net/core/wmem_max",
112 [sock_wmem_def
] = "/proc/sys/net/core/wmem_default",
115 static int get_system_socket_mem(int which
)
119 const char *file
= sock_mem
[which
];
122 fd
= open(file
, O_RDONLY
);
126 ret
= read(fd
, buff
, sizeof(buff
));
134 static void set_system_socket_mem(int which
, int val
)
137 const char *file
= sock_mem
[which
];
141 fd
= open(file
, O_WRONLY
);
145 memset(buff
, 0, sizeof(buff
));
146 slprintf(buff
, sizeof(buff
), "%d", val
);
148 ret
= write(fd
, buff
, strlen(buff
));
154 void set_system_socket_memory(int *vals
, size_t len
)
158 if ((vals
[0] = get_system_socket_mem(sock_rmem_max
)) < SMEM_SUG_MAX
)
159 set_system_socket_mem(sock_rmem_max
, SMEM_SUG_MAX
);
160 if ((vals
[1] = get_system_socket_mem(sock_rmem_def
)) < SMEM_SUG_DEF
)
161 set_system_socket_mem(sock_rmem_def
, SMEM_SUG_DEF
);
162 if ((vals
[2] = get_system_socket_mem(sock_wmem_max
)) < SMEM_SUG_MAX
)
163 set_system_socket_mem(sock_wmem_max
, SMEM_SUG_MAX
);
164 if ((vals
[3] = get_system_socket_mem(sock_wmem_def
)) < SMEM_SUG_DEF
)
165 set_system_socket_mem(sock_wmem_def
, SMEM_SUG_DEF
);
168 void reset_system_socket_memory(int *vals
, size_t len
)
172 set_system_socket_mem(sock_rmem_max
, vals
[0]);
173 set_system_socket_mem(sock_rmem_def
, vals
[1]);
174 set_system_socket_mem(sock_wmem_max
, vals
[2]);
175 set_system_socket_mem(sock_wmem_def
, vals
[3]);