<p>Oron Peled would like Tzafrir Cohen to <strong>review</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/11605">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">openr2(3/6): Convert r2links to standard Asterisk AST_LIST*<br><br>Change-Id: Ibcb2401515a58782a1488c0b9efbed201c3f3a17<br>Signed-off-by: Oron Peled <oron.peled@xorcom.com><br>---<br>M channels/chan_dahdi.c<br>1 file changed, 77 insertions(+), 45 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/05/11605/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/channels/chan_dahdi.c b/channels/chan_dahdi.c</span><br><span>index 79b2b5d..452cdc0 100644</span><br><span>--- a/channels/chan_dahdi.c</span><br><span>+++ b/channels/chan_dahdi.c</span><br><span>@@ -744,16 +744,21 @@</span><br><span> </span><br><span> /* MFC-R2 pseudo-link structure */</span><br><span> struct dahdi_mfcr2 {</span><br><span style="color: hsl(120, 100%, 40%);">+ int index; /*!< Unique index for CLI */</span><br><span> pthread_t r2master; /*!< Thread of master */</span><br><span> openr2_context_t *protocol_context; /*!< OpenR2 context handle */</span><br><span> struct dahdi_pvt *pvts[SIG_MFCR2_MAX_CHANNELS]; /*!< Member channel pvt structs */</span><br><span> int numchans; /*!< Number of channels in this R2 block */</span><br><span> int nodev; /*!< Link disconnected? */</span><br><span style="color: hsl(0, 100%, 40%);">- struct dahdi_mfcr2_conf conf; /*!< Configuration used to setup this pseudo-link */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct dahdi_mfcr2_conf conf; /*!< Configuration used to setup this pseudo-link */</span><br><span> };</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/* malloc'd array of malloc'd r2links */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct dahdi_mfcr2 **r2links;</span><br><span style="color: hsl(120, 100%, 40%);">+struct r2link_entry {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct dahdi_mfcr2 mfcr2;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_ENTRY(r2link_entry) list;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+static AST_LIST_HEAD_STATIC(r2links, r2link_entry);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* how many r2links have been malloc'd */</span><br><span> static int r2links_count = 0;</span><br><span> </span><br><span>@@ -11835,55 +11840,74 @@</span><br><span> #ifdef HAVE_OPENR2</span><br><span> static void dahdi_r2_destroy_links(void)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- int i = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!r2links) {</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%);">- for (; i < r2links_count; i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (r2links[i]->r2master != AST_PTHREADT_NULL) {</span><br><span style="color: hsl(0, 100%, 40%);">- pthread_cancel(r2links[i]->r2master);</span><br><span style="color: hsl(0, 100%, 40%);">- pthread_join(r2links[i]->r2master, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- openr2_context_delete(r2links[i]->protocol_context);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct r2link_entry *cur;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_LOCK(&r2links);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&r2links, cur, list) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct dahdi_mfcr2 *r2 = &cur->mfcr2;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Destroying R2 link\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE(&r2links, cur, list);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (r2->r2master != AST_PTHREADT_NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_cancel(r2->r2master);</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_join(r2->r2master, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ openr2_context_delete(r2->protocol_context);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(r2links[i]);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(cur);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(r2links);</span><br><span style="color: hsl(0, 100%, 40%);">- r2links = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_UNLOCK(&r2links);</span><br><span> r2links_count = 0;</span><br><span> }</span><br><span> </span><br><span> /* This is an artificial convenient capacity, to keep at most a full E1 of channels in a single thread */</span><br><span> #define R2_LINK_CAPACITY 30</span><br><span style="color: hsl(0, 100%, 40%);">-static struct dahdi_mfcr2 *dahdi_r2_get_link(const struct dahdi_chan_conf *conf)</span><br><span style="color: hsl(120, 100%, 40%);">+static struct r2link_entry *dahdi_r2_get_link(const struct dahdi_chan_conf *conf)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- struct dahdi_mfcr2 *new_r2link = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- struct dahdi_mfcr2 **new_r2links = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(120, 100%, 40%);">+ struct r2link_entry *cur = NULL;</span><br><span> /* Only create a new R2 link if</span><br><span> 1. This is the first link requested</span><br><span> 2. Configuration changed</span><br><span> 3. We got more channels than supported per link */</span><br><span style="color: hsl(0, 100%, 40%);">- if (!r2links_count ||</span><br><span style="color: hsl(0, 100%, 40%);">- memcmp(&conf->mfcr2, &r2links[r2links_count - 1]->conf, sizeof(conf->mfcr2)) ||</span><br><span style="color: hsl(0, 100%, 40%);">- (r2links[r2links_count - 1]->numchans == R2_LINK_CAPACITY)) {</span><br><span style="color: hsl(0, 100%, 40%);">- new_r2link = ast_calloc(1, sizeof(**r2links));</span><br><span style="color: hsl(0, 100%, 40%);">- if (!new_r2link) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Cannot allocate R2 link!\n");</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_LOCK(&r2links);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (! AST_LIST_EMPTY(&r2links)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ cur = AST_LIST_LAST(&r2links);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (memcmp(&conf->mfcr2, &cur->mfcr2.conf, sizeof(conf->mfcr2))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Need new R2 link because of: Configuration change\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ cur = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cur->mfcr2.numchans == R2_LINK_CAPACITY) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Need new R2 link because of: Capacity (%d)\n", R2_LINK_CAPACITY);</span><br><span style="color: hsl(120, 100%, 40%);">+ cur = NULL;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- new_r2links = ast_realloc(r2links, ((r2links_count + 1) * sizeof(*r2links)));</span><br><span style="color: hsl(0, 100%, 40%);">- if (!new_r2links) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Cannot allocate R2 link!\n");</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(new_r2link);</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- r2links = new_r2links;</span><br><span style="color: hsl(0, 100%, 40%);">- new_r2link->r2master = AST_PTHREADT_NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- r2links[r2links_count] = new_r2link;</span><br><span style="color: hsl(0, 100%, 40%);">- r2links_count++;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "Created new R2 link!\n");</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- return r2links[r2links_count - 1];</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!cur) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct r2link_entry *tmp = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ int new_idx = r2links_count + 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ int i;</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 1; i <= r2links_count; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int i_unused = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&r2links, tmp, list) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (i == tmp->mfcr2.index) {</span><br><span style="color: hsl(120, 100%, 40%);">+ i_unused = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</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%);">+ if (i_unused) {</span><br><span style="color: hsl(120, 100%, 40%);">+ new_idx = i;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</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%);">+ cur = ast_calloc(1, sizeof(*cur));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!cur) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Cannot allocate R2 link!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ cur->mfcr2.index = new_idx;</span><br><span style="color: hsl(120, 100%, 40%);">+ cur->mfcr2.r2master = AST_PTHREADT_NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ r2links_count++;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Created new R2 link #%d (now have %d)\n", new_idx, r2links_count);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_TAIL(&r2links, cur, list);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_UNLOCK(&r2links);</span><br><span style="color: hsl(120, 100%, 40%);">+ return cur;</span><br><span> }</span><br><span> </span><br><span> static int dahdi_r2_set_context(struct dahdi_mfcr2 *r2_link, const struct dahdi_chan_conf *conf)</span><br><span>@@ -12138,7 +12162,8 @@</span><br><span> #ifdef HAVE_OPENR2</span><br><span> if (chan_sig == SIG_MFCR2) {</span><br><span> struct dahdi_mfcr2 *r2_link;</span><br><span style="color: hsl(0, 100%, 40%);">- r2_link = dahdi_r2_get_link(conf);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct r2link_entry *r2_le = dahdi_r2_get_link(conf);</span><br><span style="color: hsl(120, 100%, 40%);">+ r2_link = &r2_le->mfcr2;</span><br><span> if (!r2_link) {</span><br><span> ast_log(LOG_WARNING, "Cannot get another R2 DAHDI context!\n");</span><br><span> destroy_dahdi_pvt(tmp);</span><br><span>@@ -14728,10 +14753,11 @@</span><br><span> </span><br><span> static char *handle_mfcr2_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-#define FORMAT "%4s %-7.7s %-7.7s %-8.8s %-9.9s %-16.16s %-8.8s %-8.8s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+#define FORMAT "%4s %4s %-7.7s %-7.7s %-8.8s %-9.9s %-16.16s %-8.8s %-8.8s\n"</span><br><span> int filtertype = 0;</span><br><span> int targetnum = 0;</span><br><span> char channo[5];</span><br><span style="color: hsl(120, 100%, 40%);">+ char linkno[5];</span><br><span> char anino[5];</span><br><span> char dnisno[5];</span><br><span> struct dahdi_pvt *p;</span><br><span>@@ -14763,7 +14789,7 @@</span><br><span> return CLI_SHOWUSAGE;</span><br><span> }</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, FORMAT, "Chan", "Variant", "Max ANI", "Max DNIS", "ANI First", "Immediate Accept", "Tx CAS", "Rx CAS");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, FORMAT, "Chan", "Link#", "Variant", "Max ANI", "Max DNIS", "ANI First", "Immediate Accept", "Tx CAS", "Rx CAS");</span><br><span> ast_mutex_lock(&iflock);</span><br><span> for (p = iflist; p; p = p->next) {</span><br><span> if (!(p->sig & SIG_MFCR2) || !p->r2chan) {</span><br><span>@@ -14788,9 +14814,10 @@</span><br><span> r2context = openr2_chan_get_context(p->r2chan);</span><br><span> r2variant = openr2_context_get_variant(r2context);</span><br><span> snprintf(channo, sizeof(channo), "%d", p->channel);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(linkno, sizeof(linkno), "%d", p->mfcr2->index);</span><br><span> snprintf(anino, sizeof(anino), "%d", openr2_context_get_max_ani(r2context));</span><br><span> snprintf(dnisno, sizeof(dnisno), "%d", openr2_context_get_max_dnis(r2context));</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, FORMAT, channo, openr2_proto_get_variant_string(r2variant),</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, FORMAT, channo, linkno, openr2_proto_get_variant_string(r2variant),</span><br><span> anino, dnisno, openr2_context_get_ani_first(r2context) ? "Yes" : "No",</span><br><span> openr2_context_get_immediate_accept(r2context) ? "Yes" : "No",</span><br><span> openr2_chan_get_tx_cas_string(p->r2chan), openr2_chan_get_rx_cas_string(p->r2chan));</span><br><span>@@ -19311,15 +19338,20 @@</span><br><span> #endif /* defined(HAVE_SS7) */</span><br><span> #ifdef HAVE_OPENR2</span><br><span> if (reload != 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- int x;</span><br><span style="color: hsl(0, 100%, 40%);">- for (x = 0; x < r2links_count; x++) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_pthread_create(&r2links[x]->r2master, NULL, mfcr2_monitor, r2links[x])) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct r2link_entry *cur;</span><br><span style="color: hsl(120, 100%, 40%);">+ int x = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_LOCK(&r2links);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&r2links, cur, list) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct dahdi_mfcr2 *r2 = &cur->mfcr2;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_pthread_create(&r2->r2master, NULL, mfcr2_monitor, r2)) {</span><br><span> ast_log(LOG_ERROR, "Unable to start R2 monitor on channel group %d\n", x + 1);</span><br><span> return -1;</span><br><span> } else {</span><br><span> ast_verb(2, "Starting R2 monitor on channel group %d\n", x + 1);</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ x++;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_UNLOCK(&r2links);</span><br><span> }</span><br><span> #endif</span><br><span> /* And start the monitor for the first time */</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/11605">change 11605</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/c/asterisk/+/11605"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: Ibcb2401515a58782a1488c0b9efbed201c3f3a17 </div>
<div style="display:none"> Gerrit-Change-Number: 11605 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Oron Peled <oron.peled@xorcom.com> </div>
<div style="display:none"> Gerrit-Reviewer: Tzafrir Cohen <tzafrir.cohen@xorcom.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>