Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'hsr-next'

Arvid Brodin says:

====================
net/hsr: Use list_head+rcu, better frame dispatch, etc.

This patch series is meant to improve the HSR code in several ways:

* Better code readability.
* In general, make the code structure more like the net/bridge code (HSR
operates similarly to a bridge, but uses the HSR-specific frame headers to
break up rings, instead of the STP protocol).
* Better handling of HSR ports' net_device features.
* Use list_head and the _rcu list traversing routines instead of array of slave
devices.
* Make it easy to support HSR Interlink devices (for future Redbox/Quadbox
support).
* Somewhat better throughput on non-HAVE_EFFICIENT_UNALIGNED_ACCESS archs, due
to lesser copying of skb data.

The code has been tested in a ring together with other HSR nodes running
unchanged code, on both avr32 and x86_64. There should only be one minor change
in behaviour from a user perspective:

* Anyone using the Netlink HSR_C_GET_NODE_LIST message to dump the internal
node database will notice that the database now also contains the self node.

All patches pass 'checkpatch.pl --ignore CAMELCASE --max-line-length=83
--strict' with only CHECKs, each of which have been deliberately left in place.

The final code passes sparse checks with no output.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+1370 -1142
+2 -1
net/hsr/Makefile
··· 4 4 5 5 obj-$(CONFIG_HSR) += hsr.o 6 6 7 - hsr-y := hsr_main.o hsr_framereg.o hsr_device.o hsr_netlink.o 7 + hsr-y := hsr_main.o hsr_framereg.o hsr_device.o \ 8 + hsr_netlink.o hsr_slave.o hsr_forward.o
+243 -339
net/hsr/hsr_device.c
··· 1 - /* Copyright 2011-2013 Autronica Fire and Security AS 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 2 2 * 3 3 * This program is free software; you can redistribute it and/or modify it 4 4 * under the terms of the GNU General Public License as published by the Free ··· 6 6 * any later version. 7 7 * 8 8 * Author(s): 9 - * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 10 * 11 11 * This file contains device methods for creating, using and destroying 12 12 * virtual HSR devices. ··· 15 15 #include <linux/netdevice.h> 16 16 #include <linux/skbuff.h> 17 17 #include <linux/etherdevice.h> 18 - #include <linux/if_arp.h> 19 18 #include <linux/rtnetlink.h> 20 19 #include <linux/pkt_sched.h> 21 20 #include "hsr_device.h" 21 + #include "hsr_slave.h" 22 22 #include "hsr_framereg.h" 23 23 #include "hsr_main.h" 24 + #include "hsr_forward.h" 24 25 25 26 26 27 static bool is_admin_up(struct net_device *dev) ··· 46 45 } 47 46 } 48 47 49 - void hsr_set_operstate(struct net_device *hsr_dev, struct net_device *slave1, 50 - struct net_device *slave2) 48 + static void hsr_set_operstate(struct hsr_port *master, bool has_carrier) 51 49 { 52 - if (!is_admin_up(hsr_dev)) { 53 - __hsr_set_operstate(hsr_dev, IF_OPER_DOWN); 50 + if (!is_admin_up(master->dev)) { 51 + __hsr_set_operstate(master->dev, IF_OPER_DOWN); 54 52 return; 55 53 } 56 54 57 - if (is_slave_up(slave1) || is_slave_up(slave2)) 58 - __hsr_set_operstate(hsr_dev, IF_OPER_UP); 55 + if (has_carrier) 56 + __hsr_set_operstate(master->dev, IF_OPER_UP); 59 57 else 60 - __hsr_set_operstate(hsr_dev, IF_OPER_LOWERLAYERDOWN); 58 + __hsr_set_operstate(master->dev, IF_OPER_LOWERLAYERDOWN); 61 59 } 62 60 63 - void hsr_set_carrier(struct net_device *hsr_dev, struct net_device *slave1, 64 - struct net_device *slave2) 61 + static bool hsr_check_carrier(struct hsr_port *master) 65 62 { 66 - if (is_slave_up(slave1) || is_slave_up(slave2)) 67 - netif_carrier_on(hsr_dev); 63 + struct hsr_port *port; 64 + bool has_carrier; 65 + 66 + has_carrier = false; 67 + 68 + rcu_read_lock(); 69 + hsr_for_each_port(master->hsr, port) 70 + if ((port->type != HSR_PT_MASTER) && is_slave_up(port->dev)) { 71 + has_carrier = true; 72 + break; 73 + } 74 + rcu_read_unlock(); 75 + 76 + if (has_carrier) 77 + netif_carrier_on(master->dev); 68 78 else 69 - netif_carrier_off(hsr_dev); 79 + netif_carrier_off(master->dev); 80 + 81 + return has_carrier; 70 82 } 71 83 72 84 73 - void hsr_check_announce(struct net_device *hsr_dev, int old_operstate) 85 + static void hsr_check_announce(struct net_device *hsr_dev, 86 + unsigned char old_operstate) 74 87 { 75 - struct hsr_priv *hsr_priv; 88 + struct hsr_priv *hsr; 76 89 77 - hsr_priv = netdev_priv(hsr_dev); 90 + hsr = netdev_priv(hsr_dev); 78 91 79 92 if ((hsr_dev->operstate == IF_OPER_UP) && (old_operstate != IF_OPER_UP)) { 80 93 /* Went up */ 81 - hsr_priv->announce_count = 0; 82 - hsr_priv->announce_timer.expires = jiffies + 94 + hsr->announce_count = 0; 95 + hsr->announce_timer.expires = jiffies + 83 96 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL); 84 - add_timer(&hsr_priv->announce_timer); 97 + add_timer(&hsr->announce_timer); 85 98 } 86 99 87 100 if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP)) 88 101 /* Went down */ 89 - del_timer(&hsr_priv->announce_timer); 102 + del_timer(&hsr->announce_timer); 90 103 } 91 104 92 - 93 - int hsr_get_max_mtu(struct hsr_priv *hsr_priv) 105 + void hsr_check_carrier_and_operstate(struct hsr_priv *hsr) 94 106 { 95 - int mtu_max; 107 + struct hsr_port *master; 108 + unsigned char old_operstate; 109 + bool has_carrier; 96 110 97 - if (hsr_priv->slave[0] && hsr_priv->slave[1]) 98 - mtu_max = min(hsr_priv->slave[0]->mtu, hsr_priv->slave[1]->mtu); 99 - else if (hsr_priv->slave[0]) 100 - mtu_max = hsr_priv->slave[0]->mtu; 101 - else if (hsr_priv->slave[1]) 102 - mtu_max = hsr_priv->slave[1]->mtu; 103 - else 104 - mtu_max = HSR_TAGLEN; 105 - 106 - return mtu_max - HSR_TAGLEN; 111 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 112 + /* netif_stacked_transfer_operstate() cannot be used here since 113 + * it doesn't set IF_OPER_LOWERLAYERDOWN (?) 114 + */ 115 + old_operstate = master->dev->operstate; 116 + has_carrier = hsr_check_carrier(master); 117 + hsr_set_operstate(master, has_carrier); 118 + hsr_check_announce(master->dev, old_operstate); 107 119 } 120 + 121 + int hsr_get_max_mtu(struct hsr_priv *hsr) 122 + { 123 + unsigned int mtu_max; 124 + struct hsr_port *port; 125 + 126 + mtu_max = ETH_DATA_LEN; 127 + rcu_read_lock(); 128 + hsr_for_each_port(hsr, port) 129 + if (port->type != HSR_PT_MASTER) 130 + mtu_max = min(port->dev->mtu, mtu_max); 131 + rcu_read_unlock(); 132 + 133 + if (mtu_max < HSR_HLEN) 134 + return 0; 135 + return mtu_max - HSR_HLEN; 136 + } 137 + 108 138 109 139 static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu) 110 140 { 111 - struct hsr_priv *hsr_priv; 141 + struct hsr_priv *hsr; 142 + struct hsr_port *master; 112 143 113 - hsr_priv = netdev_priv(dev); 144 + hsr = netdev_priv(dev); 145 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 114 146 115 - if (new_mtu > hsr_get_max_mtu(hsr_priv)) { 116 - netdev_info(hsr_priv->dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n", 117 - HSR_TAGLEN); 147 + if (new_mtu > hsr_get_max_mtu(hsr)) { 148 + netdev_info(master->dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n", 149 + HSR_HLEN); 118 150 return -EINVAL; 119 151 } 120 152 ··· 158 124 159 125 static int hsr_dev_open(struct net_device *dev) 160 126 { 161 - struct hsr_priv *hsr_priv; 162 - int i; 163 - char *slave_name; 127 + struct hsr_priv *hsr; 128 + struct hsr_port *port; 129 + char designation; 164 130 165 - hsr_priv = netdev_priv(dev); 131 + hsr = netdev_priv(dev); 132 + designation = '\0'; 166 133 167 - for (i = 0; i < HSR_MAX_SLAVE; i++) { 168 - if (hsr_priv->slave[i]) 169 - slave_name = hsr_priv->slave[i]->name; 170 - else 171 - slave_name = "null"; 172 - 173 - if (!is_slave_up(hsr_priv->slave[i])) 174 - netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a working HSR network\n", 175 - 'A' + i, slave_name); 134 + rcu_read_lock(); 135 + hsr_for_each_port(hsr, port) { 136 + if (port->type == HSR_PT_MASTER) 137 + continue; 138 + switch (port->type) { 139 + case HSR_PT_SLAVE_A: 140 + designation = 'A'; 141 + break; 142 + case HSR_PT_SLAVE_B: 143 + designation = 'B'; 144 + break; 145 + default: 146 + designation = '?'; 147 + } 148 + if (!is_slave_up(port->dev)) 149 + netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n", 150 + designation, port->dev->name); 176 151 } 152 + rcu_read_unlock(); 153 + 154 + if (designation == '\0') 155 + netdev_warn(dev, "No slave devices configured\n"); 177 156 178 157 return 0; 179 158 } 159 + 180 160 181 161 static int hsr_dev_close(struct net_device *dev) 182 162 { 183 - /* Nothing to do here. We could try to restore the state of the slaves 184 - * to what they were before being changed by the hsr master dev's state, 185 - * but they might have been changed manually in the mean time too, so 186 - * taking them up or down here might be confusing and is probably not a 187 - * good idea. 188 - */ 163 + /* Nothing to do here. */ 189 164 return 0; 190 165 } 191 166 192 167 193 - static void hsr_fill_tag(struct hsr_ethhdr *hsr_ethhdr, struct hsr_priv *hsr_priv) 168 + static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr, 169 + netdev_features_t features) 194 170 { 195 - unsigned long irqflags; 171 + netdev_features_t mask; 172 + struct hsr_port *port; 196 173 197 - /* IEC 62439-1:2010, p 48, says the 4-bit "path" field can take values 198 - * between 0001-1001 ("ring identifier", for regular HSR frames), 199 - * or 1111 ("HSR management", supervision frames). Unfortunately, the 200 - * spec writers forgot to explain what a "ring identifier" is, or 201 - * how it is used. So we just set this to 0001 for regular frames, 202 - * and 1111 for supervision frames. 203 - */ 204 - set_hsr_tag_path(&hsr_ethhdr->hsr_tag, 0x1); 174 + mask = features; 205 175 206 - /* IEC 62439-1:2010, p 12: "The link service data unit in an Ethernet 207 - * frame is the content of the frame located between the Length/Type 208 - * field and the Frame Check Sequence." 176 + /* Mask out all features that, if supported by one device, should be 177 + * enabled for all devices (see NETIF_F_ONE_FOR_ALL). 209 178 * 210 - * IEC 62439-3, p 48, specifies the "original LPDU" to include the 211 - * original "LT" field (what "LT" means is not explained anywhere as 212 - * far as I can see - perhaps "Length/Type"?). So LSDU_size might 213 - * equal original length + 2. 214 - * Also, the fact that this field is not used anywhere (might be used 215 - * by a RedBox connecting HSR and PRP nets?) means I cannot test its 216 - * correctness. Instead of guessing, I set this to 0 here, to make any 217 - * problems immediately apparent. Anyone using this driver with PRP/HSR 218 - * RedBoxes might need to fix this... 179 + * Anything that's off in mask will not be enabled - so only things 180 + * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL, 181 + * may become enabled. 219 182 */ 220 - set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, 0); 183 + features &= ~NETIF_F_ONE_FOR_ALL; 184 + hsr_for_each_port(hsr, port) 185 + features = netdev_increment_features(features, 186 + port->dev->features, 187 + mask); 221 188 222 - spin_lock_irqsave(&hsr_priv->seqnr_lock, irqflags); 223 - hsr_ethhdr->hsr_tag.sequence_nr = htons(hsr_priv->sequence_nr); 224 - hsr_priv->sequence_nr++; 225 - spin_unlock_irqrestore(&hsr_priv->seqnr_lock, irqflags); 226 - 227 - hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto; 228 - 229 - hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP); 189 + return features; 230 190 } 231 191 232 - static int slave_xmit(struct sk_buff *skb, struct hsr_priv *hsr_priv, 233 - enum hsr_dev_idx dev_idx) 192 + static netdev_features_t hsr_fix_features(struct net_device *dev, 193 + netdev_features_t features) 234 194 { 235 - struct hsr_ethhdr *hsr_ethhdr; 195 + struct hsr_priv *hsr = netdev_priv(dev); 236 196 237 - hsr_ethhdr = (struct hsr_ethhdr *) skb->data; 238 - 239 - skb->dev = hsr_priv->slave[dev_idx]; 240 - 241 - hsr_addr_subst_dest(hsr_priv, &hsr_ethhdr->ethhdr, dev_idx); 242 - 243 - /* Address substitution (IEC62439-3 pp 26, 50): replace mac 244 - * address of outgoing frame with that of the outgoing slave's. 245 - */ 246 - ether_addr_copy(hsr_ethhdr->ethhdr.h_source, skb->dev->dev_addr); 247 - 248 - return dev_queue_xmit(skb); 197 + return hsr_features_recompute(hsr, features); 249 198 } 250 199 251 200 252 201 static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev) 253 202 { 254 - struct hsr_priv *hsr_priv; 255 - struct hsr_ethhdr *hsr_ethhdr; 256 - struct sk_buff *skb2; 257 - int res1, res2; 203 + struct hsr_priv *hsr = netdev_priv(dev); 204 + struct hsr_port *master; 258 205 259 - hsr_priv = netdev_priv(dev); 260 - hsr_ethhdr = (struct hsr_ethhdr *) skb->data; 261 - 262 - if ((skb->protocol != htons(ETH_P_PRP)) || 263 - (hsr_ethhdr->ethhdr.h_proto != htons(ETH_P_PRP))) { 264 - hsr_fill_tag(hsr_ethhdr, hsr_priv); 265 - skb->protocol = htons(ETH_P_PRP); 266 - } 267 - 268 - skb2 = pskb_copy(skb, GFP_ATOMIC); 269 - 270 - res1 = NET_XMIT_DROP; 271 - if (likely(hsr_priv->slave[HSR_DEV_SLAVE_A])) 272 - res1 = slave_xmit(skb, hsr_priv, HSR_DEV_SLAVE_A); 273 - 274 - res2 = NET_XMIT_DROP; 275 - if (likely(skb2 && hsr_priv->slave[HSR_DEV_SLAVE_B])) 276 - res2 = slave_xmit(skb2, hsr_priv, HSR_DEV_SLAVE_B); 277 - 278 - if (likely(res1 == NET_XMIT_SUCCESS || res1 == NET_XMIT_CN || 279 - res2 == NET_XMIT_SUCCESS || res2 == NET_XMIT_CN)) { 280 - hsr_priv->dev->stats.tx_packets++; 281 - hsr_priv->dev->stats.tx_bytes += skb->len; 282 - } else { 283 - hsr_priv->dev->stats.tx_dropped++; 284 - } 206 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 207 + skb->dev = master->dev; 208 + hsr_forward_skb(skb, master); 285 209 286 210 return NETDEV_TX_OK; 287 211 } 288 212 289 213 290 - static int hsr_header_create(struct sk_buff *skb, struct net_device *dev, 291 - unsigned short type, const void *daddr, 292 - const void *saddr, unsigned int len) 293 - { 294 - int res; 295 - 296 - /* Make room for the HSR tag now. We will fill it in later (in 297 - * hsr_dev_xmit) 298 - */ 299 - if (skb_headroom(skb) < HSR_TAGLEN + ETH_HLEN) 300 - return -ENOBUFS; 301 - skb_push(skb, HSR_TAGLEN); 302 - 303 - /* To allow VLAN/HSR combos we should probably use 304 - * res = dev_hard_header(skb, dev, type, daddr, saddr, len + HSR_TAGLEN); 305 - * here instead. It would require other changes too, though - e.g. 306 - * separate headers for each slave etc... 307 - */ 308 - res = eth_header(skb, dev, type, daddr, saddr, len + HSR_TAGLEN); 309 - if (res <= 0) 310 - return res; 311 - skb_reset_mac_header(skb); 312 - 313 - return res + HSR_TAGLEN; 314 - } 315 - 316 - 317 214 static const struct header_ops hsr_header_ops = { 318 - .create = hsr_header_create, 215 + .create = eth_header, 319 216 .parse = eth_header_parse, 320 217 }; 321 218 ··· 256 291 */ 257 292 static int hsr_pad(int size) 258 293 { 259 - const int min_size = ETH_ZLEN - HSR_TAGLEN - ETH_HLEN; 294 + const int min_size = ETH_ZLEN - HSR_HLEN - ETH_HLEN; 260 295 261 296 if (size >= min_size) 262 297 return size; 263 298 return min_size; 264 299 } 265 300 266 - static void send_hsr_supervision_frame(struct net_device *hsr_dev, u8 type) 301 + static void send_hsr_supervision_frame(struct hsr_port *master, u8 type) 267 302 { 268 - struct hsr_priv *hsr_priv; 269 303 struct sk_buff *skb; 270 304 int hlen, tlen; 271 305 struct hsr_sup_tag *hsr_stag; 272 306 struct hsr_sup_payload *hsr_sp; 273 307 unsigned long irqflags; 274 308 275 - hlen = LL_RESERVED_SPACE(hsr_dev); 276 - tlen = hsr_dev->needed_tailroom; 309 + hlen = LL_RESERVED_SPACE(master->dev); 310 + tlen = master->dev->needed_tailroom; 277 311 skb = alloc_skb(hsr_pad(sizeof(struct hsr_sup_payload)) + hlen + tlen, 278 312 GFP_ATOMIC); 279 313 280 314 if (skb == NULL) 281 315 return; 282 316 283 - hsr_priv = netdev_priv(hsr_dev); 284 - 285 317 skb_reserve(skb, hlen); 286 318 287 - skb->dev = hsr_dev; 319 + skb->dev = master->dev; 288 320 skb->protocol = htons(ETH_P_PRP); 289 321 skb->priority = TC_PRIO_CONTROL; 290 322 291 323 if (dev_hard_header(skb, skb->dev, ETH_P_PRP, 292 - hsr_priv->sup_multicast_addr, 293 - skb->dev->dev_addr, skb->len) < 0) 324 + master->hsr->sup_multicast_addr, 325 + skb->dev->dev_addr, skb->len) <= 0) 294 326 goto out; 327 + skb_reset_mac_header(skb); 295 328 296 - skb_pull(skb, sizeof(struct ethhdr)); 297 - hsr_stag = (typeof(hsr_stag)) skb->data; 329 + hsr_stag = (typeof(hsr_stag)) skb_put(skb, sizeof(*hsr_stag)); 298 330 299 331 set_hsr_stag_path(hsr_stag, 0xf); 300 332 set_hsr_stag_HSR_Ver(hsr_stag, 0); 301 333 302 - spin_lock_irqsave(&hsr_priv->seqnr_lock, irqflags); 303 - hsr_stag->sequence_nr = htons(hsr_priv->sequence_nr); 304 - hsr_priv->sequence_nr++; 305 - spin_unlock_irqrestore(&hsr_priv->seqnr_lock, irqflags); 334 + spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags); 335 + hsr_stag->sequence_nr = htons(master->hsr->sequence_nr); 336 + master->hsr->sequence_nr++; 337 + spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags); 306 338 307 339 hsr_stag->HSR_TLV_Type = type; 308 340 hsr_stag->HSR_TLV_Length = 12; 309 341 310 - skb_push(skb, sizeof(struct ethhdr)); 311 - 312 342 /* Payload: MacAddressA */ 313 343 hsr_sp = (typeof(hsr_sp)) skb_put(skb, sizeof(*hsr_sp)); 314 - ether_addr_copy(hsr_sp->MacAddressA, hsr_dev->dev_addr); 344 + ether_addr_copy(hsr_sp->MacAddressA, master->dev->dev_addr); 315 345 316 - dev_queue_xmit(skb); 346 + hsr_forward_skb(skb, master); 317 347 return; 318 348 319 349 out: 350 + WARN_ON_ONCE("HSR: Could not send supervision frame\n"); 320 351 kfree_skb(skb); 321 352 } 322 353 ··· 321 360 */ 322 361 static void hsr_announce(unsigned long data) 323 362 { 324 - struct hsr_priv *hsr_priv; 363 + struct hsr_priv *hsr; 364 + struct hsr_port *master; 325 365 326 - hsr_priv = (struct hsr_priv *) data; 366 + hsr = (struct hsr_priv *) data; 327 367 328 - if (hsr_priv->announce_count < 3) { 329 - send_hsr_supervision_frame(hsr_priv->dev, HSR_TLV_ANNOUNCE); 330 - hsr_priv->announce_count++; 368 + rcu_read_lock(); 369 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 370 + 371 + if (hsr->announce_count < 3) { 372 + send_hsr_supervision_frame(master, HSR_TLV_ANNOUNCE); 373 + hsr->announce_count++; 331 374 } else { 332 - send_hsr_supervision_frame(hsr_priv->dev, HSR_TLV_LIFE_CHECK); 375 + send_hsr_supervision_frame(master, HSR_TLV_LIFE_CHECK); 333 376 } 334 377 335 - if (hsr_priv->announce_count < 3) 336 - hsr_priv->announce_timer.expires = jiffies + 378 + if (hsr->announce_count < 3) 379 + hsr->announce_timer.expires = jiffies + 337 380 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL); 338 381 else 339 - hsr_priv->announce_timer.expires = jiffies + 382 + hsr->announce_timer.expires = jiffies + 340 383 msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL); 341 384 342 - if (is_admin_up(hsr_priv->dev)) 343 - add_timer(&hsr_priv->announce_timer); 344 - } 385 + if (is_admin_up(master->dev)) 386 + add_timer(&hsr->announce_timer); 345 387 346 - 347 - static void restore_slaves(struct net_device *hsr_dev) 348 - { 349 - struct hsr_priv *hsr_priv; 350 - int i; 351 - int res; 352 - 353 - hsr_priv = netdev_priv(hsr_dev); 354 - 355 - rtnl_lock(); 356 - 357 - /* Restore promiscuity */ 358 - for (i = 0; i < HSR_MAX_SLAVE; i++) { 359 - if (!hsr_priv->slave[i]) 360 - continue; 361 - res = dev_set_promiscuity(hsr_priv->slave[i], -1); 362 - if (res) 363 - netdev_info(hsr_dev, 364 - "Cannot restore slave promiscuity (%s, %d)\n", 365 - hsr_priv->slave[i]->name, res); 366 - } 367 - 368 - rtnl_unlock(); 369 - } 370 - 371 - static void reclaim_hsr_dev(struct rcu_head *rh) 372 - { 373 - struct hsr_priv *hsr_priv; 374 - 375 - hsr_priv = container_of(rh, struct hsr_priv, rcu_head); 376 - free_netdev(hsr_priv->dev); 388 + rcu_read_unlock(); 377 389 } 378 390 379 391 ··· 355 421 */ 356 422 static void hsr_dev_destroy(struct net_device *hsr_dev) 357 423 { 358 - struct hsr_priv *hsr_priv; 424 + struct hsr_priv *hsr; 425 + struct hsr_port *port; 359 426 360 - hsr_priv = netdev_priv(hsr_dev); 427 + hsr = netdev_priv(hsr_dev); 428 + hsr_for_each_port(hsr, port) 429 + hsr_del_port(port); 361 430 362 - del_timer(&hsr_priv->announce_timer); 363 - unregister_hsr_master(hsr_priv); /* calls list_del_rcu on hsr_priv */ 364 - restore_slaves(hsr_dev); 365 - call_rcu(&hsr_priv->rcu_head, reclaim_hsr_dev); /* reclaim hsr_priv */ 431 + del_timer_sync(&hsr->prune_timer); 432 + del_timer_sync(&hsr->announce_timer); 433 + 434 + synchronize_rcu(); 435 + free_netdev(hsr_dev); 366 436 } 367 437 368 438 static const struct net_device_ops hsr_device_ops = { ··· 374 436 .ndo_open = hsr_dev_open, 375 437 .ndo_stop = hsr_dev_close, 376 438 .ndo_start_xmit = hsr_dev_xmit, 439 + .ndo_fix_features = hsr_fix_features, 377 440 }; 378 441 442 + static struct device_type hsr_type = { 443 + .name = "hsr", 444 + }; 379 445 380 446 void hsr_dev_setup(struct net_device *dev) 381 447 { 382 448 random_ether_addr(dev->dev_addr); 383 449 384 450 ether_setup(dev); 385 - dev->header_ops = &hsr_header_ops; 386 - dev->netdev_ops = &hsr_device_ops; 387 - dev->tx_queue_len = 0; 451 + dev->header_ops = &hsr_header_ops; 452 + dev->netdev_ops = &hsr_device_ops; 453 + SET_NETDEV_DEVTYPE(dev, &hsr_type); 454 + dev->tx_queue_len = 0; 388 455 389 456 dev->destructor = hsr_dev_destroy; 457 + 458 + dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | 459 + NETIF_F_GSO_MASK | NETIF_F_HW_CSUM | 460 + NETIF_F_HW_VLAN_CTAG_TX; 461 + 462 + dev->features = dev->hw_features; 463 + 464 + /* Prevent recursive tx locking */ 465 + dev->features |= NETIF_F_LLTX; 466 + /* VLAN on top of HSR needs testing and probably some work on 467 + * hsr_header_create() etc. 468 + */ 469 + dev->features |= NETIF_F_VLAN_CHALLENGED; 470 + /* Not sure about this. Taken from bridge code. netdev_features.h says 471 + * it means "Does not change network namespaces". 472 + */ 473 + dev->features |= NETIF_F_NETNS_LOCAL; 390 474 } 391 475 392 476 393 477 /* Return true if dev is a HSR master; return false otherwise. 394 478 */ 395 - bool is_hsr_master(struct net_device *dev) 479 + inline bool is_hsr_master(struct net_device *dev) 396 480 { 397 481 return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit); 398 482 } 399 - 400 - static int check_slave_ok(struct net_device *dev) 401 - { 402 - /* Don't allow HSR on non-ethernet like devices */ 403 - if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) || 404 - (dev->addr_len != ETH_ALEN)) { 405 - netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n"); 406 - return -EINVAL; 407 - } 408 - 409 - /* Don't allow enslaving hsr devices */ 410 - if (is_hsr_master(dev)) { 411 - netdev_info(dev, "Cannot create trees of HSR devices.\n"); 412 - return -EINVAL; 413 - } 414 - 415 - if (is_hsr_slave(dev)) { 416 - netdev_info(dev, "This device is already a HSR slave.\n"); 417 - return -EINVAL; 418 - } 419 - 420 - if (dev->priv_flags & IFF_802_1Q_VLAN) { 421 - netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n"); 422 - return -EINVAL; 423 - } 424 - 425 - /* HSR over bonded devices has not been tested, but I'm not sure it 426 - * won't work... 427 - */ 428 - 429 - return 0; 430 - } 431 - 432 483 433 484 /* Default multicast address for HSR Supervision frames */ 434 485 static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = { ··· 427 500 int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2], 428 501 unsigned char multicast_spec) 429 502 { 430 - struct hsr_priv *hsr_priv; 431 - int i; 503 + struct hsr_priv *hsr; 504 + struct hsr_port *port; 432 505 int res; 433 506 434 - hsr_priv = netdev_priv(hsr_dev); 435 - hsr_priv->dev = hsr_dev; 436 - INIT_LIST_HEAD(&hsr_priv->node_db); 437 - INIT_LIST_HEAD(&hsr_priv->self_node_db); 438 - for (i = 0; i < HSR_MAX_SLAVE; i++) 439 - hsr_priv->slave[i] = slave[i]; 507 + hsr = netdev_priv(hsr_dev); 508 + INIT_LIST_HEAD(&hsr->ports); 509 + INIT_LIST_HEAD(&hsr->node_db); 510 + INIT_LIST_HEAD(&hsr->self_node_db); 440 511 441 - spin_lock_init(&hsr_priv->seqnr_lock); 512 + ether_addr_copy(hsr_dev->dev_addr, slave[0]->dev_addr); 513 + 514 + /* Make sure we recognize frames from ourselves in hsr_rcv() */ 515 + res = hsr_create_self_node(&hsr->self_node_db, hsr_dev->dev_addr, 516 + slave[1]->dev_addr); 517 + if (res < 0) 518 + return res; 519 + 520 + spin_lock_init(&hsr->seqnr_lock); 442 521 /* Overflow soon to find bugs easier: */ 443 - hsr_priv->sequence_nr = USHRT_MAX - 1024; 522 + hsr->sequence_nr = HSR_SEQNR_START; 444 523 445 - init_timer(&hsr_priv->announce_timer); 446 - hsr_priv->announce_timer.function = hsr_announce; 447 - hsr_priv->announce_timer.data = (unsigned long) hsr_priv; 524 + init_timer(&hsr->announce_timer); 525 + hsr->announce_timer.function = hsr_announce; 526 + hsr->announce_timer.data = (unsigned long) hsr; 448 527 449 - ether_addr_copy(hsr_priv->sup_multicast_addr, def_multicast_addr); 450 - hsr_priv->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec; 528 + init_timer(&hsr->prune_timer); 529 + hsr->prune_timer.function = hsr_prune_nodes; 530 + hsr->prune_timer.data = (unsigned long) hsr; 451 531 452 - /* FIXME: should I modify the value of these? 453 - * 454 - * - hsr_dev->flags - i.e. 455 - * IFF_MASTER/SLAVE? 456 - * - hsr_dev->priv_flags - i.e. 457 - * IFF_EBRIDGE? 458 - * IFF_TX_SKB_SHARING? 459 - * IFF_HSR_MASTER/SLAVE? 460 - */ 532 + ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr); 533 + hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec; 461 534 462 - for (i = 0; i < HSR_MAX_SLAVE; i++) { 463 - res = check_slave_ok(slave[i]); 464 - if (res) 465 - return res; 466 - } 467 - 468 - hsr_dev->features = slave[0]->features & slave[1]->features; 469 - /* Prevent recursive tx locking */ 470 - hsr_dev->features |= NETIF_F_LLTX; 471 - /* VLAN on top of HSR needs testing and probably some work on 472 - * hsr_header_create() etc. 535 + /* FIXME: should I modify the value of these? 536 + * 537 + * - hsr_dev->flags - i.e. 538 + * IFF_MASTER/SLAVE? 539 + * - hsr_dev->priv_flags - i.e. 540 + * IFF_EBRIDGE? 541 + * IFF_TX_SKB_SHARING? 542 + * IFF_HSR_MASTER/SLAVE? 473 543 */ 474 - hsr_dev->features |= NETIF_F_VLAN_CHALLENGED; 475 - 476 - /* Set hsr_dev's MAC address to that of mac_slave1 */ 477 - ether_addr_copy(hsr_dev->dev_addr, hsr_priv->slave[0]->dev_addr); 478 - 479 - /* Set required header length */ 480 - for (i = 0; i < HSR_MAX_SLAVE; i++) { 481 - if (slave[i]->hard_header_len + HSR_TAGLEN > 482 - hsr_dev->hard_header_len) 483 - hsr_dev->hard_header_len = 484 - slave[i]->hard_header_len + HSR_TAGLEN; 485 - } 486 - 487 - /* MTU */ 488 - for (i = 0; i < HSR_MAX_SLAVE; i++) 489 - if (slave[i]->mtu - HSR_TAGLEN < hsr_dev->mtu) 490 - hsr_dev->mtu = slave[i]->mtu - HSR_TAGLEN; 491 544 492 545 /* Make sure the 1st call to netif_carrier_on() gets through */ 493 546 netif_carrier_off(hsr_dev); 494 547 495 - /* Promiscuity */ 496 - for (i = 0; i < HSR_MAX_SLAVE; i++) { 497 - res = dev_set_promiscuity(slave[i], 1); 498 - if (res) { 499 - netdev_info(hsr_dev, "Cannot set slave promiscuity (%s, %d)\n", 500 - slave[i]->name, res); 501 - goto fail; 502 - } 503 - } 504 - 505 - /* Make sure we recognize frames from ourselves in hsr_rcv() */ 506 - res = hsr_create_self_node(&hsr_priv->self_node_db, 507 - hsr_dev->dev_addr, 508 - hsr_priv->slave[1]->dev_addr); 509 - if (res < 0) 510 - goto fail; 548 + res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER); 549 + if (res) 550 + return res; 511 551 512 552 res = register_netdevice(hsr_dev); 513 553 if (res) 514 554 goto fail; 515 555 516 - register_hsr_master(hsr_priv); 556 + res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A); 557 + if (res) 558 + goto fail; 559 + res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B); 560 + if (res) 561 + goto fail; 562 + 563 + hsr->prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD); 564 + add_timer(&hsr->prune_timer); 517 565 518 566 return 0; 519 567 520 568 fail: 521 - restore_slaves(hsr_dev); 569 + hsr_for_each_port(hsr, port) 570 + hsr_del_port(port); 571 + 522 572 return res; 523 573 }
+4 -8
net/hsr/hsr_device.h
··· 1 - /* Copyright 2011-2013 Autronica Fire and Security AS 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 2 2 * 3 3 * This program is free software; you can redistribute it and/or modify it 4 4 * under the terms of the GNU General Public License as published by the Free ··· 6 6 * any later version. 7 7 * 8 8 * Author(s): 9 - * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 10 */ 11 11 12 12 #ifndef __HSR_DEVICE_H ··· 18 18 void hsr_dev_setup(struct net_device *dev); 19 19 int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2], 20 20 unsigned char multicast_spec); 21 - void hsr_set_operstate(struct net_device *hsr_dev, struct net_device *slave1, 22 - struct net_device *slave2); 23 - void hsr_set_carrier(struct net_device *hsr_dev, struct net_device *slave1, 24 - struct net_device *slave2); 25 - void hsr_check_announce(struct net_device *hsr_dev, int old_operstate); 21 + void hsr_check_carrier_and_operstate(struct hsr_priv *hsr); 26 22 bool is_hsr_master(struct net_device *dev); 27 - int hsr_get_max_mtu(struct hsr_priv *hsr_priv); 23 + int hsr_get_max_mtu(struct hsr_priv *hsr); 28 24 29 25 #endif /* __HSR_DEVICE_H */
+368
net/hsr/hsr_forward.c
··· 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 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): 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 + */ 11 + 12 + #include "hsr_forward.h" 13 + #include <linux/types.h> 14 + #include <linux/skbuff.h> 15 + #include <linux/etherdevice.h> 16 + #include <linux/if_vlan.h> 17 + #include "hsr_main.h" 18 + #include "hsr_framereg.h" 19 + 20 + 21 + struct hsr_node; 22 + 23 + struct hsr_frame_info { 24 + struct sk_buff *skb_std; 25 + struct sk_buff *skb_hsr; 26 + struct hsr_port *port_rcv; 27 + struct hsr_node *node_src; 28 + u16 sequence_nr; 29 + bool is_supervision; 30 + bool is_vlan; 31 + bool is_local_dest; 32 + bool is_local_exclusive; 33 + }; 34 + 35 + 36 + /* The uses I can see for these HSR supervision frames are: 37 + * 1) Use the frames that are sent after node initialization ("HSR_TLV.Type = 38 + * 22") to reset any sequence_nr counters belonging to that node. Useful if 39 + * the other node's counter has been reset for some reason. 40 + * -- 41 + * Or not - resetting the counter and bridging the frame would create a 42 + * loop, unfortunately. 43 + * 44 + * 2) Use the LifeCheck frames to detect ring breaks. I.e. if no LifeCheck 45 + * frame is received from a particular node, we know something is wrong. 46 + * We just register these (as with normal frames) and throw them away. 47 + * 48 + * 3) Allow different MAC addresses for the two slave interfaces, using the 49 + * MacAddressA field. 50 + */ 51 + static bool is_supervision_frame(struct hsr_priv *hsr, struct sk_buff *skb) 52 + { 53 + struct hsr_ethhdr_sp *hdr; 54 + 55 + WARN_ON_ONCE(!skb_mac_header_was_set(skb)); 56 + hdr = (struct hsr_ethhdr_sp *) skb_mac_header(skb); 57 + 58 + if (!ether_addr_equal(hdr->ethhdr.h_dest, 59 + hsr->sup_multicast_addr)) 60 + return false; 61 + 62 + if (get_hsr_stag_path(&hdr->hsr_sup) != 0x0f) 63 + return false; 64 + if ((hdr->hsr_sup.HSR_TLV_Type != HSR_TLV_ANNOUNCE) && 65 + (hdr->hsr_sup.HSR_TLV_Type != HSR_TLV_LIFE_CHECK)) 66 + return false; 67 + if (hdr->hsr_sup.HSR_TLV_Length != 12) 68 + return false; 69 + 70 + return true; 71 + } 72 + 73 + 74 + static struct sk_buff *create_stripped_skb(struct sk_buff *skb_in, 75 + struct hsr_frame_info *frame) 76 + { 77 + struct sk_buff *skb; 78 + int copylen; 79 + unsigned char *dst, *src; 80 + 81 + skb_pull(skb_in, HSR_HLEN); 82 + skb = __pskb_copy(skb_in, skb_headroom(skb_in) - HSR_HLEN, GFP_ATOMIC); 83 + skb_push(skb_in, HSR_HLEN); 84 + if (skb == NULL) 85 + return NULL; 86 + 87 + skb_reset_mac_header(skb); 88 + 89 + if (skb->ip_summed == CHECKSUM_PARTIAL) 90 + skb->csum_start -= HSR_HLEN; 91 + 92 + copylen = 2*ETH_ALEN; 93 + if (frame->is_vlan) 94 + copylen += VLAN_HLEN; 95 + src = skb_mac_header(skb_in); 96 + dst = skb_mac_header(skb); 97 + memcpy(dst, src, copylen); 98 + 99 + skb->protocol = eth_hdr(skb)->h_proto; 100 + return skb; 101 + } 102 + 103 + static struct sk_buff *frame_get_stripped_skb(struct hsr_frame_info *frame, 104 + struct hsr_port *port) 105 + { 106 + if (!frame->skb_std) 107 + frame->skb_std = create_stripped_skb(frame->skb_hsr, frame); 108 + return skb_clone(frame->skb_std, GFP_ATOMIC); 109 + } 110 + 111 + 112 + static void hsr_fill_tag(struct sk_buff *skb, struct hsr_frame_info *frame, 113 + struct hsr_port *port) 114 + { 115 + struct hsr_ethhdr *hsr_ethhdr; 116 + int lane_id; 117 + int lsdu_size; 118 + 119 + if (port->type == HSR_PT_SLAVE_A) 120 + lane_id = 0; 121 + else 122 + lane_id = 1; 123 + 124 + lsdu_size = skb->len - 14; 125 + if (frame->is_vlan) 126 + lsdu_size -= 4; 127 + 128 + hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb); 129 + 130 + set_hsr_tag_path(&hsr_ethhdr->hsr_tag, lane_id); 131 + set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, lsdu_size); 132 + hsr_ethhdr->hsr_tag.sequence_nr = htons(frame->sequence_nr); 133 + hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto; 134 + hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP); 135 + } 136 + 137 + static struct sk_buff *create_tagged_skb(struct sk_buff *skb_o, 138 + struct hsr_frame_info *frame, 139 + struct hsr_port *port) 140 + { 141 + int movelen; 142 + unsigned char *dst, *src; 143 + struct sk_buff *skb; 144 + 145 + /* Create the new skb with enough headroom to fit the HSR tag */ 146 + skb = __pskb_copy(skb_o, skb_headroom(skb_o) + HSR_HLEN, GFP_ATOMIC); 147 + if (skb == NULL) 148 + return NULL; 149 + skb_reset_mac_header(skb); 150 + 151 + if (skb->ip_summed == CHECKSUM_PARTIAL) 152 + skb->csum_start += HSR_HLEN; 153 + 154 + movelen = ETH_HLEN; 155 + if (frame->is_vlan) 156 + movelen += VLAN_HLEN; 157 + 158 + src = skb_mac_header(skb); 159 + dst = skb_push(skb, HSR_HLEN); 160 + memmove(dst, src, movelen); 161 + skb_reset_mac_header(skb); 162 + 163 + hsr_fill_tag(skb, frame, port); 164 + 165 + return skb; 166 + } 167 + 168 + /* If the original frame was an HSR tagged frame, just clone it to be sent 169 + * unchanged. Otherwise, create a private frame especially tagged for 'port'. 170 + */ 171 + static struct sk_buff *frame_get_tagged_skb(struct hsr_frame_info *frame, 172 + struct hsr_port *port) 173 + { 174 + if (frame->skb_hsr) 175 + return skb_clone(frame->skb_hsr, GFP_ATOMIC); 176 + 177 + if ((port->type != HSR_PT_SLAVE_A) && (port->type != HSR_PT_SLAVE_B)) { 178 + WARN_ONCE(1, "HSR: Bug: trying to create a tagged frame for a non-ring port"); 179 + return NULL; 180 + } 181 + 182 + return create_tagged_skb(frame->skb_std, frame, port); 183 + } 184 + 185 + 186 + static void hsr_deliver_master(struct sk_buff *skb, struct net_device *dev, 187 + struct hsr_node *node_src) 188 + { 189 + bool was_multicast_frame; 190 + int res; 191 + 192 + was_multicast_frame = (skb->pkt_type == PACKET_MULTICAST); 193 + hsr_addr_subst_source(node_src, skb); 194 + skb_pull(skb, ETH_HLEN); 195 + res = netif_rx(skb); 196 + if (res == NET_RX_DROP) { 197 + dev->stats.rx_dropped++; 198 + } else { 199 + dev->stats.rx_packets++; 200 + dev->stats.rx_bytes += skb->len; 201 + if (was_multicast_frame) 202 + dev->stats.multicast++; 203 + } 204 + } 205 + 206 + static int hsr_xmit(struct sk_buff *skb, struct hsr_port *port, 207 + struct hsr_frame_info *frame) 208 + { 209 + if (frame->port_rcv->type == HSR_PT_MASTER) { 210 + hsr_addr_subst_dest(frame->node_src, skb, port); 211 + 212 + /* Address substitution (IEC62439-3 pp 26, 50): replace mac 213 + * address of outgoing frame with that of the outgoing slave's. 214 + */ 215 + ether_addr_copy(eth_hdr(skb)->h_source, port->dev->dev_addr); 216 + } 217 + return dev_queue_xmit(skb); 218 + } 219 + 220 + 221 + /* Forward the frame through all devices except: 222 + * - Back through the receiving device 223 + * - If it's a HSR frame: through a device where it has passed before 224 + * - To the local HSR master only if the frame is directly addressed to it, or 225 + * a non-supervision multicast or broadcast frame. 226 + * 227 + * HSR slave devices should insert a HSR tag into the frame, or forward the 228 + * frame unchanged if it's already tagged. Interlink devices should strip HSR 229 + * tags if they're of the non-HSR type (but only after duplicate discard). The 230 + * master device always strips HSR tags. 231 + */ 232 + static void hsr_forward_do(struct hsr_frame_info *frame) 233 + { 234 + struct hsr_port *port; 235 + struct sk_buff *skb; 236 + 237 + hsr_for_each_port(frame->port_rcv->hsr, port) { 238 + /* Don't send frame back the way it came */ 239 + if (port == frame->port_rcv) 240 + continue; 241 + 242 + /* Don't deliver locally unless we should */ 243 + if ((port->type == HSR_PT_MASTER) && !frame->is_local_dest) 244 + continue; 245 + 246 + /* Deliver frames directly addressed to us to master only */ 247 + if ((port->type != HSR_PT_MASTER) && frame->is_local_exclusive) 248 + continue; 249 + 250 + /* Don't send frame over port where it has been sent before */ 251 + if (hsr_register_frame_out(port, frame->node_src, 252 + frame->sequence_nr)) 253 + continue; 254 + 255 + if (frame->is_supervision && (port->type == HSR_PT_MASTER)) { 256 + hsr_handle_sup_frame(frame->skb_hsr, 257 + frame->node_src, 258 + frame->port_rcv); 259 + continue; 260 + } 261 + 262 + if (port->type != HSR_PT_MASTER) 263 + skb = frame_get_tagged_skb(frame, port); 264 + else 265 + skb = frame_get_stripped_skb(frame, port); 266 + if (skb == NULL) { 267 + /* FIXME: Record the dropped frame? */ 268 + continue; 269 + } 270 + 271 + skb->dev = port->dev; 272 + if (port->type == HSR_PT_MASTER) 273 + hsr_deliver_master(skb, port->dev, frame->node_src); 274 + else 275 + hsr_xmit(skb, port, frame); 276 + } 277 + } 278 + 279 + 280 + static void check_local_dest(struct hsr_priv *hsr, struct sk_buff *skb, 281 + struct hsr_frame_info *frame) 282 + { 283 + struct net_device *master_dev; 284 + 285 + master_dev = hsr_port_get_hsr(hsr, HSR_PT_MASTER)->dev; 286 + 287 + if (hsr_addr_is_self(hsr, eth_hdr(skb)->h_dest)) { 288 + frame->is_local_exclusive = true; 289 + skb->pkt_type = PACKET_HOST; 290 + } else { 291 + frame->is_local_exclusive = false; 292 + } 293 + 294 + if ((skb->pkt_type == PACKET_HOST) || 295 + (skb->pkt_type == PACKET_MULTICAST) || 296 + (skb->pkt_type == PACKET_BROADCAST)) { 297 + frame->is_local_dest = true; 298 + } else { 299 + frame->is_local_dest = false; 300 + } 301 + } 302 + 303 + 304 + static int hsr_fill_frame_info(struct hsr_frame_info *frame, 305 + struct sk_buff *skb, struct hsr_port *port) 306 + { 307 + struct ethhdr *ethhdr; 308 + unsigned long irqflags; 309 + 310 + frame->is_supervision = is_supervision_frame(port->hsr, skb); 311 + frame->node_src = hsr_get_node(&port->hsr->node_db, skb, 312 + frame->is_supervision); 313 + if (frame->node_src == NULL) 314 + return -1; /* Unknown node and !is_supervision, or no mem */ 315 + 316 + ethhdr = (struct ethhdr *) skb_mac_header(skb); 317 + frame->is_vlan = false; 318 + if (ethhdr->h_proto == htons(ETH_P_8021Q)) { 319 + frame->is_vlan = true; 320 + /* FIXME: */ 321 + WARN_ONCE(1, "HSR: VLAN not yet supported"); 322 + } 323 + if (ethhdr->h_proto == htons(ETH_P_PRP)) { 324 + frame->skb_std = NULL; 325 + frame->skb_hsr = skb; 326 + frame->sequence_nr = hsr_get_skb_sequence_nr(skb); 327 + } else { 328 + frame->skb_std = skb; 329 + frame->skb_hsr = NULL; 330 + /* Sequence nr for the master node */ 331 + spin_lock_irqsave(&port->hsr->seqnr_lock, irqflags); 332 + frame->sequence_nr = port->hsr->sequence_nr; 333 + port->hsr->sequence_nr++; 334 + spin_unlock_irqrestore(&port->hsr->seqnr_lock, irqflags); 335 + } 336 + 337 + frame->port_rcv = port; 338 + check_local_dest(port->hsr, skb, frame); 339 + 340 + return 0; 341 + } 342 + 343 + /* Must be called holding rcu read lock (because of the port parameter) */ 344 + void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port) 345 + { 346 + struct hsr_frame_info frame; 347 + 348 + if (skb_mac_header(skb) != skb->data) { 349 + WARN_ONCE(1, "%s:%d: Malformed frame (port_src %s)\n", 350 + __FILE__, __LINE__, port->dev->name); 351 + goto out_drop; 352 + } 353 + 354 + if (hsr_fill_frame_info(&frame, skb, port) < 0) 355 + goto out_drop; 356 + hsr_register_frame_in(frame.node_src, port, frame.sequence_nr); 357 + hsr_forward_do(&frame); 358 + 359 + if (frame.skb_hsr != NULL) 360 + kfree_skb(frame.skb_hsr); 361 + if (frame.skb_std != NULL) 362 + kfree_skb(frame.skb_std); 363 + return; 364 + 365 + out_drop: 366 + port->dev->stats.tx_dropped++; 367 + kfree_skb(skb); 368 + }
+20
net/hsr/hsr_forward.h
··· 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 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): 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 + */ 11 + 12 + #ifndef __HSR_FORWARD_H 13 + #define __HSR_FORWARD_H 14 + 15 + #include <linux/netdevice.h> 16 + #include "hsr_main.h" 17 + 18 + void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port); 19 + 20 + #endif /* __HSR_FORWARD_H */
+322 -327
net/hsr/hsr_framereg.c
··· 1 - /* Copyright 2011-2013 Autronica Fire and Security AS 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 2 2 * 3 3 * This program is free software; you can redistribute it and/or modify it 4 4 * under the terms of the GNU General Public License as published by the Free ··· 6 6 * any later version. 7 7 * 8 8 * Author(s): 9 - * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 10 * 11 11 * The HSR spec says never to forward the same frame twice on the same 12 12 * interface. A frame is identified by its source MAC address and its HSR ··· 23 23 #include "hsr_netlink.h" 24 24 25 25 26 - struct node_entry { 27 - struct list_head mac_list; 28 - unsigned char MacAddressA[ETH_ALEN]; 29 - unsigned char MacAddressB[ETH_ALEN]; 30 - enum hsr_dev_idx AddrB_if; /* The local slave through which AddrB 31 - * frames are received from this node 32 - */ 33 - unsigned long time_in[HSR_MAX_SLAVE]; 34 - bool time_in_stale[HSR_MAX_SLAVE]; 35 - u16 seq_out[HSR_MAX_DEV]; 36 - struct rcu_head rcu_head; 26 + struct hsr_node { 27 + struct list_head mac_list; 28 + unsigned char MacAddressA[ETH_ALEN]; 29 + unsigned char MacAddressB[ETH_ALEN]; 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]; 35 + struct rcu_head rcu_head; 37 36 }; 38 37 38 + 39 39 /* TODO: use hash lists for mac addresses (linux/jhash.h)? */ 40 - 41 - 42 - 43 - /* Search for mac entry. Caller must hold rcu read lock. 44 - */ 45 - static struct node_entry *find_node_by_AddrA(struct list_head *node_db, 46 - const unsigned char addr[ETH_ALEN]) 47 - { 48 - struct node_entry *node; 49 - 50 - list_for_each_entry_rcu(node, node_db, mac_list) { 51 - if (ether_addr_equal(node->MacAddressA, addr)) 52 - return node; 53 - } 54 - 55 - return NULL; 56 - } 57 - 58 - 59 - /* Search for mac entry. Caller must hold rcu read lock. 60 - */ 61 - static struct node_entry *find_node_by_AddrB(struct list_head *node_db, 62 - const unsigned char addr[ETH_ALEN]) 63 - { 64 - struct node_entry *node; 65 - 66 - list_for_each_entry_rcu(node, node_db, mac_list) { 67 - if (ether_addr_equal(node->MacAddressB, addr)) 68 - return node; 69 - } 70 - 71 - return NULL; 72 - } 73 - 74 - 75 - /* Search for mac entry. Caller must hold rcu read lock. 76 - */ 77 - struct node_entry *hsr_find_node(struct list_head *node_db, struct sk_buff *skb) 78 - { 79 - struct node_entry *node; 80 - struct ethhdr *ethhdr; 81 - 82 - if (!skb_mac_header_was_set(skb)) 83 - return NULL; 84 - 85 - ethhdr = (struct ethhdr *) skb_mac_header(skb); 86 - 87 - list_for_each_entry_rcu(node, node_db, mac_list) { 88 - if (ether_addr_equal(node->MacAddressA, ethhdr->h_source)) 89 - return node; 90 - if (ether_addr_equal(node->MacAddressB, ethhdr->h_source)) 91 - return node; 92 - } 93 - 94 - return NULL; 95 - } 96 - 97 - 98 - /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize 99 - * frames from self that's been looped over the HSR ring. 100 - */ 101 - int hsr_create_self_node(struct list_head *self_node_db, 102 - unsigned char addr_a[ETH_ALEN], 103 - unsigned char addr_b[ETH_ALEN]) 104 - { 105 - struct node_entry *node, *oldnode; 106 - 107 - node = kmalloc(sizeof(*node), GFP_KERNEL); 108 - if (!node) 109 - return -ENOMEM; 110 - 111 - ether_addr_copy(node->MacAddressA, addr_a); 112 - ether_addr_copy(node->MacAddressB, addr_b); 113 - 114 - rcu_read_lock(); 115 - oldnode = list_first_or_null_rcu(self_node_db, 116 - struct node_entry, mac_list); 117 - if (oldnode) { 118 - list_replace_rcu(&oldnode->mac_list, &node->mac_list); 119 - rcu_read_unlock(); 120 - synchronize_rcu(); 121 - kfree(oldnode); 122 - } else { 123 - rcu_read_unlock(); 124 - list_add_tail_rcu(&node->mac_list, self_node_db); 125 - } 126 - 127 - return 0; 128 - } 129 - 130 - 131 - /* Add/merge node to the database of nodes. 'skb' must contain an HSR 132 - * supervision frame. 133 - * - If the supervision header's MacAddressA field is not yet in the database, 134 - * this frame is from an hitherto unknown node - add it to the database. 135 - * - If the sender's MAC address is not the same as its MacAddressA address, 136 - * the node is using PICS_SUBS (address substitution). Record the sender's 137 - * address as the node's MacAddressB. 138 - * 139 - * This function needs to work even if the sender node has changed one of its 140 - * slaves' MAC addresses. In this case, there are four different cases described 141 - * by (Addr-changed, received-from) pairs as follows. Note that changing the 142 - * SlaveA address is equal to changing the node's own address: 143 - * 144 - * - (AddrB, SlaveB): The new AddrB will be recorded by PICS_SUBS code since 145 - * node == NULL. 146 - * - (AddrB, SlaveA): Will work as usual (the AddrB change won't be detected 147 - * from this frame). 148 - * 149 - * - (AddrA, SlaveB): The old node will be found. We need to detect this and 150 - * remove the node. 151 - * - (AddrA, SlaveA): A new node will be registered (non-PICS_SUBS at first). 152 - * The old one will be pruned after HSR_NODE_FORGET_TIME. 153 - * 154 - * We also need to detect if the sender's SlaveA and SlaveB cables have been 155 - * swapped. 156 - */ 157 - struct node_entry *hsr_merge_node(struct hsr_priv *hsr_priv, 158 - struct node_entry *node, 159 - struct sk_buff *skb, 160 - enum hsr_dev_idx dev_idx) 161 - { 162 - struct hsr_sup_payload *hsr_sp; 163 - struct hsr_ethhdr_sp *hsr_ethsup; 164 - int i; 165 - unsigned long now; 166 - 167 - hsr_ethsup = (struct hsr_ethhdr_sp *) skb_mac_header(skb); 168 - hsr_sp = (struct hsr_sup_payload *) skb->data; 169 - 170 - if (node && !ether_addr_equal(node->MacAddressA, hsr_sp->MacAddressA)) { 171 - /* Node has changed its AddrA, frame was received from SlaveB */ 172 - list_del_rcu(&node->mac_list); 173 - kfree_rcu(node, rcu_head); 174 - node = NULL; 175 - } 176 - 177 - if (node && (dev_idx == node->AddrB_if) && 178 - !ether_addr_equal(node->MacAddressB, hsr_ethsup->ethhdr.h_source)) { 179 - /* Cables have been swapped */ 180 - list_del_rcu(&node->mac_list); 181 - kfree_rcu(node, rcu_head); 182 - node = NULL; 183 - } 184 - 185 - if (node && (dev_idx != node->AddrB_if) && 186 - (node->AddrB_if != HSR_DEV_NONE) && 187 - !ether_addr_equal(node->MacAddressA, hsr_ethsup->ethhdr.h_source)) { 188 - /* Cables have been swapped */ 189 - list_del_rcu(&node->mac_list); 190 - kfree_rcu(node, rcu_head); 191 - node = NULL; 192 - } 193 - 194 - if (node) 195 - return node; 196 - 197 - node = find_node_by_AddrA(&hsr_priv->node_db, hsr_sp->MacAddressA); 198 - if (node) { 199 - /* Node is known, but frame was received from an unknown 200 - * address. Node is PICS_SUBS capable; merge its AddrB. 201 - */ 202 - ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source); 203 - node->AddrB_if = dev_idx; 204 - return node; 205 - } 206 - 207 - node = kzalloc(sizeof(*node), GFP_ATOMIC); 208 - if (!node) 209 - return NULL; 210 - 211 - ether_addr_copy(node->MacAddressA, hsr_sp->MacAddressA); 212 - ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source); 213 - if (!ether_addr_equal(hsr_sp->MacAddressA, hsr_ethsup->ethhdr.h_source)) 214 - node->AddrB_if = dev_idx; 215 - else 216 - node->AddrB_if = HSR_DEV_NONE; 217 - 218 - /* We are only interested in time diffs here, so use current jiffies 219 - * as initialization. (0 could trigger an spurious ring error warning). 220 - */ 221 - now = jiffies; 222 - for (i = 0; i < HSR_MAX_SLAVE; i++) 223 - node->time_in[i] = now; 224 - for (i = 0; i < HSR_MAX_DEV; i++) 225 - node->seq_out[i] = ntohs(hsr_ethsup->hsr_sup.sequence_nr) - 1; 226 - 227 - list_add_tail_rcu(&node->mac_list, &hsr_priv->node_db); 228 - 229 - return node; 230 - } 231 - 232 - 233 - /* 'skb' is a frame meant for this host, that is to be passed to upper layers. 234 - * 235 - * If the frame was sent by a node's B interface, replace the sender 236 - * address with that node's "official" address (MacAddressA) so that upper 237 - * layers recognize where it came from. 238 - */ 239 - void hsr_addr_subst_source(struct hsr_priv *hsr_priv, struct sk_buff *skb) 240 - { 241 - struct ethhdr *ethhdr; 242 - struct node_entry *node; 243 - 244 - if (!skb_mac_header_was_set(skb)) { 245 - WARN_ONCE(1, "%s: Mac header not set\n", __func__); 246 - return; 247 - } 248 - ethhdr = (struct ethhdr *) skb_mac_header(skb); 249 - 250 - rcu_read_lock(); 251 - node = find_node_by_AddrB(&hsr_priv->node_db, ethhdr->h_source); 252 - if (node) 253 - ether_addr_copy(ethhdr->h_source, node->MacAddressA); 254 - rcu_read_unlock(); 255 - } 256 - 257 - 258 - /* 'skb' is a frame meant for another host. 259 - * 'hsr_dev_idx' is the HSR index of the outgoing device 260 - * 261 - * Substitute the target (dest) MAC address if necessary, so the it matches the 262 - * recipient interface MAC address, regardless of whether that is the 263 - * recipient's A or B interface. 264 - * This is needed to keep the packets flowing through switches that learn on 265 - * which "side" the different interfaces are. 266 - */ 267 - void hsr_addr_subst_dest(struct hsr_priv *hsr_priv, struct ethhdr *ethhdr, 268 - enum hsr_dev_idx dev_idx) 269 - { 270 - struct node_entry *node; 271 - 272 - rcu_read_lock(); 273 - node = find_node_by_AddrA(&hsr_priv->node_db, ethhdr->h_dest); 274 - if (node && (node->AddrB_if == dev_idx)) 275 - ether_addr_copy(ethhdr->h_dest, node->MacAddressB); 276 - rcu_read_unlock(); 277 - } 278 40 279 41 280 42 /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b, ··· 57 295 #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b))) 58 296 59 297 60 - void hsr_register_frame_in(struct node_entry *node, enum hsr_dev_idx dev_idx) 298 + bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr) 61 299 { 62 - if ((dev_idx < 0) || (dev_idx >= HSR_MAX_SLAVE)) { 63 - WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx); 64 - return; 300 + struct hsr_node *node; 301 + 302 + node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node, 303 + mac_list); 304 + if (!node) { 305 + WARN_ONCE(1, "HSR: No self node\n"); 306 + return false; 65 307 } 66 - node->time_in[dev_idx] = jiffies; 67 - node->time_in_stale[dev_idx] = false; 308 + 309 + if (ether_addr_equal(addr, node->MacAddressA)) 310 + return true; 311 + if (ether_addr_equal(addr, node->MacAddressB)) 312 + return true; 313 + 314 + return false; 68 315 } 69 316 317 + /* Search for mac entry. Caller must hold rcu read lock. 318 + */ 319 + static struct hsr_node *find_node_by_AddrA(struct list_head *node_db, 320 + const unsigned char addr[ETH_ALEN]) 321 + { 322 + struct hsr_node *node; 323 + 324 + list_for_each_entry_rcu(node, node_db, mac_list) { 325 + if (ether_addr_equal(node->MacAddressA, addr)) 326 + return node; 327 + } 328 + 329 + return NULL; 330 + } 331 + 332 + 333 + /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize 334 + * frames from self that's been looped over the HSR ring. 335 + */ 336 + int hsr_create_self_node(struct list_head *self_node_db, 337 + unsigned char addr_a[ETH_ALEN], 338 + unsigned char addr_b[ETH_ALEN]) 339 + { 340 + struct hsr_node *node, *oldnode; 341 + 342 + node = kmalloc(sizeof(*node), GFP_KERNEL); 343 + if (!node) 344 + return -ENOMEM; 345 + 346 + ether_addr_copy(node->MacAddressA, addr_a); 347 + ether_addr_copy(node->MacAddressB, addr_b); 348 + 349 + rcu_read_lock(); 350 + oldnode = list_first_or_null_rcu(self_node_db, 351 + struct hsr_node, mac_list); 352 + if (oldnode) { 353 + list_replace_rcu(&oldnode->mac_list, &node->mac_list); 354 + rcu_read_unlock(); 355 + synchronize_rcu(); 356 + kfree(oldnode); 357 + } else { 358 + rcu_read_unlock(); 359 + list_add_tail_rcu(&node->mac_list, self_node_db); 360 + } 361 + 362 + return 0; 363 + } 364 + 365 + 366 + /* Allocate an hsr_node and add it to node_db. 'addr' is the node's AddressA; 367 + * seq_out is used to initialize filtering of outgoing duplicate frames 368 + * originating from the newly added node. 369 + */ 370 + struct hsr_node *hsr_add_node(struct list_head *node_db, unsigned char addr[], 371 + u16 seq_out) 372 + { 373 + struct hsr_node *node; 374 + unsigned long now; 375 + int i; 376 + 377 + node = kzalloc(sizeof(*node), GFP_ATOMIC); 378 + if (!node) 379 + return NULL; 380 + 381 + ether_addr_copy(node->MacAddressA, addr); 382 + 383 + /* We are only interested in time diffs here, so use current jiffies 384 + * as initialization. (0 could trigger an spurious ring error warning). 385 + */ 386 + now = jiffies; 387 + for (i = 0; i < HSR_PT_PORTS; i++) 388 + node->time_in[i] = now; 389 + for (i = 0; i < HSR_PT_PORTS; i++) 390 + node->seq_out[i] = seq_out; 391 + 392 + list_add_tail_rcu(&node->mac_list, node_db); 393 + 394 + return node; 395 + } 396 + 397 + /* Get the hsr_node from which 'skb' was sent. 398 + */ 399 + struct hsr_node *hsr_get_node(struct list_head *node_db, struct sk_buff *skb, 400 + bool is_sup) 401 + { 402 + struct hsr_node *node; 403 + struct ethhdr *ethhdr; 404 + u16 seq_out; 405 + 406 + if (!skb_mac_header_was_set(skb)) 407 + return NULL; 408 + 409 + ethhdr = (struct ethhdr *) skb_mac_header(skb); 410 + 411 + list_for_each_entry_rcu(node, node_db, mac_list) { 412 + if (ether_addr_equal(node->MacAddressA, ethhdr->h_source)) 413 + return node; 414 + if (ether_addr_equal(node->MacAddressB, ethhdr->h_source)) 415 + return node; 416 + } 417 + 418 + if (!is_sup) 419 + return NULL; /* Only supervision frame may create node entry */ 420 + 421 + if (ethhdr->h_proto == htons(ETH_P_PRP)) { 422 + /* Use the existing sequence_nr from the tag as starting point 423 + * for filtering duplicate frames. 424 + */ 425 + seq_out = hsr_get_skb_sequence_nr(skb) - 1; 426 + } else { 427 + WARN_ONCE(1, "%s: Non-HSR frame\n", __func__); 428 + seq_out = 0; 429 + } 430 + 431 + return hsr_add_node(node_db, ethhdr->h_source, seq_out); 432 + } 433 + 434 + /* Use the Supervision frame's info about an eventual MacAddressB for merging 435 + * nodes that has previously had their MacAddressB registered as a separate 436 + * node. 437 + */ 438 + void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr, 439 + struct hsr_port *port_rcv) 440 + { 441 + struct hsr_node *node_real; 442 + struct hsr_sup_payload *hsr_sp; 443 + struct list_head *node_db; 444 + int i; 445 + 446 + skb_pull(skb, sizeof(struct hsr_ethhdr_sp)); 447 + hsr_sp = (struct hsr_sup_payload *) skb->data; 448 + 449 + if (ether_addr_equal(eth_hdr(skb)->h_source, hsr_sp->MacAddressA)) 450 + /* Not sent from MacAddressB of a PICS_SUBS capable node */ 451 + goto done; 452 + 453 + /* Merge node_curr (registered on MacAddressB) into node_real */ 454 + node_db = &port_rcv->hsr->node_db; 455 + node_real = find_node_by_AddrA(node_db, hsr_sp->MacAddressA); 456 + if (!node_real) 457 + /* No frame received from AddrA of this node yet */ 458 + node_real = hsr_add_node(node_db, hsr_sp->MacAddressA, 459 + HSR_SEQNR_START - 1); 460 + if (!node_real) 461 + goto done; /* No mem */ 462 + if (node_real == node_curr) 463 + /* Node has already been merged */ 464 + goto done; 465 + 466 + ether_addr_copy(node_real->MacAddressB, eth_hdr(skb)->h_source); 467 + for (i = 0; i < HSR_PT_PORTS; i++) { 468 + if (!node_curr->time_in_stale[i] && 469 + time_after(node_curr->time_in[i], node_real->time_in[i])) { 470 + node_real->time_in[i] = node_curr->time_in[i]; 471 + node_real->time_in_stale[i] = node_curr->time_in_stale[i]; 472 + } 473 + if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i])) 474 + node_real->seq_out[i] = node_curr->seq_out[i]; 475 + } 476 + node_real->AddrB_port = port_rcv->type; 477 + 478 + list_del_rcu(&node_curr->mac_list); 479 + kfree_rcu(node_curr, rcu_head); 480 + 481 + done: 482 + skb_push(skb, sizeof(struct hsr_ethhdr_sp)); 483 + } 484 + 485 + 486 + /* 'skb' is a frame meant for this host, that is to be passed to upper layers. 487 + * 488 + * If the frame was sent by a node's B interface, replace the source 489 + * address with that node's "official" address (MacAddressA) so that upper 490 + * layers recognize where it came from. 491 + */ 492 + void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb) 493 + { 494 + if (!skb_mac_header_was_set(skb)) { 495 + WARN_ONCE(1, "%s: Mac header not set\n", __func__); 496 + return; 497 + } 498 + 499 + memcpy(&eth_hdr(skb)->h_source, node->MacAddressA, ETH_ALEN); 500 + } 501 + 502 + /* 'skb' is a frame meant for another host. 503 + * 'port' is the outgoing interface 504 + * 505 + * Substitute the target (dest) MAC address if necessary, so the it matches the 506 + * recipient interface MAC address, regardless of whether that is the 507 + * recipient's A or B interface. 508 + * This is needed to keep the packets flowing through switches that learn on 509 + * which "side" the different interfaces are. 510 + */ 511 + void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb, 512 + struct hsr_port *port) 513 + { 514 + struct hsr_node *node_dst; 515 + 516 + if (!skb_mac_header_was_set(skb)) { 517 + WARN_ONCE(1, "%s: Mac header not set\n", __func__); 518 + return; 519 + } 520 + 521 + if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest)) 522 + return; 523 + 524 + node_dst = find_node_by_AddrA(&port->hsr->node_db, eth_hdr(skb)->h_dest); 525 + if (!node_dst) { 526 + WARN_ONCE(1, "%s: Unknown node\n", __func__); 527 + return; 528 + } 529 + if (port->type != node_dst->AddrB_port) 530 + return; 531 + if (!node_dst->MacAddressB) { 532 + WARN_ONCE(1, "%s: No MacAddressB\n", __func__); 533 + return; 534 + } 535 + 536 + ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->MacAddressB); 537 + } 538 + 539 + 540 + void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port, 541 + u16 sequence_nr) 542 + { 543 + /* Don't register incoming frames without a valid sequence number. This 544 + * ensures entries of restarted nodes gets pruned so that they can 545 + * re-register and resume communications. 546 + */ 547 + if (seq_nr_before(sequence_nr, node->seq_out[port->type])) 548 + return; 549 + 550 + node->time_in[port->type] = jiffies; 551 + node->time_in_stale[port->type] = false; 552 + } 70 553 71 554 /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid 72 555 * ethhdr->h_source address and skb->mac_header set. ··· 321 314 * 0 otherwise, or 322 315 * negative error code on error 323 316 */ 324 - int hsr_register_frame_out(struct node_entry *node, enum hsr_dev_idx dev_idx, 325 - struct sk_buff *skb) 317 + int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node, 318 + u16 sequence_nr) 326 319 { 327 - struct hsr_ethhdr *hsr_ethhdr; 328 - u16 sequence_nr; 329 - 330 - if ((dev_idx < 0) || (dev_idx >= HSR_MAX_DEV)) { 331 - WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx); 332 - return -EINVAL; 333 - } 334 - if (!skb_mac_header_was_set(skb)) { 335 - WARN_ONCE(1, "%s: Mac header not set\n", __func__); 336 - return -EINVAL; 337 - } 338 - hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb); 339 - 340 - sequence_nr = ntohs(hsr_ethhdr->hsr_tag.sequence_nr); 341 - if (seq_nr_before_or_eq(sequence_nr, node->seq_out[dev_idx])) 320 + if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type])) 342 321 return 1; 343 322 344 - node->seq_out[dev_idx] = sequence_nr; 323 + node->seq_out[port->type] = sequence_nr; 345 324 return 0; 346 325 } 347 326 348 327 349 - 350 - static bool is_late(struct node_entry *node, enum hsr_dev_idx dev_idx) 328 + static struct hsr_port *get_late_port(struct hsr_priv *hsr, 329 + struct hsr_node *node) 351 330 { 352 - enum hsr_dev_idx other; 331 + if (node->time_in_stale[HSR_PT_SLAVE_A]) 332 + return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); 333 + if (node->time_in_stale[HSR_PT_SLAVE_B]) 334 + return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 353 335 354 - if (node->time_in_stale[dev_idx]) 355 - return true; 336 + if (time_after(node->time_in[HSR_PT_SLAVE_B], 337 + node->time_in[HSR_PT_SLAVE_A] + 338 + msecs_to_jiffies(MAX_SLAVE_DIFF))) 339 + return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); 340 + if (time_after(node->time_in[HSR_PT_SLAVE_A], 341 + node->time_in[HSR_PT_SLAVE_B] + 342 + msecs_to_jiffies(MAX_SLAVE_DIFF))) 343 + return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 356 344 357 - if (dev_idx == HSR_DEV_SLAVE_A) 358 - other = HSR_DEV_SLAVE_B; 359 - else 360 - other = HSR_DEV_SLAVE_A; 361 - 362 - if (node->time_in_stale[other]) 363 - return false; 364 - 365 - if (time_after(node->time_in[other], node->time_in[dev_idx] + 366 - msecs_to_jiffies(MAX_SLAVE_DIFF))) 367 - return true; 368 - 369 - return false; 345 + return NULL; 370 346 } 371 347 372 348 373 349 /* Remove stale sequence_nr records. Called by timer every 374 350 * HSR_LIFE_CHECK_INTERVAL (two seconds or so). 375 351 */ 376 - void hsr_prune_nodes(struct hsr_priv *hsr_priv) 352 + void hsr_prune_nodes(unsigned long data) 377 353 { 378 - struct node_entry *node; 354 + struct hsr_priv *hsr; 355 + struct hsr_node *node; 356 + struct hsr_port *port; 379 357 unsigned long timestamp; 380 358 unsigned long time_a, time_b; 381 359 360 + hsr = (struct hsr_priv *) data; 361 + 382 362 rcu_read_lock(); 383 - list_for_each_entry_rcu(node, &hsr_priv->node_db, mac_list) { 363 + list_for_each_entry_rcu(node, &hsr->node_db, mac_list) { 384 364 /* Shorthand */ 385 - time_a = node->time_in[HSR_DEV_SLAVE_A]; 386 - time_b = node->time_in[HSR_DEV_SLAVE_B]; 365 + time_a = node->time_in[HSR_PT_SLAVE_A]; 366 + time_b = node->time_in[HSR_PT_SLAVE_B]; 387 367 388 368 /* Check for timestamps old enough to risk wrap-around */ 389 369 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2)) 390 - node->time_in_stale[HSR_DEV_SLAVE_A] = true; 370 + node->time_in_stale[HSR_PT_SLAVE_A] = true; 391 371 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2)) 392 - node->time_in_stale[HSR_DEV_SLAVE_B] = true; 372 + node->time_in_stale[HSR_PT_SLAVE_B] = true; 393 373 394 374 /* Get age of newest frame from node. 395 375 * At least one time_in is OK here; nodes get pruned long 396 376 * before both time_ins can get stale 397 377 */ 398 378 timestamp = time_a; 399 - if (node->time_in_stale[HSR_DEV_SLAVE_A] || 400 - (!node->time_in_stale[HSR_DEV_SLAVE_B] && 379 + if (node->time_in_stale[HSR_PT_SLAVE_A] || 380 + (!node->time_in_stale[HSR_PT_SLAVE_B] && 401 381 time_after(time_b, time_a))) 402 382 timestamp = time_b; 403 383 404 384 /* Warn of ring error only as long as we get frames at all */ 405 385 if (time_is_after_jiffies(timestamp + 406 386 msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) { 407 - 408 - if (is_late(node, HSR_DEV_SLAVE_A)) 409 - hsr_nl_ringerror(hsr_priv, node->MacAddressA, 410 - HSR_DEV_SLAVE_A); 411 - else if (is_late(node, HSR_DEV_SLAVE_B)) 412 - hsr_nl_ringerror(hsr_priv, node->MacAddressA, 413 - HSR_DEV_SLAVE_B); 387 + rcu_read_lock(); 388 + port = get_late_port(hsr, node); 389 + if (port != NULL) 390 + hsr_nl_ringerror(hsr, node->MacAddressA, port); 391 + rcu_read_unlock(); 414 392 } 415 393 416 394 /* Prune old entries */ 417 395 if (time_is_before_jiffies(timestamp + 418 396 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) { 419 - hsr_nl_nodedown(hsr_priv, node->MacAddressA); 397 + hsr_nl_nodedown(hsr, node->MacAddressA); 420 398 list_del_rcu(&node->mac_list); 421 399 /* Note that we need to free this entry later: */ 422 400 kfree_rcu(node, rcu_head); ··· 411 419 } 412 420 413 421 414 - void *hsr_get_next_node(struct hsr_priv *hsr_priv, void *_pos, 422 + void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos, 415 423 unsigned char addr[ETH_ALEN]) 416 424 { 417 - struct node_entry *node; 425 + struct hsr_node *node; 418 426 419 427 if (!_pos) { 420 - node = list_first_or_null_rcu(&hsr_priv->node_db, 421 - struct node_entry, mac_list); 428 + node = list_first_or_null_rcu(&hsr->node_db, 429 + struct hsr_node, mac_list); 422 430 if (node) 423 431 ether_addr_copy(addr, node->MacAddressA); 424 432 return node; 425 433 } 426 434 427 435 node = _pos; 428 - list_for_each_entry_continue_rcu(node, &hsr_priv->node_db, mac_list) { 436 + list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) { 429 437 ether_addr_copy(addr, node->MacAddressA); 430 438 return node; 431 439 } ··· 434 442 } 435 443 436 444 437 - int hsr_get_node_data(struct hsr_priv *hsr_priv, 445 + int hsr_get_node_data(struct hsr_priv *hsr, 438 446 const unsigned char *addr, 439 447 unsigned char addr_b[ETH_ALEN], 440 448 unsigned int *addr_b_ifindex, ··· 443 451 int *if2_age, 444 452 u16 *if2_seq) 445 453 { 446 - struct node_entry *node; 454 + struct hsr_node *node; 455 + struct hsr_port *port; 447 456 unsigned long tdiff; 448 457 449 458 450 459 rcu_read_lock(); 451 - node = find_node_by_AddrA(&hsr_priv->node_db, addr); 460 + node = find_node_by_AddrA(&hsr->node_db, addr); 452 461 if (!node) { 453 462 rcu_read_unlock(); 454 463 return -ENOENT; /* No such entry */ ··· 457 464 458 465 ether_addr_copy(addr_b, node->MacAddressB); 459 466 460 - tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_A]; 461 - if (node->time_in_stale[HSR_DEV_SLAVE_A]) 467 + tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A]; 468 + if (node->time_in_stale[HSR_PT_SLAVE_A]) 462 469 *if1_age = INT_MAX; 463 470 #if HZ <= MSEC_PER_SEC 464 471 else if (tdiff > msecs_to_jiffies(INT_MAX)) ··· 467 474 else 468 475 *if1_age = jiffies_to_msecs(tdiff); 469 476 470 - tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_B]; 471 - if (node->time_in_stale[HSR_DEV_SLAVE_B]) 477 + tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B]; 478 + if (node->time_in_stale[HSR_PT_SLAVE_B]) 472 479 *if2_age = INT_MAX; 473 480 #if HZ <= MSEC_PER_SEC 474 481 else if (tdiff > msecs_to_jiffies(INT_MAX)) ··· 478 485 *if2_age = jiffies_to_msecs(tdiff); 479 486 480 487 /* Present sequence numbers as if they were incoming on interface */ 481 - *if1_seq = node->seq_out[HSR_DEV_SLAVE_B]; 482 - *if2_seq = node->seq_out[HSR_DEV_SLAVE_A]; 488 + *if1_seq = node->seq_out[HSR_PT_SLAVE_B]; 489 + *if2_seq = node->seq_out[HSR_PT_SLAVE_A]; 483 490 484 - if ((node->AddrB_if != HSR_DEV_NONE) && hsr_priv->slave[node->AddrB_if]) 485 - *addr_b_ifindex = hsr_priv->slave[node->AddrB_if]->ifindex; 486 - else 491 + if (node->AddrB_port != HSR_PT_NONE) { 492 + port = hsr_port_get_hsr(hsr, node->AddrB_port); 493 + *addr_b_ifindex = port->dev->ifindex; 494 + } else { 487 495 *addr_b_ifindex = -1; 496 + } 488 497 489 498 rcu_read_unlock(); 490 499
+23 -22
net/hsr/hsr_framereg.h
··· 1 - /* Copyright 2011-2013 Autronica Fire and Security AS 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 2 2 * 3 3 * This program is free software; you can redistribute it and/or modify it 4 4 * under the terms of the GNU General Public License as published by the Free ··· 6 6 * any later version. 7 7 * 8 8 * Author(s): 9 - * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 10 */ 11 11 12 - #ifndef _HSR_FRAMEREG_H 13 - #define _HSR_FRAMEREG_H 12 + #ifndef __HSR_FRAMEREG_H 13 + #define __HSR_FRAMEREG_H 14 14 15 15 #include "hsr_main.h" 16 16 17 - struct node_entry; 17 + struct hsr_node; 18 18 19 - struct node_entry *hsr_find_node(struct list_head *node_db, struct sk_buff *skb); 19 + struct hsr_node *hsr_add_node(struct list_head *node_db, unsigned char addr[], 20 + u16 seq_out); 21 + struct hsr_node *hsr_get_node(struct list_head *node_db, struct sk_buff *skb, 22 + bool is_sup); 23 + void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr, 24 + struct hsr_port *port); 25 + bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr); 20 26 21 - struct node_entry *hsr_merge_node(struct hsr_priv *hsr_priv, 22 - struct node_entry *node, 23 - struct sk_buff *skb, 24 - enum hsr_dev_idx dev_idx); 27 + void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb); 28 + void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb, 29 + struct hsr_port *port); 25 30 26 - void hsr_addr_subst_source(struct hsr_priv *hsr_priv, struct sk_buff *skb); 27 - void hsr_addr_subst_dest(struct hsr_priv *hsr_priv, struct ethhdr *ethhdr, 28 - enum hsr_dev_idx dev_idx); 31 + void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port, 32 + u16 sequence_nr); 33 + int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node, 34 + u16 sequence_nr); 29 35 30 - void hsr_register_frame_in(struct node_entry *node, enum hsr_dev_idx dev_idx); 31 - 32 - int hsr_register_frame_out(struct node_entry *node, enum hsr_dev_idx dev_idx, 33 - struct sk_buff *skb); 34 - 35 - void hsr_prune_nodes(struct hsr_priv *hsr_priv); 36 + void hsr_prune_nodes(unsigned long data); 36 37 37 38 int hsr_create_self_node(struct list_head *self_node_db, 38 39 unsigned char addr_a[ETH_ALEN], 39 40 unsigned char addr_b[ETH_ALEN]); 40 41 41 - void *hsr_get_next_node(struct hsr_priv *hsr_priv, void *_pos, 42 + void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos, 42 43 unsigned char addr[ETH_ALEN]); 43 44 44 - int hsr_get_node_data(struct hsr_priv *hsr_priv, 45 + int hsr_get_node_data(struct hsr_priv *hsr, 45 46 const unsigned char *addr, 46 47 unsigned char addr_b[ETH_ALEN], 47 48 unsigned int *addr_b_ifindex, ··· 51 50 int *if2_age, 52 51 u16 *if2_seq); 53 52 54 - #endif /* _HSR_FRAMEREG_H */ 53 + #endif /* __HSR_FRAMEREG_H */
+44 -381
net/hsr/hsr_main.c
··· 1 - /* Copyright 2011-2013 Autronica Fire and Security AS 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 2 2 * 3 3 * This program is free software; you can redistribute it and/or modify it 4 4 * under the terms of the GNU General Public License as published by the Free ··· 6 6 * any later version. 7 7 * 8 8 * Author(s): 9 - * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com 10 - * 11 - * In addition to routines for registering and unregistering HSR support, this 12 - * file also contains the receive routine that handles all incoming frames with 13 - * Ethertype (protocol) ETH_P_PRP (HSRv0), and network device event handling. 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 14 10 */ 15 11 16 12 #include <linux/netdevice.h> ··· 17 21 #include "hsr_device.h" 18 22 #include "hsr_netlink.h" 19 23 #include "hsr_framereg.h" 20 - 21 - 22 - /* List of all registered virtual HSR devices */ 23 - static LIST_HEAD(hsr_list); 24 - 25 - void register_hsr_master(struct hsr_priv *hsr_priv) 26 - { 27 - list_add_tail_rcu(&hsr_priv->hsr_list, &hsr_list); 28 - } 29 - 30 - void unregister_hsr_master(struct hsr_priv *hsr_priv) 31 - { 32 - struct hsr_priv *hsr_priv_it; 33 - 34 - list_for_each_entry(hsr_priv_it, &hsr_list, hsr_list) 35 - if (hsr_priv_it == hsr_priv) { 36 - list_del_rcu(&hsr_priv_it->hsr_list); 37 - return; 38 - } 39 - } 40 - 41 - bool is_hsr_slave(struct net_device *dev) 42 - { 43 - struct hsr_priv *hsr_priv_it; 44 - 45 - list_for_each_entry_rcu(hsr_priv_it, &hsr_list, hsr_list) { 46 - if (dev == hsr_priv_it->slave[0]) 47 - return true; 48 - if (dev == hsr_priv_it->slave[1]) 49 - return true; 50 - } 51 - 52 - return false; 53 - } 54 - 55 - 56 - /* If dev is a HSR slave device, return the virtual master device. Return NULL 57 - * otherwise. 58 - */ 59 - static struct hsr_priv *get_hsr_master(struct net_device *dev) 60 - { 61 - struct hsr_priv *hsr_priv; 62 - 63 - rcu_read_lock(); 64 - list_for_each_entry_rcu(hsr_priv, &hsr_list, hsr_list) 65 - if ((dev == hsr_priv->slave[0]) || 66 - (dev == hsr_priv->slave[1])) { 67 - rcu_read_unlock(); 68 - return hsr_priv; 69 - } 70 - 71 - rcu_read_unlock(); 72 - return NULL; 73 - } 74 - 75 - 76 - /* If dev is a HSR slave device, return the other slave device. Return NULL 77 - * otherwise. 78 - */ 79 - static struct net_device *get_other_slave(struct hsr_priv *hsr_priv, 80 - struct net_device *dev) 81 - { 82 - if (dev == hsr_priv->slave[0]) 83 - return hsr_priv->slave[1]; 84 - if (dev == hsr_priv->slave[1]) 85 - return hsr_priv->slave[0]; 86 - 87 - return NULL; 88 - } 24 + #include "hsr_slave.h" 89 25 90 26 91 27 static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event, 92 28 void *ptr) 93 29 { 94 - struct net_device *slave, *other_slave; 95 - struct hsr_priv *hsr_priv; 96 - int old_operstate; 30 + struct net_device *dev; 31 + struct hsr_port *port, *master; 32 + struct hsr_priv *hsr; 97 33 int mtu_max; 98 34 int res; 99 - struct net_device *dev; 100 35 101 36 dev = netdev_notifier_info_to_dev(ptr); 102 - 103 - hsr_priv = get_hsr_master(dev); 104 - if (hsr_priv) { 105 - /* dev is a slave device */ 106 - slave = dev; 107 - other_slave = get_other_slave(hsr_priv, slave); 108 - } else { 37 + port = hsr_port_get_rtnl(dev); 38 + if (port == NULL) { 109 39 if (!is_hsr_master(dev)) 110 - return NOTIFY_DONE; 111 - hsr_priv = netdev_priv(dev); 112 - slave = hsr_priv->slave[0]; 113 - other_slave = hsr_priv->slave[1]; 40 + return NOTIFY_DONE; /* Not an HSR device */ 41 + hsr = netdev_priv(dev); 42 + port = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 43 + } else { 44 + hsr = port->hsr; 114 45 } 115 46 116 47 switch (event) { 117 48 case NETDEV_UP: /* Administrative state DOWN */ 118 49 case NETDEV_DOWN: /* Administrative state UP */ 119 50 case NETDEV_CHANGE: /* Link (carrier) state changes */ 120 - old_operstate = hsr_priv->dev->operstate; 121 - hsr_set_carrier(hsr_priv->dev, slave, other_slave); 122 - /* netif_stacked_transfer_operstate() cannot be used here since 123 - * it doesn't set IF_OPER_LOWERLAYERDOWN (?) 124 - */ 125 - hsr_set_operstate(hsr_priv->dev, slave, other_slave); 126 - hsr_check_announce(hsr_priv->dev, old_operstate); 51 + hsr_check_carrier_and_operstate(hsr); 127 52 break; 128 53 case NETDEV_CHANGEADDR: 129 - 130 - /* This should not happen since there's no ndo_set_mac_address() 131 - * for HSR devices - i.e. not supported. 132 - */ 133 - if (dev == hsr_priv->dev) 54 + if (port->type == HSR_PT_MASTER) { 55 + /* This should not happen since there's no 56 + * ndo_set_mac_address() for HSR devices - i.e. not 57 + * supported. 58 + */ 134 59 break; 60 + } 135 61 136 - if (dev == hsr_priv->slave[0]) 137 - ether_addr_copy(hsr_priv->dev->dev_addr, 138 - hsr_priv->slave[0]->dev_addr); 62 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 63 + 64 + if (port->type == HSR_PT_SLAVE_A) { 65 + ether_addr_copy(master->dev->dev_addr, dev->dev_addr); 66 + call_netdevice_notifiers(NETDEV_CHANGEADDR, master->dev); 67 + } 139 68 140 69 /* Make sure we recognize frames from ourselves in hsr_rcv() */ 141 - res = hsr_create_self_node(&hsr_priv->self_node_db, 142 - hsr_priv->dev->dev_addr, 143 - hsr_priv->slave[1] ? 144 - hsr_priv->slave[1]->dev_addr : 145 - hsr_priv->dev->dev_addr); 70 + port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 71 + res = hsr_create_self_node(&hsr->self_node_db, 72 + master->dev->dev_addr, 73 + port ? 74 + port->dev->dev_addr : 75 + master->dev->dev_addr); 146 76 if (res) 147 - netdev_warn(hsr_priv->dev, 77 + netdev_warn(master->dev, 148 78 "Could not update HSR node address.\n"); 149 - 150 - if (dev == hsr_priv->slave[0]) 151 - call_netdevice_notifiers(NETDEV_CHANGEADDR, hsr_priv->dev); 152 79 break; 153 80 case NETDEV_CHANGEMTU: 154 - if (dev == hsr_priv->dev) 81 + if (port->type == HSR_PT_MASTER) 155 82 break; /* Handled in ndo_change_mtu() */ 156 - mtu_max = hsr_get_max_mtu(hsr_priv); 157 - if (hsr_priv->dev->mtu > mtu_max) 158 - dev_set_mtu(hsr_priv->dev, mtu_max); 83 + mtu_max = hsr_get_max_mtu(port->hsr); 84 + master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER); 85 + master->dev->mtu = mtu_max; 159 86 break; 160 87 case NETDEV_UNREGISTER: 161 - if (dev == hsr_priv->slave[0]) 162 - hsr_priv->slave[0] = NULL; 163 - if (dev == hsr_priv->slave[1]) 164 - hsr_priv->slave[1] = NULL; 165 - 166 - /* There should really be a way to set a new slave device... */ 167 - 88 + hsr_del_port(port); 168 89 break; 169 90 case NETDEV_PRE_TYPE_CHANGE: 170 91 /* HSR works only on Ethernet devices. Refuse slave to change ··· 94 181 } 95 182 96 183 97 - static struct timer_list prune_timer; 98 - 99 - static void prune_nodes_all(unsigned long data) 184 + struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt) 100 185 { 101 - struct hsr_priv *hsr_priv; 186 + struct hsr_port *port; 102 187 103 - rcu_read_lock(); 104 - list_for_each_entry_rcu(hsr_priv, &hsr_list, hsr_list) 105 - hsr_prune_nodes(hsr_priv); 106 - rcu_read_unlock(); 107 - 108 - prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD); 109 - add_timer(&prune_timer); 110 - } 111 - 112 - 113 - static struct sk_buff *hsr_pull_tag(struct sk_buff *skb) 114 - { 115 - struct hsr_tag *hsr_tag; 116 - struct sk_buff *skb2; 117 - 118 - skb2 = skb_share_check(skb, GFP_ATOMIC); 119 - if (unlikely(!skb2)) 120 - goto err_free; 121 - skb = skb2; 122 - 123 - if (unlikely(!pskb_may_pull(skb, HSR_TAGLEN))) 124 - goto err_free; 125 - 126 - hsr_tag = (struct hsr_tag *) skb->data; 127 - skb->protocol = hsr_tag->encap_proto; 128 - skb_pull(skb, HSR_TAGLEN); 129 - 130 - return skb; 131 - 132 - err_free: 133 - kfree_skb(skb); 188 + hsr_for_each_port(hsr, port) 189 + if (port->type == pt) 190 + return port; 134 191 return NULL; 135 192 } 136 - 137 - 138 - /* The uses I can see for these HSR supervision frames are: 139 - * 1) Use the frames that are sent after node initialization ("HSR_TLV.Type = 140 - * 22") to reset any sequence_nr counters belonging to that node. Useful if 141 - * the other node's counter has been reset for some reason. 142 - * -- 143 - * Or not - resetting the counter and bridging the frame would create a 144 - * loop, unfortunately. 145 - * 146 - * 2) Use the LifeCheck frames to detect ring breaks. I.e. if no LifeCheck 147 - * frame is received from a particular node, we know something is wrong. 148 - * We just register these (as with normal frames) and throw them away. 149 - * 150 - * 3) Allow different MAC addresses for the two slave interfaces, using the 151 - * MacAddressA field. 152 - */ 153 - static bool is_supervision_frame(struct hsr_priv *hsr_priv, struct sk_buff *skb) 154 - { 155 - struct hsr_sup_tag *hsr_stag; 156 - 157 - if (!ether_addr_equal(eth_hdr(skb)->h_dest, 158 - hsr_priv->sup_multicast_addr)) 159 - return false; 160 - 161 - hsr_stag = (struct hsr_sup_tag *) skb->data; 162 - if (get_hsr_stag_path(hsr_stag) != 0x0f) 163 - return false; 164 - if ((hsr_stag->HSR_TLV_Type != HSR_TLV_ANNOUNCE) && 165 - (hsr_stag->HSR_TLV_Type != HSR_TLV_LIFE_CHECK)) 166 - return false; 167 - if (hsr_stag->HSR_TLV_Length != 12) 168 - return false; 169 - 170 - return true; 171 - } 172 - 173 - 174 - /* Implementation somewhat according to IEC-62439-3, p. 43 175 - */ 176 - static int hsr_rcv(struct sk_buff *skb, struct net_device *dev, 177 - struct packet_type *pt, struct net_device *orig_dev) 178 - { 179 - struct hsr_priv *hsr_priv; 180 - struct net_device *other_slave; 181 - struct node_entry *node; 182 - bool deliver_to_self; 183 - struct sk_buff *skb_deliver; 184 - enum hsr_dev_idx dev_in_idx, dev_other_idx; 185 - bool dup_out; 186 - int ret; 187 - 188 - hsr_priv = get_hsr_master(dev); 189 - 190 - if (!hsr_priv) { 191 - /* Non-HSR-slave device 'dev' is connected to a HSR network */ 192 - kfree_skb(skb); 193 - dev->stats.rx_errors++; 194 - return NET_RX_SUCCESS; 195 - } 196 - 197 - if (dev == hsr_priv->slave[0]) { 198 - dev_in_idx = HSR_DEV_SLAVE_A; 199 - dev_other_idx = HSR_DEV_SLAVE_B; 200 - } else { 201 - dev_in_idx = HSR_DEV_SLAVE_B; 202 - dev_other_idx = HSR_DEV_SLAVE_A; 203 - } 204 - 205 - node = hsr_find_node(&hsr_priv->self_node_db, skb); 206 - if (node) { 207 - /* Always kill frames sent by ourselves */ 208 - kfree_skb(skb); 209 - return NET_RX_SUCCESS; 210 - } 211 - 212 - /* Is this frame a candidate for local reception? */ 213 - deliver_to_self = false; 214 - if ((skb->pkt_type == PACKET_HOST) || 215 - (skb->pkt_type == PACKET_MULTICAST) || 216 - (skb->pkt_type == PACKET_BROADCAST)) 217 - deliver_to_self = true; 218 - else if (ether_addr_equal(eth_hdr(skb)->h_dest, 219 - hsr_priv->dev->dev_addr)) { 220 - skb->pkt_type = PACKET_HOST; 221 - deliver_to_self = true; 222 - } 223 - 224 - 225 - rcu_read_lock(); /* node_db */ 226 - node = hsr_find_node(&hsr_priv->node_db, skb); 227 - 228 - if (is_supervision_frame(hsr_priv, skb)) { 229 - skb_pull(skb, sizeof(struct hsr_sup_tag)); 230 - node = hsr_merge_node(hsr_priv, node, skb, dev_in_idx); 231 - if (!node) { 232 - rcu_read_unlock(); /* node_db */ 233 - kfree_skb(skb); 234 - hsr_priv->dev->stats.rx_dropped++; 235 - return NET_RX_DROP; 236 - } 237 - skb_push(skb, sizeof(struct hsr_sup_tag)); 238 - deliver_to_self = false; 239 - } 240 - 241 - if (!node) { 242 - /* Source node unknown; this might be a HSR frame from 243 - * another net (different multicast address). Ignore it. 244 - */ 245 - rcu_read_unlock(); /* node_db */ 246 - kfree_skb(skb); 247 - return NET_RX_SUCCESS; 248 - } 249 - 250 - /* Register ALL incoming frames as outgoing through the other interface. 251 - * This allows us to register frames as incoming only if they are valid 252 - * for the receiving interface, without using a specific counter for 253 - * incoming frames. 254 - */ 255 - dup_out = hsr_register_frame_out(node, dev_other_idx, skb); 256 - if (!dup_out) 257 - hsr_register_frame_in(node, dev_in_idx); 258 - 259 - /* Forward this frame? */ 260 - if (!dup_out && (skb->pkt_type != PACKET_HOST)) 261 - other_slave = get_other_slave(hsr_priv, dev); 262 - else 263 - other_slave = NULL; 264 - 265 - if (hsr_register_frame_out(node, HSR_DEV_MASTER, skb)) 266 - deliver_to_self = false; 267 - 268 - rcu_read_unlock(); /* node_db */ 269 - 270 - if (!deliver_to_self && !other_slave) { 271 - kfree_skb(skb); 272 - /* Circulated frame; silently remove it. */ 273 - return NET_RX_SUCCESS; 274 - } 275 - 276 - skb_deliver = skb; 277 - if (deliver_to_self && other_slave) { 278 - /* skb_clone() is not enough since we will strip the hsr tag 279 - * and do address substitution below 280 - */ 281 - skb_deliver = pskb_copy(skb, GFP_ATOMIC); 282 - if (!skb_deliver) { 283 - deliver_to_self = false; 284 - hsr_priv->dev->stats.rx_dropped++; 285 - } 286 - } 287 - 288 - if (deliver_to_self) { 289 - bool multicast_frame; 290 - 291 - skb_deliver = hsr_pull_tag(skb_deliver); 292 - if (!skb_deliver) { 293 - hsr_priv->dev->stats.rx_dropped++; 294 - goto forward; 295 - } 296 - #if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 297 - /* Move everything in the header that is after the HSR tag, 298 - * to work around alignment problems caused by the 6-byte HSR 299 - * tag. In practice, this removes/overwrites the HSR tag in 300 - * the header and restores a "standard" packet. 301 - */ 302 - memmove(skb_deliver->data - HSR_TAGLEN, skb_deliver->data, 303 - skb_headlen(skb_deliver)); 304 - 305 - /* Adjust skb members so they correspond with the move above. 306 - * This cannot possibly underflow skb->data since hsr_pull_tag() 307 - * above succeeded. 308 - * At this point in the protocol stack, the transport and 309 - * network headers have not been set yet, and we haven't touched 310 - * the mac header nor the head. So we only need to adjust data 311 - * and tail: 312 - */ 313 - skb_deliver->data -= HSR_TAGLEN; 314 - skb_deliver->tail -= HSR_TAGLEN; 315 - #endif 316 - skb_deliver->dev = hsr_priv->dev; 317 - hsr_addr_subst_source(hsr_priv, skb_deliver); 318 - multicast_frame = (skb_deliver->pkt_type == PACKET_MULTICAST); 319 - ret = netif_rx(skb_deliver); 320 - if (ret == NET_RX_DROP) { 321 - hsr_priv->dev->stats.rx_dropped++; 322 - } else { 323 - hsr_priv->dev->stats.rx_packets++; 324 - hsr_priv->dev->stats.rx_bytes += skb->len; 325 - if (multicast_frame) 326 - hsr_priv->dev->stats.multicast++; 327 - } 328 - } 329 - 330 - forward: 331 - if (other_slave) { 332 - skb_push(skb, ETH_HLEN); 333 - skb->dev = other_slave; 334 - dev_queue_xmit(skb); 335 - } 336 - 337 - return NET_RX_SUCCESS; 338 - } 339 - 340 - 341 - static struct packet_type hsr_pt __read_mostly = { 342 - .type = htons(ETH_P_PRP), 343 - .func = hsr_rcv, 344 - }; 345 193 346 194 static struct notifier_block hsr_nb = { 347 195 .notifier_call = hsr_netdev_notify, /* Slave event notifications */ ··· 113 439 { 114 440 int res; 115 441 116 - BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_TAGLEN); 117 - 118 - dev_add_pack(&hsr_pt); 119 - 120 - init_timer(&prune_timer); 121 - prune_timer.function = prune_nodes_all; 122 - prune_timer.data = 0; 123 - prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD); 124 - add_timer(&prune_timer); 442 + BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN); 125 443 126 444 register_netdevice_notifier(&hsr_nb); 127 - 128 445 res = hsr_netlink_init(); 129 446 130 447 return res; ··· 124 459 static void __exit hsr_exit(void) 125 460 { 126 461 unregister_netdevice_notifier(&hsr_nb); 127 - del_timer_sync(&prune_timer); 128 462 hsr_netlink_exit(); 129 - dev_remove_pack(&hsr_pt); 130 463 } 131 464 132 465 module_init(hsr_init);
+39 -22
net/hsr/hsr_main.h
··· 1 - /* Copyright 2011-2013 Autronica Fire and Security AS 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 2 2 * 3 3 * This program is free software; you can redistribute it and/or modify it 4 4 * under the terms of the GNU General Public License as published by the Free ··· 6 6 * any later version. 7 7 * 8 8 * Author(s): 9 - * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 10 */ 11 11 12 - #ifndef _HSR_PRIVATE_H 13 - #define _HSR_PRIVATE_H 12 + #ifndef __HSR_PRIVATE_H 13 + #define __HSR_PRIVATE_H 14 14 15 15 #include <linux/netdevice.h> 16 16 #include <linux/list.h> ··· 29 29 * each node differ before we notify of communication problem? 30 30 */ 31 31 #define MAX_SLAVE_DIFF 3000 /* ms */ 32 + #define HSR_SEQNR_START (USHRT_MAX - 1024) 32 33 33 34 34 35 /* How often shall we check for broken ring and remove node entries older than ··· 47 46 * path, LSDU_size, sequence Nr }. But we let eth_header() create { h_dest, 48 47 * h_source, h_proto = 0x88FB }, and add { path, LSDU_size, sequence Nr, 49 48 * encapsulated protocol } instead. 49 + * 50 + * Field names as defined in the IEC:2010 standard for HSR. 50 51 */ 51 - #define HSR_TAGLEN 6 52 - 53 - /* Field names below as defined in the IEC:2010 standard for HSR. */ 54 52 struct hsr_tag { 55 53 __be16 path_and_LSDU_size; 56 54 __be16 sequence_nr; 57 55 __be16 encap_proto; 58 56 } __packed; 59 57 58 + #define HSR_HLEN 6 60 59 61 60 /* The helper functions below assumes that 'path' occupies the 4 most 62 61 * significant bits of the 16-bit field shared by 'path' and 'LSDU_size' (or ··· 137 136 } __packed; 138 137 139 138 140 - enum hsr_dev_idx { 141 - HSR_DEV_NONE = -1, 142 - HSR_DEV_SLAVE_A = 0, 143 - HSR_DEV_SLAVE_B, 144 - HSR_DEV_MASTER, 139 + enum hsr_port_type { 140 + HSR_PT_NONE = 0, /* Must be 0, used by framereg */ 141 + HSR_PT_SLAVE_A, 142 + HSR_PT_SLAVE_B, 143 + HSR_PT_INTERLINK, 144 + HSR_PT_MASTER, 145 + HSR_PT_PORTS, /* This must be the last item in the enum */ 145 146 }; 146 - #define HSR_MAX_SLAVE (HSR_DEV_SLAVE_B + 1) 147 - #define HSR_MAX_DEV (HSR_DEV_MASTER + 1) 147 + 148 + struct hsr_port { 149 + struct list_head port_list; 150 + struct net_device *dev; 151 + struct hsr_priv *hsr; 152 + enum hsr_port_type type; 153 + }; 148 154 149 155 struct hsr_priv { 150 - struct list_head hsr_list; /* List of hsr devices */ 151 156 struct rcu_head rcu_head; 152 - struct net_device *dev; 153 - struct net_device *slave[HSR_MAX_SLAVE]; 154 - struct list_head node_db; /* Other HSR nodes */ 157 + struct list_head ports; 158 + struct list_head node_db; /* Known HSR nodes */ 155 159 struct list_head self_node_db; /* MACs of slaves */ 156 160 struct timer_list announce_timer; /* Supervision frame dispatch */ 161 + struct timer_list prune_timer; 157 162 int announce_count; 158 163 u16 sequence_nr; 159 164 spinlock_t seqnr_lock; /* locking for sequence_nr */ 160 165 unsigned char sup_multicast_addr[ETH_ALEN]; 161 166 }; 162 167 163 - void register_hsr_master(struct hsr_priv *hsr_priv); 164 - void unregister_hsr_master(struct hsr_priv *hsr_priv); 165 - bool is_hsr_slave(struct net_device *dev); 168 + #define hsr_for_each_port(hsr, port) \ 169 + list_for_each_entry_rcu((port), &(hsr)->ports, port_list) 166 170 167 - #endif /* _HSR_PRIVATE_H */ 171 + struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt); 172 + 173 + /* Caller must ensure skb is a valid HSR frame */ 174 + static inline u16 hsr_get_skb_sequence_nr(struct sk_buff *skb) 175 + { 176 + struct hsr_ethhdr *hsr_ethhdr; 177 + 178 + hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb); 179 + return ntohs(hsr_ethhdr->hsr_tag.sequence_nr); 180 + } 181 + 182 + #endif /* __HSR_PRIVATE_H */
+65 -37
net/hsr/hsr_netlink.c
··· 1 - /* Copyright 2011-2013 Autronica Fire and Security AS 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 2 2 * 3 3 * This program is free software; you can redistribute it and/or modify it 4 4 * under the terms of the GNU General Public License as published by the Free ··· 6 6 * any later version. 7 7 * 8 8 * Author(s): 9 - * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 10 * 11 11 * Routines for handling Netlink messages for HSR. 12 12 */ ··· 37 37 struct net_device *link[2]; 38 38 unsigned char multicast_spec; 39 39 40 + if (!data) { 41 + netdev_info(dev, "HSR: No slave devices specified\n"); 42 + return -EINVAL; 43 + } 40 44 if (!data[IFLA_HSR_SLAVE1]) { 41 - netdev_info(dev, "IFLA_HSR_SLAVE1 missing!\n"); 45 + netdev_info(dev, "HSR: Slave1 device not specified\n"); 42 46 return -EINVAL; 43 47 } 44 48 link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1])); 45 49 if (!data[IFLA_HSR_SLAVE2]) { 46 - netdev_info(dev, "IFLA_HSR_SLAVE2 missing!\n"); 50 + netdev_info(dev, "HSR: Slave2 device not specified\n"); 47 51 return -EINVAL; 48 52 } 49 53 link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2])); ··· 67 63 68 64 static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev) 69 65 { 70 - struct hsr_priv *hsr_priv; 66 + struct hsr_priv *hsr; 67 + struct hsr_port *port; 68 + int res; 71 69 72 - hsr_priv = netdev_priv(dev); 70 + hsr = netdev_priv(dev); 73 71 74 - if (hsr_priv->slave[0]) 75 - if (nla_put_u32(skb, IFLA_HSR_SLAVE1, hsr_priv->slave[0]->ifindex)) 76 - goto nla_put_failure; 72 + res = 0; 77 73 78 - if (hsr_priv->slave[1]) 79 - if (nla_put_u32(skb, IFLA_HSR_SLAVE2, hsr_priv->slave[1]->ifindex)) 80 - goto nla_put_failure; 74 + rcu_read_lock(); 75 + port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); 76 + if (port) 77 + res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex); 78 + rcu_read_unlock(); 79 + if (res) 80 + goto nla_put_failure; 81 + 82 + rcu_read_lock(); 83 + port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 84 + if (port) 85 + res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex); 86 + rcu_read_unlock(); 87 + if (res) 88 + goto nla_put_failure; 81 89 82 90 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN, 83 - hsr_priv->sup_multicast_addr) || 84 - nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr_priv->sequence_nr)) 91 + hsr->sup_multicast_addr) || 92 + nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr)) 85 93 goto nla_put_failure; 86 94 87 95 return 0; ··· 144 128 * over one of the slave interfaces. This would indicate an open network ring 145 129 * (i.e. a link has failed somewhere). 146 130 */ 147 - void hsr_nl_ringerror(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN], 148 - enum hsr_dev_idx dev_idx) 131 + void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN], 132 + struct hsr_port *port) 149 133 { 150 134 struct sk_buff *skb; 151 135 void *msg_head; 136 + struct hsr_port *master; 152 137 int res; 153 - int ifindex; 154 138 155 139 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); 156 140 if (!skb) ··· 164 148 if (res < 0) 165 149 goto nla_put_failure; 166 150 167 - if (hsr_priv->slave[dev_idx]) 168 - ifindex = hsr_priv->slave[dev_idx]->ifindex; 169 - else 170 - ifindex = -1; 171 - res = nla_put_u32(skb, HSR_A_IFINDEX, ifindex); 151 + res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex); 172 152 if (res < 0) 173 153 goto nla_put_failure; 174 154 ··· 177 165 kfree_skb(skb); 178 166 179 167 fail: 180 - netdev_warn(hsr_priv->dev, "Could not send HSR ring error message\n"); 168 + rcu_read_lock(); 169 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 170 + netdev_warn(master->dev, "Could not send HSR ring error message\n"); 171 + rcu_read_unlock(); 181 172 } 182 173 183 174 /* This is called when we haven't heard from the node with MAC address addr for 184 175 * some time (just before the node is removed from the node table/list). 185 176 */ 186 - void hsr_nl_nodedown(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN]) 177 + void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN]) 187 178 { 188 179 struct sk_buff *skb; 189 180 void *msg_head; 181 + struct hsr_port *master; 190 182 int res; 191 183 192 184 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); ··· 215 199 kfree_skb(skb); 216 200 217 201 fail: 218 - netdev_warn(hsr_priv->dev, "Could not send HSR node down\n"); 202 + rcu_read_lock(); 203 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 204 + netdev_warn(master->dev, "Could not send HSR node down\n"); 205 + rcu_read_unlock(); 219 206 } 220 207 221 208 ··· 239 220 /* For sending */ 240 221 struct sk_buff *skb_out; 241 222 void *msg_head; 242 - struct hsr_priv *hsr_priv; 223 + struct hsr_priv *hsr; 224 + struct hsr_port *port; 243 225 unsigned char hsr_node_addr_b[ETH_ALEN]; 244 226 int hsr_node_if1_age; 245 227 u16 hsr_node_if1_seq; ··· 287 267 if (res < 0) 288 268 goto nla_put_failure; 289 269 290 - hsr_priv = netdev_priv(hsr_dev); 291 - res = hsr_get_node_data(hsr_priv, 270 + hsr = netdev_priv(hsr_dev); 271 + res = hsr_get_node_data(hsr, 292 272 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]), 293 273 hsr_node_addr_b, 294 274 &addr_b_ifindex, ··· 321 301 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq); 322 302 if (res < 0) 323 303 goto nla_put_failure; 324 - if (hsr_priv->slave[0]) 304 + rcu_read_lock(); 305 + port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); 306 + if (port) 325 307 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX, 326 - hsr_priv->slave[0]->ifindex); 308 + port->dev->ifindex); 309 + rcu_read_unlock(); 327 310 if (res < 0) 328 311 goto nla_put_failure; 329 312 ··· 336 313 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq); 337 314 if (res < 0) 338 315 goto nla_put_failure; 339 - if (hsr_priv->slave[1]) 316 + rcu_read_lock(); 317 + port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 318 + if (port) 340 319 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX, 341 - hsr_priv->slave[1]->ifindex); 320 + port->dev->ifindex); 321 + rcu_read_unlock(); 322 + if (res < 0) 323 + goto nla_put_failure; 342 324 343 325 genlmsg_end(skb_out, msg_head); 344 326 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid); ··· 362 334 return res; 363 335 } 364 336 365 - /* Get a list of MacAddressA of all nodes known to this node (other than self). 337 + /* Get a list of MacAddressA of all nodes known to this node (including self). 366 338 */ 367 339 static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info) 368 340 { ··· 373 345 /* For sending */ 374 346 struct sk_buff *skb_out; 375 347 void *msg_head; 376 - struct hsr_priv *hsr_priv; 348 + struct hsr_priv *hsr; 377 349 void *pos; 378 350 unsigned char addr[ETH_ALEN]; 379 351 int res; ··· 413 385 if (res < 0) 414 386 goto nla_put_failure; 415 387 416 - hsr_priv = netdev_priv(hsr_dev); 388 + hsr = netdev_priv(hsr_dev); 417 389 418 390 rcu_read_lock(); 419 - pos = hsr_get_next_node(hsr_priv, NULL, addr); 391 + pos = hsr_get_next_node(hsr, NULL, addr); 420 392 while (pos) { 421 393 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr); 422 394 if (res < 0) { 423 395 rcu_read_unlock(); 424 396 goto nla_put_failure; 425 397 } 426 - pos = hsr_get_next_node(hsr_priv, pos, addr); 398 + pos = hsr_get_next_node(hsr, pos, addr); 427 399 } 428 400 rcu_read_unlock(); 429 401
+6 -5
net/hsr/hsr_netlink.h
··· 1 - /* Copyright 2011-2013 Autronica Fire and Security AS 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 2 2 * 3 3 * This program is free software; you can redistribute it and/or modify it 4 4 * under the terms of the GNU General Public License as published by the Free ··· 6 6 * any later version. 7 7 * 8 8 * Author(s): 9 - * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 10 */ 11 11 12 12 #ifndef __HSR_NETLINK_H ··· 17 17 #include <uapi/linux/hsr_netlink.h> 18 18 19 19 struct hsr_priv; 20 + struct hsr_port; 20 21 21 22 int __init hsr_netlink_init(void); 22 23 void __exit hsr_netlink_exit(void); 23 24 24 - void hsr_nl_ringerror(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN], 25 - int dev_idx); 26 - void hsr_nl_nodedown(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN]); 25 + void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN], 26 + struct hsr_port *port); 27 + void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN]); 27 28 void hsr_nl_framedrop(int dropcount, int dev_idx); 28 29 void hsr_nl_linkdown(int dev_idx); 29 30
+196
net/hsr/hsr_slave.c
··· 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 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): 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 + */ 11 + 12 + #include "hsr_slave.h" 13 + #include <linux/etherdevice.h> 14 + #include <linux/if_arp.h> 15 + #include "hsr_main.h" 16 + #include "hsr_device.h" 17 + #include "hsr_forward.h" 18 + #include "hsr_framereg.h" 19 + 20 + 21 + static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb) 22 + { 23 + struct sk_buff *skb = *pskb; 24 + struct hsr_port *port; 25 + 26 + if (!skb_mac_header_was_set(skb)) { 27 + WARN_ONCE(1, "%s: skb invalid", __func__); 28 + return RX_HANDLER_PASS; 29 + } 30 + 31 + rcu_read_lock(); /* hsr->node_db, hsr->ports */ 32 + port = hsr_port_get_rcu(skb->dev); 33 + 34 + if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) { 35 + /* Directly kill frames sent by ourselves */ 36 + kfree_skb(skb); 37 + goto finish_consume; 38 + } 39 + 40 + if (eth_hdr(skb)->h_proto != htons(ETH_P_PRP)) 41 + goto finish_pass; 42 + 43 + skb_push(skb, ETH_HLEN); 44 + 45 + hsr_forward_skb(skb, port); 46 + 47 + finish_consume: 48 + rcu_read_unlock(); /* hsr->node_db, hsr->ports */ 49 + return RX_HANDLER_CONSUMED; 50 + 51 + finish_pass: 52 + rcu_read_unlock(); /* hsr->node_db, hsr->ports */ 53 + return RX_HANDLER_PASS; 54 + } 55 + 56 + bool hsr_port_exists(const struct net_device *dev) 57 + { 58 + return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame; 59 + } 60 + 61 + 62 + static int hsr_check_dev_ok(struct net_device *dev) 63 + { 64 + /* Don't allow HSR on non-ethernet like devices */ 65 + if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) || 66 + (dev->addr_len != ETH_ALEN)) { 67 + netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n"); 68 + return -EINVAL; 69 + } 70 + 71 + /* Don't allow enslaving hsr devices */ 72 + if (is_hsr_master(dev)) { 73 + netdev_info(dev, "Cannot create trees of HSR devices.\n"); 74 + return -EINVAL; 75 + } 76 + 77 + if (hsr_port_exists(dev)) { 78 + netdev_info(dev, "This device is already a HSR slave.\n"); 79 + return -EINVAL; 80 + } 81 + 82 + if (dev->priv_flags & IFF_802_1Q_VLAN) { 83 + netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n"); 84 + return -EINVAL; 85 + } 86 + 87 + if (dev->priv_flags & IFF_DONT_BRIDGE) { 88 + netdev_info(dev, "This device does not support bridging.\n"); 89 + return -EOPNOTSUPP; 90 + } 91 + 92 + /* HSR over bonded devices has not been tested, but I'm not sure it 93 + * won't work... 94 + */ 95 + 96 + return 0; 97 + } 98 + 99 + 100 + /* Setup device to be added to the HSR bridge. */ 101 + static int hsr_portdev_setup(struct net_device *dev, struct hsr_port *port) 102 + { 103 + int res; 104 + 105 + dev_hold(dev); 106 + res = dev_set_promiscuity(dev, 1); 107 + if (res) 108 + goto fail_promiscuity; 109 + 110 + /* FIXME: 111 + * What does net device "adjacency" mean? Should we do 112 + * res = netdev_master_upper_dev_link(port->dev, port->hsr->dev); ? 113 + */ 114 + 115 + res = netdev_rx_handler_register(dev, hsr_handle_frame, port); 116 + if (res) 117 + goto fail_rx_handler; 118 + dev_disable_lro(dev); 119 + 120 + return 0; 121 + 122 + fail_rx_handler: 123 + dev_set_promiscuity(dev, -1); 124 + fail_promiscuity: 125 + dev_put(dev); 126 + 127 + return res; 128 + } 129 + 130 + int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev, 131 + enum hsr_port_type type) 132 + { 133 + struct hsr_port *port, *master; 134 + int res; 135 + 136 + if (type != HSR_PT_MASTER) { 137 + res = hsr_check_dev_ok(dev); 138 + if (res) 139 + return res; 140 + } 141 + 142 + port = hsr_port_get_hsr(hsr, type); 143 + if (port != NULL) 144 + return -EBUSY; /* This port already exists */ 145 + 146 + port = kzalloc(sizeof(*port), GFP_KERNEL); 147 + if (port == NULL) 148 + return -ENOMEM; 149 + 150 + if (type != HSR_PT_MASTER) { 151 + res = hsr_portdev_setup(dev, port); 152 + if (res) 153 + goto fail_dev_setup; 154 + } 155 + 156 + port->hsr = hsr; 157 + port->dev = dev; 158 + port->type = type; 159 + 160 + list_add_tail_rcu(&port->port_list, &hsr->ports); 161 + synchronize_rcu(); 162 + 163 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 164 + netdev_update_features(master->dev); 165 + dev_set_mtu(master->dev, hsr_get_max_mtu(hsr)); 166 + 167 + return 0; 168 + 169 + fail_dev_setup: 170 + kfree(port); 171 + return res; 172 + } 173 + 174 + void hsr_del_port(struct hsr_port *port) 175 + { 176 + struct hsr_priv *hsr; 177 + struct hsr_port *master; 178 + 179 + hsr = port->hsr; 180 + master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 181 + list_del_rcu(&port->port_list); 182 + 183 + if (port != master) { 184 + netdev_update_features(master->dev); 185 + dev_set_mtu(master->dev, hsr_get_max_mtu(hsr)); 186 + netdev_rx_handler_unregister(port->dev); 187 + dev_set_promiscuity(port->dev, -1); 188 + } 189 + 190 + /* FIXME? 191 + * netdev_upper_dev_unlink(port->dev, port->hsr->dev); 192 + */ 193 + 194 + synchronize_rcu(); 195 + dev_put(port->dev); 196 + }
+38
net/hsr/hsr_slave.h
··· 1 + /* Copyright 2011-2014 Autronica Fire and Security AS 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): 9 + * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 10 + */ 11 + 12 + #ifndef __HSR_SLAVE_H 13 + #define __HSR_SLAVE_H 14 + 15 + #include <linux/skbuff.h> 16 + #include <linux/netdevice.h> 17 + #include <linux/rtnetlink.h> 18 + #include "hsr_main.h" 19 + 20 + int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev, 21 + enum hsr_port_type pt); 22 + void hsr_del_port(struct hsr_port *port); 23 + bool hsr_port_exists(const struct net_device *dev); 24 + 25 + static inline struct hsr_port *hsr_port_get_rtnl(const struct net_device *dev) 26 + { 27 + ASSERT_RTNL(); 28 + return hsr_port_exists(dev) ? 29 + rtnl_dereference(dev->rx_handler_data) : NULL; 30 + } 31 + 32 + static inline struct hsr_port *hsr_port_get_rcu(const struct net_device *dev) 33 + { 34 + return hsr_port_exists(dev) ? 35 + rcu_dereference(dev->rx_handler_data) : NULL; 36 + } 37 + 38 + #endif /* __HSR_SLAVE_H */