Hello all,

I am currently looking into issue https://github.com/kamailio/kamailio/issues/3823 regarding the tls.reload and the constant increase in memory usage when it's called.

I tried to look something up related to CRL and related functions for what causing it but nothing to obvious. The only culprit i could find was this function call in  SSL_verify_load_locations in load_crl(). But the same exact function is also called in load_ca_list() with no such behaviour. 

Next thing i notice, is that on the first rpc call of tls.reload, there is a "significant" memory allocation. 
What i mean by this is that when a fresh kamailio (5.8) is started up (with -m 7, for low shared memory availability to try and replicate the issue and children=2) and then check free shared memory usage using `kamcmd core.shmmem` is around 2196096 (bytes i am assuming) for my system. 

On the first call of tls.reload, it drops to 539248. That's a heavy drop so i started digging why on so much allocation is caused. When loading a tls configuration, ksr_fix_domain allocates a new SSL_CTX per process. From what i am seeing this SSL_CTX, is basically the same for each one, with same configuration without any difference on settings.

So, what i tried is to allocated it once (see tls_reload branch in my repo), and just share this context to all of the process and the memory used is way less and also the unbounding memory seems to fixed or at least it's slowed that's barely visible.

What are your thoughts on these? Is this something we can done? Is the assumption that tls config will and be the same for every process or can one somehow differentiate from another?

Thanks,
Xenofon

Some numbers for anyone interested:

Before the patch 5.8 branch and with settings such as -m 7 (shared memory) and children=2 (with children=8 it doesn't even start, failed to load cyphers in SSL_CTX):
Fresh kamailio start shared memory:          total: 7340032
                                                   free:  2196096

After one tls.reload:                           free:   539248
After 100 tls.reload:                         free:    483776
                                             

After patch on master (kamailio -m 7 and children=2) (with children=8 it starts normally): 
Fresh kamailio start shared memory:          total: 7340032
                                                   free:  3772000

After one tls.reload:                            free:  3690192
After 100 tls.reload:                            free:  3681056