hsr_main.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Copyright 2011-2013 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-2013 Arvid Brodin, arvid.brodin@xdin.com
  10. */
  11. #ifndef _HSR_PRIVATE_H
  12. #define _HSR_PRIVATE_H
  13. #include <linux/netdevice.h>
  14. #include <linux/list.h>
  15. /* Time constants as specified in the HSR specification (IEC-62439-3 2010)
  16. * Table 8.
  17. * All values in milliseconds.
  18. */
  19. #define HSR_LIFE_CHECK_INTERVAL 2000 /* ms */
  20. #define HSR_NODE_FORGET_TIME 60000 /* ms */
  21. #define HSR_ANNOUNCE_INTERVAL 100 /* ms */
  22. /* By how much may slave1 and slave2 timestamps of latest received frame from
  23. * each node differ before we notify of communication problem?
  24. */
  25. #define MAX_SLAVE_DIFF 3000 /* ms */
  26. /* How often shall we check for broken ring and remove node entries older than
  27. * HSR_NODE_FORGET_TIME?
  28. */
  29. #define PRUNE_PERIOD 3000 /* ms */
  30. #define HSR_TLV_ANNOUNCE 22
  31. #define HSR_TLV_LIFE_CHECK 23
  32. /* HSR Tag.
  33. * As defined in IEC-62439-3:2010, the HSR tag is really { ethertype = 0x88FB,
  34. * path, LSDU_size, sequence Nr }. But we let eth_header() create { h_dest,
  35. * h_source, h_proto = 0x88FB }, and add { path, LSDU_size, sequence Nr,
  36. * encapsulated protocol } instead.
  37. */
  38. #define HSR_TAGLEN 6
  39. /* Field names below as defined in the IEC:2010 standard for HSR. */
  40. struct hsr_tag {
  41. __be16 path_and_LSDU_size;
  42. __be16 sequence_nr;
  43. __be16 encap_proto;
  44. } __packed;
  45. /* The helper functions below assumes that 'path' occupies the 4 most
  46. * significant bits of the 16-bit field shared by 'path' and 'LSDU_size' (or
  47. * equivalently, the 4 most significant bits of HSR tag byte 14).
  48. *
  49. * This is unclear in the IEC specification; its definition of MAC addresses
  50. * indicates the spec is written with the least significant bit first (to the
  51. * left). This, however, would mean that the LSDU field would be split in two
  52. * with the path field in-between, which seems strange. I'm guessing the MAC
  53. * address definition is in error.
  54. */
  55. static inline u16 get_hsr_tag_path(struct hsr_tag *ht)
  56. {
  57. return ntohs(ht->path_and_LSDU_size) >> 12;
  58. }
  59. static inline u16 get_hsr_tag_LSDU_size(struct hsr_tag *ht)
  60. {
  61. return ntohs(ht->path_and_LSDU_size) & 0x0FFF;
  62. }
  63. static inline void set_hsr_tag_path(struct hsr_tag *ht, u16 path)
  64. {
  65. ht->path_and_LSDU_size = htons(
  66. (ntohs(ht->path_and_LSDU_size) & 0x0FFF) | (path << 12));
  67. }
  68. static inline void set_hsr_tag_LSDU_size(struct hsr_tag *ht, u16 LSDU_size)
  69. {
  70. ht->path_and_LSDU_size = htons(
  71. (ntohs(ht->path_and_LSDU_size) & 0xF000) |
  72. (LSDU_size & 0x0FFF));
  73. }
  74. struct hsr_ethhdr {
  75. struct ethhdr ethhdr;
  76. struct hsr_tag hsr_tag;
  77. } __packed;
  78. /* HSR Supervision Frame data types.
  79. * Field names as defined in the IEC:2010 standard for HSR.
  80. */
  81. struct hsr_sup_tag {
  82. __be16 path_and_HSR_Ver;
  83. __be16 sequence_nr;
  84. __u8 HSR_TLV_Type;
  85. __u8 HSR_TLV_Length;
  86. } __packed;
  87. struct hsr_sup_payload {
  88. unsigned char MacAddressA[ETH_ALEN];
  89. } __packed;
  90. static inline u16 get_hsr_stag_path(struct hsr_sup_tag *hst)
  91. {
  92. return get_hsr_tag_path((struct hsr_tag *) hst);
  93. }
  94. static inline u16 get_hsr_stag_HSR_ver(struct hsr_sup_tag *hst)
  95. {
  96. return get_hsr_tag_LSDU_size((struct hsr_tag *) hst);
  97. }
  98. static inline void set_hsr_stag_path(struct hsr_sup_tag *hst, u16 path)
  99. {
  100. set_hsr_tag_path((struct hsr_tag *) hst, path);
  101. }
  102. static inline void set_hsr_stag_HSR_Ver(struct hsr_sup_tag *hst, u16 HSR_Ver)
  103. {
  104. set_hsr_tag_LSDU_size((struct hsr_tag *) hst, HSR_Ver);
  105. }
  106. struct hsr_ethhdr_sp {
  107. struct ethhdr ethhdr;
  108. struct hsr_sup_tag hsr_sup;
  109. } __packed;
  110. enum hsr_dev_idx {
  111. HSR_DEV_NONE = -1,
  112. HSR_DEV_SLAVE_A = 0,
  113. HSR_DEV_SLAVE_B,
  114. HSR_DEV_MASTER,
  115. };
  116. #define HSR_MAX_SLAVE (HSR_DEV_SLAVE_B + 1)
  117. #define HSR_MAX_DEV (HSR_DEV_MASTER + 1)
  118. struct hsr_priv {
  119. struct list_head hsr_list; /* List of hsr devices */
  120. struct rcu_head rcu_head;
  121. struct net_device *dev;
  122. struct net_device *slave[HSR_MAX_SLAVE];
  123. struct list_head node_db; /* Other HSR nodes */
  124. struct list_head self_node_db; /* MACs of slaves */
  125. struct timer_list announce_timer; /* Supervision frame dispatch */
  126. int announce_count;
  127. u16 sequence_nr;
  128. spinlock_t seqnr_lock; /* locking for sequence_nr */
  129. unsigned char sup_multicast_addr[ETH_ALEN];
  130. };
  131. void register_hsr_master(struct hsr_priv *hsr_priv);
  132. void unregister_hsr_master(struct hsr_priv *hsr_priv);
  133. bool is_hsr_slave(struct net_device *dev);
  134. #endif /* _HSR_PRIVATE_H */