What is Checksum?
The Checksum module provides data integrity verification. It computes CRC32 or simple checksum to detect accidental data corruption.
Key idea:** Know when data is corrupted. Don't silently process bad data.
Quick Start
#include "rte/checksum/rte_checksum.h"
uint32_t crc = rte_checksum_crc32(data, length);
uint32_t stored_crc = get_stored_checksum();
if (crc != stored_crc) {
log_error("Data corrupted: CRC mismatch");
}
#define RTE_SAFESTATE(level, reason)
Explicitly enters the given safe-state level with a reason code, capturing the call site automaticall...
@ RTE_SAFESTATE_LEVEL_SAFE
Functions
uint32_t rte_checksum_crc32(const uint8_t *data, size_t length);
uint8_t rte_checksum_xor8(const uint8_t *data, size_t length);
uint16_t rte_checksum_crc16(const uint8_t *data, size_t length);
Practical Examples
Example 1: Message Integrity Check
typedef struct {
uint32_t command_id;
uint8_t data[256];
uint32_t checksum;
} message_t;
uint32_t computed = rte_checksum_crc32((const uint8_t *)msg,
offsetof(message_t, checksum));
if (computed != msg->checksum) {
log_error("Message corrupted");
}
execute_command(msg->command_id, msg->data);
}
rte_status_t
Common result/status codes.
@ RTE_STATUS_INVALID_PARAM
Example 2: Configuration Validation
typedef struct {
uint32_t version;
char device_name[32];
uint32_t timeout_ms;
uint32_t crc;
} config_t;
read_nvm(0, (uint8_t *)cfg, sizeof(config_t));
size_t data_len = offsetof(config_t, crc);
uint32_t computed = rte_checksum_crc32((const uint8_t *)cfg, data_len);
if (computed != cfg->crc) {
log_error("Configuration corrupted, using defaults");
init_default_config(cfg);
}
}
Best Practices
- Use CRC32 for Large Data
- Better collision detection
- Slightly slower but worth it
- Checksum Storage
- Store checksum with data
- Compute over all data except checksum field
- Verify on load/receive
- When to Verify
- After receiving over network
- After reading from storage
- Before executing critical commands
- Handling Corruption
- Don't try to "fix" corrupted data
- Log the corruption
- Either reload from backup or trigger safe-state
See Also