net/hsr: Use list_head (and rcu) instead of array for slave devices.
[deliverable/linux.git] / net / hsr / hsr_framereg.c
CommitLineData
70ebe4a4 1/* Copyright 2011-2014 Autronica Fire and Security AS
f421436a
AB
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
70ebe4a4 9 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
f421436a
AB
10 *
11 * The HSR spec says never to forward the same frame twice on the same
12 * interface. A frame is identified by its source MAC address and its HSR
13 * sequence number. This code keeps track of senders and their sequence numbers
14 * to allow filtering of duplicate frames, and to detect HSR ring errors.
15 */
16
17#include <linux/if_ether.h>
18#include <linux/etherdevice.h>
19#include <linux/slab.h>
20#include <linux/rculist.h>
21#include "hsr_main.h"
22#include "hsr_framereg.h"
23#include "hsr_netlink.h"
24
25
70ebe4a4
AB
26struct hsr_node {
27 struct list_head mac_list;
28 unsigned char MacAddressA[ETH_ALEN];
29 unsigned char MacAddressB[ETH_ALEN];
c5a75911
AB
30 /* Local slave through which AddrB frames are received from this node */
31 enum hsr_port_type AddrB_port;
32 unsigned long time_in[HSR_PT_PORTS];
33 bool time_in_stale[HSR_PT_PORTS];
34 u16 seq_out[HSR_PT_PORTS];
70ebe4a4 35 struct rcu_head rcu_head;
f421436a
AB
36};
37
38/* TODO: use hash lists for mac addresses (linux/jhash.h)? */
39
40
41
42/* Search for mac entry. Caller must hold rcu read lock.
43 */
70ebe4a4
AB
44static struct hsr_node *find_node_by_AddrA(struct list_head *node_db,
45 const unsigned char addr[ETH_ALEN])
f421436a 46{
70ebe4a4 47 struct hsr_node *node;
f421436a
AB
48
49 list_for_each_entry_rcu(node, node_db, mac_list) {
50 if (ether_addr_equal(node->MacAddressA, addr))
51 return node;
52 }
53
54 return NULL;
55}
56
57
58/* Search for mac entry. Caller must hold rcu read lock.
59 */
70ebe4a4
AB
60static struct hsr_node *find_node_by_AddrB(struct list_head *node_db,
61 const unsigned char addr[ETH_ALEN])
f421436a 62{
70ebe4a4 63 struct hsr_node *node;
f421436a
AB
64
65 list_for_each_entry_rcu(node, node_db, mac_list) {
66 if (ether_addr_equal(node->MacAddressB, addr))
67 return node;
68 }
69
70 return NULL;
71}
72
73
74/* Search for mac entry. Caller must hold rcu read lock.
75 */
70ebe4a4 76struct hsr_node *hsr_find_node(struct list_head *node_db, struct sk_buff *skb)
f421436a 77{
70ebe4a4 78 struct hsr_node *node;
f421436a
AB
79 struct ethhdr *ethhdr;
80
81 if (!skb_mac_header_was_set(skb))
82 return NULL;
83
84 ethhdr = (struct ethhdr *) skb_mac_header(skb);
85
86 list_for_each_entry_rcu(node, node_db, mac_list) {
87 if (ether_addr_equal(node->MacAddressA, ethhdr->h_source))
88 return node;
89 if (ether_addr_equal(node->MacAddressB, ethhdr->h_source))
90 return node;
91 }
92
93 return NULL;
94}
95
96
97/* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
98 * frames from self that's been looped over the HSR ring.
99 */
100int hsr_create_self_node(struct list_head *self_node_db,
101 unsigned char addr_a[ETH_ALEN],
102 unsigned char addr_b[ETH_ALEN])
103{
70ebe4a4 104 struct hsr_node *node, *oldnode;
f421436a
AB
105
106 node = kmalloc(sizeof(*node), GFP_KERNEL);
107 if (!node)
108 return -ENOMEM;
109
e83abe37
JP
110 ether_addr_copy(node->MacAddressA, addr_a);
111 ether_addr_copy(node->MacAddressB, addr_b);
f421436a
AB
112
113 rcu_read_lock();
114 oldnode = list_first_or_null_rcu(self_node_db,
70ebe4a4 115 struct hsr_node, mac_list);
f421436a
AB
116 if (oldnode) {
117 list_replace_rcu(&oldnode->mac_list, &node->mac_list);
118 rcu_read_unlock();
119 synchronize_rcu();
120 kfree(oldnode);
121 } else {
122 rcu_read_unlock();
123 list_add_tail_rcu(&node->mac_list, self_node_db);
124 }
125
126 return 0;
127}
128
f421436a
AB
129
130/* Add/merge node to the database of nodes. 'skb' must contain an HSR
131 * supervision frame.
132 * - If the supervision header's MacAddressA field is not yet in the database,
133 * this frame is from an hitherto unknown node - add it to the database.
134 * - If the sender's MAC address is not the same as its MacAddressA address,
135 * the node is using PICS_SUBS (address substitution). Record the sender's
136 * address as the node's MacAddressB.
137 *
138 * This function needs to work even if the sender node has changed one of its
139 * slaves' MAC addresses. In this case, there are four different cases described
140 * by (Addr-changed, received-from) pairs as follows. Note that changing the
141 * SlaveA address is equal to changing the node's own address:
142 *
143 * - (AddrB, SlaveB): The new AddrB will be recorded by PICS_SUBS code since
144 * node == NULL.
145 * - (AddrB, SlaveA): Will work as usual (the AddrB change won't be detected
146 * from this frame).
147 *
148 * - (AddrA, SlaveB): The old node will be found. We need to detect this and
149 * remove the node.
150 * - (AddrA, SlaveA): A new node will be registered (non-PICS_SUBS at first).
151 * The old one will be pruned after HSR_NODE_FORGET_TIME.
152 *
153 * We also need to detect if the sender's SlaveA and SlaveB cables have been
154 * swapped.
155 */
c5a75911
AB
156struct hsr_node *hsr_merge_node(struct hsr_node *node, struct sk_buff *skb,
157 struct hsr_port *port)
f421436a 158{
c5a75911 159 struct hsr_priv *hsr;
f421436a
AB
160 struct hsr_sup_payload *hsr_sp;
161 struct hsr_ethhdr_sp *hsr_ethsup;
162 int i;
163 unsigned long now;
164
165 hsr_ethsup = (struct hsr_ethhdr_sp *) skb_mac_header(skb);
166 hsr_sp = (struct hsr_sup_payload *) skb->data;
c5a75911 167 hsr = port->hsr;
f421436a
AB
168
169 if (node && !ether_addr_equal(node->MacAddressA, hsr_sp->MacAddressA)) {
170 /* Node has changed its AddrA, frame was received from SlaveB */
171 list_del_rcu(&node->mac_list);
1aee6cc2 172 kfree_rcu(node, rcu_head);
f421436a
AB
173 node = NULL;
174 }
175
c5a75911 176 if (node && (port->type == node->AddrB_port) &&
f421436a
AB
177 !ether_addr_equal(node->MacAddressB, hsr_ethsup->ethhdr.h_source)) {
178 /* Cables have been swapped */
179 list_del_rcu(&node->mac_list);
1aee6cc2 180 kfree_rcu(node, rcu_head);
f421436a
AB
181 node = NULL;
182 }
183
c5a75911
AB
184 if (node && (port->type != node->AddrB_port) &&
185 (node->AddrB_port != HSR_PT_NONE) &&
f421436a
AB
186 !ether_addr_equal(node->MacAddressA, hsr_ethsup->ethhdr.h_source)) {
187 /* Cables have been swapped */
188 list_del_rcu(&node->mac_list);
1aee6cc2 189 kfree_rcu(node, rcu_head);
f421436a
AB
190 node = NULL;
191 }
192
193 if (node)
194 return node;
195
70ebe4a4 196 node = find_node_by_AddrA(&hsr->node_db, hsr_sp->MacAddressA);
f421436a
AB
197 if (node) {
198 /* Node is known, but frame was received from an unknown
199 * address. Node is PICS_SUBS capable; merge its AddrB.
200 */
e83abe37 201 ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source);
c5a75911 202 node->AddrB_port = port->type;
f421436a
AB
203 return node;
204 }
205
206 node = kzalloc(sizeof(*node), GFP_ATOMIC);
207 if (!node)
208 return NULL;
209
e83abe37
JP
210 ether_addr_copy(node->MacAddressA, hsr_sp->MacAddressA);
211 ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source);
f421436a 212 if (!ether_addr_equal(hsr_sp->MacAddressA, hsr_ethsup->ethhdr.h_source))
c5a75911 213 node->AddrB_port = port->type;
f421436a
AB
214
215 /* We are only interested in time diffs here, so use current jiffies
216 * as initialization. (0 could trigger an spurious ring error warning).
217 */
218 now = jiffies;
c5a75911 219 for (i = 0; i < HSR_PT_PORTS; i++)
f421436a 220 node->time_in[i] = now;
c5a75911 221 for (i = 0; i < HSR_PT_PORTS; i++)
f421436a
AB
222 node->seq_out[i] = ntohs(hsr_ethsup->hsr_sup.sequence_nr) - 1;
223
70ebe4a4 224 list_add_tail_rcu(&node->mac_list, &hsr->node_db);
f421436a
AB
225
226 return node;
227}
228
229
230/* 'skb' is a frame meant for this host, that is to be passed to upper layers.
231 *
232 * If the frame was sent by a node's B interface, replace the sender
233 * address with that node's "official" address (MacAddressA) so that upper
234 * layers recognize where it came from.
235 */
70ebe4a4 236void hsr_addr_subst_source(struct hsr_priv *hsr, struct sk_buff *skb)
f421436a
AB
237{
238 struct ethhdr *ethhdr;
70ebe4a4 239 struct hsr_node *node;
f421436a
AB
240
241 if (!skb_mac_header_was_set(skb)) {
242 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
243 return;
244 }
245 ethhdr = (struct ethhdr *) skb_mac_header(skb);
246
247 rcu_read_lock();
70ebe4a4 248 node = find_node_by_AddrB(&hsr->node_db, ethhdr->h_source);
f421436a 249 if (node)
e83abe37 250 ether_addr_copy(ethhdr->h_source, node->MacAddressA);
f421436a
AB
251 rcu_read_unlock();
252}
253
254
255/* 'skb' is a frame meant for another host.
256 * 'hsr_dev_idx' is the HSR index of the outgoing device
257 *
258 * Substitute the target (dest) MAC address if necessary, so the it matches the
259 * recipient interface MAC address, regardless of whether that is the
260 * recipient's A or B interface.
261 * This is needed to keep the packets flowing through switches that learn on
262 * which "side" the different interfaces are.
263 */
70ebe4a4 264void hsr_addr_subst_dest(struct hsr_priv *hsr, struct ethhdr *ethhdr,
c5a75911 265 struct hsr_port *port)
f421436a 266{
70ebe4a4 267 struct hsr_node *node;
f421436a
AB
268
269 rcu_read_lock();
70ebe4a4 270 node = find_node_by_AddrA(&hsr->node_db, ethhdr->h_dest);
c5a75911 271 if (node && (node->AddrB_port == port->type))
e83abe37 272 ether_addr_copy(ethhdr->h_dest, node->MacAddressB);
f421436a
AB
273 rcu_read_unlock();
274}
275
276
277/* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
278 * false otherwise.
279 */
280static bool seq_nr_after(u16 a, u16 b)
281{
282 /* Remove inconsistency where
213e3bc7
AB
283 * seq_nr_after(a, b) == seq_nr_before(a, b)
284 */
f421436a
AB
285 if ((int) b - a == 32768)
286 return false;
287
288 return (((s16) (b - a)) < 0);
289}
290#define seq_nr_before(a, b) seq_nr_after((b), (a))
291#define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
292#define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
293
294
c5a75911 295void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port)
f421436a 296{
c5a75911
AB
297 node->time_in[port->type] = jiffies;
298 node->time_in_stale[port->type] = false;
f421436a
AB
299}
300
301
302/* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
303 * ethhdr->h_source address and skb->mac_header set.
304 *
305 * Return:
306 * 1 if frame can be shown to have been sent recently on this interface,
307 * 0 otherwise, or
308 * negative error code on error
309 */
c5a75911 310int hsr_register_frame_out(struct hsr_node *node, struct hsr_port *port,
f421436a
AB
311 struct sk_buff *skb)
312{
313 struct hsr_ethhdr *hsr_ethhdr;
314 u16 sequence_nr;
315
f421436a
AB
316 if (!skb_mac_header_was_set(skb)) {
317 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
318 return -EINVAL;
319 }
320 hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb);
321
322 sequence_nr = ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
c5a75911 323 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
f421436a
AB
324 return 1;
325
c5a75911 326 node->seq_out[port->type] = sequence_nr;
f421436a
AB
327 return 0;
328}
329
330
c5a75911
AB
331static struct hsr_port *get_late_port(struct hsr_priv *hsr,
332 struct hsr_node *node)
f421436a 333{
c5a75911
AB
334 if (node->time_in_stale[HSR_PT_SLAVE_A])
335 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
336 if (node->time_in_stale[HSR_PT_SLAVE_B])
337 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
338
339 if (time_after(node->time_in[HSR_PT_SLAVE_B],
340 node->time_in[HSR_PT_SLAVE_A] +
341 msecs_to_jiffies(MAX_SLAVE_DIFF)))
342 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
343 if (time_after(node->time_in[HSR_PT_SLAVE_A],
344 node->time_in[HSR_PT_SLAVE_B] +
345 msecs_to_jiffies(MAX_SLAVE_DIFF)))
346 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
f421436a 347
c5a75911 348 return NULL;
f421436a
AB
349}
350
351
352/* Remove stale sequence_nr records. Called by timer every
353 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
354 */
abff7162 355void hsr_prune_nodes(unsigned long data)
f421436a 356{
abff7162 357 struct hsr_priv *hsr;
70ebe4a4 358 struct hsr_node *node;
c5a75911 359 struct hsr_port *port;
f421436a
AB
360 unsigned long timestamp;
361 unsigned long time_a, time_b;
362
abff7162
AB
363 hsr = (struct hsr_priv *) data;
364
f421436a 365 rcu_read_lock();
70ebe4a4 366 list_for_each_entry_rcu(node, &hsr->node_db, mac_list) {
f421436a 367 /* Shorthand */
c5a75911
AB
368 time_a = node->time_in[HSR_PT_SLAVE_A];
369 time_b = node->time_in[HSR_PT_SLAVE_B];
f421436a
AB
370
371 /* Check for timestamps old enough to risk wrap-around */
372 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
c5a75911 373 node->time_in_stale[HSR_PT_SLAVE_A] = true;
f421436a 374 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
c5a75911 375 node->time_in_stale[HSR_PT_SLAVE_B] = true;
f421436a
AB
376
377 /* Get age of newest frame from node.
378 * At least one time_in is OK here; nodes get pruned long
379 * before both time_ins can get stale
380 */
381 timestamp = time_a;
c5a75911
AB
382 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
383 (!node->time_in_stale[HSR_PT_SLAVE_B] &&
f421436a
AB
384 time_after(time_b, time_a)))
385 timestamp = time_b;
386
387 /* Warn of ring error only as long as we get frames at all */
388 if (time_is_after_jiffies(timestamp +
389 msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
c5a75911
AB
390 rcu_read_lock();
391 port = get_late_port(hsr, node);
392 if (port != NULL)
393 hsr_nl_ringerror(hsr, node->MacAddressA, port);
394 rcu_read_unlock();
f421436a
AB
395 }
396
397 /* Prune old entries */
398 if (time_is_before_jiffies(timestamp +
399 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
70ebe4a4 400 hsr_nl_nodedown(hsr, node->MacAddressA);
f421436a
AB
401 list_del_rcu(&node->mac_list);
402 /* Note that we need to free this entry later: */
1aee6cc2 403 kfree_rcu(node, rcu_head);
f421436a
AB
404 }
405 }
406 rcu_read_unlock();
407}
408
409
70ebe4a4 410void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
f421436a
AB
411 unsigned char addr[ETH_ALEN])
412{
70ebe4a4 413 struct hsr_node *node;
f421436a
AB
414
415 if (!_pos) {
70ebe4a4
AB
416 node = list_first_or_null_rcu(&hsr->node_db,
417 struct hsr_node, mac_list);
f421436a 418 if (node)
e83abe37 419 ether_addr_copy(addr, node->MacAddressA);
f421436a
AB
420 return node;
421 }
422
423 node = _pos;
70ebe4a4 424 list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
e83abe37 425 ether_addr_copy(addr, node->MacAddressA);
f421436a
AB
426 return node;
427 }
428
429 return NULL;
430}
431
432
70ebe4a4 433int hsr_get_node_data(struct hsr_priv *hsr,
f421436a
AB
434 const unsigned char *addr,
435 unsigned char addr_b[ETH_ALEN],
436 unsigned int *addr_b_ifindex,
437 int *if1_age,
438 u16 *if1_seq,
439 int *if2_age,
440 u16 *if2_seq)
441{
70ebe4a4 442 struct hsr_node *node;
c5a75911 443 struct hsr_port *port;
f421436a
AB
444 unsigned long tdiff;
445
446
447 rcu_read_lock();
70ebe4a4 448 node = find_node_by_AddrA(&hsr->node_db, addr);
f421436a
AB
449 if (!node) {
450 rcu_read_unlock();
451 return -ENOENT; /* No such entry */
452 }
453
e83abe37 454 ether_addr_copy(addr_b, node->MacAddressB);
f421436a 455
c5a75911
AB
456 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
457 if (node->time_in_stale[HSR_PT_SLAVE_A])
f421436a
AB
458 *if1_age = INT_MAX;
459#if HZ <= MSEC_PER_SEC
460 else if (tdiff > msecs_to_jiffies(INT_MAX))
461 *if1_age = INT_MAX;
462#endif
463 else
464 *if1_age = jiffies_to_msecs(tdiff);
465
c5a75911
AB
466 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
467 if (node->time_in_stale[HSR_PT_SLAVE_B])
f421436a
AB
468 *if2_age = INT_MAX;
469#if HZ <= MSEC_PER_SEC
470 else if (tdiff > msecs_to_jiffies(INT_MAX))
471 *if2_age = INT_MAX;
472#endif
473 else
474 *if2_age = jiffies_to_msecs(tdiff);
475
476 /* Present sequence numbers as if they were incoming on interface */
c5a75911
AB
477 *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
478 *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
f421436a 479
c5a75911
AB
480 if (node->AddrB_port != HSR_PT_NONE) {
481 port = hsr_port_get_hsr(hsr, node->AddrB_port);
482 *addr_b_ifindex = port->dev->ifindex;
483 } else {
f421436a 484 *addr_b_ifindex = -1;
c5a75911 485 }
f421436a
AB
486
487 rcu_read_unlock();
488
489 return 0;
490}
This page took 0.08396 seconds and 5 git commands to generate.