<p>Joshua Colp has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/9802">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_registrar: Improve performance on inbound handling.<br><br>This change removes a sorcery lookup for retrieving all<br>contacts at the end of the registration process by keeping<br>track of the contacts that are added/updated/deleted.<br><br>This ensures at the end of the process the container of<br>contacts we have is the current state.<br><br>Pool usage has also been reduced by allocating one for<br>usage throughout the handling of a REGISTER and resetting<br>it to a clean state. This ensures that in most cases<br>we allocate once and just reuse it.<br><br>ASTERISK-28001<br><br>Change-Id: I1a78b2d46f9a2045dbbff1a3fd6dba84b612b3cb<br>---<br>M res/res_pjsip_registrar.c<br>1 file changed, 91 insertions(+), 43 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/02/9802/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_pjsip_registrar.c b/res/res_pjsip_registrar.c</span><br><span>index bf51733..18ed9f6 100644</span><br><span>--- a/res/res_pjsip_registrar.c</span><br><span>+++ b/res/res_pjsip_registrar.c</span><br><span>@@ -128,18 +128,15 @@</span><br><span> }</span><br><span> </span><br><span> /*! \brief Internal function which validates provided Contact headers to confirm that they are acceptable, and returns number of contacts */</span><br><span style="color: hsl(0, 100%, 40%);">-static int registrar_validate_contacts(const pjsip_rx_data *rdata, struct ao2_container *contacts, struct ast_sip_aor *aor, int *added, int *updated, int *deleted)</span><br><span style="color: hsl(120, 100%, 40%);">+static int registrar_validate_contacts(const pjsip_rx_data *rdata, pj_pool_t *pool, struct ao2_container *contacts,</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_aor *aor, int *added, int *updated, int *deleted)</span><br><span> {</span><br><span> pjsip_contact_hdr *previous = NULL;</span><br><span> pjsip_contact_hdr *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr;</span><br><span> struct registrar_contact_details details = {</span><br><span style="color: hsl(0, 100%, 40%);">- .pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Contact Comparison", 256, 256),</span><br><span style="color: hsl(120, 100%, 40%);">+ .pool = pool,</span><br><span> };</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!details.pool) {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> while ((contact = (pjsip_contact_hdr *) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next))) {</span><br><span> int expiration = registrar_get_expiration(aor, contact, rdata);</span><br><span> struct ast_sip_contact *existing;</span><br><span>@@ -148,7 +145,6 @@</span><br><span> if (contact->star) {</span><br><span> /* The expiration MUST be 0 when a '*' contact is used and there must be no other contact */</span><br><span> if (expiration != 0 || previous) {</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span> return -1;</span><br><span> }</span><br><span> /* Count all contacts to delete */</span><br><span>@@ -157,7 +153,6 @@</span><br><span> continue;</span><br><span> } else if (previous && previous->star) {</span><br><span> /* If there is a previous contact and it is a '*' this is a deal breaker */</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span> return -1;</span><br><span> }</span><br><span> previous = contact;</span><br><span>@@ -171,13 +166,11 @@</span><br><span> /* pjsip_uri_print returns -1 if there's not enough room in the buffer */</span><br><span> if (pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, details.uri, contact_uri, sizeof(contact_uri)) < 0) {</span><br><span> /* If the total length of the uri is greater than pjproject can handle, go no further */</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span> if (details.uri->host.slen >= pj_max_hostname) {</span><br><span> /* If the length of the hostname is greater than pjproject can handle, go no further */</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span>@@ -196,24 +189,21 @@</span><br><span> }</span><br><span> </span><br><span> /* The provided contacts are acceptable, huzzah! */</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_pool_reset(pool);</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Callback function which prunes static contacts */</span><br><span style="color: hsl(0, 100%, 40%);">-static int registrar_prune_static(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_sip_contact *contact = obj;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return ast_tvzero(contact->expiration_time) ? CMP_MATCH : 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> /*! \brief Internal function used to delete a contact from an AOR */</span><br><span> static int registrar_delete_contact(void *obj, void *arg, int flags)</span><br><span> {</span><br><span> struct ast_sip_contact *contact = obj;</span><br><span> const char *aor_name = arg;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* Permanent contacts can't be deleted */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_tvzero(contact->expiration_time)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> ast_sip_location_delete_contact(contact);</span><br><span> if (!ast_strlen_zero(aor_name)) {</span><br><span> ast_verb(3, "Removed contact '%s' from AOR '%s' due to request\n", contact->uri, aor_name);</span><br><span>@@ -442,7 +432,8 @@</span><br><span> *</span><br><span> * \return Nothing</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">-static void remove_excess_contacts(struct ao2_container *contacts, unsigned int to_remove)</span><br><span style="color: hsl(120, 100%, 40%);">+static void remove_excess_contacts(struct ao2_container *contacts, struct ao2_container *response_contacts,</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int to_remove)</span><br><span> {</span><br><span> struct excess_contact_vector contact_vec;</span><br><span> </span><br><span>@@ -482,11 +473,28 @@</span><br><span> contact->uri,</span><br><span> contact->aor,</span><br><span> contact->user_agent);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlink(response_contacts, contact);</span><br><span> }</span><br><span> </span><br><span> AST_VECTOR_FREE(&contact_vec);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Callback function which adds non-permanent contacts to a container */</span><br><span style="color: hsl(120, 100%, 40%);">+static int registrar_add_non_permanent(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_contact *contact = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *container = arg;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_tvzero(contact->expiration_time)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(container, contact);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> struct aor_core_response {</span><br><span> /*! Tx data to use for statefull response. NULL for stateless response. */</span><br><span> pjsip_tx_data *tdata;</span><br><span>@@ -506,7 +514,9 @@</span><br><span> int added = 0;</span><br><span> int updated = 0;</span><br><span> int deleted = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int permanent = 0;</span><br><span> int contact_count;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *existing_contacts = NULL;</span><br><span> pjsip_contact_hdr *contact_hdr = NULL;</span><br><span> struct registrar_contact_details details = { 0, };</span><br><span> pjsip_tx_data *tdata;</span><br><span>@@ -523,15 +533,21 @@</span><br><span> char *call_id = NULL;</span><br><span> size_t alloc_size;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* So we don't count static contacts against max_contacts we prune them out from the container */</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(contacts, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, registrar_prune_static, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* We create a single pool and use it throughout this function where we need one */</span><br><span style="color: hsl(120, 100%, 40%);">+ details.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),</span><br><span style="color: hsl(120, 100%, 40%);">+ "Contact Comparison", 256, 256);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!details.pool) {</span><br><span style="color: hsl(120, 100%, 40%);">+ response->code = 500;</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (registrar_validate_contacts(rdata, contacts, aor, &added, &updated, &deleted)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (registrar_validate_contacts(rdata, details.pool, contacts, aor, &added, &updated, &deleted)) {</span><br><span> /* The provided Contact headers do not conform to the specification */</span><br><span> ast_sip_report_failed_acl(endpoint, rdata, "registrar_invalid_contacts_provided");</span><br><span> ast_log(LOG_WARNING, "Failed to validate contacts in REGISTER request from '%s'\n",</span><br><span> ast_sorcery_object_get_id(endpoint));</span><br><span> response->code = 400;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span> return;</span><br><span> }</span><br><span> </span><br><span>@@ -540,15 +556,36 @@</span><br><span> ast_log(LOG_WARNING, "Invalid modifications made to REGISTER request from '%s' by intervening proxy\n",</span><br><span> ast_sorcery_object_get_id(endpoint));</span><br><span> response->code = 420;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span> return;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* If there are any permanent contacts configured on the AOR we need to take them into</span><br><span style="color: hsl(120, 100%, 40%);">+ * account when counting contacts.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (aor->permanent_contacts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ permanent = ao2_container_count(aor->permanent_contacts);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (aor->remove_existing) {</span><br><span> /* Cumulative number of contacts affected by this registration */</span><br><span> contact_count = MAX(updated + added - deleted, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* We need to keep track of only existing contacts so we can later</span><br><span style="color: hsl(120, 100%, 40%);">+ * remove them if need be.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ existing_contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!existing_contacts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ response->code = 500;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(contacts, OBJ_NODATA, registrar_add_non_permanent, existing_contacts);</span><br><span> } else {</span><br><span> /* Total contacts after this registration */</span><br><span style="color: hsl(0, 100%, 40%);">- contact_count = ao2_container_count(contacts) + added - deleted;</span><br><span style="color: hsl(120, 100%, 40%);">+ contact_count = ao2_container_count(contacts) - permanent + added - deleted;</span><br><span> }</span><br><span> if (contact_count > aor->max_contacts) {</span><br><span> /* Enforce the maximum number of contacts */</span><br><span>@@ -556,13 +593,8 @@</span><br><span> ast_log(LOG_WARNING, "Registration attempt from endpoint '%s' to AOR '%s' will exceed max contacts of %u\n",</span><br><span> ast_sorcery_object_get_id(endpoint), aor_name, aor->max_contacts);</span><br><span> response->code = 403;</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- details.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),</span><br><span style="color: hsl(0, 100%, 40%);">- "Contact Comparison", 256, 256);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!details.pool) {</span><br><span style="color: hsl(0, 100%, 40%);">- response->code = 500;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(existing_contacts);</span><br><span> return;</span><br><span> }</span><br><span> </span><br><span>@@ -604,6 +636,13 @@</span><br><span> /* A star means to unregister everything, so do so for the possible contacts */</span><br><span> ao2_callback(contacts, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE,</span><br><span> registrar_delete_contact, (void *)aor_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If we are keeping track of existing contacts for removal then, well, there is</span><br><span style="color: hsl(120, 100%, 40%);">+ * absolutely nothing left so no need to try to remove any.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (existing_contacts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(existing_contacts, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ existing_contacts = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> break;</span><br><span> }</span><br><span> </span><br><span>@@ -617,6 +656,14 @@</span><br><span> pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, details.uri, contact_uri, sizeof(contact_uri));</span><br><span> </span><br><span> contact = ao2_callback(contacts, OBJ_UNLINK, registrar_find_contact, &details);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If a contact was returned and we need to keep track of existing contacts then it</span><br><span style="color: hsl(120, 100%, 40%);">+ * should be removed.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (contact && existing_contacts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlink(existing_contacts, contact);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (!contact) {</span><br><span> int prune_on_boot;</span><br><span> </span><br><span>@@ -672,6 +719,8 @@</span><br><span> aor_name,</span><br><span> expiration,</span><br><span> user_agent);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(contacts, contact);</span><br><span> } else if (expiration) {</span><br><span> struct ast_sip_contact *contact_update;</span><br><span> </span><br><span>@@ -712,6 +761,7 @@</span><br><span> aor_name,</span><br><span> expiration,</span><br><span> contact_update->user_agent);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(contacts, contact_update);</span><br><span> ao2_cleanup(contact_update);</span><br><span> } else {</span><br><span> if (contact->prune_on_boot) {</span><br><span>@@ -740,6 +790,9 @@</span><br><span> aor_name,</span><br><span> contact->user_agent);</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* We reset the pool so we don't need to allocate additional blocks of memory on the next run */</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_pool_reset(details.pool);</span><br><span> }</span><br><span> </span><br><span> pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span>@@ -749,24 +802,20 @@</span><br><span> * that have not been updated/added/deleted as a result of this</span><br><span> * REGISTER do so.</span><br><span> *</span><br><span style="color: hsl(0, 100%, 40%);">- * The contacts container currently holds the existing contacts that</span><br><span style="color: hsl(0, 100%, 40%);">- * were not affected by this REGISTER.</span><br><span style="color: hsl(120, 100%, 40%);">+ * The existing contacts container holds all contacts that were not</span><br><span style="color: hsl(120, 100%, 40%);">+ * involved in this REGISTER.</span><br><span style="color: hsl(120, 100%, 40%);">+ * The contacts container holds the current contacst of the AOR.</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">- if (aor->remove_existing) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (aor->remove_existing && existing_contacts) {</span><br><span> /* Total contacts after this registration */</span><br><span style="color: hsl(0, 100%, 40%);">- contact_count = ao2_container_count(contacts) + updated + added;</span><br><span style="color: hsl(120, 100%, 40%);">+ contact_count = ao2_container_count(existing_contacts) + updated + added;</span><br><span> if (contact_count > aor->max_contacts) {</span><br><span> /* Remove excess existing contacts that expire the soonest */</span><br><span style="color: hsl(0, 100%, 40%);">- remove_excess_contacts(contacts, contact_count - aor->max_contacts);</span><br><span style="color: hsl(120, 100%, 40%);">+ remove_excess_contacts(existing_contacts, contacts, contact_count - aor->max_contacts);</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(existing_contacts, -1);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Re-retrieve contacts. Caller will clean up the original container. */</span><br><span style="color: hsl(0, 100%, 40%);">- contacts = ast_sip_location_retrieve_aor_contacts_nolock(aor);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!contacts) {</span><br><span style="color: hsl(0, 100%, 40%);">- response->code = 500;</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span> response_contact = ao2_callback(contacts, 0, NULL, NULL);</span><br><span> </span><br><span> /* Send a response containing all of the contacts (including static) that are present on this AOR */</span><br><span>@@ -782,7 +831,6 @@</span><br><span> registrar_add_date_header(tdata);</span><br><span> </span><br><span> ao2_callback(contacts, 0, registrar_add_contact, tdata);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(contacts);</span><br><span> </span><br><span> if ((expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL))) {</span><br><span> expires_hdr = pjsip_expires_hdr_create(tdata->pool, registrar_get_expiration(aor, NULL, rdata));</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/9802">change 9802</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/9802"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I1a78b2d46f9a2045dbbff1a3fd6dba84b612b3cb </div>
<div style="display:none"> Gerrit-Change-Number: 9802 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Joshua Colp <jcolp@digium.com> </div>