2014-07-05 05:34:38 +08:00
|
|
|
/* Copyright 2011-2014 Autronica Fire and Security AS
|
2013-10-31 04:10:47 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* Author(s):
|
2014-07-05 05:34:38 +08:00
|
|
|
* 2011-2014 Arvid Brodin, arvid.brodin@alten.se
|
2013-10-31 04:10:47 +08:00
|
|
|
*
|
|
|
|
* The HSR spec says never to forward the same frame twice on the same
|
|
|
|
* interface. A frame is identified by its source MAC address and its HSR
|
|
|
|
* sequence number. This code keeps track of senders and their sequence numbers
|
|
|
|
* to allow filtering of duplicate frames, and to detect HSR ring errors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <linux/etherdevice.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/rculist.h>
|
|
|
|
#include "hsr_main.h"
|
|
|
|
#include "hsr_framereg.h"
|
|
|
|
#include "hsr_netlink.h"
|
|
|
|
|
2014-07-05 05:34:38 +08:00
|
|
|
struct hsr_node {
|
|
|
|
struct list_head mac_list;
|
2019-04-06 01:31:32 +08:00
|
|
|
unsigned char macaddress_A[ETH_ALEN];
|
|
|
|
unsigned char macaddress_B[ETH_ALEN];
|
2014-07-05 05:38:05 +08:00
|
|
|
/* Local slave through which AddrB frames are received from this node */
|
2019-04-06 01:31:32 +08:00
|
|
|
enum hsr_port_type addr_B_port;
|
2014-07-05 05:38:05 +08:00
|
|
|
unsigned long time_in[HSR_PT_PORTS];
|
|
|
|
bool time_in_stale[HSR_PT_PORTS];
|
|
|
|
u16 seq_out[HSR_PT_PORTS];
|
2014-07-05 05:34:38 +08:00
|
|
|
struct rcu_head rcu_head;
|
2013-10-31 04:10:47 +08:00
|
|
|
};
|
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
/* TODO: use hash lists for mac addresses (linux/jhash.h)? */
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
/* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
|
|
|
|
* false otherwise.
|
2013-10-31 04:10:47 +08:00
|
|
|
*/
|
2014-07-05 05:41:03 +08:00
|
|
|
static bool seq_nr_after(u16 a, u16 b)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2014-07-05 05:41:03 +08:00
|
|
|
/* Remove inconsistency where
|
|
|
|
* seq_nr_after(a, b) == seq_nr_before(a, b)
|
|
|
|
*/
|
2019-04-06 01:31:29 +08:00
|
|
|
if ((int)b - a == 32768)
|
2014-07-05 05:41:03 +08:00
|
|
|
return false;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2019-04-06 01:31:29 +08:00
|
|
|
return (((s16)(b - a)) < 0);
|
2013-10-31 04:10:47 +08:00
|
|
|
}
|
2014-07-05 05:41:03 +08:00
|
|
|
#define seq_nr_before(a, b) seq_nr_after((b), (a))
|
|
|
|
#define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
|
|
|
|
#define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2014-07-05 05:34:38 +08:00
|
|
|
struct hsr_node *node;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
|
|
|
|
mac_list);
|
|
|
|
if (!node) {
|
|
|
|
WARN_ONCE(1, "HSR: No self node\n");
|
|
|
|
return false;
|
2013-10-31 04:10:47 +08:00
|
|
|
}
|
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
if (ether_addr_equal(addr, node->macaddress_A))
|
2014-07-05 05:41:03 +08:00
|
|
|
return true;
|
2019-04-06 01:31:32 +08:00
|
|
|
if (ether_addr_equal(addr, node->macaddress_B))
|
2014-07-05 05:41:03 +08:00
|
|
|
return true;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
/* Search for mac entry. Caller must hold rcu read lock.
|
|
|
|
*/
|
2019-04-06 01:31:32 +08:00
|
|
|
static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
|
|
|
|
const unsigned char addr[ETH_ALEN])
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2014-07-05 05:34:38 +08:00
|
|
|
struct hsr_node *node;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
list_for_each_entry_rcu(node, node_db, mac_list) {
|
2019-04-06 01:31:32 +08:00
|
|
|
if (ether_addr_equal(node->macaddress_A, addr))
|
2013-10-31 04:10:47 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
|
|
|
|
* frames from self that's been looped over the HSR ring.
|
|
|
|
*/
|
|
|
|
int hsr_create_self_node(struct list_head *self_node_db,
|
|
|
|
unsigned char addr_a[ETH_ALEN],
|
|
|
|
unsigned char addr_b[ETH_ALEN])
|
|
|
|
{
|
2014-07-05 05:34:38 +08:00
|
|
|
struct hsr_node *node, *oldnode;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
node = kmalloc(sizeof(*node), GFP_KERNEL);
|
|
|
|
if (!node)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
ether_addr_copy(node->macaddress_A, addr_a);
|
|
|
|
ether_addr_copy(node->macaddress_B, addr_b);
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
oldnode = list_first_or_null_rcu(self_node_db,
|
2019-04-06 01:31:26 +08:00
|
|
|
struct hsr_node, mac_list);
|
2013-10-31 04:10:47 +08:00
|
|
|
if (oldnode) {
|
|
|
|
list_replace_rcu(&oldnode->mac_list, &node->mac_list);
|
|
|
|
rcu_read_unlock();
|
|
|
|
synchronize_rcu();
|
|
|
|
kfree(oldnode);
|
|
|
|
} else {
|
|
|
|
rcu_read_unlock();
|
|
|
|
list_add_tail_rcu(&node->mac_list, self_node_db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:45:01 +08:00
|
|
|
void hsr_del_node(struct list_head *self_node_db)
|
|
|
|
{
|
|
|
|
struct hsr_node *node;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list);
|
|
|
|
rcu_read_unlock();
|
|
|
|
if (node) {
|
|
|
|
list_del_rcu(&node->mac_list);
|
|
|
|
kfree(node);
|
|
|
|
}
|
|
|
|
}
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
/* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
|
2014-07-05 05:41:03 +08:00
|
|
|
* seq_out is used to initialize filtering of outgoing duplicate frames
|
|
|
|
* originating from the newly added node.
|
2013-10-31 04:10:47 +08:00
|
|
|
*/
|
2014-07-05 05:41:03 +08:00
|
|
|
struct hsr_node *hsr_add_node(struct list_head *node_db, unsigned char addr[],
|
|
|
|
u16 seq_out)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2014-07-05 05:41:03 +08:00
|
|
|
struct hsr_node *node;
|
2013-10-31 04:10:47 +08:00
|
|
|
unsigned long now;
|
2014-07-05 05:41:03 +08:00
|
|
|
int i;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
node = kzalloc(sizeof(*node), GFP_ATOMIC);
|
|
|
|
if (!node)
|
|
|
|
return NULL;
|
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
ether_addr_copy(node->macaddress_A, addr);
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
/* We are only interested in time diffs here, so use current jiffies
|
|
|
|
* as initialization. (0 could trigger an spurious ring error warning).
|
|
|
|
*/
|
|
|
|
now = jiffies;
|
2014-07-05 05:38:05 +08:00
|
|
|
for (i = 0; i < HSR_PT_PORTS; i++)
|
2013-10-31 04:10:47 +08:00
|
|
|
node->time_in[i] = now;
|
2014-07-05 05:38:05 +08:00
|
|
|
for (i = 0; i < HSR_PT_PORTS; i++)
|
2014-07-05 05:41:03 +08:00
|
|
|
node->seq_out[i] = seq_out;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
list_add_tail_rcu(&node->mac_list, node_db);
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
/* Get the hsr_node from which 'skb' was sent.
|
|
|
|
*/
|
2017-06-13 03:06:26 +08:00
|
|
|
struct hsr_node *hsr_get_node(struct hsr_port *port, struct sk_buff *skb,
|
2014-07-05 05:41:03 +08:00
|
|
|
bool is_sup)
|
|
|
|
{
|
2017-06-13 03:06:26 +08:00
|
|
|
struct list_head *node_db = &port->hsr->node_db;
|
2014-07-05 05:41:03 +08:00
|
|
|
struct hsr_node *node;
|
|
|
|
struct ethhdr *ethhdr;
|
|
|
|
u16 seq_out;
|
|
|
|
|
|
|
|
if (!skb_mac_header_was_set(skb))
|
|
|
|
return NULL;
|
|
|
|
|
2019-04-06 01:31:29 +08:00
|
|
|
ethhdr = (struct ethhdr *)skb_mac_header(skb);
|
2014-07-05 05:41:03 +08:00
|
|
|
|
|
|
|
list_for_each_entry_rcu(node, node_db, mac_list) {
|
2019-04-06 01:31:32 +08:00
|
|
|
if (ether_addr_equal(node->macaddress_A, ethhdr->h_source))
|
2014-07-05 05:41:03 +08:00
|
|
|
return node;
|
2019-04-06 01:31:32 +08:00
|
|
|
if (ether_addr_equal(node->macaddress_B, ethhdr->h_source))
|
2014-07-05 05:41:03 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2016-04-13 19:52:22 +08:00
|
|
|
/* Everyone may create a node entry, connected node to a HSR device. */
|
2014-07-05 05:41:03 +08:00
|
|
|
|
2019-04-06 01:31:30 +08:00
|
|
|
if (ethhdr->h_proto == htons(ETH_P_PRP) ||
|
|
|
|
ethhdr->h_proto == htons(ETH_P_HSR)) {
|
2014-07-05 05:41:03 +08:00
|
|
|
/* Use the existing sequence_nr from the tag as starting point
|
|
|
|
* for filtering duplicate frames.
|
|
|
|
*/
|
|
|
|
seq_out = hsr_get_skb_sequence_nr(skb) - 1;
|
|
|
|
} else {
|
2017-06-13 03:06:26 +08:00
|
|
|
/* this is called also for frames from master port and
|
|
|
|
* so warn only for non master ports
|
|
|
|
*/
|
|
|
|
if (port->type != HSR_PT_MASTER)
|
|
|
|
WARN_ONCE(1, "%s: Non-HSR frame\n", __func__);
|
2016-04-13 19:52:22 +08:00
|
|
|
seq_out = HSR_SEQNR_START;
|
2014-07-05 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return hsr_add_node(node_db, ethhdr->h_source, seq_out);
|
|
|
|
}
|
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
/* Use the Supervision frame's info about an eventual macaddress_B for merging
|
|
|
|
* nodes that has previously had their macaddress_B registered as a separate
|
2014-07-05 05:41:03 +08:00
|
|
|
* node.
|
|
|
|
*/
|
|
|
|
void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr,
|
|
|
|
struct hsr_port *port_rcv)
|
|
|
|
{
|
2016-04-13 19:52:22 +08:00
|
|
|
struct ethhdr *ethhdr;
|
2014-07-05 05:41:03 +08:00
|
|
|
struct hsr_node *node_real;
|
|
|
|
struct hsr_sup_payload *hsr_sp;
|
|
|
|
struct list_head *node_db;
|
|
|
|
int i;
|
|
|
|
|
2019-04-06 01:31:29 +08:00
|
|
|
ethhdr = (struct ethhdr *)skb_mac_header(skb);
|
2014-07-05 05:41:03 +08:00
|
|
|
|
2016-04-13 19:52:22 +08:00
|
|
|
/* Leave the ethernet header. */
|
|
|
|
skb_pull(skb, sizeof(struct ethhdr));
|
|
|
|
|
|
|
|
/* And leave the HSR tag. */
|
|
|
|
if (ethhdr->h_proto == htons(ETH_P_HSR))
|
|
|
|
skb_pull(skb, sizeof(struct hsr_tag));
|
|
|
|
|
|
|
|
/* And leave the HSR sup tag. */
|
|
|
|
skb_pull(skb, sizeof(struct hsr_sup_tag));
|
|
|
|
|
2019-04-06 01:31:29 +08:00
|
|
|
hsr_sp = (struct hsr_sup_payload *)skb->data;
|
2014-07-05 05:41:03 +08:00
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
/* Merge node_curr (registered on macaddress_B) into node_real */
|
2014-07-05 05:41:03 +08:00
|
|
|
node_db = &port_rcv->hsr->node_db;
|
2019-04-06 01:31:32 +08:00
|
|
|
node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
|
2014-07-05 05:41:03 +08:00
|
|
|
if (!node_real)
|
|
|
|
/* No frame received from AddrA of this node yet */
|
2019-04-06 01:31:32 +08:00
|
|
|
node_real = hsr_add_node(node_db, hsr_sp->macaddress_A,
|
2014-07-05 05:41:03 +08:00
|
|
|
HSR_SEQNR_START - 1);
|
|
|
|
if (!node_real)
|
|
|
|
goto done; /* No mem */
|
|
|
|
if (node_real == node_curr)
|
|
|
|
/* Node has already been merged */
|
|
|
|
goto done;
|
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
|
2014-07-05 05:41:03 +08:00
|
|
|
for (i = 0; i < HSR_PT_PORTS; i++) {
|
|
|
|
if (!node_curr->time_in_stale[i] &&
|
|
|
|
time_after(node_curr->time_in[i], node_real->time_in[i])) {
|
|
|
|
node_real->time_in[i] = node_curr->time_in[i];
|
2019-04-06 01:31:23 +08:00
|
|
|
node_real->time_in_stale[i] =
|
|
|
|
node_curr->time_in_stale[i];
|
2014-07-05 05:41:03 +08:00
|
|
|
}
|
|
|
|
if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
|
|
|
|
node_real->seq_out[i] = node_curr->seq_out[i];
|
|
|
|
}
|
2019-04-06 01:31:32 +08:00
|
|
|
node_real->addr_B_port = port_rcv->type;
|
2014-07-05 05:41:03 +08:00
|
|
|
|
|
|
|
list_del_rcu(&node_curr->mac_list);
|
|
|
|
kfree_rcu(node_curr, rcu_head);
|
|
|
|
|
|
|
|
done:
|
2016-04-13 19:52:22 +08:00
|
|
|
skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
|
2014-07-05 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2013-10-31 04:10:47 +08:00
|
|
|
/* 'skb' is a frame meant for this host, that is to be passed to upper layers.
|
|
|
|
*
|
2014-07-05 05:41:03 +08:00
|
|
|
* If the frame was sent by a node's B interface, replace the source
|
2019-04-06 01:31:32 +08:00
|
|
|
* address with that node's "official" address (macaddress_A) so that upper
|
2013-10-31 04:10:47 +08:00
|
|
|
* layers recognize where it came from.
|
|
|
|
*/
|
2014-07-05 05:41:03 +08:00
|
|
|
void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
|
|
|
if (!skb_mac_header_was_set(skb)) {
|
|
|
|
WARN_ONCE(1, "%s: Mac header not set\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
|
2013-10-31 04:10:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 'skb' is a frame meant for another host.
|
2014-07-05 05:41:03 +08:00
|
|
|
* 'port' is the outgoing interface
|
2013-10-31 04:10:47 +08:00
|
|
|
*
|
|
|
|
* Substitute the target (dest) MAC address if necessary, so the it matches the
|
|
|
|
* recipient interface MAC address, regardless of whether that is the
|
|
|
|
* recipient's A or B interface.
|
|
|
|
* This is needed to keep the packets flowing through switches that learn on
|
|
|
|
* which "side" the different interfaces are.
|
|
|
|
*/
|
2014-07-05 05:41:03 +08:00
|
|
|
void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
|
2014-07-05 05:38:05 +08:00
|
|
|
struct hsr_port *port)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2014-07-05 05:41:03 +08:00
|
|
|
struct hsr_node *node_dst;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
if (!skb_mac_header_was_set(skb)) {
|
|
|
|
WARN_ONCE(1, "%s: Mac header not set\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
|
|
|
|
return;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
node_dst = find_node_by_addr_A(&port->hsr->node_db,
|
|
|
|
eth_hdr(skb)->h_dest);
|
2014-07-05 05:41:03 +08:00
|
|
|
if (!node_dst) {
|
|
|
|
WARN_ONCE(1, "%s: Unknown node\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-06 01:31:32 +08:00
|
|
|
if (port->type != node_dst->addr_B_port)
|
2014-07-05 05:41:03 +08:00
|
|
|
return;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
|
2013-10-31 04:10:47 +08:00
|
|
|
}
|
|
|
|
|
2014-07-05 05:41:03 +08:00
|
|
|
void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
|
|
|
|
u16 sequence_nr)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2014-07-05 05:41:03 +08:00
|
|
|
/* Don't register incoming frames without a valid sequence number. This
|
|
|
|
* ensures entries of restarted nodes gets pruned so that they can
|
|
|
|
* re-register and resume communications.
|
|
|
|
*/
|
|
|
|
if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
|
|
|
|
return;
|
|
|
|
|
2014-07-05 05:38:05 +08:00
|
|
|
node->time_in[port->type] = jiffies;
|
|
|
|
node->time_in_stale[port->type] = false;
|
2013-10-31 04:10:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
|
|
|
|
* ethhdr->h_source address and skb->mac_header set.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* 1 if frame can be shown to have been sent recently on this interface,
|
|
|
|
* 0 otherwise, or
|
|
|
|
* negative error code on error
|
|
|
|
*/
|
2014-07-05 05:41:03 +08:00
|
|
|
int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
|
|
|
|
u16 sequence_nr)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2014-07-05 05:38:05 +08:00
|
|
|
if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
|
2013-10-31 04:10:47 +08:00
|
|
|
return 1;
|
|
|
|
|
2014-07-05 05:38:05 +08:00
|
|
|
node->seq_out[port->type] = sequence_nr;
|
2013-10-31 04:10:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-05 05:38:05 +08:00
|
|
|
static struct hsr_port *get_late_port(struct hsr_priv *hsr,
|
|
|
|
struct hsr_node *node)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2014-07-05 05:38:05 +08:00
|
|
|
if (node->time_in_stale[HSR_PT_SLAVE_A])
|
|
|
|
return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
|
|
|
|
if (node->time_in_stale[HSR_PT_SLAVE_B])
|
|
|
|
return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
|
|
|
|
|
|
|
|
if (time_after(node->time_in[HSR_PT_SLAVE_B],
|
|
|
|
node->time_in[HSR_PT_SLAVE_A] +
|
|
|
|
msecs_to_jiffies(MAX_SLAVE_DIFF)))
|
|
|
|
return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
|
|
|
|
if (time_after(node->time_in[HSR_PT_SLAVE_A],
|
|
|
|
node->time_in[HSR_PT_SLAVE_B] +
|
|
|
|
msecs_to_jiffies(MAX_SLAVE_DIFF)))
|
|
|
|
return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:38:05 +08:00
|
|
|
return NULL;
|
2013-10-31 04:10:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove stale sequence_nr records. Called by timer every
|
|
|
|
* HSR_LIFE_CHECK_INTERVAL (two seconds or so).
|
|
|
|
*/
|
2017-10-24 16:46:16 +08:00
|
|
|
void hsr_prune_nodes(struct timer_list *t)
|
2013-10-31 04:10:47 +08:00
|
|
|
{
|
2017-10-24 16:46:16 +08:00
|
|
|
struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
|
2014-07-05 05:34:38 +08:00
|
|
|
struct hsr_node *node;
|
2014-07-05 05:38:05 +08:00
|
|
|
struct hsr_port *port;
|
2013-10-31 04:10:47 +08:00
|
|
|
unsigned long timestamp;
|
|
|
|
unsigned long time_a, time_b;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2014-07-05 05:34:38 +08:00
|
|
|
list_for_each_entry_rcu(node, &hsr->node_db, mac_list) {
|
2013-10-31 04:10:47 +08:00
|
|
|
/* Shorthand */
|
2014-07-05 05:38:05 +08:00
|
|
|
time_a = node->time_in[HSR_PT_SLAVE_A];
|
|
|
|
time_b = node->time_in[HSR_PT_SLAVE_B];
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
/* Check for timestamps old enough to risk wrap-around */
|
2019-04-06 01:31:31 +08:00
|
|
|
if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
|
2014-07-05 05:38:05 +08:00
|
|
|
node->time_in_stale[HSR_PT_SLAVE_A] = true;
|
2019-04-06 01:31:31 +08:00
|
|
|
if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
|
2014-07-05 05:38:05 +08:00
|
|
|
node->time_in_stale[HSR_PT_SLAVE_B] = true;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
/* Get age of newest frame from node.
|
|
|
|
* At least one time_in is OK here; nodes get pruned long
|
|
|
|
* before both time_ins can get stale
|
|
|
|
*/
|
|
|
|
timestamp = time_a;
|
2014-07-05 05:38:05 +08:00
|
|
|
if (node->time_in_stale[HSR_PT_SLAVE_A] ||
|
|
|
|
(!node->time_in_stale[HSR_PT_SLAVE_B] &&
|
2013-10-31 04:10:47 +08:00
|
|
|
time_after(time_b, time_a)))
|
|
|
|
timestamp = time_b;
|
|
|
|
|
|
|
|
/* Warn of ring error only as long as we get frames at all */
|
|
|
|
if (time_is_after_jiffies(timestamp +
|
2019-04-06 01:31:31 +08:00
|
|
|
msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
|
2014-07-05 05:38:05 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
port = get_late_port(hsr, node);
|
2019-04-06 01:31:28 +08:00
|
|
|
if (port)
|
2019-04-06 01:31:32 +08:00
|
|
|
hsr_nl_ringerror(hsr, node->macaddress_A, port);
|
2014-07-05 05:38:05 +08:00
|
|
|
rcu_read_unlock();
|
2013-10-31 04:10:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Prune old entries */
|
|
|
|
if (time_is_before_jiffies(timestamp +
|
2019-04-06 01:31:23 +08:00
|
|
|
msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
|
2019-04-06 01:31:32 +08:00
|
|
|
hsr_nl_nodedown(hsr, node->macaddress_A);
|
2013-10-31 04:10:47 +08:00
|
|
|
list_del_rcu(&node->mac_list);
|
|
|
|
/* Note that we need to free this entry later: */
|
2013-12-16 14:05:50 +08:00
|
|
|
kfree_rcu(node, rcu_head);
|
2013-10-31 04:10:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2014-07-05 05:34:38 +08:00
|
|
|
void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
|
2013-10-31 04:10:47 +08:00
|
|
|
unsigned char addr[ETH_ALEN])
|
|
|
|
{
|
2014-07-05 05:34:38 +08:00
|
|
|
struct hsr_node *node;
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
if (!_pos) {
|
2014-07-05 05:34:38 +08:00
|
|
|
node = list_first_or_null_rcu(&hsr->node_db,
|
|
|
|
struct hsr_node, mac_list);
|
2013-10-31 04:10:47 +08:00
|
|
|
if (node)
|
2019-04-06 01:31:32 +08:00
|
|
|
ether_addr_copy(addr, node->macaddress_A);
|
2013-10-31 04:10:47 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = _pos;
|
2014-07-05 05:34:38 +08:00
|
|
|
list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
|
2019-04-06 01:31:32 +08:00
|
|
|
ether_addr_copy(addr, node->macaddress_A);
|
2013-10-31 04:10:47 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-05 05:34:38 +08:00
|
|
|
int hsr_get_node_data(struct hsr_priv *hsr,
|
2013-10-31 04:10:47 +08:00
|
|
|
const unsigned char *addr,
|
|
|
|
unsigned char addr_b[ETH_ALEN],
|
|
|
|
unsigned int *addr_b_ifindex,
|
|
|
|
int *if1_age,
|
|
|
|
u16 *if1_seq,
|
|
|
|
int *if2_age,
|
|
|
|
u16 *if2_seq)
|
|
|
|
{
|
2014-07-05 05:34:38 +08:00
|
|
|
struct hsr_node *node;
|
2014-07-05 05:38:05 +08:00
|
|
|
struct hsr_port *port;
|
2013-10-31 04:10:47 +08:00
|
|
|
unsigned long tdiff;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2019-04-06 01:31:32 +08:00
|
|
|
node = find_node_by_addr_A(&hsr->node_db, addr);
|
2013-10-31 04:10:47 +08:00
|
|
|
if (!node) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
return -ENOENT; /* No such entry */
|
|
|
|
}
|
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
ether_addr_copy(addr_b, node->macaddress_B);
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2014-07-05 05:38:05 +08:00
|
|
|
tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
|
|
|
|
if (node->time_in_stale[HSR_PT_SLAVE_A])
|
2013-10-31 04:10:47 +08:00
|
|
|
*if1_age = INT_MAX;
|
|
|
|
#if HZ <= MSEC_PER_SEC
|
|
|
|
else if (tdiff > msecs_to_jiffies(INT_MAX))
|
|
|
|
*if1_age = INT_MAX;
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
*if1_age = jiffies_to_msecs(tdiff);
|
|
|
|
|
2014-07-05 05:38:05 +08:00
|
|
|
tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
|
|
|
|
if (node->time_in_stale[HSR_PT_SLAVE_B])
|
2013-10-31 04:10:47 +08:00
|
|
|
*if2_age = INT_MAX;
|
|
|
|
#if HZ <= MSEC_PER_SEC
|
|
|
|
else if (tdiff > msecs_to_jiffies(INT_MAX))
|
|
|
|
*if2_age = INT_MAX;
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
*if2_age = jiffies_to_msecs(tdiff);
|
|
|
|
|
|
|
|
/* Present sequence numbers as if they were incoming on interface */
|
2014-07-05 05:38:05 +08:00
|
|
|
*if1_seq = node->seq_out[HSR_PT_SLAVE_B];
|
|
|
|
*if2_seq = node->seq_out[HSR_PT_SLAVE_A];
|
2013-10-31 04:10:47 +08:00
|
|
|
|
2019-04-06 01:31:32 +08:00
|
|
|
if (node->addr_B_port != HSR_PT_NONE) {
|
|
|
|
port = hsr_port_get_hsr(hsr, node->addr_B_port);
|
2014-07-05 05:38:05 +08:00
|
|
|
*addr_b_ifindex = port->dev->ifindex;
|
|
|
|
} else {
|
2013-10-31 04:10:47 +08:00
|
|
|
*addr_b_ifindex = -1;
|
2014-07-05 05:38:05 +08:00
|
|
|
}
|
2013-10-31 04:10:47 +08:00
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|