1 /** @file
2 @brief Ethernet
3
4 This is not to be included by the application.
5 */
6
7 /*
8 * Copyright (c) 2016 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13 #ifndef ZEPHYR_INCLUDE_NET_ETHERNET_H_
14 #define ZEPHYR_INCLUDE_NET_ETHERNET_H_
15
16 #include <zephyr/kernel.h>
17 #include <zephyr/types.h>
18 #include <stdbool.h>
19 #include <zephyr/sys/atomic.h>
20
21 #include <zephyr/net/net_ip.h>
22 #include <zephyr/net/net_pkt.h>
23
24 #if defined(CONFIG_NET_LLDP)
25 #include <zephyr/net/lldp.h>
26 #endif
27
28 #include <zephyr/sys/util.h>
29 #include <zephyr/net/net_if.h>
30 #include <zephyr/net/ethernet_vlan.h>
31 #include <zephyr/net/ptp_time.h>
32
33 #if defined(CONFIG_NET_DSA)
34 #include <zephyr/net/dsa.h>
35 #endif
36
37 #if defined(CONFIG_NET_ETHERNET_BRIDGE)
38 #include <zephyr/net/ethernet_bridge.h>
39 #endif
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /**
46 * @brief Ethernet support functions
47 * @defgroup ethernet Ethernet Support Functions
48 * @ingroup networking
49 * @{
50 */
51
52 /** @cond INTERNAL_HIDDEN */
53
54 struct net_eth_addr {
55 uint8_t addr[6];
56 };
57
58 #define NET_ETH_HDR(pkt) ((struct net_eth_hdr *)net_pkt_data(pkt))
59
60 #define NET_ETH_PTYPE_ARP 0x0806
61 #define NET_ETH_PTYPE_IP 0x0800
62 #define NET_ETH_PTYPE_TSN 0x22f0 /* TSN (IEEE 1722) packet */
63 #define NET_ETH_PTYPE_IPV6 0x86dd
64 #define NET_ETH_PTYPE_VLAN 0x8100
65 #define NET_ETH_PTYPE_PTP 0x88f7
66 #define NET_ETH_PTYPE_LLDP 0x88cc
67 #define NET_ETH_PTYPE_ALL 0x0003 /* from linux/if_ether.h */
68 #define NET_ETH_PTYPE_ECAT 0x88a4
69 #define NET_ETH_PTYPE_EAPOL 0x888e
70 #define NET_ETH_PTYPE_IEEE802154 0x00F6 /* from linux/if_ether.h: IEEE802.15.4 frame */
71
72 #if !defined(ETH_P_ALL)
73 #define ETH_P_ALL NET_ETH_PTYPE_ALL
74 #endif
75 #if !defined(ETH_P_IP)
76 #define ETH_P_IP NET_ETH_PTYPE_IP
77 #endif
78 #if !defined(ETH_P_ARP)
79 #define ETH_P_ARP NET_ETH_PTYPE_ARP
80 #endif
81 #if !defined(ETH_P_IPV6)
82 #define ETH_P_IPV6 NET_ETH_PTYPE_IPV6
83 #endif
84 #if !defined(ETH_P_8021Q)
85 #define ETH_P_8021Q NET_ETH_PTYPE_VLAN
86 #endif
87 #if !defined(ETH_P_TSN)
88 #define ETH_P_TSN NET_ETH_PTYPE_TSN
89 #endif
90 #if !defined(ETH_P_ECAT)
91 #define ETH_P_ECAT NET_ETH_PTYPE_ECAT
92 #endif
93 #if !defined(ETH_P_IEEE802154)
94 #define ETH_P_IEEE802154 NET_ETH_PTYPE_IEEE802154
95 #endif
96
97 #define NET_ETH_MINIMAL_FRAME_SIZE 60
98 #define NET_ETH_MTU 1500
99
100 #if defined(CONFIG_NET_VLAN)
101 #define _NET_ETH_MAX_HDR_SIZE (sizeof(struct net_eth_vlan_hdr))
102 #else
103 #define _NET_ETH_MAX_HDR_SIZE (sizeof(struct net_eth_hdr))
104 #endif
105
106 #define _NET_ETH_MAX_FRAME_SIZE (NET_ETH_MTU + _NET_ETH_MAX_HDR_SIZE)
107 /*
108 * Extend the max frame size for DSA (KSZ8794) by one byte (to 1519) to
109 * store tail tag.
110 */
111 #if defined(CONFIG_NET_DSA)
112 #define NET_ETH_MAX_FRAME_SIZE (_NET_ETH_MAX_FRAME_SIZE + DSA_TAG_SIZE)
113 #define NET_ETH_MAX_HDR_SIZE (_NET_ETH_MAX_HDR_SIZE + DSA_TAG_SIZE)
114 #else
115 #define NET_ETH_MAX_FRAME_SIZE (_NET_ETH_MAX_FRAME_SIZE)
116 #define NET_ETH_MAX_HDR_SIZE (_NET_ETH_MAX_HDR_SIZE)
117 #endif
118
119 #define NET_ETH_VLAN_HDR_SIZE 4
120
121 /** @endcond */
122
123 /** Ethernet hardware capabilities */
124 enum ethernet_hw_caps {
125 /** TX Checksum offloading supported for all of IPv4, UDP, TCP */
126 ETHERNET_HW_TX_CHKSUM_OFFLOAD = BIT(0),
127
128 /** RX Checksum offloading supported for all of IPv4, UDP, TCP */
129 ETHERNET_HW_RX_CHKSUM_OFFLOAD = BIT(1),
130
131 /** VLAN supported */
132 ETHERNET_HW_VLAN = BIT(2),
133
134 /** Enabling/disabling auto negotiation supported */
135 ETHERNET_AUTO_NEGOTIATION_SET = BIT(3),
136
137 /** 10 Mbits link supported */
138 ETHERNET_LINK_10BASE_T = BIT(4),
139
140 /** 100 Mbits link supported */
141 ETHERNET_LINK_100BASE_T = BIT(5),
142
143 /** 1 Gbits link supported */
144 ETHERNET_LINK_1000BASE_T = BIT(6),
145
146 /** Changing duplex (half/full) supported */
147 ETHERNET_DUPLEX_SET = BIT(7),
148
149 /** IEEE 802.1AS (gPTP) clock supported */
150 ETHERNET_PTP = BIT(8),
151
152 /** IEEE 802.1Qav (credit-based shaping) supported */
153 ETHERNET_QAV = BIT(9),
154
155 /** Promiscuous mode supported */
156 ETHERNET_PROMISC_MODE = BIT(10),
157
158 /** Priority queues available */
159 ETHERNET_PRIORITY_QUEUES = BIT(11),
160
161 /** MAC address filtering supported */
162 ETHERNET_HW_FILTERING = BIT(12),
163
164 /** Link Layer Discovery Protocol supported */
165 ETHERNET_LLDP = BIT(13),
166
167 /** VLAN Tag stripping */
168 ETHERNET_HW_VLAN_TAG_STRIP = BIT(14),
169
170 /** DSA switch */
171 ETHERNET_DSA_SLAVE_PORT = BIT(15),
172 ETHERNET_DSA_MASTER_PORT = BIT(16),
173
174 /** IEEE 802.1Qbv (scheduled traffic) supported */
175 ETHERNET_QBV = BIT(17),
176
177 /** IEEE 802.1Qbu (frame preemption) supported */
178 ETHERNET_QBU = BIT(18),
179
180 /** TXTIME supported */
181 ETHERNET_TXTIME = BIT(19),
182 };
183
184 /** @cond INTERNAL_HIDDEN */
185
186 enum ethernet_config_type {
187 ETHERNET_CONFIG_TYPE_AUTO_NEG,
188 ETHERNET_CONFIG_TYPE_LINK,
189 ETHERNET_CONFIG_TYPE_DUPLEX,
190 ETHERNET_CONFIG_TYPE_MAC_ADDRESS,
191 ETHERNET_CONFIG_TYPE_QAV_PARAM,
192 ETHERNET_CONFIG_TYPE_QBV_PARAM,
193 ETHERNET_CONFIG_TYPE_QBU_PARAM,
194 ETHERNET_CONFIG_TYPE_TXTIME_PARAM,
195 ETHERNET_CONFIG_TYPE_PROMISC_MODE,
196 ETHERNET_CONFIG_TYPE_PRIORITY_QUEUES_NUM,
197 ETHERNET_CONFIG_TYPE_FILTER,
198 ETHERNET_CONFIG_TYPE_PORTS_NUM,
199 };
200
201 enum ethernet_qav_param_type {
202 ETHERNET_QAV_PARAM_TYPE_DELTA_BANDWIDTH,
203 ETHERNET_QAV_PARAM_TYPE_IDLE_SLOPE,
204 ETHERNET_QAV_PARAM_TYPE_OPER_IDLE_SLOPE,
205 ETHERNET_QAV_PARAM_TYPE_TRAFFIC_CLASS,
206 ETHERNET_QAV_PARAM_TYPE_STATUS,
207 };
208
209 /** @endcond */
210
211 struct ethernet_qav_param {
212 /** ID of the priority queue to use */
213 int queue_id;
214 /** Type of Qav parameter */
215 enum ethernet_qav_param_type type;
216 union {
217 /** True if Qav is enabled for queue */
218 bool enabled;
219 /** Delta Bandwidth (percentage of bandwidth) */
220 unsigned int delta_bandwidth;
221 /** Idle Slope (bits per second) */
222 unsigned int idle_slope;
223 /** Oper Idle Slope (bits per second) */
224 unsigned int oper_idle_slope;
225 /** Traffic class the queue is bound to */
226 unsigned int traffic_class;
227 };
228 };
229
230 /** @cond INTERNAL_HIDDEN */
231
232 enum ethernet_qbv_param_type {
233 ETHERNET_QBV_PARAM_TYPE_STATUS,
234 ETHERNET_QBV_PARAM_TYPE_GATE_CONTROL_LIST,
235 ETHERNET_QBV_PARAM_TYPE_GATE_CONTROL_LIST_LEN,
236 ETHERNET_QBV_PARAM_TYPE_TIME,
237 };
238
239 enum ethernet_qbv_state_type {
240 ETHERNET_QBV_STATE_TYPE_ADMIN,
241 ETHERNET_QBV_STATE_TYPE_OPER,
242 };
243
244 enum ethernet_gate_state_operation {
245 ETHERNET_SET_GATE_STATE,
246 ETHERNET_SET_AND_HOLD_MAC_STATE,
247 ETHERNET_SET_AND_RELEASE_MAC_STATE,
248 };
249
250 /** @endcond */
251
252 struct ethernet_qbv_param {
253 /** Port id */
254 int port_id;
255 /** Type of Qbv parameter */
256 enum ethernet_qbv_param_type type;
257 /** What state (Admin/Oper) parameters are these */
258 enum ethernet_qbv_state_type state;
259 union {
260 /** True if Qbv is enabled or not */
261 bool enabled;
262
263 struct {
264 /** True = open, False = closed */
265 bool gate_status[NET_TC_TX_COUNT];
266
267 /** GateState operation */
268 enum ethernet_gate_state_operation operation;
269
270 /** Time interval ticks (nanoseconds) */
271 uint32_t time_interval;
272
273 /** Gate control list row */
274 uint16_t row;
275 } gate_control;
276
277 /** Number of entries in gate control list */
278 uint32_t gate_control_list_len;
279
280 /* The time values are set in one go when type is set to
281 * ETHERNET_QBV_PARAM_TYPE_TIME
282 */
283 struct {
284 /** Base time */
285 struct net_ptp_extended_time base_time;
286
287 /** Cycle time */
288 struct net_ptp_time cycle_time;
289
290 /** Extension time (nanoseconds) */
291 uint32_t extension_time;
292 };
293 };
294 };
295
296 /** @cond INTERNAL_HIDDEN */
297
298 enum ethernet_qbu_param_type {
299 ETHERNET_QBU_PARAM_TYPE_STATUS,
300 ETHERNET_QBU_PARAM_TYPE_RELEASE_ADVANCE,
301 ETHERNET_QBU_PARAM_TYPE_HOLD_ADVANCE,
302 ETHERNET_QBU_PARAM_TYPE_PREEMPTION_STATUS_TABLE,
303
304 /* Some preemption settings are from Qbr spec. */
305 ETHERNET_QBR_PARAM_TYPE_LINK_PARTNER_STATUS,
306 ETHERNET_QBR_PARAM_TYPE_ADDITIONAL_FRAGMENT_SIZE,
307 };
308
309 enum ethernet_qbu_preempt_status {
310 ETHERNET_QBU_STATUS_EXPRESS,
311 ETHERNET_QBU_STATUS_PREEMPTABLE
312 } __packed;
313
314 /** @endcond */
315
316 struct ethernet_qbu_param {
317 /** Port id */
318 int port_id;
319 /** Type of Qbu parameter */
320 enum ethernet_qbu_param_type type;
321 union {
322 /** Hold advance (nanoseconds) */
323 uint32_t hold_advance;
324
325 /** Release advance (nanoseconds) */
326 uint32_t release_advance;
327
328 /** sequence of framePreemptionAdminStatus values.
329 */
330 enum ethernet_qbu_preempt_status
331 frame_preempt_statuses[NET_TC_TX_COUNT];
332
333 /** True if Qbu is enabled or not */
334 bool enabled;
335
336 /** Link partner status (from Qbr) */
337 bool link_partner_status;
338
339 /** Additional fragment size (from Qbr). The minimum non-final
340 * fragment size is (additional_fragment_size + 1) * 64 octets
341 */
342 uint8_t additional_fragment_size : 2;
343 };
344 };
345
346
347 /** @cond INTERNAL_HIDDEN */
348
349 enum ethernet_filter_type {
350 ETHERNET_FILTER_TYPE_SRC_MAC_ADDRESS,
351 ETHERNET_FILTER_TYPE_DST_MAC_ADDRESS,
352 };
353
354 /** @endcond */
355
356 /** Types of Ethernet L2 */
357 enum ethernet_if_types {
358 /** IEEE 802.3 Ethernet (default) */
359 L2_ETH_IF_TYPE_ETHERNET,
360
361 /** IEEE 802.11 Wi-Fi*/
362 L2_ETH_IF_TYPE_WIFI,
363 } __packed;
364
365
366 struct ethernet_filter {
367 /** Type of filter */
368 enum ethernet_filter_type type;
369 /** MAC address to filter */
370 struct net_eth_addr mac_address;
371 /** Set (true) or unset (false) the filter */
372 bool set;
373 };
374
375 /** @cond INTERNAL_HIDDEN */
376
377 enum ethernet_txtime_param_type {
378 ETHERNET_TXTIME_PARAM_TYPE_ENABLE_QUEUES,
379 };
380
381 /** @endcond */
382
383 struct ethernet_txtime_param {
384 /** Type of TXTIME parameter */
385 enum ethernet_txtime_param_type type;
386 /** Queue number for configuring TXTIME */
387 int queue_id;
388 /** Enable or disable TXTIME per queue */
389 bool enable_txtime;
390 };
391
392 /** @cond INTERNAL_HIDDEN */
393 struct ethernet_config {
394 union {
395 bool auto_negotiation;
396 bool full_duplex;
397 bool promisc_mode;
398
399 struct {
400 bool link_10bt;
401 bool link_100bt;
402 bool link_1000bt;
403 } l;
404
405 struct net_eth_addr mac_address;
406
407 struct ethernet_qav_param qav_param;
408 struct ethernet_qbv_param qbv_param;
409 struct ethernet_qbu_param qbu_param;
410 struct ethernet_txtime_param txtime_param;
411
412 int priority_queues_num;
413 int ports_num;
414
415 struct ethernet_filter filter;
416 };
417 };
418 /** @endcond */
419
420 struct ethernet_api {
421 /**
422 * The net_if_api must be placed in first position in this
423 * struct so that we are compatible with network interface API.
424 */
425 struct net_if_api iface_api;
426
427 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
428 /** Collect optional ethernet specific statistics. This pointer
429 * should be set by driver if statistics needs to be collected
430 * for that driver.
431 */
432 struct net_stats_eth *(*get_stats)(const struct device *dev);
433 #endif
434
435 /** Start the device */
436 int (*start)(const struct device *dev);
437
438 /** Stop the device */
439 int (*stop)(const struct device *dev);
440
441 /** Get the device capabilities */
442 enum ethernet_hw_caps (*get_capabilities)(const struct device *dev);
443
444 /** Set specific hardware configuration */
445 int (*set_config)(const struct device *dev,
446 enum ethernet_config_type type,
447 const struct ethernet_config *config);
448
449 /** Get hardware specific configuration */
450 int (*get_config)(const struct device *dev,
451 enum ethernet_config_type type,
452 struct ethernet_config *config);
453
454 #if defined(CONFIG_NET_VLAN)
455 /** The IP stack will call this function when a VLAN tag is enabled
456 * or disabled. If enable is set to true, then the VLAN tag was added,
457 * if it is false then the tag was removed. The driver can utilize
458 * this information if needed.
459 */
460 int (*vlan_setup)(const struct device *dev, struct net_if *iface,
461 uint16_t tag, bool enable);
462 #endif /* CONFIG_NET_VLAN */
463
464 #if defined(CONFIG_PTP_CLOCK)
465 /** Return ptp_clock device that is tied to this ethernet device */
466 const struct device *(*get_ptp_clock)(const struct device *dev);
467 #endif /* CONFIG_PTP_CLOCK */
468
469 /** Send a network packet */
470 int (*send)(const struct device *dev, struct net_pkt *pkt);
471 };
472
473 /* Make sure that the network interface API is properly setup inside
474 * Ethernet API struct (it is the first one).
475 */
476 BUILD_ASSERT(offsetof(struct ethernet_api, iface_api) == 0);
477
478 /** @cond INTERNAL_HIDDEN */
479 struct net_eth_hdr {
480 struct net_eth_addr dst;
481 struct net_eth_addr src;
482 uint16_t type;
483 } __packed;
484
485 struct ethernet_vlan {
486 /** Network interface that has VLAN enabled */
487 struct net_if *iface;
488
489 /** VLAN tag */
490 uint16_t tag;
491 };
492
493 #if defined(CONFIG_NET_VLAN_COUNT)
494 #define NET_VLAN_MAX_COUNT CONFIG_NET_VLAN_COUNT
495 #else
496 /* Even thou there are no VLAN support, the minimum count must be set to 1.
497 */
498 #define NET_VLAN_MAX_COUNT 1
499 #endif
500
501 /** @endcond */
502
503 #if defined(CONFIG_NET_LLDP)
504 struct ethernet_lldp {
505 /** Used for track timers */
506 sys_snode_t node;
507
508 /** LLDP Data Unit mandatory TLVs for the interface. */
509 const struct net_lldpdu *lldpdu;
510
511 /** LLDP Data Unit optional TLVs for the interface */
512 const uint8_t *optional_du;
513
514 /** Length of the optional Data Unit TLVs */
515 size_t optional_len;
516
517 /** Network interface that has LLDP supported. */
518 struct net_if *iface;
519
520 /** LLDP TX timer start time */
521 int64_t tx_timer_start;
522
523 /** LLDP TX timeout */
524 uint32_t tx_timer_timeout;
525
526 /** LLDP RX callback function */
527 net_lldp_recv_cb_t cb;
528 };
529 #endif /* CONFIG_NET_LLDP */
530
531 enum ethernet_flags {
532 ETH_CARRIER_UP,
533 };
534
535 /** Ethernet L2 context that is needed for VLAN */
536 struct ethernet_context {
537 /** Flags representing ethernet state, which are accessed from multiple
538 * threads.
539 */
540 atomic_t flags;
541
542 #if defined(CONFIG_NET_VLAN)
543 struct ethernet_vlan vlan[NET_VLAN_MAX_COUNT];
544
545 /** Array that will help when checking if VLAN is enabled for
546 * some specific network interface. Requires that VLAN count
547 * NET_VLAN_MAX_COUNT is not smaller than the actual number
548 * of network interfaces.
549 */
550 ATOMIC_DEFINE(interfaces, NET_VLAN_MAX_COUNT);
551 #endif
552
553 #if defined(CONFIG_NET_ETHERNET_BRIDGE)
554 struct eth_bridge_iface_context bridge;
555 #endif
556
557 /** Carrier ON/OFF handler worker. This is used to create
558 * network interface UP/DOWN event when ethernet L2 driver
559 * notices carrier ON/OFF situation. We must not create another
560 * network management event from inside management handler thus
561 * we use worker thread to trigger the UP/DOWN event.
562 */
563 struct k_work carrier_work;
564
565 /** Network interface. */
566 struct net_if *iface;
567
568 #if defined(CONFIG_NET_LLDP)
569 struct ethernet_lldp lldp[NET_VLAN_MAX_COUNT];
570 #endif
571
572 /**
573 * This tells what L2 features does ethernet support.
574 */
575 enum net_l2_flags ethernet_l2_flags;
576
577 #if defined(CONFIG_NET_L2_PTP)
578 /** The PTP port number for this network device. We need to store the
579 * port number here so that we do not need to fetch it for every
580 * incoming PTP packet.
581 */
582 int port;
583 #endif
584
585 #if defined(CONFIG_NET_DSA)
586 /** DSA RX callback function - for custom processing - like e.g.
587 * redirecting packets when MAC address is caught
588 */
589 dsa_net_recv_cb_t dsa_recv_cb;
590
591 /** Switch physical port number */
592 uint8_t dsa_port_idx;
593
594 /** DSA context pointer */
595 struct dsa_context *dsa_ctx;
596
597 /** Send a network packet via DSA master port */
598 dsa_send_t dsa_send;
599 #endif
600
601 #if defined(CONFIG_NET_VLAN)
602 /** Flag that tells whether how many VLAN tags are enabled for this
603 * context. The same information can be dug from the vlan array but
604 * this saves some time in RX path.
605 */
606 int8_t vlan_enabled;
607 #endif
608
609 /** Is network carrier up */
610 bool is_net_carrier_up : 1;
611
612 /** Is this context already initialized */
613 bool is_init : 1;
614
615 /** Types of Ethernet network interfaces */
616 enum ethernet_if_types eth_if_type;
617 };
618
619 /**
620 * @brief Initialize Ethernet L2 stack for a given interface
621 *
622 * @param iface A valid pointer to a network interface
623 */
624 void ethernet_init(struct net_if *iface);
625
626 /** @cond INTERNAL_HIDDEN */
627
628 #define ETHERNET_L2_CTX_TYPE struct ethernet_context
629
630 /* Separate header for VLAN as some of device interfaces might not
631 * support VLAN.
632 */
633 struct net_eth_vlan_hdr {
634 struct net_eth_addr dst;
635 struct net_eth_addr src;
636 struct {
637 uint16_t tpid; /* tag protocol id */
638 uint16_t tci; /* tag control info */
639 } vlan;
640 uint16_t type;
641 } __packed;
642
643
net_eth_is_addr_broadcast(struct net_eth_addr * addr)644 static inline bool net_eth_is_addr_broadcast(struct net_eth_addr *addr)
645 {
646 if (addr->addr[0] == 0xff &&
647 addr->addr[1] == 0xff &&
648 addr->addr[2] == 0xff &&
649 addr->addr[3] == 0xff &&
650 addr->addr[4] == 0xff &&
651 addr->addr[5] == 0xff) {
652 return true;
653 }
654
655 return false;
656 }
657
net_eth_is_addr_unspecified(struct net_eth_addr * addr)658 static inline bool net_eth_is_addr_unspecified(struct net_eth_addr *addr)
659 {
660 if (addr->addr[0] == 0x00 &&
661 addr->addr[1] == 0x00 &&
662 addr->addr[2] == 0x00 &&
663 addr->addr[3] == 0x00 &&
664 addr->addr[4] == 0x00 &&
665 addr->addr[5] == 0x00) {
666 return true;
667 }
668
669 return false;
670 }
671
net_eth_is_addr_multicast(struct net_eth_addr * addr)672 static inline bool net_eth_is_addr_multicast(struct net_eth_addr *addr)
673 {
674 #if defined(CONFIG_NET_IPV6)
675 if (addr->addr[0] == 0x33 &&
676 addr->addr[1] == 0x33) {
677 return true;
678 }
679 #endif
680
681 #if defined(CONFIG_NET_IPV4)
682 if (addr->addr[0] == 0x01 &&
683 addr->addr[1] == 0x00 &&
684 addr->addr[2] == 0x5e) {
685 return true;
686 }
687 #endif
688
689 return false;
690 }
691
net_eth_is_addr_group(struct net_eth_addr * addr)692 static inline bool net_eth_is_addr_group(struct net_eth_addr *addr)
693 {
694 return addr->addr[0] & 0x01;
695 }
696
net_eth_is_addr_valid(struct net_eth_addr * addr)697 static inline bool net_eth_is_addr_valid(struct net_eth_addr *addr)
698 {
699 return !net_eth_is_addr_unspecified(addr) && !net_eth_is_addr_group(addr);
700 }
701
net_eth_is_addr_lldp_multicast(struct net_eth_addr * addr)702 static inline bool net_eth_is_addr_lldp_multicast(struct net_eth_addr *addr)
703 {
704 #if defined(CONFIG_NET_GPTP) || defined(CONFIG_NET_LLDP)
705 if (addr->addr[0] == 0x01 &&
706 addr->addr[1] == 0x80 &&
707 addr->addr[2] == 0xc2 &&
708 addr->addr[3] == 0x00 &&
709 addr->addr[4] == 0x00 &&
710 addr->addr[5] == 0x0e) {
711 return true;
712 }
713 #endif
714
715 return false;
716 }
717
net_eth_is_addr_ptp_multicast(struct net_eth_addr * addr)718 static inline bool net_eth_is_addr_ptp_multicast(struct net_eth_addr *addr)
719 {
720 #if defined(CONFIG_NET_GPTP)
721 if (addr->addr[0] == 0x01 &&
722 addr->addr[1] == 0x1b &&
723 addr->addr[2] == 0x19 &&
724 addr->addr[3] == 0x00 &&
725 addr->addr[4] == 0x00 &&
726 addr->addr[5] == 0x00) {
727 return true;
728 }
729 #endif
730
731 return false;
732 }
733
734 const struct net_eth_addr *net_eth_broadcast_addr(void);
735
736 /** @endcond */
737
738 /**
739 * @brief Convert IPv4 multicast address to Ethernet address.
740 *
741 * @param ipv4_addr IPv4 multicast address
742 * @param mac_addr Output buffer for Ethernet address
743 */
744 void net_eth_ipv4_mcast_to_mac_addr(const struct in_addr *ipv4_addr,
745 struct net_eth_addr *mac_addr);
746
747 /**
748 * @brief Convert IPv6 multicast address to Ethernet address.
749 *
750 * @param ipv6_addr IPv6 multicast address
751 * @param mac_addr Output buffer for Ethernet address
752 */
753 void net_eth_ipv6_mcast_to_mac_addr(const struct in6_addr *ipv6_addr,
754 struct net_eth_addr *mac_addr);
755
756 /**
757 * @brief Return ethernet device hardware capability information.
758 *
759 * @param iface Network interface
760 *
761 * @return Hardware capabilities
762 */
763 static inline
net_eth_get_hw_capabilities(struct net_if * iface)764 enum ethernet_hw_caps net_eth_get_hw_capabilities(struct net_if *iface)
765 {
766 const struct ethernet_api *eth =
767 (struct ethernet_api *)net_if_get_device(iface)->api;
768
769 if (!eth->get_capabilities) {
770 return (enum ethernet_hw_caps)0;
771 }
772
773 return eth->get_capabilities(net_if_get_device(iface));
774 }
775
776 /**
777 * @brief Add VLAN tag to the interface.
778 *
779 * @param iface Interface to use.
780 * @param tag VLAN tag to add
781 *
782 * @return 0 if ok, <0 if error
783 */
784 #if defined(CONFIG_NET_VLAN)
785 int net_eth_vlan_enable(struct net_if *iface, uint16_t tag);
786 #else
net_eth_vlan_enable(struct net_if * iface,uint16_t tag)787 static inline int net_eth_vlan_enable(struct net_if *iface, uint16_t tag)
788 {
789 return -EINVAL;
790 }
791 #endif
792
793 /**
794 * @brief Remove VLAN tag from the interface.
795 *
796 * @param iface Interface to use.
797 * @param tag VLAN tag to remove
798 *
799 * @return 0 if ok, <0 if error
800 */
801 #if defined(CONFIG_NET_VLAN)
802 int net_eth_vlan_disable(struct net_if *iface, uint16_t tag);
803 #else
net_eth_vlan_disable(struct net_if * iface,uint16_t tag)804 static inline int net_eth_vlan_disable(struct net_if *iface, uint16_t tag)
805 {
806 return -EINVAL;
807 }
808 #endif
809
810 /**
811 * @brief Return VLAN tag specified to network interface
812 *
813 * @param iface Network interface.
814 *
815 * @return VLAN tag for this interface or NET_VLAN_TAG_UNSPEC if VLAN
816 * is not configured for that interface.
817 */
818 #if defined(CONFIG_NET_VLAN)
819 uint16_t net_eth_get_vlan_tag(struct net_if *iface);
820 #else
net_eth_get_vlan_tag(struct net_if * iface)821 static inline uint16_t net_eth_get_vlan_tag(struct net_if *iface)
822 {
823 return NET_VLAN_TAG_UNSPEC;
824 }
825 #endif
826
827 /**
828 * @brief Return network interface related to this VLAN tag
829 *
830 * @param iface Master network interface. This is used to get the
831 * pointer to ethernet L2 context
832 * @param tag VLAN tag
833 *
834 * @return Network interface related to this tag or NULL if no such interface
835 * exists.
836 */
837 #if defined(CONFIG_NET_VLAN)
838 struct net_if *net_eth_get_vlan_iface(struct net_if *iface, uint16_t tag);
839 #else
840 static inline
net_eth_get_vlan_iface(struct net_if * iface,uint16_t tag)841 struct net_if *net_eth_get_vlan_iface(struct net_if *iface, uint16_t tag)
842 {
843 return NULL;
844 }
845 #endif
846
847 /**
848 * @brief Check if VLAN is enabled for a specific network interface.
849 *
850 * @param ctx Ethernet context
851 * @param iface Network interface
852 *
853 * @return True if VLAN is enabled for this network interface, false if not.
854 */
855 #if defined(CONFIG_NET_VLAN)
856 bool net_eth_is_vlan_enabled(struct ethernet_context *ctx,
857 struct net_if *iface);
858 #else
net_eth_is_vlan_enabled(struct ethernet_context * ctx,struct net_if * iface)859 static inline bool net_eth_is_vlan_enabled(struct ethernet_context *ctx,
860 struct net_if *iface)
861 {
862 return false;
863 }
864 #endif
865
866 /**
867 * @brief Get VLAN status for a given network interface (enabled or not).
868 *
869 * @param iface Network interface
870 *
871 * @return True if VLAN is enabled for this network interface, false if not.
872 */
873 #if defined(CONFIG_NET_VLAN)
874 bool net_eth_get_vlan_status(struct net_if *iface);
875 #else
net_eth_get_vlan_status(struct net_if * iface)876 static inline bool net_eth_get_vlan_status(struct net_if *iface)
877 {
878 return false;
879 }
880 #endif
881
882 #if defined(CONFIG_NET_VLAN)
883 #define Z_ETH_NET_DEVICE_INIT(node_id, dev_id, name, init_fn, pm, data, \
884 config, prio, api, mtu) \
885 Z_DEVICE_STATE_DEFINE(dev_id); \
886 Z_DEVICE_DEFINE(node_id, dev_id, name, init_fn, pm, data, \
887 config, POST_KERNEL, prio, api, \
888 &Z_DEVICE_STATE_NAME(dev_id)); \
889 NET_L2_DATA_INIT(dev_id, 0, NET_L2_GET_CTX_TYPE(ETHERNET_L2)); \
890 NET_IF_INIT(dev_id, 0, ETHERNET_L2, mtu, NET_VLAN_MAX_COUNT)
891
892 #else /* CONFIG_NET_VLAN */
893
894 #define Z_ETH_NET_DEVICE_INIT(node_id, dev_id, name, init_fn, pm, data, \
895 config, prio, api, mtu) \
896 Z_NET_DEVICE_INIT(node_id, dev_id, name, init_fn, pm, data, \
897 config, prio, api, ETHERNET_L2, \
898 NET_L2_GET_CTX_TYPE(ETHERNET_L2), mtu)
899 #endif /* CONFIG_NET_VLAN */
900
901 /**
902 * @brief Create an Ethernet network interface and bind it to network device.
903 *
904 * @param dev_id Network device id.
905 * @param name The name this instance of the driver exposes to
906 * the system.
907 * @param init_fn Address to the init function of the driver.
908 * @param pm Reference to struct pm_device associated with the device.
909 * (optional).
910 * @param data Pointer to the device's private data.
911 * @param config The address to the structure containing the
912 * configuration information for this instance of the driver.
913 * @param prio The initialization level at which configuration occurs.
914 * @param api Provides an initial pointer to the API function struct
915 * used by the driver. Can be NULL.
916 * @param mtu Maximum transfer unit in bytes for this network interface.
917 */
918 #define ETH_NET_DEVICE_INIT(dev_id, name, init_fn, pm, data, config, \
919 prio, api, mtu) \
920 Z_ETH_NET_DEVICE_INIT(DT_INVALID_NODE, dev_id, name, init_fn, \
921 pm, data, config, prio, api, mtu)
922
923 /**
924 * @brief Like ETH_NET_DEVICE_INIT but taking metadata from a devicetree.
925 * Create an Ethernet network interface and bind it to network device.
926 *
927 * @param node_id The devicetree node identifier.
928 * @param init_fn Address to the init function of the driver.
929 * @param pm Reference to struct pm_device associated with the device.
930 * (optional).
931 * @param data Pointer to the device's private data.
932 * @param config The address to the structure containing the
933 * configuration information for this instance of the driver.
934 * @param prio The initialization level at which configuration occurs.
935 * @param api Provides an initial pointer to the API function struct
936 * used by the driver. Can be NULL.
937 * @param mtu Maximum transfer unit in bytes for this network interface.
938 */
939 #define ETH_NET_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, \
940 prio, api, mtu) \
941 Z_ETH_NET_DEVICE_INIT(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
942 DEVICE_DT_NAME(node_id), init_fn, pm, \
943 data, config, prio, api, mtu)
944
945 /**
946 * @brief Like ETH_NET_DEVICE_DT_DEFINE for an instance of a DT_DRV_COMPAT
947 * compatible
948 *
949 * @param inst instance number. This is replaced by
950 * <tt>DT_DRV_COMPAT(inst)</tt> in the call to ETH_NET_DEVICE_DT_DEFINE.
951 *
952 * @param ... other parameters as expected by ETH_NET_DEVICE_DT_DEFINE.
953 */
954 #define ETH_NET_DEVICE_DT_INST_DEFINE(inst, ...) \
955 ETH_NET_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
956
957 /**
958 * @brief Inform ethernet L2 driver that ethernet carrier is detected.
959 * This happens when cable is connected.
960 *
961 * @param iface Network interface
962 */
963 void net_eth_carrier_on(struct net_if *iface);
964
965 /**
966 * @brief Inform ethernet L2 driver that ethernet carrier was lost.
967 * This happens when cable is disconnected.
968 *
969 * @param iface Network interface
970 */
971 void net_eth_carrier_off(struct net_if *iface);
972
973 /**
974 * @brief Set promiscuous mode either ON or OFF.
975 *
976 * @param iface Network interface
977 *
978 * @param enable on (true) or off (false)
979 *
980 * @return 0 if mode set or unset was successful, <0 otherwise.
981 */
982 int net_eth_promisc_mode(struct net_if *iface, bool enable);
983
984 /**
985 * @brief Return PTP clock that is tied to this ethernet network interface.
986 *
987 * @param iface Network interface
988 *
989 * @return Pointer to PTP clock if found, NULL if not found or if this
990 * ethernet interface does not support PTP.
991 */
992 #if defined(CONFIG_PTP_CLOCK)
993 const struct device *net_eth_get_ptp_clock(struct net_if *iface);
994 #else
net_eth_get_ptp_clock(struct net_if * iface)995 static inline const struct device *net_eth_get_ptp_clock(struct net_if *iface)
996 {
997 ARG_UNUSED(iface);
998
999 return NULL;
1000 }
1001 #endif
1002
1003 /**
1004 * @brief Return PTP clock that is tied to this ethernet network interface
1005 * index.
1006 *
1007 * @param index Network interface index
1008 *
1009 * @return Pointer to PTP clock if found, NULL if not found or if this
1010 * ethernet interface index does not support PTP.
1011 */
1012 __syscall const struct device *net_eth_get_ptp_clock_by_index(int index);
1013
1014 /**
1015 * @brief Return PTP port number attached to this interface.
1016 *
1017 * @param iface Network interface
1018 *
1019 * @return Port number, no such port if < 0
1020 */
1021 #if defined(CONFIG_NET_L2_PTP)
1022 int net_eth_get_ptp_port(struct net_if *iface);
1023 #else
net_eth_get_ptp_port(struct net_if * iface)1024 static inline int net_eth_get_ptp_port(struct net_if *iface)
1025 {
1026 ARG_UNUSED(iface);
1027
1028 return -ENODEV;
1029 }
1030 #endif /* CONFIG_NET_L2_PTP */
1031
1032 /**
1033 * @brief Set PTP port number attached to this interface.
1034 *
1035 * @param iface Network interface
1036 * @param port Port number to set
1037 */
1038 #if defined(CONFIG_NET_L2_PTP)
1039 void net_eth_set_ptp_port(struct net_if *iface, int port);
1040 #else
net_eth_set_ptp_port(struct net_if * iface,int port)1041 static inline void net_eth_set_ptp_port(struct net_if *iface, int port)
1042 {
1043 ARG_UNUSED(iface);
1044 ARG_UNUSED(port);
1045 }
1046 #endif /* CONFIG_NET_L2_PTP */
1047
1048 /**
1049 * @brief Check if the Ethernet L2 network interface can perform Wi-Fi.
1050 *
1051 * @param iface Pointer to network interface
1052 *
1053 * @return True if interface supports Wi-Fi, False otherwise.
1054 */
net_eth_type_is_wifi(struct net_if * iface)1055 static inline bool net_eth_type_is_wifi(struct net_if *iface)
1056 {
1057 const struct ethernet_context *ctx = (struct ethernet_context *)
1058 net_if_l2_data(iface);
1059
1060 return ctx->eth_if_type == L2_ETH_IF_TYPE_WIFI;
1061 }
1062
1063 /**
1064 * @}
1065 */
1066
1067 #ifdef __cplusplus
1068 }
1069 #endif
1070
1071 #include <syscalls/ethernet.h>
1072
1073 #endif /* ZEPHYR_INCLUDE_NET_ETHERNET_H_ */
1074