2023-04-05 05:51:08

by D. Starke

[permalink] [raw]
Subject: [PATCH 1/9] tty: n_gsm: fix redundant assignment of gsm->encoding

From: Daniel Starke <[email protected]>

The function gsmld_open() contains a redundant assignment of gsm->encoding.
The same value of GSM_ADV_OPT is already assigned to it during the
initialization of the struct in gsm_alloc_mux() a few lines earlier.

Fix this by removing the redundant second assignment of gsm->encoding in
gsmld_open().

Fixes: e1eaea46bb40 ("tty: n_gsm line discipline")
Signed-off-by: Daniel Starke <[email protected]>
---
drivers/tty/n_gsm.c | 1 -
1 file changed, 1 deletion(-)

diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index b7e1369a035c..c42c8b89fd46 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -3585,7 +3585,6 @@ static int gsmld_open(struct tty_struct *tty)
tty->receive_room = 65536;

/* Attach the initial passive connection */
- gsm->encoding = GSM_ADV_OPT;
gsmld_attach_gsm(tty, gsm);

/* The mux will not be activated yet, we wait for correct
--
2.34.1


2023-04-05 05:51:27

by D. Starke

[permalink] [raw]
Subject: [PATCH 8/9] tty: n_gsm: add DLCI specific rx/tx statistics

From: Daniel Starke <[email protected]>

Add counters for the number of data bytes received/transmitted per DLCI in
for preparation for an upcoming patch which will expose these values to the
user.

Signed-off-by: Daniel Starke <[email protected]>
---
drivers/tty/n_gsm.c | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 49cb2dbfa233..61f9825fde3c 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -185,6 +185,9 @@ struct gsm_dlci {
void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
struct net_device *net; /* network interface, if created */
+ /* Statistics (not currently exposed) */
+ u64 tx; /* Data bytes sent on this DLCI */
+ u64 rx; /* Data bytes received on this DLCI */
};

/*
@@ -1215,6 +1218,7 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
tty_port_tty_wakeup(&dlci->port);

__gsm_data_queue(dlci, msg);
+ dlci->tx += len;
/* Bytes of data we used up */
return size;
}
@@ -1459,6 +1463,7 @@ static int gsm_control_command(struct gsm_mux *gsm, int cmd, const u8 *data,
msg->data[1] = (dlen << 1) | EA;
memcpy(msg->data + 2, data, dlen);
gsm_data_queue(gsm->dlci[0], msg);
+ gsm->dlci[0]->tx += dlen;

return 0;
}
@@ -1485,6 +1490,7 @@ static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
msg->data[1] = (dlen << 1) | EA;
memcpy(msg->data + 2, data, dlen);
gsm_data_queue(gsm->dlci[0], msg);
+ gsm->dlci[0]->tx += dlen;
}

/**
@@ -1849,10 +1855,13 @@ static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
const u8 *data, int clen)
{
u8 buf[1];
+ struct gsm_dlci *dlci = gsm->dlci[0];
+
+ if (dlci)
+ dlci->rx += clen;

switch (command) {
case CMD_CLD: {
- struct gsm_dlci *dlci = gsm->dlci[0];
/* Modem wishes to close down */
if (dlci) {
dlci->dead = true;
@@ -1931,6 +1940,8 @@ static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,

ctrl = gsm->pending_cmd;
dlci = gsm->dlci[0];
+ if (dlci)
+ dlci->rx += clen;
command |= 1;
/* Does the reply match our command */
if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
@@ -2295,6 +2306,9 @@ static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
need_pn = true;
}

+ dlci->tx = 0;
+ dlci->rx = 0;
+
switch (dlci->state) {
case DLCI_CLOSED:
case DLCI_WAITING_CONFIG:
@@ -2327,6 +2341,9 @@ static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
*/
static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
{
+ dlci->tx = 0;
+ dlci->rx = 0;
+
switch (dlci->state) {
case DLCI_CLOSED:
case DLCI_WAITING_CONFIG:
@@ -2346,6 +2363,9 @@ static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
*/
static void gsm_dlci_set_wait_config(struct gsm_dlci *dlci)
{
+ dlci->tx = 0;
+ dlci->rx = 0;
+
switch (dlci->state) {
case DLCI_CLOSED:
case DLCI_CLOSING:
@@ -2422,6 +2442,7 @@ static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
fallthrough;
case 1: /* Line state will go via DLCI 0 controls only */
default:
+ dlci->rx += clen;
tty_insert_flip_string(port, data, clen);
tty_flip_buffer_push(port);
}
@@ -2782,6 +2803,7 @@ static void gsm_queue(struct gsm_mux *gsm)
gsm->open_error++;
return;
}
+ dlci->rx += gsm->len;
if (dlci->dead)
gsm_response(gsm, address, DM|PF);
else {
--
2.34.1

2023-04-05 05:54:02

by D. Starke

[permalink] [raw]
Subject: [PATCH 9/9] tty: n_gsm: cleanup gsm_control_command and gsm_control_reply

From: Daniel Starke <[email protected]>

There are multiple places in gsm_control_command and gsm_control_reply that
derive the specific DLCI handle directly out of the DLCI table in gsm.

Add a local variable which holds this handle and use it instead to improve
code readability.

Signed-off-by: Daniel Starke <[email protected]>
---
drivers/tty/n_gsm.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 61f9825fde3c..87720ebc38d7 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -1454,16 +1454,17 @@ static int gsm_control_command(struct gsm_mux *gsm, int cmd, const u8 *data,
int dlen)
{
struct gsm_msg *msg;
+ struct gsm_dlci *dlci = gsm->dlci[0];

- msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->dlci[0]->ftype);
+ msg = gsm_data_alloc(gsm, 0, dlen + 2, dlci->ftype);
if (msg == NULL)
return -ENOMEM;

msg->data[0] = (cmd << 1) | CR | EA; /* Set C/R */
msg->data[1] = (dlen << 1) | EA;
memcpy(msg->data + 2, data, dlen);
- gsm_data_queue(gsm->dlci[0], msg);
- gsm->dlci[0]->tx += dlen;
+ gsm_data_queue(dlci, msg);
+ dlci->tx += dlen;

return 0;
}
@@ -1482,15 +1483,16 @@ static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
int dlen)
{
struct gsm_msg *msg;
+ struct gsm_dlci *dlci = gsm->dlci[0];

- msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->dlci[0]->ftype);
+ msg = gsm_data_alloc(gsm, 0, dlen + 2, dlci->ftype);
if (msg == NULL)
return;
msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
msg->data[1] = (dlen << 1) | EA;
memcpy(msg->data + 2, data, dlen);
- gsm_data_queue(gsm->dlci[0], msg);
- gsm->dlci[0]->tx += dlen;
+ gsm_data_queue(dlci, msg);
+ dlci->tx += dlen;
}

/**
--
2.34.1

2023-04-05 08:31:22

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH 1/9] tty: n_gsm: fix redundant assignment of gsm->encoding

On Wed, 5 Apr 2023, D. Starke wrote:

> From: Daniel Starke <[email protected]>
>
> The function gsmld_open() contains a redundant assignment of gsm->encoding.
> The same value of GSM_ADV_OPT is already assigned to it during the
> initialization of the struct in gsm_alloc_mux() a few lines earlier.
>
> Fix this by removing the redundant second assignment of gsm->encoding in
> gsmld_open().
>
> Fixes: e1eaea46bb40 ("tty: n_gsm line discipline")
> Signed-off-by: Daniel Starke <[email protected]>
> ---
> drivers/tty/n_gsm.c | 1 -
> 1 file changed, 1 deletion(-)
>
> diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
> index b7e1369a035c..c42c8b89fd46 100644
> --- a/drivers/tty/n_gsm.c
> +++ b/drivers/tty/n_gsm.c
> @@ -3585,7 +3585,6 @@ static int gsmld_open(struct tty_struct *tty)
> tty->receive_room = 65536;
>
> /* Attach the initial passive connection */
> - gsm->encoding = GSM_ADV_OPT;
> gsmld_attach_gsm(tty, gsm);
>
> /* The mux will not be activated yet, we wait for correct
>

Reviewed-by: Ilpo J?rvinen <[email protected]>

--
i.

2023-04-05 09:26:37

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH 8/9] tty: n_gsm: add DLCI specific rx/tx statistics

On Wed, 5 Apr 2023, D. Starke wrote:

> From: Daniel Starke <[email protected]>
>
> Add counters for the number of data bytes received/transmitted per DLCI in
> for preparation for an upcoming patch which will expose these values to the
> user.
>
> Signed-off-by: Daniel Starke <[email protected]>
> ---
> drivers/tty/n_gsm.c | 24 +++++++++++++++++++++++-
> 1 file changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
> index 49cb2dbfa233..61f9825fde3c 100644
> --- a/drivers/tty/n_gsm.c
> +++ b/drivers/tty/n_gsm.c
> @@ -185,6 +185,9 @@ struct gsm_dlci {
> void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
> void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
> struct net_device *net; /* network interface, if created */
> + /* Statistics (not currently exposed) */
> + u64 tx; /* Data bytes sent on this DLCI */
> + u64 rx; /* Data bytes received on this DLCI */
> };
>
> /*
> @@ -1215,6 +1218,7 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
> tty_port_tty_wakeup(&dlci->port);
>
> __gsm_data_queue(dlci, msg);
> + dlci->tx += len;
> /* Bytes of data we used up */
> return size;
> }

Reading the function comments and your changelog, I'm left to wonder why
gsm_dlci_data_output() is supposed to increment ->tx but
gsm_dlci_data_output_framed() is not?

--
i.

> @@ -1459,6 +1463,7 @@ static int gsm_control_command(struct gsm_mux *gsm, int cmd, const u8 *data,
> msg->data[1] = (dlen << 1) | EA;
> memcpy(msg->data + 2, data, dlen);
> gsm_data_queue(gsm->dlci[0], msg);
> + gsm->dlci[0]->tx += dlen;
>
> return 0;
> }
> @@ -1485,6 +1490,7 @@ static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
> msg->data[1] = (dlen << 1) | EA;
> memcpy(msg->data + 2, data, dlen);
> gsm_data_queue(gsm->dlci[0], msg);
> + gsm->dlci[0]->tx += dlen;
> }
>
> /**
> @@ -1849,10 +1855,13 @@ static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
> const u8 *data, int clen)
> {
> u8 buf[1];
> + struct gsm_dlci *dlci = gsm->dlci[0];
> +
> + if (dlci)
> + dlci->rx += clen;
>
> switch (command) {
> case CMD_CLD: {
> - struct gsm_dlci *dlci = gsm->dlci[0];
> /* Modem wishes to close down */
> if (dlci) {
> dlci->dead = true;
> @@ -1931,6 +1940,8 @@ static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
>
> ctrl = gsm->pending_cmd;
> dlci = gsm->dlci[0];
> + if (dlci)
> + dlci->rx += clen;
> command |= 1;
> /* Does the reply match our command */
> if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
> @@ -2295,6 +2306,9 @@ static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
> need_pn = true;
> }
>
> + dlci->tx = 0;
> + dlci->rx = 0;
> +
> switch (dlci->state) {
> case DLCI_CLOSED:
> case DLCI_WAITING_CONFIG:
> @@ -2327,6 +2341,9 @@ static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
> */
> static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
> {
> + dlci->tx = 0;
> + dlci->rx = 0;
> +
> switch (dlci->state) {
> case DLCI_CLOSED:
> case DLCI_WAITING_CONFIG:
> @@ -2346,6 +2363,9 @@ static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
> */
> static void gsm_dlci_set_wait_config(struct gsm_dlci *dlci)
> {
> + dlci->tx = 0;
> + dlci->rx = 0;
> +
> switch (dlci->state) {
> case DLCI_CLOSED:
> case DLCI_CLOSING:
> @@ -2422,6 +2442,7 @@ static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
> fallthrough;
> case 1: /* Line state will go via DLCI 0 controls only */
> default:
> + dlci->rx += clen;
> tty_insert_flip_string(port, data, clen);
> tty_flip_buffer_push(port);
> }
> @@ -2782,6 +2803,7 @@ static void gsm_queue(struct gsm_mux *gsm)
> gsm->open_error++;
> return;
> }
> + dlci->rx += gsm->len;
> if (dlci->dead)
> gsm_response(gsm, address, DM|PF);
> else {
>

2023-04-05 09:27:25

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH 9/9] tty: n_gsm: cleanup gsm_control_command and gsm_control_reply

On Wed, 5 Apr 2023, D. Starke wrote:

> From: Daniel Starke <[email protected]>
>
> There are multiple places in gsm_control_command and gsm_control_reply that
> derive the specific DLCI handle directly out of the DLCI table in gsm.
>
> Add a local variable which holds this handle and use it instead to improve
> code readability.
>
> Signed-off-by: Daniel Starke <[email protected]>
> ---
> drivers/tty/n_gsm.c | 14 ++++++++------
> 1 file changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
> index 61f9825fde3c..87720ebc38d7 100644
> --- a/drivers/tty/n_gsm.c
> +++ b/drivers/tty/n_gsm.c
> @@ -1454,16 +1454,17 @@ static int gsm_control_command(struct gsm_mux *gsm, int cmd, const u8 *data,
> int dlen)
> {
> struct gsm_msg *msg;
> + struct gsm_dlci *dlci = gsm->dlci[0];
>
> - msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->dlci[0]->ftype);
> + msg = gsm_data_alloc(gsm, 0, dlen + 2, dlci->ftype);
> if (msg == NULL)
> return -ENOMEM;
>
> msg->data[0] = (cmd << 1) | CR | EA; /* Set C/R */
> msg->data[1] = (dlen << 1) | EA;
> memcpy(msg->data + 2, data, dlen);
> - gsm_data_queue(gsm->dlci[0], msg);
> - gsm->dlci[0]->tx += dlen;
> + gsm_data_queue(dlci, msg);
> + dlci->tx += dlen;
>
> return 0;
> }
> @@ -1482,15 +1483,16 @@ static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
> int dlen)
> {
> struct gsm_msg *msg;
> + struct gsm_dlci *dlci = gsm->dlci[0];
>
> - msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->dlci[0]->ftype);
> + msg = gsm_data_alloc(gsm, 0, dlen + 2, dlci->ftype);
> if (msg == NULL)
> return;
> msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
> msg->data[1] = (dlen << 1) | EA;
> memcpy(msg->data + 2, data, dlen);
> - gsm_data_queue(gsm->dlci[0], msg);
> - gsm->dlci[0]->tx += dlen;
> + gsm_data_queue(dlci, msg);
> + dlci->tx += dlen;
> }
>
> /**
>

IMO, this patch should be done before patch 8/9.

--
i.

2023-04-06 06:02:58

by D. Starke

[permalink] [raw]
Subject: RE: [PATCH 8/9] tty: n_gsm: add DLCI specific rx/tx statistics

> > @@ -1215,6 +1218,7 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
> > tty_port_tty_wakeup(&dlci->port);
> >
> > __gsm_data_queue(dlci, msg);
> > + dlci->tx += len;
> > /* Bytes of data we used up */
> > return size;
> > }
>
> Reading the function comments and your changelog, I'm left to wonder why
> gsm_dlci_data_output() is supposed to increment ->tx but
> gsm_dlci_data_output_framed() is not?

Thank you for pointing this out. I will add the part for
gsm_dlci_data_output_framed() it in the next version of this patch.

Best regards,
Daniel Starke

2023-04-06 06:09:50

by D. Starke

[permalink] [raw]
Subject: RE: [PATCH 9/9] tty: n_gsm: cleanup gsm_control_command and gsm_control_reply

> IMO, this patch should be done before patch 8/9.

I will change the order in the next version of this patch.

Best regards,
Daniel Starke