Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 1 | /* Copyright 2011-2014 Autronica Fire and Security AS |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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): |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 9 | * 2011-2014 Arvid Brodin, arvid.brodin@alten.se |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 26 | struct hsr_node { |
| 27 | struct list_head mac_list; |
| 28 | unsigned char MacAddressA[ETH_ALEN]; |
| 29 | unsigned char MacAddressB[ETH_ALEN]; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 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]; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 35 | struct rcu_head rcu_head; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 44 | static struct hsr_node *find_node_by_AddrA(struct list_head *node_db, |
| 45 | const unsigned char addr[ETH_ALEN]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 46 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 47 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 60 | static struct hsr_node *find_node_by_AddrB(struct list_head *node_db, |
| 61 | const unsigned char addr[ETH_ALEN]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 62 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 63 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 76 | struct hsr_node *hsr_find_node(struct list_head *node_db, struct sk_buff *skb) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 77 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 78 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
| 100 | int 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 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 104 | struct hsr_node *node, *oldnode; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 105 | |
| 106 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 107 | if (!node) |
| 108 | return -ENOMEM; |
| 109 | |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 110 | ether_addr_copy(node->MacAddressA, addr_a); |
| 111 | ether_addr_copy(node->MacAddressB, addr_b); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 112 | |
| 113 | rcu_read_lock(); |
| 114 | oldnode = list_first_or_null_rcu(self_node_db, |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 115 | struct hsr_node, mac_list); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 156 | struct hsr_node *hsr_merge_node(struct hsr_node *node, struct sk_buff *skb, |
| 157 | struct hsr_port *port) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 158 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 159 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 167 | hsr = port->hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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); |
Wei Yongjun | 1aee6cc | 2013-12-16 14:05:50 +0800 | [diff] [blame] | 172 | kfree_rcu(node, rcu_head); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 173 | node = NULL; |
| 174 | } |
| 175 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 176 | if (node && (port->type == node->AddrB_port) && |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 177 | !ether_addr_equal(node->MacAddressB, hsr_ethsup->ethhdr.h_source)) { |
| 178 | /* Cables have been swapped */ |
| 179 | list_del_rcu(&node->mac_list); |
Wei Yongjun | 1aee6cc | 2013-12-16 14:05:50 +0800 | [diff] [blame] | 180 | kfree_rcu(node, rcu_head); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 181 | node = NULL; |
| 182 | } |
| 183 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 184 | if (node && (port->type != node->AddrB_port) && |
| 185 | (node->AddrB_port != HSR_PT_NONE) && |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 186 | !ether_addr_equal(node->MacAddressA, hsr_ethsup->ethhdr.h_source)) { |
| 187 | /* Cables have been swapped */ |
| 188 | list_del_rcu(&node->mac_list); |
Wei Yongjun | 1aee6cc | 2013-12-16 14:05:50 +0800 | [diff] [blame] | 189 | kfree_rcu(node, rcu_head); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 190 | node = NULL; |
| 191 | } |
| 192 | |
| 193 | if (node) |
| 194 | return node; |
| 195 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 196 | node = find_node_by_AddrA(&hsr->node_db, hsr_sp->MacAddressA); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 201 | ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 202 | node->AddrB_port = port->type; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 203 | return node; |
| 204 | } |
| 205 | |
| 206 | node = kzalloc(sizeof(*node), GFP_ATOMIC); |
| 207 | if (!node) |
| 208 | return NULL; |
| 209 | |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 210 | ether_addr_copy(node->MacAddressA, hsr_sp->MacAddressA); |
| 211 | ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 212 | if (!ether_addr_equal(hsr_sp->MacAddressA, hsr_ethsup->ethhdr.h_source)) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 213 | node->AddrB_port = port->type; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 219 | for (i = 0; i < HSR_PT_PORTS; i++) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 220 | node->time_in[i] = now; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 221 | for (i = 0; i < HSR_PT_PORTS; i++) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 222 | node->seq_out[i] = ntohs(hsr_ethsup->hsr_sup.sequence_nr) - 1; |
| 223 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 224 | list_add_tail_rcu(&node->mac_list, &hsr->node_db); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 236 | void hsr_addr_subst_source(struct hsr_priv *hsr, struct sk_buff *skb) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 237 | { |
| 238 | struct ethhdr *ethhdr; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 239 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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(); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 248 | node = find_node_by_AddrB(&hsr->node_db, ethhdr->h_source); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 249 | if (node) |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 250 | ether_addr_copy(ethhdr->h_source, node->MacAddressA); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 264 | void hsr_addr_subst_dest(struct hsr_priv *hsr, struct ethhdr *ethhdr, |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 265 | struct hsr_port *port) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 266 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 267 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 268 | |
| 269 | rcu_read_lock(); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 270 | node = find_node_by_AddrA(&hsr->node_db, ethhdr->h_dest); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 271 | if (node && (node->AddrB_port == port->type)) |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 272 | ether_addr_copy(ethhdr->h_dest, node->MacAddressB); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
| 280 | static bool seq_nr_after(u16 a, u16 b) |
| 281 | { |
| 282 | /* Remove inconsistency where |
Arvid Brodin | 213e3bc | 2013-11-29 23:37:07 +0100 | [diff] [blame] | 283 | * seq_nr_after(a, b) == seq_nr_before(a, b) |
| 284 | */ |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 295 | void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 296 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 297 | node->time_in[port->type] = jiffies; |
| 298 | node->time_in_stale[port->type] = false; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 310 | int hsr_register_frame_out(struct hsr_node *node, struct hsr_port *port, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 311 | struct sk_buff *skb) |
| 312 | { |
| 313 | struct hsr_ethhdr *hsr_ethhdr; |
| 314 | u16 sequence_nr; |
| 315 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 323 | if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type])) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 324 | return 1; |
| 325 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 326 | node->seq_out[port->type] = sequence_nr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 331 | static struct hsr_port *get_late_port(struct hsr_priv *hsr, |
| 332 | struct hsr_node *node) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 333 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 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); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 338 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 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); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 347 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 348 | return NULL; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
| 352 | /* Remove stale sequence_nr records. Called by timer every |
| 353 | * HSR_LIFE_CHECK_INTERVAL (two seconds or so). |
| 354 | */ |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame] | 355 | void hsr_prune_nodes(unsigned long data) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 356 | { |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame] | 357 | struct hsr_priv *hsr; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 358 | struct hsr_node *node; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 359 | struct hsr_port *port; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 360 | unsigned long timestamp; |
| 361 | unsigned long time_a, time_b; |
| 362 | |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame] | 363 | hsr = (struct hsr_priv *) data; |
| 364 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 365 | rcu_read_lock(); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 366 | list_for_each_entry_rcu(node, &hsr->node_db, mac_list) { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 367 | /* Shorthand */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 368 | time_a = node->time_in[HSR_PT_SLAVE_A]; |
| 369 | time_b = node->time_in[HSR_PT_SLAVE_B]; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 370 | |
| 371 | /* Check for timestamps old enough to risk wrap-around */ |
| 372 | if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2)) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 373 | node->time_in_stale[HSR_PT_SLAVE_A] = true; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 374 | if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2)) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 375 | node->time_in_stale[HSR_PT_SLAVE_B] = true; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 382 | if (node->time_in_stale[HSR_PT_SLAVE_A] || |
| 383 | (!node->time_in_stale[HSR_PT_SLAVE_B] && |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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))) { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 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(); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* Prune old entries */ |
| 398 | if (time_is_before_jiffies(timestamp + |
| 399 | msecs_to_jiffies(HSR_NODE_FORGET_TIME))) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 400 | hsr_nl_nodedown(hsr, node->MacAddressA); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 401 | list_del_rcu(&node->mac_list); |
| 402 | /* Note that we need to free this entry later: */ |
Wei Yongjun | 1aee6cc | 2013-12-16 14:05:50 +0800 | [diff] [blame] | 403 | kfree_rcu(node, rcu_head); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | rcu_read_unlock(); |
| 407 | } |
| 408 | |
| 409 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 410 | void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 411 | unsigned char addr[ETH_ALEN]) |
| 412 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 413 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 414 | |
| 415 | if (!_pos) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 416 | node = list_first_or_null_rcu(&hsr->node_db, |
| 417 | struct hsr_node, mac_list); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 418 | if (node) |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 419 | ether_addr_copy(addr, node->MacAddressA); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 420 | return node; |
| 421 | } |
| 422 | |
| 423 | node = _pos; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 424 | list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) { |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 425 | ether_addr_copy(addr, node->MacAddressA); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 426 | return node; |
| 427 | } |
| 428 | |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 433 | int hsr_get_node_data(struct hsr_priv *hsr, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 442 | struct hsr_node *node; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 443 | struct hsr_port *port; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 444 | unsigned long tdiff; |
| 445 | |
| 446 | |
| 447 | rcu_read_lock(); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 448 | node = find_node_by_AddrA(&hsr->node_db, addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 449 | if (!node) { |
| 450 | rcu_read_unlock(); |
| 451 | return -ENOENT; /* No such entry */ |
| 452 | } |
| 453 | |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 454 | ether_addr_copy(addr_b, node->MacAddressB); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 455 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 456 | tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A]; |
| 457 | if (node->time_in_stale[HSR_PT_SLAVE_A]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 466 | tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B]; |
| 467 | if (node->time_in_stale[HSR_PT_SLAVE_B]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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 */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 477 | *if1_seq = node->seq_out[HSR_PT_SLAVE_B]; |
| 478 | *if2_seq = node->seq_out[HSR_PT_SLAVE_A]; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 479 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 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 { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 484 | *addr_b_ifindex = -1; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 485 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 486 | |
| 487 | rcu_read_unlock(); |
| 488 | |
| 489 | return 0; |
| 490 | } |