Skip to content

Commit 05f1e5f

Browse files
New source for sockets
1 parent 9ae0f95 commit 05f1e5f

File tree

8 files changed

+240
-178
lines changed

8 files changed

+240
-178
lines changed

modules/rest_client/rest_cb.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ size_t header_func(char *ptr, size_t size, size_t nmemb, void *userdata)
109109
return len;
110110
}
111111

112+
int timer_cb(CURLM *multi_handle, long timeout_ms, void *cbp)
113+
{
114+
LM_DBG("multi_handle timer called %d\n", timeout_ms);
115+
long *p = (long*) cbp;
116+
117+
*p = timeout_ms;
118+
119+
return 0;
120+
}
121+
112122
int prereq_callback(void *cbp,
113123
char *conn_primary_ip,
114124
char *conn_local_ip,

modules/rest_client/rest_cb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enum curl_status {
4747

4848
size_t write_func(char *ptr, size_t size, size_t nmemb, void *userdata);
4949
size_t header_func(char *ptr, size_t size, size_t nmemb, void *userdata);
50+
int timer_cb(CURLM *multi_handle, long timeout_ms, void *cbp);
5051
int prereq_callback(void *cbp, char *conn_primary_ip, char *conn_local_ip, int conn_primary_port, int conn_local_port);
5152

5253
#endif /* _REST_CB_H_ */

modules/rest_client/rest_client.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <stdlib.h>
2929
#include <curl/curl.h>
3030

31+
#include <sys/resource.h>
32+
3133
#include "../../async.h"
3234
#include "../../sr_module.h"
3335
#include "../../dprint.h"
@@ -397,7 +399,7 @@ static int child_init(int rank)
397399
return 0;
398400
}
399401

400-
if (init_process_limits() != 0) {
402+
if (init_process_limits(lim.rlim_cur) != 0) {
401403
LM_WARN("Could not set file descriptor limits\n");
402404
return 0;
403405
}

modules/rest_client/rest_client.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#ifndef _REST_CLIENT_
2727
#define _REST_CLIENT_
2828

29-
#include <sys/resource.h>
30-
3129
enum tr_rest_subtype {
3230
TR_REST_ESCAPE, TR_REST_UNESCAPE
3331
};
@@ -40,6 +38,5 @@ typedef struct _preconnect_urls {
4038

4139
extern int enable_expect_100;
4240
extern unsigned int max_transfer_size;
43-
extern struct rlimit lim;
4441

4542
#endif /* _REST_CLIENT_ */

modules/rest_client/rest_methods.c

Lines changed: 16 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "rest_client.h"
4242
#include "rest_methods.h"
4343
#include "rest_cb.h"
44+
#include "rest_sockets.h"
4445

4546
#define REST_CORRELATION_COOKIE "RESTCORR"
4647

@@ -70,10 +71,12 @@ extern int _async_resume_retr_itv;
7071
#define MAX_HOST_LENGTH 128
7172

7273
/* file descriptor limits */
73-
#define WORD_SIZE_BITS 64
74-
#define WORD_SIZE_BYTES 16
75-
#define BYTE_LEN 8
76-
extern struct rlimit lim;
74+
// #define WORD_SIZE_BITS 64
75+
// #define WORD_SIZE_BYTES 16
76+
// #define BYTE_LEN 8
77+
// extern struct rlimit lim;
78+
79+
static CURLSH *curl_share;
7780

7881
#define REST_TRACE_API_MODULE "proto_hep"
7982
extern int rest_proto_id;
@@ -459,168 +462,6 @@ static int init_transfer(CURL *handle, char *url, unsigned long timeout_s)
459462
return -1;
460463
}
461464

462-
typedef struct _file_descriptors {
463-
unsigned char *tracked_socks;
464-
int max_fd_index;
465-
} file_descriptors;
466-
467-
static CURLSH *curl_share = NULL;
468-
static file_descriptors fds;
469-
size_t aligned_bitset_len;
470-
int total_chunks;
471-
472-
int init_process_limits(void) {
473-
aligned_bitset_len = (((lim.rlim_cur + WORD_SIZE_BITS - 1) / WORD_SIZE_BITS) * WORD_SIZE_BITS) / WORD_SIZE_BITS;
474-
475-
fds.tracked_socks = (unsigned char*) pkg_malloc(aligned_bitset_len);
476-
fds.max_fd_index = 0;
477-
total_chunks = aligned_bitset_len / sizeof(uint64_t);
478-
479-
if (fds.tracked_socks == NULL) {
480-
return -1;
481-
}
482-
483-
return 0;
484-
}
485-
486-
static inline int get_max_fd(file_descriptors *fds) {
487-
uint64_t *socket_bitmask;
488-
uint64_t sockets;
489-
int start_index, bitmask_index;
490-
491-
bitmask_index = fds->max_fd_index -1;
492-
493-
if (bitmask_index < 0) {
494-
return -2;
495-
}
496-
497-
socket_bitmask = (uint64_t*) fds->tracked_socks;
498-
sockets = socket_bitmask[bitmask_index];
499-
500-
if (!sockets) {
501-
fds->max_fd_index--;
502-
return -1;
503-
}
504-
505-
start_index = (fds->max_fd_index * (WORD_SIZE_BITS)) - 1;
506-
507-
return start_index - __builtin_clzll(sockets);
508-
}
509-
510-
/*
511-
* Adds the socket to a bitmask and calculates index of that socket in the bitmask
512-
* eg. s = 63 then index is 1 and if s = 65 then index is 2, in increments of 64 bits
513-
* The index is the max index to check when actioning on the sockets
514-
*/
515-
static inline void add_sock(file_descriptors *fds, int s) {
516-
int sock_index = (s >> 6) + 1;
517-
518-
if (sock_index > fds->max_fd_index) {
519-
fds->max_fd_index = sock_index;
520-
}
521-
522-
fds->tracked_socks[s / BYTE_LEN] |= (1 << (s % BYTE_LEN));
523-
}
524-
525-
/*
526-
* Removes the socket from the bitmask and then checks the max bitmask for any setbits
527-
* If no bits are set then the max index is decremented regardless if the socket removed was in that chunk
528-
*/
529-
static inline void remove_sock(file_descriptors *fds, int s) {
530-
uint64_t sockets;
531-
uint64_t *socket_bitmask;
532-
int bitmask_index;
533-
534-
fds->tracked_socks[s / BYTE_LEN] &= ~(1 << (s % BYTE_LEN));
535-
536-
bitmask_index = fds->max_fd_index -1;
537-
socket_bitmask = (uint64_t*) fds->tracked_socks;
538-
539-
if (bitmask_index > 0) {
540-
memcpy(&sockets, socket_bitmask + fds->max_fd_index - 1, sizeof(sockets));
541-
542-
if (!sockets) {
543-
fds->max_fd_index--;
544-
}
545-
}
546-
}
547-
548-
static int socket_action_cb(CURL *e, curl_socket_t s, int event, void *cbp, void *sockp)
549-
{
550-
LM_DBG("called for socket %d status %d\n", s, event);
551-
file_descriptors *fds = (file_descriptors*) cbp;
552-
553-
if (event != CURL_POLL_REMOVE) {
554-
add_sock(fds, s);
555-
} else if (event == CURL_POLL_REMOVE) {
556-
remove_sock(fds, s);
557-
}
558-
559-
return 0;
560-
}
561-
562-
static int timer_cb(CURLM *multi_handle, long timeout_ms, void *cbp)
563-
{
564-
LM_DBG("multi_handle timer called %d\n", timeout_ms);
565-
long *p = (long*) cbp;
566-
567-
*p = timeout_ms;
568-
569-
return 0;
570-
}
571-
572-
static int start_multi_socket(CURLM *multi_handle) {
573-
CURLMcode mrc;
574-
int running;
575-
576-
memset(fds.tracked_socks, 0, aligned_bitset_len);
577-
fds.max_fd_index = 0;
578-
mrc = curl_multi_socket_action(multi_handle, CURL_SOCKET_TIMEOUT, 0, &running);
579-
580-
if (mrc != CURLM_OK) {
581-
LM_ERR("curl_multi_socket_action: %s\n", curl_multi_strerror(mrc));
582-
return -1;
583-
}
584-
585-
return running;
586-
}
587-
588-
static int run_multi_socket(CURLM *multi_handle) {
589-
CURLMcode mrc;
590-
int running;
591-
uint64_t sockets;
592-
uint64_t *socket_bitmask;
593-
594-
socket_bitmask = (uint64_t*) fds.tracked_socks;
595-
596-
if (fds.max_fd_index > 0) {
597-
memcpy(&sockets, socket_bitmask + fds.max_fd_index - 1, sizeof(sockets));
598-
599-
if (!sockets) {
600-
fds.max_fd_index--;
601-
}
602-
}
603-
604-
for (int i = 0; i < fds.max_fd_index; i++) {
605-
memcpy(&sockets, socket_bitmask + i, sizeof(sockets));
606-
607-
while (sockets) {
608-
int curl_s = (i * WORD_SIZE_BITS) + __builtin_ctzll(sockets);
609-
LM_DBG("Action on socket %d\n", curl_s);
610-
611-
mrc = curl_multi_socket_action(multi_handle, curl_s, 0, &running);
612-
if (mrc != CURLM_OK) {
613-
LM_ERR("curl_multi_socket_action: %s\n", curl_multi_strerror(mrc));
614-
return -1;
615-
}
616-
617-
sockets &= sockets - 1;
618-
}
619-
}
620-
621-
return running;
622-
}
623-
624465
int connect_only(preconnect_urls *precon_urls, int total_cons) {
625466
CURLcode rc;
626467
CURLMcode mrc;
@@ -680,13 +521,14 @@ int connect_only(preconnect_urls *precon_urls, int total_cons) {
680521

681522
busy_wait = connect_poll_interval;
682523

524+
if (setsocket_callback(multi_handle) != 0) {
525+
goto cleanup;
526+
}
527+
683528
// TODO Need to expose these settings for connect and async req
684529
w_curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, (long) num_of_connections);
685530
w_curl_multi_setopt(multi_handle, CURLMOPT_MAXCONNECTS, (long) num_of_connections);
686531

687-
w_curl_multi_setopt(multi_handle, CURLMOPT_SOCKETFUNCTION, socket_action_cb);
688-
w_curl_multi_setopt(multi_handle, CURLMOPT_SOCKETDATA, &fds);
689-
690532
w_curl_multi_setopt(multi_handle, CURLMOPT_TIMERFUNCTION, timer_cb);
691533
w_curl_multi_setopt(multi_handle, CURLMOPT_TIMERDATA, &timer);
692534

@@ -1192,6 +1034,10 @@ int start_async_http_req(struct sip_msg *msg, enum rest_client_method method,
11921034
multi_handle = multi_list->multi_handle;
11931035
curl_multi_add_handle(multi_handle, handle);
11941036

1037+
if (setsocket_callback(multi_handle) != 0) {
1038+
goto cleanup;
1039+
}
1040+
11951041
w_curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, (long) max_host_connection);
11961042
w_curl_multi_setopt(multi_handle, CURLMOPT_MAXCONNECTS, (long) max_async_transfers);
11971043

@@ -1203,9 +1049,6 @@ int start_async_http_req(struct sip_msg *msg, enum rest_client_method method,
12031049
timeout = connect_timeout;
12041050
busy_wait = connect_poll_interval;
12051051

1206-
w_curl_multi_setopt(multi_handle, CURLMOPT_SOCKETFUNCTION, socket_action_cb);
1207-
w_curl_multi_setopt(multi_handle, CURLMOPT_SOCKETDATA, &fds);
1208-
12091052
w_curl_multi_setopt(multi_handle, CURLMOPT_TIMERFUNCTION, timer_cb);
12101053
w_curl_multi_setopt(multi_handle, CURLMOPT_TIMERDATA, &timer);
12111054

@@ -1236,7 +1079,7 @@ int start_async_http_req(struct sip_msg *msg, enum rest_client_method method,
12361079
async_parm->handle = handle;
12371080
async_parm->multi_list = multi_list;
12381081
header_list = NULL;
1239-
*out_fd = get_max_fd(&fds); // Running only one socket at a time so it's always the max
1082+
*out_fd = get_max_fd(); // Running only one socket at a time so it's always the max
12401083
return RCL_OK;
12411084

12421085
error:

modules/rest_client/rest_methods.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ enum async_ret_code time_out_async_http_req(int fd, struct sip_msg *msg, void *_
145145
int rest_append_hf_method(struct sip_msg *msg, str *hfv);
146146
int rest_init_client_tls(struct sip_msg *msg, str *tls_client_dom);
147147
int connect_only(preconnect_urls *precon_urls, int total_cons);
148-
int init_process_limits(void);
149148

150149
#endif /* _REST_METHODS_ */
151150

0 commit comments

Comments
 (0)