Change in libosmocore[master]: gprs_ns2: Encapsulate setting NSE dialect

laforge gerrit-no-reply at lists.osmocom.org
Fri Mar 5 09:41:13 UTC 2021


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/23246 )


Change subject: gprs_ns2: Encapsulate setting NSE dialect
......................................................................

gprs_ns2: Encapsulate setting NSE dialect

Setting the NSE dialect possibly involves the creating (or destruction)
of a IP-SNS FSM.  Encapsulate that, rather than having every caller
re-implement that.

Change-Id: I24fdc26fbcfda039bd58ea166f4d5c2fd1801da1
---
M src/gb/gprs_ns2.c
M src/gb/gprs_ns2_internal.h
M src/gb/gprs_ns2_vty.c
3 files changed, 52 insertions(+), 37 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/46/23246/1

diff --git a/src/gb/gprs_ns2.c b/src/gb/gprs_ns2.c
index e19ae78..67403f4 100644
--- a/src/gb/gprs_ns2.c
+++ b/src/gb/gprs_ns2.c
@@ -781,7 +781,6 @@
 					 enum gprs_ns2_ll linklayer, enum gprs_ns2_dialect dialect)
 {
 	struct gprs_ns2_nse *nse;
-	char sns[16];
 
 	nse = gprs_ns2_nse_by_nsei(nsi, nsei);
 	if (nse) {
@@ -792,17 +791,13 @@
 	nse = talloc_zero(nsi, struct gprs_ns2_nse);
 	if (!nse)
 		return NULL;
+	nse->dialect = GPRS_NS2_DIALECT_UNDEF;
 
-	if (dialect == GPRS_NS2_DIALECT_SNS) {
-		snprintf(sns, sizeof(sns), "NSE%05u-SNS", nsei);
-		nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns);
-		if (!nse->bss_sns_fi) {
-			talloc_free(nse);
-			return NULL;
-		}
+	if (ns2_nse_set_dialect(nse, dialect) < 0) {
+		talloc_free(nse);
+		return NULL;
 	}
 
-	nse->dialect = dialect;
 	nse->ll = linklayer;
 	nse->nsei = nsei;
 	nse->nsi = nsi;
@@ -814,6 +809,37 @@
 	return nse;
 }
 
+int ns2_nse_set_dialect(struct gprs_ns2_nse *nse, enum gprs_ns2_dialect dialect)
+{
+	char sns[16];
+
+	if (nse->dialect == dialect)
+		return 0;
+
+	switch (nse->dialect) {
+	case GPRS_NS2_DIALECT_UNDEF:
+		if (dialect == GPRS_NS2_DIALECT_SNS) {
+			snprintf(sns, sizeof(sns), "NSE%05u-SNS", nse->nsei);
+			nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns);
+			if (!nse->bss_sns_fi)
+				return -1;
+		}
+		nse->dialect = dialect;
+		break;
+	default:
+		if (dialect == GPRS_NS2_DIALECT_UNDEF) {
+			osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
+			nse->bss_sns_fi = NULL;
+			nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		} else {
+			/* we don't support arbitrary changes without going through UNDEF first */
+			return -EPERM;
+		}
+	}
+
+	return 0;
+}
+
 /*! Return the NSEI
  * \param[in] nse NS Entity
  * \return the nsei.
diff --git a/src/gb/gprs_ns2_internal.h b/src/gb/gprs_ns2_internal.h
index 80e664c..688a587 100644
--- a/src/gb/gprs_ns2_internal.h
+++ b/src/gb/gprs_ns2_internal.h
@@ -306,6 +306,7 @@
 			 enum gprs_ns2_affecting_cause cause);
 void ns2_nse_notify_alive(struct gprs_ns2_vc *nsvc, bool alive);
 void ns2_nse_update_mtu(struct gprs_ns2_nse *nse);
+int ns2_nse_set_dialect(struct gprs_ns2_nse *nse, enum gprs_ns2_dialect dialect);
 
 /* message */
 int ns2_validate(struct gprs_ns2_vc *nsvc,
diff --git a/src/gb/gprs_ns2_vty.c b/src/gb/gprs_ns2_vty.c
index 97e8413..f4a1ff2 100644
--- a/src/gb/gprs_ns2_vty.c
+++ b/src/gb/gprs_ns2_vty.c
@@ -900,7 +900,7 @@
 	}
 
 	if (nse->dialect == GPRS_NS2_DIALECT_UNDEF) {
-		nse->dialect = GPRS_NS2_DIALECT_STATIC_RESETBLOCK;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_STATIC_RESETBLOCK);
 		dialect_modified = true;
 	}
 
@@ -935,7 +935,7 @@
 	if (ll_modified)
 		nse->ll = GPRS_NS2_LL_UNDEF;
 	if (dialect_modified)
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 
 	return CMD_WARNING;
 }
@@ -986,7 +986,7 @@
 	gprs_ns2_free_nsvc(nsvc);
 	if (llist_empty(&nse->nsvc)) {
 		nse->ll = GPRS_NS2_LL_UNDEF;
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 	}
 
 	return CMD_SUCCESS;
@@ -1032,7 +1032,7 @@
 	gprs_ns2_free_nsvc(nsvc);
 	if (llist_empty(&nse->nsvc)) {
 		nse->ll = GPRS_NS2_LL_UNDEF;
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 	}
 
 	return CMD_SUCCESS;
@@ -1056,7 +1056,7 @@
 	}
 
 	if (nse->dialect == GPRS_NS2_DIALECT_UNDEF) {
-		nse->dialect = GPRS_NS2_DIALECT_STATIC_ALIVE;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_STATIC_ALIVE);
 		dialect_modified = true;
 	}
 
@@ -1117,7 +1117,7 @@
 	if (ll_modified)
 		nse->ll = GPRS_NS2_LL_UNDEF;
 	if (dialect_modified)
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 	return CMD_WARNING;
 }
 
@@ -1232,7 +1232,7 @@
 	gprs_ns2_free_nsvc(nsvc);
 	if (llist_empty(&nse->nsvc)) {
 		nse->ll = GPRS_NS2_LL_UNDEF;
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 	}
 
 	return CMD_SUCCESS;
@@ -1267,7 +1267,7 @@
 	}
 
 	if (nse->dialect == GPRS_NS2_DIALECT_UNDEF) {
-		nse->dialect = GPRS_NS2_DIALECT_IPACCESS;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_IPACCESS);
 		dialect_modified = true;
 	}
 
@@ -1317,7 +1317,7 @@
 	if (ll_modified)
 		nse->ll = GPRS_NS2_LL_UNDEF;
 	if (dialect_modified)
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 	return CMD_WARNING;
 }
 
@@ -1407,7 +1407,7 @@
 	gprs_ns2_free_nsvc(nsvc);
 	if (llist_empty(&nse->nsvc)) {
 		nse->ll = GPRS_NS2_LL_UNDEF;
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 	}
 
 	return CMD_SUCCESS;
@@ -1436,12 +1436,8 @@
 	}
 
 	if (nse->dialect == GPRS_NS2_DIALECT_UNDEF) {
-		char sns[16];
-		snprintf(sns, sizeof(sns), "NSE%05u-SNS", nse->nsei);
-		nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns);
-		if (!nse->bss_sns_fi)
+		if (ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_SNS) < 0)
 			goto err;
-		nse->dialect = GPRS_NS2_DIALECT_SNS;
 		dialect_modified = true;
 	}
 
@@ -1481,7 +1477,7 @@
 	if (ll_modified)
 		nse->ll = GPRS_NS2_LL_UNDEF;
 	if (dialect_modified)
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 	return CMD_WARNING;
 }
 
@@ -1528,10 +1524,8 @@
 		return CMD_SUCCESS;
 	} else {
 		/* clean up nse to allow other nsvc commands */
-		osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
-		nse->bss_sns_fi = NULL;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 		nse->ll = GPRS_NS2_LL_UNDEF;
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
 	}
 
 	return CMD_SUCCESS;
@@ -1557,12 +1551,8 @@
 	}
 
 	if (nse->dialect == GPRS_NS2_DIALECT_UNDEF) {
-		char sns[16];
-		snprintf(sns, sizeof(sns), "NSE%05u-SNS", nse->nsei);
-		nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns);
-		if (!nse->bss_sns_fi)
+		if (ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_SNS) < 0)
 			goto err;
-		nse->dialect = GPRS_NS2_DIALECT_SNS;
 		dialect_modified = true;
 	}
 
@@ -1632,7 +1622,7 @@
 	if (ll_modified)
 		nse->ll = GPRS_NS2_LL_UNDEF;
 	if (dialect_modified)
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 
 	return CMD_WARNING;
 }
@@ -1698,10 +1688,8 @@
 
 	if (!vty_nse_check_sns(nse)) {
 		/* clean up nse to allow other nsvc commands */
-		osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
-		nse->bss_sns_fi = NULL;
+		ns2_nse_set_dialect(nse, GPRS_NS2_DIALECT_UNDEF);
 		nse->ll = GPRS_NS2_LL_UNDEF;
-		nse->dialect = GPRS_NS2_DIALECT_UNDEF;
 	}
 
 	return CMD_SUCCESS;

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/23246
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I24fdc26fbcfda039bd58ea166f4d5c2fd1801da1
Gerrit-Change-Number: 23246
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210305/3a5584f9/attachment.htm>


More information about the gerrit-log mailing list