-
Notifications
You must be signed in to change notification settings - Fork 8.3k
doc: connectivity: networking: Add MAC address configuration page #100120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pdgendt
wants to merge
2
commits into
zephyrproject-rtos:main
Choose a base branch
from
pdgendt:doc-eth-mac
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−34
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| .. _mac_address_config: | ||
|
|
||
| MAC Address Configuration | ||
| ************************* | ||
|
|
||
| Ethernet drivers can delegate most MAC address handling during initialization to | ||
| :c:struct:`net_eth_mac_config` and :c:func:`net_eth_mac_load`. The structure | ||
| is typically stored in the driver configuration and initialized with | ||
| :c:macro:`NET_ETH_MAC_DT_CONFIG_INIT` or :c:macro:`NET_ETH_MAC_DT_INST_CONFIG_INIT`, | ||
| which translate the devicetree properties into one of the following behaviors: | ||
|
|
||
| * :c:enumerator:`NET_ETH_MAC_STATIC` – use the full ``local-mac-address`` property. | ||
| * :c:enumerator:`NET_ETH_MAC_RANDOM` – generate a random locally administered MAC address, | ||
| optionally using the bytes provided in ``zephyr,mac-address-prefix`` as the first octets. | ||
| * :c:enumerator:`NET_ETH_MAC_NVMEM` – read the remaining bytes from the ``"mac-address"`` | ||
| :ref:`NVMEM<nvmem>` cell, again optionally prefixed by ``zephyr,mac-address-prefix``. | ||
| * :c:enumerator:`NET_ETH_MAC_DEFAULT` – fall back to the driver's default logic (for | ||
| example, a factory-programmed MAC address stored in peripheral registers). | ||
|
|
||
| Driver integration | ||
| ================== | ||
|
|
||
| Embed the :c:struct:`net_eth_mac_config` structure inside the driver's configuration | ||
| and a static buffer inside the driver's data: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| struct my_eth_config { | ||
| struct net_eth_mac_config mac_cfg; | ||
| /* more config fields */ | ||
| }; | ||
|
|
||
| struct my_eth_data { | ||
| uint8_t mac_addr[NET_ETH_ADDR_LEN]; | ||
| /* more data fields */ | ||
| }; | ||
|
|
||
| static const struct my_eth_config my_eth_config_0 = { | ||
| .mac_cfg = NET_ETH_MAC_DT_INST_CONFIG_INIT(0), | ||
| }; | ||
| static struct my_eth_data my_eth_data_0; | ||
|
|
||
| During initialization, call :c:func:`net_eth_mac_load` before registering | ||
| the address with the network interface. The helper copies any statically provided | ||
| bytes, fills the remaining octets, and performs the necessary validation. | ||
| Drivers can still fall back to SoC-specific storage when no configuration was provided: | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| static int my_eth_init(const struct device *dev) | ||
| { | ||
| const struct my_eth_config *cfg = dev->config; | ||
| struct my_eth_data *data = dev->data; | ||
| int ret; | ||
|
|
||
| ret = net_eth_mac_load(&cfg->mac_cfg, data->mac_addr); | ||
| if (ret == -ENODATA) { | ||
| ret = my_eth_hw_read_mac(dev, data->mac_addr); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| static void my_eth_iface_init(struct net_if *iface) | ||
| { | ||
| const struct device *dev = net_if_get_device(iface); | ||
| struct my_eth_data *data = dev->data; | ||
|
|
||
| net_if_set_link_addr(iface, data->mac_addr, sizeof(data->mac_addr), NET_LINK_ETHERNET); | ||
| } | ||
|
|
||
| Devicetree examples | ||
| =================== | ||
|
|
||
| The examples below show how to pick a MAC address configuration for an ethernet controller node | ||
| such as ``ð0``. | ||
|
|
||
| Static MAC address | ||
| ------------------ | ||
|
|
||
| .. code-block:: devicetree | ||
|
|
||
| ð0 { | ||
| local-mac-address = [00 11 22 33 44 55]; | ||
| }; | ||
|
|
||
| Random MAC address with prefix | ||
| ------------------------------ | ||
|
|
||
| .. code-block:: devicetree | ||
|
|
||
| ð0 { | ||
| zephyr,mac-address-prefix = [00 04 25]; | ||
| zephyr,random-mac-address; | ||
| }; | ||
|
|
||
| NVMEM-provided MAC address with prefix | ||
| -------------------------------------- | ||
|
|
||
| .. code-block:: devicetree | ||
|
|
||
| ð0 { | ||
| zephyr,mac-address-prefix = [00 12 34]; | ||
| nvmem-cells = <&macaddr_cell>; | ||
| nvmem-cell-names = "mac-address"; | ||
| }; | ||
|
|
||
| &eeprom0 { | ||
| nvmem-layout { | ||
| compatible = "fixed-layout"; | ||
| #address-cells = <1>; | ||
| #size-cells = <1>; | ||
|
|
||
| macaddr_cell: cell@0 { | ||
| reg = <0x0 0x6>; | ||
| #nvmem-cell-cells = <0>; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| When no MAC-related properties are present, :c:func:`net_eth_mac_load` returns ``-ENODATA`` and | ||
| the driver is expected to use its existing mechanism (for example, reading the hardware registers or | ||
| using a build-time constant). | ||
|
|
||
| Changing the MAC address at runtime | ||
| =================================== | ||
|
|
||
| Applications that need to set MAC addresses dynamically (for example to adopt an address obtained | ||
| from a management interface) need to enable the networking management API with | ||
| :kconfig:option:`CONFIG_NET_MGMT` and use :c:macro:`net_mgmt` with | ||
| :c:macro:`NET_REQUEST_ETHERNET_SET_MAC_ADDRESS`. | ||
| The request internally calls the driver's :c:func:`ethernet_api.set_config` implementation. | ||
|
|
||
| .. code-block:: c | ||
|
|
||
| static int app_set_mac_address(const struct device *dev) | ||
| { | ||
| struct net_if *iface = net_if_lookup_by_dev(dev); | ||
| struct ethernet_req_params params = { | ||
| .mac_address = { { 0x02, 0x00, 0x5E, 0x01, 0x02, 0x03 } }, | ||
| }; | ||
|
|
||
| /* Make sure the iface is down */ | ||
|
|
||
| return net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, iface, | ||
| ¶ms, sizeof(params)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.