Skip to content

Commit dc1aed3

Browse files
Erik HugnePaul Gortmaker
authored andcommitted
tipc: phase out most of the struct print_buf usage
The tipc_printf is renamed to tipc_snprintf, as the new name describes more what the function actually does. It is also changed to take a buffer and length parameter and return number of characters written to the buffer. All callers of this function that used to pass a print_buf are updated. Final removal of the struct print_buf itself will be done synchronously with the pending removal of the deprecated logging code that also was using it. Functions that build up a response message with a list of ports, nametable contents etc. are changed to return the number of characters written to the output buffer. This information was previously hidden in a field of the print_buf struct, and the number of chars written was fetched with a call to tipc_printbuf_validate. This function is removed since it is no longer referenced nor needed. A generic max size ULTRA_STRING_MAX_LEN is defined, named in keeping with the existing TIPC_TLV_ULTRA_STRING, and the various definitions in port, link and nametable code that largely duplicated this information are removed. This means that amount of link statistics that can be returned is now increased from 2k to 32k. The buffer overflow check is now done just before the reply message is passed over netlink or TIPC to a remote node and the message indicating a truncated buffer is changed to a less dramatic one (less CAPS), placed at the end of the message. Signed-off-by: Erik Hugne <erik.hugne@ericsson.com> Signed-off-by: Jon Maloy <jon.maloy@ericsson.com> Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
1 parent e2dbd60 commit dc1aed3

10 files changed

Lines changed: 213 additions & 222 deletions

File tree

‎net/tipc/bcast.c‎

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -701,48 +701,43 @@ void tipc_bcbearer_sort(void)
701701

702702
int tipc_bclink_stats(char *buf, const u32 buf_size)
703703
{
704-
struct print_buf pb;
704+
int ret;
705+
struct tipc_stats *s;
705706

706707
if (!bcl)
707708
return 0;
708709

709-
tipc_printbuf_init(&pb, buf, buf_size);
710-
711710
spin_lock_bh(&bc_lock);
712711

713-
tipc_printf(&pb, "Link <%s>\n"
714-
" Window:%u packets\n",
715-
bcl->name, bcl->queue_limit[0]);
716-
tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
717-
bcl->stats.recv_info,
718-
bcl->stats.recv_fragments,
719-
bcl->stats.recv_fragmented,
720-
bcl->stats.recv_bundles,
721-
bcl->stats.recv_bundled);
722-
tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
723-
bcl->stats.sent_info,
724-
bcl->stats.sent_fragments,
725-
bcl->stats.sent_fragmented,
726-
bcl->stats.sent_bundles,
727-
bcl->stats.sent_bundled);
728-
tipc_printf(&pb, " RX naks:%u defs:%u dups:%u\n",
729-
bcl->stats.recv_nacks,
730-
bcl->stats.deferred_recv,
731-
bcl->stats.duplicates);
732-
tipc_printf(&pb, " TX naks:%u acks:%u dups:%u\n",
733-
bcl->stats.sent_nacks,
734-
bcl->stats.sent_acks,
735-
bcl->stats.retransmitted);
736-
tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
737-
bcl->stats.bearer_congs,
738-
bcl->stats.link_congs,
739-
bcl->stats.max_queue_sz,
740-
bcl->stats.queue_sz_counts
741-
? (bcl->stats.accu_queue_sz / bcl->stats.queue_sz_counts)
742-
: 0);
712+
s = &bcl->stats;
713+
714+
ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
715+
" Window:%u packets\n",
716+
bcl->name, bcl->queue_limit[0]);
717+
ret += tipc_snprintf(buf + ret, buf_size - ret,
718+
" RX packets:%u fragments:%u/%u bundles:%u/%u\n",
719+
s->recv_info, s->recv_fragments,
720+
s->recv_fragmented, s->recv_bundles,
721+
s->recv_bundled);
722+
ret += tipc_snprintf(buf + ret, buf_size - ret,
723+
" TX packets:%u fragments:%u/%u bundles:%u/%u\n",
724+
s->sent_info, s->sent_fragments,
725+
s->sent_fragmented, s->sent_bundles,
726+
s->sent_bundled);
727+
ret += tipc_snprintf(buf + ret, buf_size - ret,
728+
" RX naks:%u defs:%u dups:%u\n",
729+
s->recv_nacks, s->deferred_recv, s->duplicates);
730+
ret += tipc_snprintf(buf + ret, buf_size - ret,
731+
" TX naks:%u acks:%u dups:%u\n",
732+
s->sent_nacks, s->sent_acks, s->retransmitted);
733+
ret += tipc_snprintf(buf + ret, buf_size - ret,
734+
" Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
735+
s->bearer_congs, s->link_congs, s->max_queue_sz,
736+
s->queue_sz_counts ?
737+
(s->accu_queue_sz / s->queue_sz_counts) : 0);
743738

744739
spin_unlock_bh(&bc_lock);
745-
return tipc_printbuf_validate(&pb);
740+
return ret;
746741
}
747742

748743
int tipc_bclink_reset_stats(void)

‎net/tipc/bearer.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,23 @@ int tipc_register_media(struct tipc_media *m_ptr)
130130
/**
131131
* tipc_media_addr_printf - record media address in print buffer
132132
*/
133-
void tipc_media_addr_printf(struct print_buf *pb, struct tipc_media_addr *a)
133+
void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
134134
{
135135
char addr_str[MAX_ADDR_STR];
136136
struct tipc_media *m_ptr;
137+
int ret;
137138

138139
m_ptr = media_find_id(a->media_id);
139140

140141
if (m_ptr && !m_ptr->addr2str(a, addr_str, sizeof(addr_str)))
141-
tipc_printf(pb, "%s(%s)", m_ptr->name, addr_str);
142+
ret = tipc_snprintf(buf, len, "%s(%s)", m_ptr->name, addr_str);
142143
else {
143144
u32 i;
144145

145-
tipc_printf(pb, "UNKNOWN(%u)", a->media_id);
146+
ret = tipc_snprintf(buf, len, "UNKNOWN(%u)", a->media_id);
146147
for (i = 0; i < sizeof(a->value); i++)
147-
tipc_printf(pb, "-%02x", a->value[i]);
148+
ret += tipc_snprintf(buf - ret, len + ret,
149+
"-%02x", a->value[i]);
148150
}
149151
}
150152

‎net/tipc/bearer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void tipc_eth_media_stop(void);
179179

180180
int tipc_media_set_priority(const char *name, u32 new_value);
181181
int tipc_media_set_window(const char *name, u32 new_value);
182-
void tipc_media_addr_printf(struct print_buf *pb, struct tipc_media_addr *a);
182+
void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a);
183183
struct sk_buff *tipc_media_get_names(void);
184184

185185
struct sk_buff *tipc_bearer_get_names(void);

‎net/tipc/config.c‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include "name_table.h"
4040
#include "config.h"
4141

42+
#define REPLY_TRUNCATED "<truncated>\n"
43+
4244
static u32 config_port_ref;
4345

4446
static DEFINE_SPINLOCK(config_lock);
@@ -104,13 +106,12 @@ struct sk_buff *tipc_cfg_reply_string_type(u16 tlv_type, char *string)
104106
return buf;
105107
}
106108

107-
#define MAX_STATS_INFO 2000
108-
109109
static struct sk_buff *tipc_show_stats(void)
110110
{
111111
struct sk_buff *buf;
112112
struct tlv_desc *rep_tlv;
113-
struct print_buf pb;
113+
char *pb;
114+
int pb_len;
114115
int str_len;
115116
u32 value;
116117

@@ -121,17 +122,16 @@ static struct sk_buff *tipc_show_stats(void)
121122
if (value != 0)
122123
return tipc_cfg_reply_error_string("unsupported argument");
123124

124-
buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_STATS_INFO));
125+
buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
125126
if (buf == NULL)
126127
return NULL;
127128

128129
rep_tlv = (struct tlv_desc *)buf->data;
129-
tipc_printbuf_init(&pb, (char *)TLV_DATA(rep_tlv), MAX_STATS_INFO);
130-
131-
tipc_printf(&pb, "TIPC version " TIPC_MOD_VER "\n");
130+
pb = TLV_DATA(rep_tlv);
131+
pb_len = ULTRA_STRING_MAX_LEN;
132132

133-
/* Use additional tipc_printf()'s to return more info ... */
134-
str_len = tipc_printbuf_validate(&pb);
133+
str_len = tipc_snprintf(pb, pb_len, "TIPC version " TIPC_MOD_VER "\n");
134+
str_len += 1; /* for "\0" */
135135
skb_put(buf, TLV_SPACE(str_len));
136136
TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
137137

@@ -408,6 +408,15 @@ struct sk_buff *tipc_cfg_do_cmd(u32 orig_node, u16 cmd, const void *request_area
408408
break;
409409
}
410410

411+
WARN_ON(rep_tlv_buf->len > TLV_SPACE(ULTRA_STRING_MAX_LEN));
412+
413+
/* Append an error message if we cannot return all requested data */
414+
if (rep_tlv_buf->len == TLV_SPACE(ULTRA_STRING_MAX_LEN)) {
415+
if (*(rep_tlv_buf->data + ULTRA_STRING_MAX_LEN) != '\0')
416+
sprintf(rep_tlv_buf->data + rep_tlv_buf->len -
417+
sizeof(REPLY_TRUNCATED) - 1, REPLY_TRUNCATED);
418+
}
419+
411420
/* Return reply buffer */
412421
exit:
413422
spin_unlock_bh(&config_lock);

‎net/tipc/core.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060

6161
#define TIPC_MOD_VER "2.0.0"
6262

63+
#define ULTRA_STRING_MAX_LEN 32768
64+
6365
struct tipc_msg; /* msg.h */
6466
struct print_buf; /* log.h */
6567

@@ -82,7 +84,7 @@ extern struct print_buf *const TIPC_NULL;
8284
extern struct print_buf *const TIPC_CONS;
8385
extern struct print_buf *const TIPC_LOG;
8486

85-
void tipc_printf(struct print_buf *, const char *fmt, ...);
87+
int tipc_snprintf(char *buf, int len, const char *fmt, ...);
8688

8789
/*
8890
* TIPC_OUTPUT is the destination print buffer for system messages.

‎net/tipc/discover.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,10 @@ static void disc_dupl_alert(struct tipc_bearer *b_ptr, u32 node_addr,
100100
{
101101
char node_addr_str[16];
102102
char media_addr_str[64];
103-
struct print_buf pb;
104103

105104
tipc_addr_string_fill(node_addr_str, node_addr);
106-
tipc_printbuf_init(&pb, media_addr_str, sizeof(media_addr_str));
107-
tipc_media_addr_printf(&pb, media_addr);
108-
tipc_printbuf_validate(&pb);
105+
tipc_media_addr_printf(media_addr_str, sizeof(media_addr_str),
106+
media_addr);
109107
pr_warn("Duplicate %s using %s seen on <%s>\n", node_addr_str,
110108
media_addr_str, b_ptr->name);
111109
}

‎net/tipc/link.c‎

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,112 +2866,114 @@ static u32 percent(u32 count, u32 total)
28662866
*/
28672867
static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
28682868
{
2869-
struct print_buf pb;
2870-
struct tipc_link *l_ptr;
2869+
struct tipc_link *l;
2870+
struct tipc_stats *s;
28712871
struct tipc_node *node;
28722872
char *status;
28732873
u32 profile_total = 0;
2874+
int ret;
28742875

28752876
if (!strcmp(name, tipc_bclink_name))
28762877
return tipc_bclink_stats(buf, buf_size);
28772878

2878-
tipc_printbuf_init(&pb, buf, buf_size);
2879-
28802879
read_lock_bh(&tipc_net_lock);
2881-
l_ptr = link_find_link(name, &node);
2882-
if (!l_ptr) {
2880+
l = link_find_link(name, &node);
2881+
if (!l) {
28832882
read_unlock_bh(&tipc_net_lock);
28842883
return 0;
28852884
}
28862885
tipc_node_lock(node);
2886+
s = &l->stats;
28872887

2888-
if (tipc_link_is_active(l_ptr))
2888+
if (tipc_link_is_active(l))
28892889
status = "ACTIVE";
2890-
else if (tipc_link_is_up(l_ptr))
2890+
else if (tipc_link_is_up(l))
28912891
status = "STANDBY";
28922892
else
28932893
status = "DEFUNCT";
2894-
tipc_printf(&pb, "Link <%s>\n"
2895-
" %s MTU:%u Priority:%u Tolerance:%u ms"
2896-
" Window:%u packets\n",
2897-
l_ptr->name, status, l_ptr->max_pkt,
2898-
l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
2899-
tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2900-
l_ptr->next_in_no - l_ptr->stats.recv_info,
2901-
l_ptr->stats.recv_fragments,
2902-
l_ptr->stats.recv_fragmented,
2903-
l_ptr->stats.recv_bundles,
2904-
l_ptr->stats.recv_bundled);
2905-
tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2906-
l_ptr->next_out_no - l_ptr->stats.sent_info,
2907-
l_ptr->stats.sent_fragments,
2908-
l_ptr->stats.sent_fragmented,
2909-
l_ptr->stats.sent_bundles,
2910-
l_ptr->stats.sent_bundled);
2911-
profile_total = l_ptr->stats.msg_length_counts;
2894+
2895+
ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2896+
" %s MTU:%u Priority:%u Tolerance:%u ms"
2897+
" Window:%u packets\n",
2898+
l->name, status, l->max_pkt, l->priority,
2899+
l->tolerance, l->queue_limit[0]);
2900+
2901+
ret += tipc_snprintf(buf + ret, buf_size - ret,
2902+
" RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2903+
l->next_in_no - s->recv_info, s->recv_fragments,
2904+
s->recv_fragmented, s->recv_bundles,
2905+
s->recv_bundled);
2906+
2907+
ret += tipc_snprintf(buf + ret, buf_size - ret,
2908+
" TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2909+
l->next_out_no - s->sent_info, s->sent_fragments,
2910+
s->sent_fragmented, s->sent_bundles,
2911+
s->sent_bundled);
2912+
2913+
profile_total = s->msg_length_counts;
29122914
if (!profile_total)
29132915
profile_total = 1;
2914-
tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
2915-
" 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2916-
"-16384:%u%% -32768:%u%% -66000:%u%%\n",
2917-
l_ptr->stats.msg_length_counts,
2918-
l_ptr->stats.msg_lengths_total / profile_total,
2919-
percent(l_ptr->stats.msg_length_profile[0], profile_total),
2920-
percent(l_ptr->stats.msg_length_profile[1], profile_total),
2921-
percent(l_ptr->stats.msg_length_profile[2], profile_total),
2922-
percent(l_ptr->stats.msg_length_profile[3], profile_total),
2923-
percent(l_ptr->stats.msg_length_profile[4], profile_total),
2924-
percent(l_ptr->stats.msg_length_profile[5], profile_total),
2925-
percent(l_ptr->stats.msg_length_profile[6], profile_total));
2926-
tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
2927-
l_ptr->stats.recv_states,
2928-
l_ptr->stats.recv_probes,
2929-
l_ptr->stats.recv_nacks,
2930-
l_ptr->stats.deferred_recv,
2931-
l_ptr->stats.duplicates);
2932-
tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
2933-
l_ptr->stats.sent_states,
2934-
l_ptr->stats.sent_probes,
2935-
l_ptr->stats.sent_nacks,
2936-
l_ptr->stats.sent_acks,
2937-
l_ptr->stats.retransmitted);
2938-
tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
2939-
l_ptr->stats.bearer_congs,
2940-
l_ptr->stats.link_congs,
2941-
l_ptr->stats.max_queue_sz,
2942-
l_ptr->stats.queue_sz_counts
2943-
? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
2944-
: 0);
2916+
2917+
ret += tipc_snprintf(buf + ret, buf_size - ret,
2918+
" TX profile sample:%u packets average:%u octets\n"
2919+
" 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2920+
"-16384:%u%% -32768:%u%% -66000:%u%%\n",
2921+
s->msg_length_counts,
2922+
s->msg_lengths_total / profile_total,
2923+
percent(s->msg_length_profile[0], profile_total),
2924+
percent(s->msg_length_profile[1], profile_total),
2925+
percent(s->msg_length_profile[2], profile_total),
2926+
percent(s->msg_length_profile[3], profile_total),
2927+
percent(s->msg_length_profile[4], profile_total),
2928+
percent(s->msg_length_profile[5], profile_total),
2929+
percent(s->msg_length_profile[6], profile_total));
2930+
2931+
ret += tipc_snprintf(buf + ret, buf_size - ret,
2932+
" RX states:%u probes:%u naks:%u defs:%u"
2933+
" dups:%u\n", s->recv_states, s->recv_probes,
2934+
s->recv_nacks, s->deferred_recv, s->duplicates);
2935+
2936+
ret += tipc_snprintf(buf + ret, buf_size - ret,
2937+
" TX states:%u probes:%u naks:%u acks:%u"
2938+
" dups:%u\n", s->sent_states, s->sent_probes,
2939+
s->sent_nacks, s->sent_acks, s->retransmitted);
2940+
2941+
ret += tipc_snprintf(buf + ret, buf_size - ret,
2942+
" Congestion bearer:%u link:%u Send queue"
2943+
" max:%u avg:%u\n", s->bearer_congs, s->link_congs,
2944+
s->max_queue_sz, s->queue_sz_counts ?
2945+
(s->accu_queue_sz / s->queue_sz_counts) : 0);
29452946

29462947
tipc_node_unlock(node);
29472948
read_unlock_bh(&tipc_net_lock);
2948-
return tipc_printbuf_validate(&pb);
2949+
return ret;
29492950
}
29502951

2951-
#define MAX_LINK_STATS_INFO 2000
2952-
29532952
struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
29542953
{
29552954
struct sk_buff *buf;
29562955
struct tlv_desc *rep_tlv;
29572956
int str_len;
2957+
int pb_len;
2958+
char *pb;
29582959

29592960
if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
29602961
return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
29612962

2962-
buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
2963+
buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
29632964
if (!buf)
29642965
return NULL;
29652966

29662967
rep_tlv = (struct tlv_desc *)buf->data;
2967-
2968+
pb = TLV_DATA(rep_tlv);
2969+
pb_len = ULTRA_STRING_MAX_LEN;
29682970
str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
2969-
(char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
2971+
pb, pb_len);
29702972
if (!str_len) {
29712973
kfree_skb(buf);
29722974
return tipc_cfg_reply_error_string("link not found");
29732975
}
2974-
2976+
str_len += 1; /* for "\0" */
29752977
skb_put(buf, TLV_SPACE(str_len));
29762978
TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
29772979

0 commit comments

Comments
 (0)