Skip to content

Commit 89d8a8e

Browse files
committed
api: document common headers
Add comments to utility headers for bit operations, error codes, macros, network types and string helpers. Signed-off-by: Robin Jarry <[email protected]> Reviewed-by: Christophe Fontaine <[email protected]>
1 parent d5404f0 commit 89d8a8e

7 files changed

Lines changed: 31 additions & 8 deletions

File tree

api/gr_bitops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <stdint.h>
77

8+
// Bit manipulation utilities for flags and bitmasks.
89
#define GR_BIT8(n) (UINT8_C(1) << (n))
910
#define GR_BIT16(n) (UINT16_C(1) << (n))
1011
#define GR_BIT32(n) (UINT32_C(1) << (n))

api/gr_clock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
#include <stdint.h>
77
#include <time.h>
88

9-
//! Get the elapsed time since last boot (using a common clock across all processes).
9+
// Get the elapsed time since last boot (using a common clock across all processes).
1010
static inline struct timespec gr_clock_raw(void) {
1111
struct timespec tp = {0};
1212
clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
1313
return tp;
1414
}
1515

16-
//! Get elapsed time since last boot in microseconds.
16+
// Get elapsed time since last boot in microseconds.
1717
static inline clock_t gr_clock_us(void) {
1818
struct timespec tp = gr_clock_raw();
1919
return (tp.tv_sec * CLOCKS_PER_SEC) + (tp.tv_nsec / 1000);

api/gr_errno.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
#include <errno.h>
77
#include <stddef.h>
88

9+
// Set errno and return negative error code.
910
static inline int errno_set(int errnum) {
1011
errno = errnum;
1112
return -errnum;
1213
}
1314

15+
// Set errno and return NULL pointer.
1416
static inline void *errno_set_null(int errnum) {
1517
errno = errnum;
1618
return NULL;

api/gr_macro.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
#include <errno.h>
77
#include <limits.h>
88

9+
// Get number of elements in a static array.
910
#define ARRAY_DIM(array) (sizeof(array) / sizeof(array[0]))
11+
12+
// Get size of a specific member in a struct type.
1013
#define MEMBER_SIZE(type, member) (sizeof(((type *)0)->member))
14+
15+
// Get pointer to payload data immediately following a header.
1116
#define PAYLOAD(header) ((void *)(header + 1))
17+
18+
// Get maximum number of values for an unsigned integer type.
1219
#define UINT_NUM_VALUES(type) (1ULL << (sizeof(type) * CHAR_BIT))
1320

1421
// Define a structure as a base for another one using anonymous tagged structure extension.

api/gr_net_types.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
#include <gr_net_compat.h>
2323
#endif
2424

25+
// Address family enumeration.
2526
typedef enum : uint8_t {
2627
GR_AF_UNSPEC = AF_UNSPEC,
2728
GR_AF_IP4 = AF_INET,
2829
GR_AF_IP6 = AF_INET6,
2930
} addr_family_t;
3031

32+
// Convert address family enum to string representation.
3133
static inline const char *gr_af_name(addr_family_t af) {
3234
switch (af) {
3335
case GR_AF_UNSPEC:
@@ -40,7 +42,7 @@ static inline const char *gr_af_name(addr_family_t af) {
4042
return "?";
4143
}
4244

43-
// Custom printf specifiers
45+
// Custom printf specifiers for network addresses.
4446

4547
// struct rte_ether_addr *
4648
#define ETH_F "%2p"
@@ -61,20 +63,24 @@ static inline const char *gr_af_name(addr_family_t af) {
6163
#define IPV4_RE "^" __IPV4_RE "$"
6264
#define IPV4_NET_RE "^" __IPV4_RE __IPV4_PREFIX_RE "$"
6365

66+
// IPv4 address type (network byte order).
6467
typedef uint32_t ip4_addr_t;
6568

69+
// IPv4 network with prefix length.
6670
struct ip4_net {
6771
ip4_addr_t ip;
6872
uint8_t prefixlen;
6973
};
7074

75+
// Check if two IPv4 addresses are in the same subnet.
7176
static inline bool ip4_addr_same_subnet(ip4_addr_t a, ip4_addr_t b, uint8_t prefixlen) {
7277
ip4_addr_t mask = htonl(~(UINT32_MAX >> prefixlen));
7378
return ((a ^ b) & mask) == 0;
7479
}
7580

7681
#define IPV4_ADDR_BCAST RTE_BE32(0xffffffff)
7782

83+
// Check if the provided IPv4 address is multicast.
7884
static inline bool ip4_addr_is_mcast(const ip4_addr_t ip) {
7985
const union {
8086
ip4_addr_t ip;
@@ -83,6 +89,7 @@ static inline bool ip4_addr_is_mcast(const ip4_addr_t ip) {
8389
return addr.u8[0] >= 224 && addr.u8[0] <= 239;
8490
}
8591

92+
// Parse IPv4 network string (e.g. "192.168.1.0/24") into ip4_net structure.
8693
static inline int ip4_net_parse(const char *s, struct ip4_net *net, bool zero_mask) {
8794
char *addr = NULL;
8895
int ret = -1;
@@ -115,11 +122,13 @@ static inline int ip4_net_parse(const char *s, struct ip4_net *net, bool zero_ma
115122
#define IPV6_RE "^" __IPV6_RE "$"
116123
#define IPV6_NET_RE "^" __IPV6_RE __IPV6_PREFIX_RE "$"
117124

125+
// IPv6 network with prefix length.
118126
struct ip6_net {
119127
struct rte_ipv6_addr ip;
120128
uint8_t prefixlen;
121129
};
122130

131+
// Parse IPv6 network string (e.g. "2001:db8::/32") into ip6_net structure.
123132
static inline int ip6_net_parse(const char *s, struct ip6_net *net, bool zero_mask) {
124133
char *addr = NULL;
125134
int ret = -1;

api/gr_string.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
#include <sched.h>
77
#include <stddef.h>
88

9+
// Concatenate formatted string to existing buffer (realloc as needed).
910
char *astrcat(char *buf, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
11+
12+
// Join array of strings with separator.
1013
char *strjoin(char **array, size_t len, const char *sep);
14+
15+
// Check if buffer contains only printable characters up to maxlen.
1116
int charset_check(const char *buf, size_t maxlen);
1217

13-
// Return human readable representation of a cpuset. The output format is
14-
// a list of CPUs with ranges (for example, "0,1,3-9").
18+
// Format CPU set as human readable string with ranges (e.g. "0,1,3-9").
1519
int cpuset_format(char *buf, size_t len, const cpu_set_t *set);
1620

17-
// Parse a list of CPUs (e.g. "0,1,3-9") to a cpu_set_t object.
21+
// Parse CPU list string (e.g. "0,1,3-9") into a cpu_set_t object.
1822
int cpuset_parse(cpu_set_t *set, const char *buf);

main/gr_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ struct gr_config {
2222
bool log_syslog;
2323
bool log_packets;
2424
gr_vec char **eal_extra_args;
25-
cpu_set_t control_cpus; //!< control plane threads allowed CPUs
26-
cpu_set_t datapath_cpus; //!< datapath threads allowed CPUs
25+
cpu_set_t control_cpus; // control plane threads allowed CPUs
26+
cpu_set_t datapath_cpus; // datapath threads allowed CPUs
2727
};
2828

2929
extern struct gr_config gr_config;

0 commit comments

Comments
 (0)