Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 1 | /* Copyright 2011-2014 Autronica Fire and Security AS |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify it |
| 4 | * under the terms of the GNU General Public License as published by the Free |
| 5 | * Software Foundation; either version 2 of the License, or (at your option) |
| 6 | * any later version. |
| 7 | * |
| 8 | * Author(s): |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 9 | * 2011-2014 Arvid Brodin, arvid.brodin@alten.se |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 10 | * |
| 11 | * This file contains device methods for creating, using and destroying |
| 12 | * virtual HSR devices. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/etherdevice.h> |
| 18 | #include <linux/if_arp.h> |
| 19 | #include <linux/rtnetlink.h> |
| 20 | #include <linux/pkt_sched.h> |
| 21 | #include "hsr_device.h" |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 22 | #include "hsr_slave.h" |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 23 | #include "hsr_framereg.h" |
| 24 | #include "hsr_main.h" |
| 25 | |
| 26 | |
| 27 | static bool is_admin_up(struct net_device *dev) |
| 28 | { |
| 29 | return dev && (dev->flags & IFF_UP); |
| 30 | } |
| 31 | |
| 32 | static bool is_slave_up(struct net_device *dev) |
| 33 | { |
| 34 | return dev && is_admin_up(dev) && netif_oper_up(dev); |
| 35 | } |
| 36 | |
| 37 | static void __hsr_set_operstate(struct net_device *dev, int transition) |
| 38 | { |
| 39 | write_lock_bh(&dev_base_lock); |
| 40 | if (dev->operstate != transition) { |
| 41 | dev->operstate = transition; |
| 42 | write_unlock_bh(&dev_base_lock); |
| 43 | netdev_state_change(dev); |
| 44 | } else { |
| 45 | write_unlock_bh(&dev_base_lock); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void hsr_set_operstate(struct net_device *hsr_dev, struct net_device *slave1, |
| 50 | struct net_device *slave2) |
| 51 | { |
| 52 | if (!is_admin_up(hsr_dev)) { |
| 53 | __hsr_set_operstate(hsr_dev, IF_OPER_DOWN); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if (is_slave_up(slave1) || is_slave_up(slave2)) |
| 58 | __hsr_set_operstate(hsr_dev, IF_OPER_UP); |
| 59 | else |
| 60 | __hsr_set_operstate(hsr_dev, IF_OPER_LOWERLAYERDOWN); |
| 61 | } |
| 62 | |
| 63 | void hsr_set_carrier(struct net_device *hsr_dev, struct net_device *slave1, |
| 64 | struct net_device *slave2) |
| 65 | { |
| 66 | if (is_slave_up(slave1) || is_slave_up(slave2)) |
| 67 | netif_carrier_on(hsr_dev); |
| 68 | else |
| 69 | netif_carrier_off(hsr_dev); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void hsr_check_announce(struct net_device *hsr_dev, int old_operstate) |
| 74 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 75 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 76 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 77 | hsr = netdev_priv(hsr_dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 78 | |
| 79 | if ((hsr_dev->operstate == IF_OPER_UP) && (old_operstate != IF_OPER_UP)) { |
| 80 | /* Went up */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 81 | hsr->announce_count = 0; |
| 82 | hsr->announce_timer.expires = jiffies + |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 83 | msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 84 | add_timer(&hsr->announce_timer); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP)) |
| 88 | /* Went down */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 89 | del_timer(&hsr->announce_timer); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 93 | int hsr_get_max_mtu(struct hsr_priv *hsr) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 94 | { |
| 95 | int mtu_max; |
| 96 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 97 | if (hsr->slave[0] && hsr->slave[1]) |
| 98 | mtu_max = min(hsr->slave[0]->mtu, hsr->slave[1]->mtu); |
| 99 | else if (hsr->slave[0]) |
| 100 | mtu_max = hsr->slave[0]->mtu; |
| 101 | else if (hsr->slave[1]) |
| 102 | mtu_max = hsr->slave[1]->mtu; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 103 | else |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 104 | mtu_max = HSR_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 105 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 106 | return mtu_max - HSR_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu) |
| 110 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 111 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 112 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 113 | hsr = netdev_priv(dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 114 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 115 | if (new_mtu > hsr_get_max_mtu(hsr)) { |
| 116 | netdev_info(hsr->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_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | dev->mtu = new_mtu; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int hsr_dev_open(struct net_device *dev) |
| 127 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 128 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 129 | int i; |
| 130 | char *slave_name; |
| 131 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 132 | hsr = netdev_priv(dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 133 | |
| 134 | for (i = 0; i < HSR_MAX_SLAVE; i++) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 135 | if (hsr->slave[i]) |
| 136 | slave_name = hsr->slave[i]->name; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 137 | else |
| 138 | slave_name = "null"; |
| 139 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 140 | if (!is_slave_up(hsr->slave[i])) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 141 | netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a working HSR network\n", |
| 142 | 'A' + i, slave_name); |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int hsr_dev_close(struct net_device *dev) |
| 149 | { |
| 150 | /* Nothing to do here. We could try to restore the state of the slaves |
| 151 | * to what they were before being changed by the hsr master dev's state, |
| 152 | * but they might have been changed manually in the mean time too, so |
| 153 | * taking them up or down here might be confusing and is probably not a |
| 154 | * good idea. |
| 155 | */ |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 160 | static void hsr_fill_tag(struct hsr_ethhdr *hsr_ethhdr, struct hsr_priv *hsr) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 161 | { |
| 162 | unsigned long irqflags; |
| 163 | |
| 164 | /* IEC 62439-1:2010, p 48, says the 4-bit "path" field can take values |
| 165 | * between 0001-1001 ("ring identifier", for regular HSR frames), |
| 166 | * or 1111 ("HSR management", supervision frames). Unfortunately, the |
| 167 | * spec writers forgot to explain what a "ring identifier" is, or |
| 168 | * how it is used. So we just set this to 0001 for regular frames, |
| 169 | * and 1111 for supervision frames. |
| 170 | */ |
| 171 | set_hsr_tag_path(&hsr_ethhdr->hsr_tag, 0x1); |
| 172 | |
| 173 | /* IEC 62439-1:2010, p 12: "The link service data unit in an Ethernet |
| 174 | * frame is the content of the frame located between the Length/Type |
| 175 | * field and the Frame Check Sequence." |
| 176 | * |
| 177 | * IEC 62439-3, p 48, specifies the "original LPDU" to include the |
| 178 | * original "LT" field (what "LT" means is not explained anywhere as |
| 179 | * far as I can see - perhaps "Length/Type"?). So LSDU_size might |
| 180 | * equal original length + 2. |
| 181 | * Also, the fact that this field is not used anywhere (might be used |
| 182 | * by a RedBox connecting HSR and PRP nets?) means I cannot test its |
| 183 | * correctness. Instead of guessing, I set this to 0 here, to make any |
| 184 | * problems immediately apparent. Anyone using this driver with PRP/HSR |
| 185 | * RedBoxes might need to fix this... |
| 186 | */ |
| 187 | set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, 0); |
| 188 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 189 | spin_lock_irqsave(&hsr->seqnr_lock, irqflags); |
| 190 | hsr_ethhdr->hsr_tag.sequence_nr = htons(hsr->sequence_nr); |
| 191 | hsr->sequence_nr++; |
| 192 | spin_unlock_irqrestore(&hsr->seqnr_lock, irqflags); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 193 | |
| 194 | hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto; |
| 195 | |
| 196 | hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP); |
| 197 | } |
| 198 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 199 | static int slave_xmit(struct sk_buff *skb, struct hsr_priv *hsr, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 200 | enum hsr_dev_idx dev_idx) |
| 201 | { |
| 202 | struct hsr_ethhdr *hsr_ethhdr; |
| 203 | |
| 204 | hsr_ethhdr = (struct hsr_ethhdr *) skb->data; |
| 205 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 206 | skb->dev = hsr->slave[dev_idx]; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 207 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 208 | hsr_addr_subst_dest(hsr, &hsr_ethhdr->ethhdr, dev_idx); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 209 | |
| 210 | /* Address substitution (IEC62439-3 pp 26, 50): replace mac |
| 211 | * address of outgoing frame with that of the outgoing slave's. |
| 212 | */ |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 213 | ether_addr_copy(hsr_ethhdr->ethhdr.h_source, skb->dev->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 214 | |
| 215 | return dev_queue_xmit(skb); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev) |
| 220 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 221 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 222 | struct hsr_ethhdr *hsr_ethhdr; |
| 223 | struct sk_buff *skb2; |
| 224 | int res1, res2; |
| 225 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 226 | hsr = netdev_priv(dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 227 | hsr_ethhdr = (struct hsr_ethhdr *) skb->data; |
| 228 | |
| 229 | if ((skb->protocol != htons(ETH_P_PRP)) || |
| 230 | (hsr_ethhdr->ethhdr.h_proto != htons(ETH_P_PRP))) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 231 | hsr_fill_tag(hsr_ethhdr, hsr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 232 | skb->protocol = htons(ETH_P_PRP); |
| 233 | } |
| 234 | |
| 235 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 236 | |
| 237 | res1 = NET_XMIT_DROP; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 238 | if (likely(hsr->slave[HSR_DEV_SLAVE_A])) |
| 239 | res1 = slave_xmit(skb, hsr, HSR_DEV_SLAVE_A); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 240 | |
| 241 | res2 = NET_XMIT_DROP; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 242 | if (likely(skb2 && hsr->slave[HSR_DEV_SLAVE_B])) |
| 243 | res2 = slave_xmit(skb2, hsr, HSR_DEV_SLAVE_B); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 244 | |
| 245 | if (likely(res1 == NET_XMIT_SUCCESS || res1 == NET_XMIT_CN || |
| 246 | res2 == NET_XMIT_SUCCESS || res2 == NET_XMIT_CN)) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 247 | hsr->dev->stats.tx_packets++; |
| 248 | hsr->dev->stats.tx_bytes += skb->len; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 249 | } else { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 250 | hsr->dev->stats.tx_dropped++; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return NETDEV_TX_OK; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | static int hsr_header_create(struct sk_buff *skb, struct net_device *dev, |
| 258 | unsigned short type, const void *daddr, |
| 259 | const void *saddr, unsigned int len) |
| 260 | { |
| 261 | int res; |
| 262 | |
| 263 | /* Make room for the HSR tag now. We will fill it in later (in |
| 264 | * hsr_dev_xmit) |
| 265 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 266 | if (skb_headroom(skb) < HSR_HLEN + ETH_HLEN) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 267 | return -ENOBUFS; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 268 | skb_push(skb, HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 269 | |
| 270 | /* To allow VLAN/HSR combos we should probably use |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 271 | * res = dev_hard_header(skb, dev, type, daddr, saddr, len + HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 272 | * here instead. It would require other changes too, though - e.g. |
| 273 | * separate headers for each slave etc... |
| 274 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 275 | res = eth_header(skb, dev, type, daddr, saddr, len + HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 276 | if (res <= 0) |
| 277 | return res; |
| 278 | skb_reset_mac_header(skb); |
| 279 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 280 | return res + HSR_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | static const struct header_ops hsr_header_ops = { |
| 285 | .create = hsr_header_create, |
| 286 | .parse = eth_header_parse, |
| 287 | }; |
| 288 | |
| 289 | |
| 290 | /* HSR:2010 supervision frames should be padded so that the whole frame, |
| 291 | * including headers and FCS, is 64 bytes (without VLAN). |
| 292 | */ |
| 293 | static int hsr_pad(int size) |
| 294 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 295 | const int min_size = ETH_ZLEN - HSR_HLEN - ETH_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 296 | |
| 297 | if (size >= min_size) |
| 298 | return size; |
| 299 | return min_size; |
| 300 | } |
| 301 | |
| 302 | static void send_hsr_supervision_frame(struct net_device *hsr_dev, u8 type) |
| 303 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 304 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 305 | struct sk_buff *skb; |
| 306 | int hlen, tlen; |
| 307 | struct hsr_sup_tag *hsr_stag; |
| 308 | struct hsr_sup_payload *hsr_sp; |
| 309 | unsigned long irqflags; |
| 310 | |
| 311 | hlen = LL_RESERVED_SPACE(hsr_dev); |
| 312 | tlen = hsr_dev->needed_tailroom; |
| 313 | skb = alloc_skb(hsr_pad(sizeof(struct hsr_sup_payload)) + hlen + tlen, |
| 314 | GFP_ATOMIC); |
| 315 | |
| 316 | if (skb == NULL) |
| 317 | return; |
| 318 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 319 | hsr = netdev_priv(hsr_dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 320 | |
| 321 | skb_reserve(skb, hlen); |
| 322 | |
| 323 | skb->dev = hsr_dev; |
| 324 | skb->protocol = htons(ETH_P_PRP); |
| 325 | skb->priority = TC_PRIO_CONTROL; |
| 326 | |
| 327 | if (dev_hard_header(skb, skb->dev, ETH_P_PRP, |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 328 | hsr->sup_multicast_addr, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 329 | skb->dev->dev_addr, skb->len) < 0) |
| 330 | goto out; |
| 331 | |
| 332 | skb_pull(skb, sizeof(struct ethhdr)); |
| 333 | hsr_stag = (typeof(hsr_stag)) skb->data; |
| 334 | |
| 335 | set_hsr_stag_path(hsr_stag, 0xf); |
| 336 | set_hsr_stag_HSR_Ver(hsr_stag, 0); |
| 337 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 338 | spin_lock_irqsave(&hsr->seqnr_lock, irqflags); |
| 339 | hsr_stag->sequence_nr = htons(hsr->sequence_nr); |
| 340 | hsr->sequence_nr++; |
| 341 | spin_unlock_irqrestore(&hsr->seqnr_lock, irqflags); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 342 | |
| 343 | hsr_stag->HSR_TLV_Type = type; |
| 344 | hsr_stag->HSR_TLV_Length = 12; |
| 345 | |
| 346 | skb_push(skb, sizeof(struct ethhdr)); |
| 347 | |
| 348 | /* Payload: MacAddressA */ |
| 349 | hsr_sp = (typeof(hsr_sp)) skb_put(skb, sizeof(*hsr_sp)); |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 350 | ether_addr_copy(hsr_sp->MacAddressA, hsr_dev->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 351 | |
| 352 | dev_queue_xmit(skb); |
| 353 | return; |
| 354 | |
| 355 | out: |
| 356 | kfree_skb(skb); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /* Announce (supervision frame) timer function |
| 361 | */ |
| 362 | static void hsr_announce(unsigned long data) |
| 363 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 364 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 365 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 366 | hsr = (struct hsr_priv *) data; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 367 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 368 | if (hsr->announce_count < 3) { |
| 369 | send_hsr_supervision_frame(hsr->dev, HSR_TLV_ANNOUNCE); |
| 370 | hsr->announce_count++; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 371 | } else { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 372 | send_hsr_supervision_frame(hsr->dev, HSR_TLV_LIFE_CHECK); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 375 | if (hsr->announce_count < 3) |
| 376 | hsr->announce_timer.expires = jiffies + |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 377 | msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL); |
| 378 | else |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 379 | hsr->announce_timer.expires = jiffies + |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 380 | msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL); |
| 381 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 382 | if (is_admin_up(hsr->dev)) |
| 383 | add_timer(&hsr->announce_timer); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | |
| 387 | static void restore_slaves(struct net_device *hsr_dev) |
| 388 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 389 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 390 | int i; |
| 391 | int res; |
| 392 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 393 | hsr = netdev_priv(hsr_dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 394 | |
| 395 | rtnl_lock(); |
| 396 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 397 | for (i = 0; i < HSR_MAX_SLAVE; i++) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 398 | if (!hsr->slave[i]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 399 | continue; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 400 | res = dev_set_promiscuity(hsr->slave[i], -1); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 401 | if (res) |
| 402 | netdev_info(hsr_dev, |
| 403 | "Cannot restore slave promiscuity (%s, %d)\n", |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 404 | hsr->slave[i]->name, res); |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 405 | |
| 406 | if (hsr->slave[i]->rx_handler == hsr_handle_frame) |
| 407 | netdev_rx_handler_unregister(hsr->slave[i]); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 410 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 411 | rtnl_unlock(); |
| 412 | } |
| 413 | |
| 414 | static void reclaim_hsr_dev(struct rcu_head *rh) |
| 415 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 416 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 417 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 418 | hsr = container_of(rh, struct hsr_priv, rcu_head); |
| 419 | free_netdev(hsr->dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | |
| 423 | /* According to comments in the declaration of struct net_device, this function |
| 424 | * is "Called from unregister, can be used to call free_netdev". Ok then... |
| 425 | */ |
| 426 | static void hsr_dev_destroy(struct net_device *hsr_dev) |
| 427 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 428 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 429 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 430 | hsr = netdev_priv(hsr_dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 431 | |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame^] | 432 | del_timer_sync(&hsr->prune_timer); |
| 433 | del_timer_sync(&hsr->announce_timer); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 434 | unregister_hsr_master(hsr); /* calls list_del_rcu on hsr */ |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 435 | restore_slaves(hsr_dev); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 436 | call_rcu(&hsr->rcu_head, reclaim_hsr_dev); /* reclaim hsr */ |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static const struct net_device_ops hsr_device_ops = { |
| 440 | .ndo_change_mtu = hsr_dev_change_mtu, |
| 441 | .ndo_open = hsr_dev_open, |
| 442 | .ndo_stop = hsr_dev_close, |
| 443 | .ndo_start_xmit = hsr_dev_xmit, |
| 444 | }; |
| 445 | |
| 446 | |
| 447 | void hsr_dev_setup(struct net_device *dev) |
| 448 | { |
| 449 | random_ether_addr(dev->dev_addr); |
| 450 | |
| 451 | ether_setup(dev); |
| 452 | dev->header_ops = &hsr_header_ops; |
| 453 | dev->netdev_ops = &hsr_device_ops; |
| 454 | dev->tx_queue_len = 0; |
| 455 | |
| 456 | dev->destructor = hsr_dev_destroy; |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /* Return true if dev is a HSR master; return false otherwise. |
| 461 | */ |
| 462 | bool is_hsr_master(struct net_device *dev) |
| 463 | { |
| 464 | return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit); |
| 465 | } |
| 466 | |
| 467 | static int check_slave_ok(struct net_device *dev) |
| 468 | { |
| 469 | /* Don't allow HSR on non-ethernet like devices */ |
| 470 | if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) || |
| 471 | (dev->addr_len != ETH_ALEN)) { |
| 472 | netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n"); |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | /* Don't allow enslaving hsr devices */ |
| 477 | if (is_hsr_master(dev)) { |
| 478 | netdev_info(dev, "Cannot create trees of HSR devices.\n"); |
| 479 | return -EINVAL; |
| 480 | } |
| 481 | |
| 482 | if (is_hsr_slave(dev)) { |
| 483 | netdev_info(dev, "This device is already a HSR slave.\n"); |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | if (dev->priv_flags & IFF_802_1Q_VLAN) { |
| 488 | netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n"); |
| 489 | return -EINVAL; |
| 490 | } |
| 491 | |
| 492 | /* HSR over bonded devices has not been tested, but I'm not sure it |
| 493 | * won't work... |
| 494 | */ |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | |
| 500 | /* Default multicast address for HSR Supervision frames */ |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 501 | static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 502 | 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00 |
| 503 | }; |
| 504 | |
| 505 | int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2], |
| 506 | unsigned char multicast_spec) |
| 507 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 508 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 509 | int i; |
| 510 | int res; |
| 511 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 512 | hsr = netdev_priv(hsr_dev); |
| 513 | hsr->dev = hsr_dev; |
| 514 | INIT_LIST_HEAD(&hsr->node_db); |
| 515 | INIT_LIST_HEAD(&hsr->self_node_db); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 516 | for (i = 0; i < HSR_MAX_SLAVE; i++) |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 517 | hsr->slave[i] = slave[i]; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 518 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 519 | spin_lock_init(&hsr->seqnr_lock); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 520 | /* Overflow soon to find bugs easier: */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 521 | hsr->sequence_nr = USHRT_MAX - 1024; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 522 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 523 | init_timer(&hsr->announce_timer); |
| 524 | hsr->announce_timer.function = hsr_announce; |
| 525 | hsr->announce_timer.data = (unsigned long) hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 526 | |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame^] | 527 | init_timer(&hsr->prune_timer); |
| 528 | hsr->prune_timer.function = hsr_prune_nodes; |
| 529 | hsr->prune_timer.data = (unsigned long) hsr; |
| 530 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 531 | ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr); |
| 532 | hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 533 | |
| 534 | /* FIXME: should I modify the value of these? |
| 535 | * |
| 536 | * - hsr_dev->flags - i.e. |
| 537 | * IFF_MASTER/SLAVE? |
| 538 | * - hsr_dev->priv_flags - i.e. |
| 539 | * IFF_EBRIDGE? |
| 540 | * IFF_TX_SKB_SHARING? |
| 541 | * IFF_HSR_MASTER/SLAVE? |
| 542 | */ |
| 543 | |
| 544 | for (i = 0; i < HSR_MAX_SLAVE; i++) { |
| 545 | res = check_slave_ok(slave[i]); |
| 546 | if (res) |
| 547 | return res; |
| 548 | } |
| 549 | |
| 550 | hsr_dev->features = slave[0]->features & slave[1]->features; |
| 551 | /* Prevent recursive tx locking */ |
| 552 | hsr_dev->features |= NETIF_F_LLTX; |
| 553 | /* VLAN on top of HSR needs testing and probably some work on |
| 554 | * hsr_header_create() etc. |
| 555 | */ |
| 556 | hsr_dev->features |= NETIF_F_VLAN_CHALLENGED; |
| 557 | |
| 558 | /* Set hsr_dev's MAC address to that of mac_slave1 */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 559 | ether_addr_copy(hsr_dev->dev_addr, hsr->slave[0]->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 560 | |
| 561 | /* Set required header length */ |
| 562 | for (i = 0; i < HSR_MAX_SLAVE; i++) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 563 | if (slave[i]->hard_header_len + HSR_HLEN > |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 564 | hsr_dev->hard_header_len) |
| 565 | hsr_dev->hard_header_len = |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 566 | slave[i]->hard_header_len + HSR_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* MTU */ |
| 570 | for (i = 0; i < HSR_MAX_SLAVE; i++) |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 571 | if (slave[i]->mtu - HSR_HLEN < hsr_dev->mtu) |
| 572 | hsr_dev->mtu = slave[i]->mtu - HSR_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 573 | |
| 574 | /* Make sure the 1st call to netif_carrier_on() gets through */ |
| 575 | netif_carrier_off(hsr_dev); |
| 576 | |
| 577 | /* Promiscuity */ |
| 578 | for (i = 0; i < HSR_MAX_SLAVE; i++) { |
| 579 | res = dev_set_promiscuity(slave[i], 1); |
| 580 | if (res) { |
| 581 | netdev_info(hsr_dev, "Cannot set slave promiscuity (%s, %d)\n", |
| 582 | slave[i]->name, res); |
| 583 | goto fail; |
| 584 | } |
| 585 | } |
| 586 | |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 587 | for (i = 0; i < HSR_MAX_SLAVE; i++) { |
| 588 | res = netdev_rx_handler_register(slave[i], hsr_handle_frame, |
| 589 | hsr); |
| 590 | if (res) |
| 591 | goto fail; |
| 592 | } |
| 593 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 594 | /* Make sure we recognize frames from ourselves in hsr_rcv() */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 595 | res = hsr_create_self_node(&hsr->self_node_db, hsr_dev->dev_addr, |
| 596 | hsr->slave[1]->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 597 | if (res < 0) |
| 598 | goto fail; |
| 599 | |
| 600 | res = register_netdevice(hsr_dev); |
| 601 | if (res) |
| 602 | goto fail; |
| 603 | |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame^] | 604 | hsr->prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD); |
| 605 | add_timer(&hsr->prune_timer); |
| 606 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 607 | register_hsr_master(hsr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 608 | |
| 609 | return 0; |
| 610 | |
| 611 | fail: |
| 612 | restore_slaves(hsr_dev); |
| 613 | return res; |
| 614 | } |