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