Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CoAP block size configurable at build- and run-time #596

Merged
merged 3 commits into from
May 11, 2021

Conversation

mlasch
Copy link
Contributor

@mlasch mlasch commented Apr 26, 2021

This patch makes the block size used in CoAP block transfers configurable in two ways.

  1. Build-time configurable default value via a cmake variable.
  2. Run-time configurable via new -S command line argument for all examples. This will simplify (automated) testing with different block sizes.

⚠️ The new default value for the block size will now be 1024 bytes with no further configuration. The idea is to avoid block transfers in common use-cases. Any objections?

- -s Set the device management or bootstrap server Pre-Shared-Key. If not set use none secure mode
```
-i Set the device management or bootstrap server PSK identity. If not set use none secure mode
-s Set the device management or bootstrap server Pre-Shared-Key. If not set use none secure mode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CoAP block size and Pre-Shared-Key are both -s.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that. I would go with -S then. Another option would be -a since other obvious options are already used.

@@ -57,6 +57,7 @@

#include "liblwm2m.h"
#include "connection.h"
#include "commandline.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the additional include? I don't think the lightclient supports commands.

Copy link
Contributor Author

@mlasch mlasch Apr 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put validate_coap_block_size in commandline.c because it is shared code for the examples. Therefore this include is needed in the lightclient. Another place would be core/utils.c and liblwm2m.h.

{
return coap_block_size_arg >= 16 && \
coap_block_size_arg <= 1024 && \
coap_block_size_arg % 2 == 0;
Copy link
Contributor

@sbertin-telular sbertin-telular Apr 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow any even block size from 16 to 1024, not just 16, 32, 64, 128, 256, 512, 1024.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update this part and move into core/utils.c

@@ -65,6 +65,7 @@
/*- Variables -----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
static uint16_t current_mid = 0;
uint16_t coap_block_size = COAP_DEFAULT_BLOCK_SIZE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is declared in liblwm2m.h. I think the storage and default value should be set in one of the core files, not here. I would suggest core/packet.c since that is where most of the usage occurs.

@@ -97,6 +97,8 @@ extern "C" {
#error "LWM2M_BOOTSTRAP and LWM2M_BOOTSTRAP_SERVER_MODE cannot be defined at the same time!"
#endif

extern uint16_t coap_block_size;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there are any other variables publicly exported by Wakaama. I would suggest moving this to core/internals.h and creating a function to set it. That would also give a point to notify a CoAP stack (if we support a different one in the future) of a change in the block size.

On another note, anything meant to be accessed by the user's code should start with lwm2m.

@@ -1014,6 +1015,22 @@ int main(int argc, char *argv[])
case '4':
data.addressFamily = AF_INET;
break;
case 's':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate case if WITH_TINYDTLS is defined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rettichschnidi, the CI didn't catch this. Possibly another area for improvement. I'm not sure if it is doing a DTLS build or not.

@sbertin-telular
Copy link
Contributor

1024 bytes for the block size agrees with RFC7252 section 4.6

@mlasch mlasch force-pushed the ml/make-block-size-configurable branch from 824e7be to a08f7d7 Compare April 27, 2021 11:27
@mlasch mlasch requested a review from rettichschnidi April 27, 2021 12:43
@rettichschnidi
Copy link
Contributor

@mlasch Please rebase on the current master 😸

@mlasch mlasch force-pushed the ml/make-block-size-configurable branch from a08f7d7 to b65fb0f Compare April 27, 2021 13:00
core/packet.c Outdated

bool lwm2m_validate_block_size(uint16_t coap_block_size_arg)
{
uint16_t valid_block_sizes[7] = {16, 32, 64, 128, 256, 512, 1024};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add const here. That will allow it to remain in flash on some devices rather than being copied to RAM and using more resources.

core/packet.c Outdated

void lwm2m_set_block_size(uint16_t coap_block_size_arg)
{
coap_block_size = coap_block_size_arg;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see the set do the validation and return a true/false indication to prevent misuse and simplify the API for the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds sensible. I will remove lwm2m_validate_block_size from the public interface then.

@mlasch mlasch self-assigned this Apr 27, 2021
@mlasch mlasch force-pushed the ml/make-block-size-configurable branch from b65fb0f to dffbfb7 Compare April 27, 2021 21:18
@mlasch
Copy link
Contributor Author

mlasch commented Apr 27, 2021

Pushed again, let clang-format do the formatting.

Copy link
Contributor

@sbertin-telular sbertin-telular left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

README.md Outdated Show resolved Hide resolved
coap/er-coap-13/er-coap-13.h Show resolved Hide resolved
core/internals.h Outdated Show resolved Hide resolved
core/packet.c Show resolved Hide resolved
core/packet.c Show resolved Hide resolved
Comment on lines 379 to 387
uint16_t coap_block_size_arg;
if (1 == sscanf(argv[opt], "%hu", &coap_block_size_arg) && lwm2m_set_block_size(coap_block_size_arg)) {
break;
} else {
print_usage();
return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@@ -1217,7 +1232,7 @@ int main(int argc, char *argv[])
in_port_t port;
connection_t * connP;

s[0] = 0;
s[0] = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwanted cosmetic change?

Comment on lines 1149 to 1155
uint16_t coap_block_size_arg;
if (1 == sscanf(argv[opt], "%hu", &coap_block_size_arg) && lwm2m_set_block_size(coap_block_size_arg)) {
break;
} else {
print_usage();
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@@ -277,6 +277,11 @@ void lwm2m_list_free(lwm2m_list_t * head);
#define LWM2M_LIST_FIND(H,I) lwm2m_list_find((lwm2m_list_t *)H, I)
#define LWM2M_LIST_FREE(H) lwm2m_list_free((lwm2m_list_t *)H)

/*
* Helper functions for coap block size settings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Helper functions for coap block size settings.
* Helper functions for CoAP block size settings.

coap/transaction.c Outdated Show resolved Hide resolved
@mlasch mlasch force-pushed the ml/make-block-size-configurable branch from dffbfb7 to d92c2c4 Compare May 4, 2021 10:04
core/packet.c Outdated
@@ -93,6 +93,28 @@ Contains code snippets which are:

#include <stdio.h>

uint16_t coap_block_size = LWM2M_COAP_DEFAULT_BLOCK_SIZE;

static bool validate_block_size(uint16_t coap_block_size_arg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static bool validate_block_size(uint16_t coap_block_size_arg) {
static bool validate_block_size(const uint16_t coap_block_size_arg) {

core/packet.c Outdated
return false;
}

bool lwm2m_set_coap_block_size(uint16_t coap_block_size_arg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool lwm2m_set_coap_block_size(uint16_t coap_block_size_arg) {
bool lwm2m_set_coap_block_size(const uint16_t coap_block_size_arg) {

@mlasch mlasch force-pushed the ml/make-block-size-configurable branch from d92c2c4 to 1ca6295 Compare May 4, 2021 12:29
return 0;
}
uint16_t coap_block_size_arg;
if (1 == sscanf(argv[opt], "%hu", &coap_block_size_arg) && lwm2m_set_coap_block_size(coap_block_size_arg)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather use C99 format specifiers:

Suggested change
if (1 == sscanf(argv[opt], "%hu", &coap_block_size_arg) && lwm2m_set_coap_block_size(coap_block_size_arg)) {
if (1 == sscanf(argv[opt], "%" PRIu16, &coap_block_size_arg) && lwm2m_set_coap_block_size(coap_block_size_arg)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you meant SCNu16 in this case. I can change those lines.

return 0;
}
uint16_t coap_block_size_arg;
if (1 == sscanf(argv[opt], "%hu", &coap_block_size_arg) && lwm2m_set_coap_block_size(coap_block_size_arg)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather use C99 format specifiers.

return 0;
}
uint16_t coap_block_size_arg;
if (1 == sscanf(argv[opt], "%hu", &coap_block_size_arg) && lwm2m_set_coap_block_size(coap_block_size_arg)) {
Copy link
Contributor

@rettichschnidi rettichschnidi May 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather use C99 format specifiers.

return 0;
}
uint16_t coap_block_size_arg;
if (1 == sscanf(argv[opt], "%hu", &coap_block_size_arg) && lwm2m_set_coap_block_size(coap_block_size_arg)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather use C99 format specifiers.

* Helper functions for CoAP block size settings.
*/
bool lwm2m_set_coap_block_size(uint16_t coap_block_size_arg);
uint16_t lwm2m_get_coap_block_size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use prototypes in C:

Suggested change
uint16_t lwm2m_get_coap_block_size();
uint16_t lwm2m_get_coap_block_size(void);

@mlasch mlasch force-pushed the ml/make-block-size-configurable branch from 1ca6295 to f93b8c1 Compare May 5, 2021 18:19
@mlasch mlasch requested a review from rettichschnidi May 5, 2021 18:19
@mlasch mlasch force-pushed the ml/make-block-size-configurable branch from f93b8c1 to e8d92c7 Compare May 5, 2021 18:37
Copy link
Contributor

@rettichschnidi rettichschnidi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small fix/improvement I feel is worth to take into account. See comment. Everything else LGTM.

Comment on lines 14 to 16
if (LWM2M_COAP_DEFAULT_BLOCK_SIZE)
add_compile_definitions(LWM2M_COAP_DEFAULT_BLOCK_SIZE=${LWM2M_COAP_DEFAULT_BLOCK_SIZE})
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Wakaama code will neither compile nor work when LWM2M_COAP_DEFAULT_BLOCK_SIZE is zero. Therefore, this if should not exist.

Suggested change
if (LWM2M_COAP_DEFAULT_BLOCK_SIZE)
add_compile_definitions(LWM2M_COAP_DEFAULT_BLOCK_SIZE=${LWM2M_COAP_DEFAULT_BLOCK_SIZE})
endif()
add_compile_definitions(LWM2M_COAP_DEFAULT_BLOCK_SIZE=${LWM2M_COAP_DEFAULT_BLOCK_SIZE})

core/packet.c Show resolved Hide resolved
@qleisan
Copy link

qleisan commented May 10, 2021

Once this PR is merged (with the modified default blocksize) other PRs related to integration tests can me merged

mlasch added 3 commits May 10, 2021 15:55
This patch makes the CoAP block size, used in block transfers,
configurable in two ways.
 1. Build-time configurable default value via a cmake variable.
    LWM2M_COAP_DEFAULT_BLOCK_SIZE
 2. Run-time configurable via new -S command line argument.

Add this new function to the interface:
 - lwm2m_set_coap_block_size()
 - lwm2m_get_coap_block_size()

The new default value for the block size will now be 1024
with no further configuration. This avoids block transfers
in common use-cases.

Signed-off-by: Marc Lasch <marc.lasch@husqvarnagroup.com>
clang-format enforced changes for unkown reasons.

Signed-off-by: Marc Lasch <marc.lasch@husqvarnagroup.com>
Adjust git blame ignores

Signed-off-by: Marc Lasch <marc.lasch@husqvarnagroup.com>
@mlasch mlasch force-pushed the ml/make-block-size-configurable branch from e8d92c7 to 17b6ac6 Compare May 10, 2021 13:59
@mlasch mlasch merged commit ec3bc98 into eclipse-wakaama:master May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants