-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathpf_lldp.c
2007 lines (1810 loc) · 56.5 KB
/
pf_lldp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
* _ _ _
* _ __ | |_ _ | | __ _ | |__ ___
* | '__|| __|(_)| | / _` || '_ \ / __|
* | | | |_ _ | || (_| || |_) |\__ \
* |_| \__|(_)|_| \__,_||_.__/ |___/
*
* www.rt-labs.com
* Copyright 2018 rt-labs AB, Sweden.
*
* This software is dual-licensed under GPLv3 and a commercial
* license. See the file LICENSE.md distributed with this software for
* full license information.
********************************************************************/
#ifdef UNIT_TEST
#define pnal_get_system_uptime_10ms mock_pnal_get_system_uptime_10ms
#define pnal_get_interface_index mock_pnal_get_interface_index
#define pnal_eth_get_status mock_pnal_eth_get_status
#endif
#define STRINGIFY(s) STRINGIFIED (s)
#define STRINGIFIED(s) #s
/**
* @file
* @brief Implements Link Layer Discovery Protocol (LLDP), for neighbourhood
* detection.
*
* Builds and sends LLDP frames.
* Receives and parses LLDP frames.
*
* This file should not have any knowledge of Profinet slots, subslots etc.
*
* ToDo: Differentiate between device and port MAC addresses.
*/
#include "pf_includes.h"
#include "pf_block_writer.h"
#include "pf_block_reader.h"
#include "pf_cmrpc.h"
#include <string.h>
#include <ctype.h>
#if PNET_MAX_PORT_DESCRIPTION_SIZE > 256
#error "Port description can't be larger than 255 bytes plus termination"
#endif
#if PNET_MAX_PORT_DESCRIPTION_SIZE < PNET_INTERFACE_NAME_MAX_SIZE
#error "Port description should be at least as large as interface name"
#endif
#define LLDP_TYPE_END 0
#define LLDP_TYPE_CHASSIS_ID 1
#define LLDP_TYPE_PORT_ID 2
#define LLDP_TYPE_TTL 3
#define LLDP_TYPE_PORT_DESCRIPTION 4
#define LLDP_TYPE_MANAGEMENT_ADDRESS 8
#define LLDP_TYPE_ORG_SPEC 127
#define LLDP_IEEE_SUBTYPE_MAC_PHY 1
#define LLDP_TYPE_MASK 0xFE00
#define LLDP_TYPE_SHIFT 0x9
#define LLDP_LENGTH_MASK 0x1FF
#define LLDP_MAX_TLV 512
#define LLDP_PEER_MAX_TTL_IN_SECS \
60 /* Profinet peers should have TTL = 20 seconds. Use some margin. */
#define LLDP_PNIO_SUBTYPE_MEAS_DELAY_VALUES_TLV_LEN 24
#define LLDP_PNIO_SUBTYPE_PORT_STATUS_TLV_LEN 8
#define LLDP_PNIO_SUBTYPE_CHASSIS_MAC_TLV_LEN 10
#define LLDP_IEEE_SUBTYPE_MAC_PHY_TLV_LEN 9
/* Autonegotiation status */
#define LLDP_AUTONEG_SUPPORTED BIT (0)
#define LLDP_AUTONEG_ENABLED BIT (1)
typedef struct lldp_tlv
{
uint8_t type;
uint16_t len;
const uint8_t * p_data;
} lldp_tlv_t;
/** Header for Organizationally Specific TLV */
typedef struct pf_lldp_org_header
{
uint8_t id[3]; /**< Organizationally unique identifier */
uint8_t subtype; /**< Organizationally defined subtype */
} pf_lldp_org_header_t;
static const char org_id_pnio[] = {0x00, 0x0e, 0xcf};
static const char org_id_ieee_8023[] = {0x00, 0x12, 0x0f};
static const char * shed_tag_tx = "lldp_tx";
static const char * shed_tag_rx = "lldp_rx";
typedef enum lldp_pnio_subtype_values
{
LLDP_PNIO_SUBTYPE_RESERVED = 0,
LLDP_PNIO_SUBTYPE_MEAS_DELAY_VALUES = 1,
LLDP_PNIO_SUBTYPE_PORT_STATUS = 2,
LLDP_PNIO_SUBTYPE_PORT_ALIAS = 3,
LLDP_PNIO_SUBTYPE_MRP_RING_PORT_STATUS = 4,
LLDP_PNIO_SUBTYPE_CHASSIS_MAC = 5,
LLDP_PNIO_SUBTYPE_PTCP_STATUS = 6,
LLDP_PNIO_SUBTYPE_MAU_TYPE_EXTENSION = 7,
LLDP_PNIO_SUBTYPE_MRP_INTERCONNECTION_PORT_STATUS = 8,
/* 0x09..0xff reserved */
} lldp_pnio_subtype_values_t;
static const pnet_ethaddr_t lldp_dst_addr = {
{0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e} /* LLDP Multicast */
};
/******************* Insert data into buffer ********************************/
/**
* @internal
* Insert header of a TLV field into a buffer.
* Each TLV is structured as follows:
* - Type = 7 bits
* - Length = 9 bits
* - data = 0-511 bytes
*
* This is for the type and the payload length.
*
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The buffer position.
* @param typ In: The TLV header type.
* @param len In: The TLV payload length.
*/
static inline void pf_lldp_add_tlv_header (
uint8_t * p_buf,
uint16_t * p_pos,
uint8_t typ,
uint8_t len)
{
uint16_t tlv_header = ((typ) << LLDP_TYPE_SHIFT) + ((len)&LLDP_LENGTH_MASK);
pf_put_uint16 (true, tlv_header, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
}
/**
* @internal
* Parse LLDP TLV header.
* Each TLV is structured as follows:
* - Type = 7 bits
* - Length = 9 bits
* - data = 0-511 bytes
*
* @param p_info InOut: Parser state
* @param p_pos InOut: Offset in parsed buffer
* @return parsed frame. If parsing fails, frame type is set to LLDP_TYPE_END
* and len field set to 0.
*/
lldp_tlv_t pf_lldp_get_tlv_from_packet (pf_get_info_t * p_info, uint16_t * p_pos)
{
lldp_tlv_t tlv;
uint16_t tlv_head = pf_get_uint16 (p_info, p_pos);
if (p_info->result == PF_PARSE_OK)
{
tlv.type = (tlv_head & LLDP_TYPE_MASK) >> LLDP_TYPE_SHIFT;
tlv.len = (tlv_head & LLDP_LENGTH_MASK);
tlv.p_data = &p_info->p_buf[*p_pos];
}
else
{
tlv.type = LLDP_TYPE_END;
tlv.len = 0;
tlv.p_data = NULL;
}
return tlv;
}
/**
* Insert Ethernet header into a buffer.
*
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The buffer position.
* @param dst_addr In: Destination MAC address.
* @param src_addr In: Source MAC address.
*/
static void pf_lldp_add_ethernet_header (
uint8_t * p_buf,
uint16_t * p_pos,
pnet_ethaddr_t dst_addr,
pnet_ethaddr_t src_addr)
{
pf_put_mem (
dst_addr.addr,
sizeof (dst_addr.addr),
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
pf_put_mem (
src_addr.addr,
sizeof (src_addr.addr),
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
pf_put_uint16 (true, PNAL_ETHTYPE_LLDP, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
}
/**
* @internal
* Insert a Profinet-specific header for a TLV field into a buffer.
*
* This inserts a TLV header with type="organisation-specific", and
* the Profinet organisation identifier as the first part of the TLV payload.
*
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The buffer position.
* @param len In: The TLV payload length (for the part after the
* organisation identifier)
*/
static inline void pf_lldp_add_pnio_header (
uint8_t * p_buf,
uint16_t * p_pos,
uint8_t len)
{
pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_ORG_SPEC, (len) + 3);
pf_put_byte (0x00, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_byte (0x0e, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_byte (0xcf, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
}
/**
* @internal
* Insert a IEEE 802.3-specific header for a TLV field into a buffer.
*
* This inserts a TLV header with type="organisation-specific", and
* the IEEE 802.3 organisation identifier as the first part of the TLV payload.
*
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The buffer position.
* @param len In: The TLV payload length (for the part after the
* organisation identifier)
*/
static inline void pf_lldp_add_ieee_header (
uint8_t * p_buf,
uint16_t * p_pos,
uint8_t len)
{
pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_ORG_SPEC, (len) + 3);
pf_put_byte (0x00, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_byte (0x12, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_byte (0x0f, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
}
/**
* @internal
* Insert the mandatory chassis_id TLV into a buffer.
*
* @param p_chassis_id In: The Chassis ID.
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The position in the buffer.
*/
static void pf_lldp_add_chassis_id_tlv (
const pf_lldp_chassis_id_t * p_chassis_id,
uint8_t * p_buf,
uint16_t * p_pos)
{
pf_lldp_add_tlv_header (
p_buf,
p_pos,
LLDP_TYPE_CHASSIS_ID,
1 + p_chassis_id->len);
pf_put_byte (p_chassis_id->subtype, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_mem (
p_chassis_id->string,
p_chassis_id->len,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
}
/**
* @internal
* Insert the mandatory port_id TLV into a buffer.
* @param p_port_id In: Port Id
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The position in the buffer.
*/
static void pf_lldp_add_port_id_tlv (
const pf_lldp_port_id_t * p_port_id,
uint8_t * p_buf,
uint16_t * p_pos)
{
pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_PORT_ID, 1 + p_port_id->len);
pf_put_byte (p_port_id->subtype, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_mem (
p_port_id->string,
p_port_id->len,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
}
/**
* @internal
* Insert the mandatory time-to-live (TTL) TLV into a buffer.
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The position in the buffer.
*/
static void pf_lldp_add_ttl_tlv (uint8_t * p_buf, uint16_t * p_pos)
{
pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_TTL, 2);
pf_put_uint16 (true, PF_LLDP_TTL, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
}
/**
* @internal
* Insert the optional Profinet port status TLV into a buffer.
*
* The port status TLV is mandatory for ProfiNet.
* @param rtclass_2_status In: Use 0. Not supported.
* @param rtclass_3_status In: Use 0. Not supported.
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The position in the buffer.
*/
static void pf_lldp_add_port_status (
uint16_t rtclass_2_status,
uint16_t rtclass_3_status,
uint8_t * p_buf,
uint16_t * p_pos)
{
pf_lldp_add_pnio_header (p_buf, p_pos, 5);
pf_put_byte (
LLDP_PNIO_SUBTYPE_PORT_STATUS,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
pf_put_uint16 (true, rtclass_2_status, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_uint16 (true, rtclass_3_status, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
}
/**
* @internal
* Insert the optional Profinet chassis MAC TLV into a buffer.
*
* The chassis MAC TLV is mandatory for ProfiNet.
* @param mac_address In: Device MAC address.
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The position in the buffer.
*/
static void pf_lldp_add_chassis_mac (
const pnet_ethaddr_t * mac_address,
uint8_t * p_buf,
uint16_t * p_pos)
{
pf_lldp_add_pnio_header (p_buf, p_pos, 1 + sizeof (pnet_ethaddr_t));
pf_put_byte (
LLDP_PNIO_SUBTYPE_CHASSIS_MAC,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
memcpy (&p_buf[*p_pos], mac_address->addr, sizeof (pnet_ethaddr_t));
(*p_pos) += sizeof (pnet_ethaddr_t);
}
/**
* @internal
* Insert the optional IEEE 802.3 MAC TLV into a buffer.
*
* This is the autonegotiation capabilities and available speeds, and cable MAU
* type.
*
* The IEEE 802.3 MAC TLV is mandatory for ProfiNet on 803.2 interfaces.
* @param p_status In: Link status for this port.
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The position in the buffer.
*/
static void pf_lldp_add_ieee_mac_phy (
const pf_lldp_link_status_t * p_status,
uint8_t * p_buf,
uint16_t * p_pos)
{
uint8_t autoneg_status;
autoneg_status = 0x00;
if (p_status->is_autonegotiation_supported)
{
autoneg_status |= LLDP_AUTONEG_SUPPORTED;
}
if (p_status->is_autonegotiation_enabled)
{
autoneg_status |= LLDP_AUTONEG_ENABLED;
}
pf_lldp_add_ieee_header (p_buf, p_pos, 6);
pf_put_byte (LLDP_IEEE_SUBTYPE_MAC_PHY, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_byte (autoneg_status, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_uint16 (
true,
p_status->autonegotiation_advertised_capabilities,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
pf_put_uint16 (
true,
p_status->operational_mau_type,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
}
/**
* Insert the optional management data TLV into a buffer.
* It is mandatory for ProfiNet.
*
* Contains the IP address as well as information about the
* management interface.
*
* @param p_man_address In: Management address
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The position in the buffer.
*/
static void pf_lldp_add_management (
const pf_lldp_management_address_t * p_man_address,
uint8_t * p_buf,
uint16_t * p_pos)
{
pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_MANAGEMENT_ADDRESS, 12);
/* Management address length, type and value */
pf_put_byte (1 + p_man_address->len, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_byte (p_man_address->subtype, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_mem (
p_man_address->value,
p_man_address->len,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
/* Interface number for management interface */
pf_put_byte (
p_man_address->interface_number.subtype,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
pf_put_uint32 (
true,
p_man_address->interface_number.value,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
/* Management OID. Not supported */
pf_put_byte (0, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
}
void pf_lldp_invalidate_peer_info (pnet_t * net, int loc_port_num)
{
pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
os_mutex_lock (net->lldp_mutex);
p_port_data->lldp.is_peer_info_received = false;
p_port_data->lldp.peer_info.chassis_id.is_valid = false;
p_port_data->lldp.peer_info.port_id.is_valid = false;
p_port_data->lldp.peer_info.port_description.is_valid = false;
p_port_data->lldp.peer_info.management_address.is_valid = false;
p_port_data->lldp.peer_info.port_delay.is_valid = false;
p_port_data->lldp.peer_info.phy_config.is_valid = false;
p_port_data->lldp.peer_info.ttl = 0;
os_mutex_unlock (net->lldp_mutex);
}
/********************* Initialize and send **********************************/
/**
* @internal
* Handle LLDP peer timeout. Timeout occurs if no peer LLDP data is received
* on port within the LLPD TTL timeout interval.
*
* This is a callback for the scheduler. Arguments should fulfill
* pf_scheduler_timeout_ftn_t
*
* @param net InOut: The p-net stack instance
* @param arg In: Port instance, p_port_data
* @param current_time In: Not used
*/
static void pf_lldp_receive_timeout (
pnet_t * net,
void * arg,
uint32_t current_time)
{
pf_port_t * p_port_data = (pf_port_t *)arg;
p_port_data->lldp.rx_timeout = 0;
LOG_INFO (
PF_LLDP_LOG,
"LLDP(%d): Port %d, receive timeout expired\n",
__LINE__,
p_port_data->port_num);
pf_lldp_invalidate_peer_info (net, p_port_data->port_num);
pf_pdport_peer_indication (net, p_port_data->port_num);
}
void pf_lldp_restart_peer_timeout (
pnet_t * net,
int loc_port_num,
uint16_t timeout_in_secs)
{
pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
if (p_port_data->lldp.rx_timeout != 0)
{
pf_scheduler_remove (net, shed_tag_rx, p_port_data->lldp.rx_timeout);
p_port_data->lldp.rx_timeout = 0;
}
/*
* Profinet states that the time to live shall be 20 seconds,
* See IEC61158-6-10 6.3.13.2.2
*
* To handle situations where ttl is longer than 20s and timeout gets
* longer than scheduler can handle, only start receive timeout if
* ttl is within configured max ttl.
* Timeout will always be started if a profinet enabled switch
* with 20s LLDP peer ttl is used.
*/
if (timeout_in_secs <= LLDP_PEER_MAX_TTL_IN_SECS)
{
if (
pf_scheduler_add (
net,
timeout_in_secs * 1000000,
shed_tag_rx,
pf_lldp_receive_timeout,
p_port_data,
&p_port_data->lldp.rx_timeout) != 0)
{
LOG_ERROR (
PF_LLDP_LOG,
"LLDP(%d): Failed to add LLDP timeout\n",
__LINE__);
}
}
}
void pf_lldp_stop_peer_timeout (pnet_t * net, int loc_port_num)
{
pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
if (p_port_data->lldp.rx_timeout != 0)
{
pf_scheduler_remove (net, shed_tag_rx, p_port_data->lldp.rx_timeout);
p_port_data->lldp.rx_timeout = 0;
}
}
/**
* @internal
* Check if management address is of type IPv4
*
* @param address In: Management address.
* @return true if address is a valid IPv4 address,
* false if not.
*/
static bool pf_lldp_management_address_is_ipv4 (
const pf_lldp_management_address_t * address)
{
return address->is_valid && address->subtype == 1 && address->len == 4;
}
int pf_lldp_get_peer_timestamp (
pnet_t * net,
int loc_port_num,
uint32_t * timestamp_10ms)
{
bool is_received;
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
os_mutex_lock (net->lldp_mutex);
*timestamp_10ms = p_port_data->lldp.timestamp_for_last_peer_change;
is_received = p_port_data->lldp.is_peer_info_received;
os_mutex_unlock (net->lldp_mutex);
return is_received ? 0 : -1;
}
void pf_lldp_get_chassis_id (pnet_t * net, pf_lldp_chassis_id_t * p_chassis_id)
{
const pnet_ethaddr_t * device_mac_address =
pf_cmina_get_device_macaddr (net);
char station_name[PNET_STATION_NAME_MAX_SIZE]; /** Terminated */
if (
net->pf_interface.name_of_device_mode.mode ==
PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY)
{
/*
* FIXME: Use of pf_cmina_get_station_name() is not thread-safe.
* Fix this, e.g. using a mutex.
*/
pf_cmina_get_station_name (net, station_name);
p_chassis_id->len = strlen (station_name);
if (
p_chassis_id->len == 0 ||
p_chassis_id->len >= PNET_LLDP_CHASSIS_ID_MAX_SIZE)
{
/* Use the device MAC address */
p_chassis_id->subtype = PF_LLDP_SUBTYPE_MAC;
p_chassis_id->len = sizeof (pnet_ethaddr_t);
memcpy (
p_chassis_id->string,
device_mac_address->addr,
sizeof (pnet_ethaddr_t));
}
else
{
/* Use the station name (NameOfStation/NameOfInterface) */
p_chassis_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
memcpy (p_chassis_id->string, station_name, p_chassis_id->len);
}
}
else /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
{
p_chassis_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
p_chassis_id->len = pf_lldp_get_system_description (
net,
p_chassis_id->string,
sizeof (p_chassis_id->string));
}
p_chassis_id->string[p_chassis_id->len] = '\0';
p_chassis_id->is_valid = true;
}
int pf_lldp_get_peer_chassis_id (
pnet_t * net,
int loc_port_num,
pf_lldp_chassis_id_t * p_chassis_id)
{
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
os_mutex_lock (net->lldp_mutex);
*p_chassis_id = p_port_data->lldp.peer_info.chassis_id;
os_mutex_unlock (net->lldp_mutex);
return p_chassis_id->is_valid ? 0 : -1;
}
void pf_lldp_get_port_id (
pnet_t * net,
int loc_port_num,
pf_lldp_port_id_t * p_port_id)
{
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
char station_name[PNET_STATION_NAME_MAX_SIZE]; /** Terminated */
const pnet_ethaddr_t * device_mac_address =
pf_cmina_get_device_macaddr (net);
if (
net->pf_interface.name_of_device_mode.mode ==
PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY)
{
snprintf (
p_port_id->string,
sizeof (p_port_id->string),
"%s",
p_port_data->port_name);
}
else /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
{
pf_cmina_get_station_name (net, station_name);
if (strlen (station_name) == 0)
{
/* Profinet 2.4 Protocol, table 335 "LLDP substitutions":
Use chassis MAC address if no NameOfStation is assigned */
pf_lldp_mac_address_to_string (
device_mac_address->addr,
station_name,
sizeof (station_name));
}
snprintf (
p_port_id->string,
sizeof (p_port_id->string),
"%s.%s",
p_port_data->port_name,
station_name);
}
p_port_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
p_port_id->len = strlen (p_port_id->string);
p_port_id->is_valid = true;
}
int pf_lldp_get_peer_port_id (
pnet_t * net,
int loc_port_num,
pf_lldp_port_id_t * p_port_id)
{
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
os_mutex_lock (net->lldp_mutex);
*p_port_id = p_port_data->lldp.peer_info.port_id;
os_mutex_unlock (net->lldp_mutex);
return p_port_id->is_valid ? 0 : -1;
}
void pf_lldp_get_port_description (
pnet_t * net,
int loc_port_num,
pf_lldp_port_description_t * p_port_descr)
{
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
snprintf (
p_port_descr->string,
sizeof (p_port_descr->string),
"%s",
p_port_data->netif.name);
p_port_descr->len = strlen (p_port_descr->string);
p_port_descr->is_valid = true;
}
int pf_lldp_get_peer_port_description (
pnet_t * net,
int loc_port_num,
pf_lldp_port_description_t * p_port_desc)
{
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
os_mutex_lock (net->lldp_mutex);
*p_port_desc = p_port_data->lldp.peer_info.port_description;
os_mutex_unlock (net->lldp_mutex);
return p_port_desc->is_valid ? 0 : -1;
}
void pf_lldp_get_management_address (
pnet_t * net,
pf_lldp_management_address_t * p_man_address)
{
pnal_ipaddr_t ipaddr = pf_cmina_get_ipaddr (net);
p_man_address->subtype = 1; /* IPv4 */
ipaddr = CC_TO_BE32 (ipaddr);
memcpy (p_man_address->value, &ipaddr, sizeof (ipaddr));
p_man_address->len = sizeof (ipaddr);
p_man_address->interface_number.subtype = 2; /* ifIndex */
p_man_address->interface_number.value =
pnal_get_interface_index (net->pf_interface.main_port.name);
p_man_address->is_valid = true;
}
int pf_lldp_get_peer_management_address (
pnet_t * net,
int loc_port_num,
pf_lldp_management_address_t * p_man_address)
{
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
os_mutex_lock (net->lldp_mutex);
*p_man_address = p_port_data->lldp.peer_info.management_address;
os_mutex_unlock (net->lldp_mutex);
return p_man_address->is_valid ? 0 : -1;
}
int pf_lldp_get_peer_station_name (
pnet_t * net,
int loc_port_num,
pf_lldp_station_name_t * p_station_name)
{
pf_lldp_chassis_id_t chassis_id;
pf_lldp_port_id_t port_id;
char * p;
bool found = false;
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
memset (p_station_name, 0, sizeof (*p_station_name));
os_mutex_lock (net->lldp_mutex);
port_id = p_port_data->lldp.peer_info.port_id;
chassis_id = p_port_data->lldp.peer_info.chassis_id;
os_mutex_unlock (net->lldp_mutex);
if (port_id.is_valid)
{
p = strchr (port_id.string, '.');
if (p != NULL)
{
/* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD
* Example: port-001.dut
*/
snprintf (
p_station_name->string,
sizeof (p_station_name->string),
"%s",
p + 1);
found = true;
}
}
if (!found && chassis_id.is_valid)
{
/* PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY */
if (chassis_id.subtype != PF_LLDP_SUBTYPE_MAC)
{
snprintf (
p_station_name->string,
sizeof (p_station_name->string),
"%s",
chassis_id.string);
}
else
{
pf_lldp_mac_address_to_string (
(uint8_t *)chassis_id.string,
p_station_name->string,
sizeof (p_station_name->string));
}
}
/* Do not use peer mac address here since its status is unknown. */
p_station_name->len = strlen (p_station_name->string);
return p_station_name->len ? 0 : -1;
}
int pf_lldp_get_peer_port_name (
pnet_t * net,
int loc_port_num,
pf_lldp_port_name_t * p_port_name)
{
pf_lldp_port_id_t port_id;
size_t i;
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
memset (p_port_name, 0, sizeof (*p_port_name));
os_mutex_lock (net->lldp_mutex);
port_id = p_port_data->lldp.peer_info.port_id;
os_mutex_unlock (net->lldp_mutex);
if (port_id.is_valid)
{
for (i = 0; (i < port_id.len) && (port_id.string[i] != '\0') &&
(i < (sizeof (p_port_name->string) - 1));
i++)
{
if (port_id.string[i] == '.')
{
/* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
break;
}
p_port_name->string[i] = port_id.string[i];
}
p_port_name->len = strlen (p_port_name->string);
}
else
{
return -1;
}
return p_port_name->len ? 0 : -1;
}
void pf_lldp_get_signal_delays (
pnet_t * net,
int loc_port_num,
pf_lldp_signal_delay_t * p_delays)
{
/* Line delay measurement is not supported */
p_delays->cable_delay_local = 0; /* Not measured */
p_delays->rx_delay_local = 0; /* Not measured */
p_delays->tx_delay_local = 0; /* Not measured */
p_delays->rx_delay_remote = 0; /* Not measured */
p_delays->tx_delay_remote = 0; /* Not measured */
p_delays->is_valid = true;
}
int pf_lldp_get_peer_signal_delays (
pnet_t * net,
int loc_port_num,
pf_lldp_signal_delay_t * p_delays)
{
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
os_mutex_lock (net->lldp_mutex);
*p_delays = p_port_data->lldp.peer_info.port_delay;
os_mutex_unlock (net->lldp_mutex);
return p_delays->is_valid ? 0 : -1;
}
void pf_lldp_get_link_status (
pnet_t * net,
int loc_port_num,
pf_lldp_link_status_t * p_link_status)
{
pnal_eth_status_t status;
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
/* TODO: Better error handling */
if (pnal_eth_get_status (p_port_data->netif.name, &status) != 0)
{
LOG_ERROR (
PF_LLDP_LOG,
"LLDP(%d): Failed to read Ethernet port status for port %d\n",
__LINE__,
loc_port_num);
memset (&status, 0, sizeof (status));
}
p_link_status->is_autonegotiation_supported =
status.is_autonegotiation_supported;
p_link_status->is_autonegotiation_enabled =
status.is_autonegotiation_enabled;
p_link_status->autonegotiation_advertised_capabilities =
status.autonegotiation_advertised_capabilities;
p_link_status->operational_mau_type = status.operational_mau_type;
p_link_status->is_valid = true;
}
int pf_lldp_get_peer_link_status (
pnet_t * net,
int loc_port_num,
pf_lldp_link_status_t * p_link_status)
{
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
os_mutex_lock (net->lldp_mutex);
*p_link_status = p_port_data->lldp.peer_info.phy_config;
os_mutex_unlock (net->lldp_mutex);
return p_link_status->is_valid ? 0 : -1;
}
size_t pf_lldp_get_system_description (
const pnet_t * net,
char * system_description,
size_t system_description_max_len)
{
return snprintf (
system_description,
system_description_max_len,
/* clang-format off */
"%-" STRINGIFY (PNET_PRODUCT_NAME_MAX_LEN) "s "
"%-" STRINGIFY (PNET_ORDER_ID_MAX_LEN) "s "
"%-" STRINGIFY (PNET_SERIAL_NUMBER_MAX_LEN) "s "
"%5u "
"%c%3u%3u%3u",
/* clang-format on */
net->fspm_cfg.product_name,
net->fspm_cfg.im_0_data.im_order_id,
net->fspm_cfg.im_0_data.im_serial_number,
net->fspm_cfg.im_0_data.im_hardware_revision,
net->fspm_cfg.im_0_data.im_sw_revision_prefix,
net->fspm_cfg.im_0_data.im_sw_revision_functional_enhancement,
net->fspm_cfg.im_0_data.im_sw_revision_bug_fix,
net->fspm_cfg.im_0_data.im_sw_revision_internal_change);
}
void pf_lldp_mac_address_to_string (
const uint8_t * mac,
char * mac_str,
size_t mac_str_max_len)
{
snprintf (
mac_str,
mac_str_max_len,
"%02X-%02X-%02X-%02X-%02X-%02X",
mac[0],
mac[1],
mac[2],
mac[3],
mac[4],
mac[5]);
}
/**
* Construct Ethernet frame containing LLDP PDU as payload
*
* @param net In: The p-net stack instance.
* @param loc_port_num In: Local port number.
* Valid range: 1 .. num_physical_ports
* @param buf Out: Ethernet frame buffer of size
* PF_FRAME_BUFFER_SIZE bytes.
*
* @return Size of constructed frame, in bytes.
*/
size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[])