Skip to content

Commit 440d896

Browse files
Jon Paul Maloydavem330
authored andcommitted
tipc: clean up link creation
We simplify the link creation function tipc_link_create() and the way the link struct it is connected to the node struct. In particular, we remove the duplicate initialization of some fields which are anyway set in tipc_link_reset(). Tested-by: Ying Xue <[email protected]> Signed-off-by: Jon Maloy <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 9073fb8 commit 440d896

File tree

4 files changed

+86
-121
lines changed

4 files changed

+86
-121
lines changed

net/tipc/core.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ struct tipc_net {
109109
atomic_t subscription_count;
110110
};
111111

112+
static inline struct tipc_net *tipc_net(struct net *net)
113+
{
114+
return net_generic(net, tipc_net_id);
115+
}
116+
112117
static inline u16 mod(u16 x)
113118
{
114119
return x & 0xffffu;

net/tipc/link.c

Lines changed: 60 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -147,87 +147,71 @@ int tipc_link_is_active(struct tipc_link *l)
147147
return (node_active_link(n, 0) == l) || (node_active_link(n, 1) == l);
148148
}
149149

150+
static u32 link_own_addr(struct tipc_link *l)
151+
{
152+
return msg_prevnode(l->pmsg);
153+
}
154+
150155
/**
151156
* tipc_link_create - create a new link
152-
* @n_ptr: pointer to associated node
153-
* @b_ptr: pointer to associated bearer
154-
* @media_addr: media address to use when sending messages over link
157+
* @n: pointer to associated node
158+
* @b: pointer to associated bearer
159+
* @ownnode: identity of own node
160+
* @peer: identity of peer node
161+
* @maddr: media address to be used
162+
* @inputq: queue to put messages ready for delivery
163+
* @namedq: queue to put binding table update messages ready for delivery
164+
* @link: return value, pointer to put the created link
155165
*
156-
* Returns pointer to link.
166+
* Returns true if link was created, otherwise false
157167
*/
158-
struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
159-
struct tipc_bearer *b_ptr,
160-
const struct tipc_media_addr *media_addr,
161-
struct sk_buff_head *inputq,
162-
struct sk_buff_head *namedq)
168+
bool tipc_link_create(struct tipc_node *n, struct tipc_bearer *b, u32 session,
169+
u32 ownnode, u32 peer, struct tipc_media_addr *maddr,
170+
struct sk_buff_head *inputq, struct sk_buff_head *namedq,
171+
struct tipc_link **link)
163172
{
164-
struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
165-
struct tipc_link *l_ptr;
166-
struct tipc_msg *msg;
173+
struct tipc_link *l;
174+
struct tipc_msg *hdr;
167175
char *if_name;
168-
char addr_string[16];
169-
u32 peer = n_ptr->addr;
170176

171-
if (n_ptr->link_cnt >= MAX_BEARERS) {
172-
tipc_addr_string_fill(addr_string, n_ptr->addr);
173-
pr_err("Cannot establish %uth link to %s. Max %u allowed.\n",
174-
n_ptr->link_cnt, addr_string, MAX_BEARERS);
175-
return NULL;
176-
}
177+
l = kzalloc(sizeof(*l), GFP_ATOMIC);
178+
if (!l)
179+
return false;
180+
*link = l;
177181

178-
if (n_ptr->links[b_ptr->identity].link) {
179-
tipc_addr_string_fill(addr_string, n_ptr->addr);
180-
pr_err("Attempt to establish second link on <%s> to %s\n",
181-
b_ptr->name, addr_string);
182-
return NULL;
183-
}
182+
/* Note: peer i/f name is completed by reset/activate message */
183+
if_name = strchr(b->name, ':') + 1;
184+
sprintf(l->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
185+
tipc_zone(ownnode), tipc_cluster(ownnode), tipc_node(ownnode),
186+
if_name, tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
184187

185-
l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
186-
if (!l_ptr) {
187-
pr_warn("Link creation failed, no memory\n");
188-
return NULL;
189-
}
190-
l_ptr->addr = peer;
191-
if_name = strchr(b_ptr->name, ':') + 1;
192-
sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
193-
tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
194-
tipc_node(tn->own_addr),
195-
if_name,
196-
tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
197-
/* note: peer i/f name is updated by reset/activate message */
198-
memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
199-
l_ptr->owner = n_ptr;
200-
l_ptr->peer_session = WILDCARD_SESSION;
201-
l_ptr->bearer_id = b_ptr->identity;
202-
l_ptr->tolerance = b_ptr->tolerance;
203-
l_ptr->snd_nxt = 1;
204-
l_ptr->rcv_nxt = 1;
205-
l_ptr->state = LINK_RESET;
206-
207-
l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
208-
msg = l_ptr->pmsg;
209-
tipc_msg_init(tn->own_addr, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
210-
l_ptr->addr);
211-
msg_set_size(msg, sizeof(l_ptr->proto_msg));
212-
msg_set_session(msg, (tn->random & 0xffff));
213-
msg_set_bearer_id(msg, b_ptr->identity);
214-
strcpy((char *)msg_data(msg), if_name);
215-
l_ptr->net_plane = b_ptr->net_plane;
216-
l_ptr->advertised_mtu = b_ptr->mtu;
217-
l_ptr->mtu = l_ptr->advertised_mtu;
218-
l_ptr->priority = b_ptr->priority;
219-
tipc_link_set_queue_limits(l_ptr, b_ptr->window);
220-
l_ptr->snd_nxt = 1;
221-
__skb_queue_head_init(&l_ptr->transmq);
222-
__skb_queue_head_init(&l_ptr->backlogq);
223-
__skb_queue_head_init(&l_ptr->deferdq);
224-
skb_queue_head_init(&l_ptr->wakeupq);
225-
l_ptr->inputq = inputq;
226-
l_ptr->namedq = namedq;
227-
skb_queue_head_init(l_ptr->inputq);
228-
link_reset_statistics(l_ptr);
229-
tipc_node_attach_link(n_ptr, l_ptr);
230-
return l_ptr;
188+
l->addr = peer;
189+
l->media_addr = maddr;
190+
l->owner = n;
191+
l->peer_session = WILDCARD_SESSION;
192+
l->bearer_id = b->identity;
193+
l->tolerance = b->tolerance;
194+
l->net_plane = b->net_plane;
195+
l->advertised_mtu = b->mtu;
196+
l->mtu = b->mtu;
197+
l->priority = b->priority;
198+
tipc_link_set_queue_limits(l, b->window);
199+
l->inputq = inputq;
200+
l->namedq = namedq;
201+
l->state = LINK_RESETTING;
202+
l->pmsg = (struct tipc_msg *)&l->proto_msg;
203+
hdr = l->pmsg;
204+
tipc_msg_init(ownnode, hdr, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, peer);
205+
msg_set_size(hdr, sizeof(l->proto_msg));
206+
msg_set_session(hdr, session);
207+
msg_set_bearer_id(hdr, l->bearer_id);
208+
strcpy((char *)msg_data(hdr), if_name);
209+
__skb_queue_head_init(&l->transmq);
210+
__skb_queue_head_init(&l->backlogq);
211+
__skb_queue_head_init(&l->deferdq);
212+
skb_queue_head_init(&l->wakeupq);
213+
skb_queue_head_init(l->inputq);
214+
return true;
231215
}
232216

233217
/* tipc_link_build_bcast_sync_msg() - synchronize broadcast link endpoints.
@@ -643,7 +627,7 @@ int __tipc_link_xmit(struct net *net, struct tipc_link *link,
643627
u16 ack = mod(link->rcv_nxt - 1);
644628
u16 seqno = link->snd_nxt;
645629
u16 bc_last_in = link->owner->bclink.last_in;
646-
struct tipc_media_addr *addr = &link->media_addr;
630+
struct tipc_media_addr *addr = link->media_addr;
647631
struct sk_buff_head *transmq = &link->transmq;
648632
struct sk_buff_head *backlogq = &link->backlogq;
649633
struct sk_buff *skb, *bskb;
@@ -809,7 +793,7 @@ void tipc_link_push_packets(struct tipc_link *link)
809793
link->rcv_unacked = 0;
810794
__skb_queue_tail(&link->transmq, skb);
811795
tipc_bearer_send(link->owner->net, link->bearer_id,
812-
skb, &link->media_addr);
796+
skb, link->media_addr);
813797
}
814798
link->snd_nxt = seqno;
815799
}
@@ -912,7 +896,7 @@ void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
912896
msg_set_ack(msg, mod(l_ptr->rcv_nxt - 1));
913897
msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
914898
tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
915-
&l_ptr->media_addr);
899+
l_ptr->media_addr);
916900
retransmits--;
917901
l_ptr->stats.retransmitted++;
918902
}
@@ -1200,7 +1184,7 @@ void tipc_link_proto_xmit(struct tipc_link *l, u32 msg_typ, int probe_msg,
12001184
skb = __skb_dequeue(&xmitq);
12011185
if (!skb)
12021186
return;
1203-
tipc_bearer_send(l->owner->net, l->bearer_id, skb, &l->media_addr);
1187+
tipc_bearer_send(l->owner->net, l->bearer_id, skb, l->media_addr);
12041188
l->rcv_unacked = 0;
12051189
kfree_skb(skb);
12061190
}

net/tipc/link.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ struct tipc_stats {
148148
struct tipc_link {
149149
u32 addr;
150150
char name[TIPC_MAX_LINK_NAME];
151-
struct tipc_media_addr media_addr;
151+
struct tipc_media_addr *media_addr;
152152
struct tipc_node *owner;
153153

154154
/* Management and link supervision data */
@@ -205,13 +205,10 @@ struct tipc_link {
205205
struct tipc_stats stats;
206206
};
207207

208-
struct tipc_port;
209-
210-
struct tipc_link *tipc_link_create(struct tipc_node *n,
211-
struct tipc_bearer *b,
212-
const struct tipc_media_addr *maddr,
213-
struct sk_buff_head *inputq,
214-
struct sk_buff_head *namedq);
208+
bool tipc_link_create(struct tipc_node *n, struct tipc_bearer *b, u32 session,
209+
u32 ownnode, u32 peer, struct tipc_media_addr *maddr,
210+
struct sk_buff_head *inputq, struct sk_buff_head *namedq,
211+
struct tipc_link **link);
215212
void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
216213
int mtyp, struct sk_buff_head *xmitq);
217214
void tipc_link_build_bcast_sync_msg(struct tipc_link *l,
@@ -246,13 +243,8 @@ int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info);
246243
int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info);
247244
int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info);
248245
int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[]);
249-
void link_prepare_wakeup(struct tipc_link *l);
250246
int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq);
251247
int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
252248
struct sk_buff_head *xmitq);
253-
static inline u32 link_own_addr(struct tipc_link *l)
254-
{
255-
return msg_prevnode(l->pmsg);
256-
}
257249

258250
#endif

net/tipc/node.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,6 @@ static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
320320
if (!nl || !tipc_link_is_up(nl))
321321
return;
322322

323-
if (n->working_links > 1) {
324-
pr_warn("Attempt to establish 3rd link to %x\n", n->addr);
325-
return;
326-
}
327323
n->working_links++;
328324
n->action_flags |= TIPC_NOTIFY_LINK_UP;
329325
n->link_id = nl->peer_bearer_id << 16 | bearer_id;
@@ -470,13 +466,13 @@ void tipc_node_check_dest(struct net *net, u32 onode,
470466
{
471467
struct tipc_node *n;
472468
struct tipc_link *l;
473-
struct tipc_media_addr *curr_maddr;
474-
struct sk_buff_head *inputq;
469+
struct tipc_link_entry *le;
475470
bool addr_match = false;
476471
bool sign_match = false;
477472
bool link_up = false;
478473
bool accept_addr = false;
479474
bool reset = true;
475+
480476
*dupl_addr = false;
481477
*respond = false;
482478

@@ -486,13 +482,12 @@ void tipc_node_check_dest(struct net *net, u32 onode,
486482

487483
tipc_node_lock(n);
488484

489-
curr_maddr = &n->links[b->identity].maddr;
490-
inputq = &n->links[b->identity].inputq;
485+
le = &n->links[b->identity];
491486

492487
/* Prepare to validate requesting node's signature and media address */
493-
l = n->links[b->identity].link;
488+
l = le->link;
494489
link_up = l && tipc_link_is_up(l);
495-
addr_match = l && !memcmp(curr_maddr, maddr, sizeof(*maddr));
490+
addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
496491
sign_match = (signature == n->signature);
497492

498493
/* These three flags give us eight permutations: */
@@ -559,18 +554,25 @@ void tipc_node_check_dest(struct net *net, u32 onode,
559554

560555
/* Now create new link if not already existing */
561556
if (!l) {
562-
l = tipc_link_create(n, b, maddr, inputq, &n->bclink.namedq);
563-
if (!l) {
557+
if (n->link_cnt == 2) {
558+
pr_warn("Cannot establish 3rd link to %x\n", n->addr);
559+
goto exit;
560+
}
561+
if (!tipc_link_create(n, b, mod(tipc_net(net)->random),
562+
tipc_own_addr(net), onode, &le->maddr,
563+
&le->inputq, &n->bclink.namedq, &l)) {
564564
*respond = false;
565565
goto exit;
566566
}
567+
tipc_link_reset(l);
568+
le->link = l;
569+
n->link_cnt++;
567570
tipc_node_calculate_timer(n, l);
568571
if (n->link_cnt == 1)
569572
if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
570573
tipc_node_get(n);
571574
}
572-
memcpy(&l->media_addr, maddr, sizeof(*maddr));
573-
memcpy(curr_maddr, maddr, sizeof(*maddr));
575+
memcpy(&le->maddr, maddr, sizeof(*maddr));
574576
exit:
575577
tipc_node_unlock(n);
576578
if (reset)
@@ -603,24 +605,6 @@ static void tipc_node_reset_links(struct tipc_node *n)
603605
}
604606
}
605607

606-
void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
607-
{
608-
n_ptr->links[l_ptr->bearer_id].link = l_ptr;
609-
n_ptr->link_cnt++;
610-
}
611-
612-
void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
613-
{
614-
int i;
615-
616-
for (i = 0; i < MAX_BEARERS; i++) {
617-
if (l_ptr != n_ptr->links[i].link)
618-
continue;
619-
n_ptr->links[i].link = NULL;
620-
n_ptr->link_cnt--;
621-
}
622-
}
623-
624608
/* tipc_node_fsm_evt - node finite state machine
625609
* Determines when contact is allowed with peer node
626610
*/

0 commit comments

Comments
 (0)