connectivity/networking/api/net_mgmt.html · connectivity/networking/api/net_mgmt.rst 공식 원문 ↗

Zephyr 3.7.0 · Connectivity · Networking API

Network Management

단일 request API와 layer별 event listener로 network procedure를 호출하고 상태 변화를 받습니다.

Official pathconnectivity/networking/api/net_mgmt.html
Source fileconnectivity/networking/api/net_mgmt.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Direct net_mgmt request, runtime·compile-time event callback, false-positive 방어와 custom procedure 등록을 설명합니다.

Request

net_mgmt

Events

Callback · Mask

Safety

Verify actual event code

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 7EDF3FD1DB48E1708FCD6EAAE35C4948445D38918A50A09C58FD7E024B40527A
.. _net_mgmt_interface:

Network Management
##################

.. contents::
    :local:
    :depth: 2

Overview
********

The Network Management APIs allow applications, as well as network
layer code itself, to call defined network routines at any level in
the IP stack, or receive notifications on relevant network events. For
example, by using these APIs, application code can request a scan be done on a
Wi-Fi- or Bluetooth-based network interface, or request notification if
a network interface IP address changes.

The Network Management API implementation is designed to save memory
by eliminating code at build time for management routines that are not
used. Distinct and statically defined APIs for network management
procedures are not used.  Instead, defined procedure handlers are
registered by using a :c:macro:`NET_MGMT_REGISTER_REQUEST_HANDLER`
macro. Procedure requests are done through a single :c:func:`net_mgmt` API
that invokes the registered handler for the corresponding request.

The current implementation is experimental and may change and improve
in future releases.

Requesting a defined procedure
******************************

All network management requests are of the form
``net_mgmt(mgmt_request, ...)``. The ``mgmt_request`` parameter is a bit
mask that tells which stack layer is targeted, if a ``net_if`` object is
implied, and the specific management procedure being requested. The
available procedure requests depend on what has been implemented in
the stack.

To avoid extra cost, all :c:func:`net_mgmt` calls are direct. Though this
may change in a future release, it will not affect the users of this
function.

.. _net_mgmt_listening:

Listening to network events
***************************

You can receive notifications on network events by registering a
callback function and specifying a set of events used to filter when
your callback is invoked. The callback will have to be unique for a
pair of layer and code, whereas on the command part it will be a mask of
events.

At runtime two functions are available, :c:func:`net_mgmt_add_event_callback`
for registering the callback function, and :c:func:`net_mgmt_del_event_callback`
for unregistering a callback. A helper function,
:c:func:`net_mgmt_init_event_callback`, can
be used to ease the initialization of the callback structure.

Additionally :c:macro:`NET_MGMT_REGISTER_EVENT_HANDLER` can be used to
register a callback handler at compile time.

When an event occurs that matches a callback's event set, the
associated callback function is invoked with the actual event
code. This makes it possible for different events to be handled by the
same callback function, if desired.

.. warning::

   Event set filtering allows false positives for events that have the same
   layer and layer code.  A callback handler function **must** check
   the event code (passed as an argument) against the specific network
   events it will handle, **regardless** of how many events were in the
   set passed to :c:func:`net_mgmt_init_event_callback`.

   Note that in order to receive events from multiple layers, one must have
   multiple listeners registered, one for each layer being listened.
   The callback handler function can be shared between different layer events.

   (False positives can occur for events which have the same layer and
   layer code.)

An example follows.

.. code-block:: c

	/*
	 * Set of events to handle.
	 * See e.g. include/net/net_event.h for some NET_EVENT_xxx values.
	 */
	#define EVENT_IFACE_SET (NET_EVENT_IF_xxx | NET_EVENT_IF_yyy)
	#define EVENT_IPV4_SET (NET_EVENT_IPV4_xxx | NET_EVENT_IPV4_yyy)

	struct net_mgmt_event_callback iface_callback;
	struct net_mgmt_event_callback ipv4_callback;

	void callback_handler(struct net_mgmt_event_callback *cb,
			      uint32_t mgmt_event,
			      struct net_if *iface)
	{
		if (mgmt_event == NET_EVENT_IF_xxx) {
			/* Handle NET_EVENT_IF_xxx */
		} else if (mgmt_event == NET_EVENT_IF_yyy) {
			/* Handle NET_EVENT_IF_yyy */
		} else if (mgmt_event == NET_EVENT_IPV4_xxx) {
			/* Handle NET_EVENT_IPV4_xxx */
		} else if (mgmt_event == NET_EVENT_IPV4_yyy) {
			/* Handle NET_EVENT_IPV4_yyy */
		} else {
			/* Spurious (false positive) invocation. */
		}
	}

	void register_cb(void)
	{
		net_mgmt_init_event_callback(&iface_callback, callback_handler,
					     EVENT_IFACE_SET);
		net_mgmt_init_event_callback(&ipv4_callback, callback_handler,
					     EVENT_IPV4_SET);
		net_mgmt_add_event_callback(&iface_callback);
		net_mgmt_add_event_callback(&ipv4_callback);
	}

Or similarly using :c:macro:`NET_MGMT_REGISTER_EVENT_HANDLER`.

.. note::

   The ``info`` and ``info_length`` arguments are only usable if
   :kconfig:option:`CONFIG_NET_MGMT_EVENT_INFO` is enabled. Otherwise these are
   ``NULL`` and zero.

.. code-block:: c

	/*
	 * Set of events to handle.
	 */
	#define EVENT_IFACE_SET (NET_EVENT_IF_xxx | NET_EVENT_IF_yyy)
	#define EVENT_IPV4_SET (NET_EVENT_IPV4_xxx | NET_EVENT_IPV4_yyy)

	static void event_handler(uint32_t mgmt_event, struct net_if *iface,
				  void *info, size_t info_length,
				  void *user_data)
	{
		if (mgmt_event == NET_EVENT_IF_xxx) {
			/* Handle NET_EVENT_IF_xxx */
		} else if (mgmt_event == NET_EVENT_IF_yyy) {
			/* Handle NET_EVENT_IF_yyy */
		} else if (mgmt_event == NET_EVENT_IPV4_xxx) {
			/* Handle NET_EVENT_IPV4_xxx */
		} else if (mgmt_event == NET_EVENT_IPV4_yyy) {
			/* Handle NET_EVENT_IPV4_yyy */
		} else {
			/* Spurious (false positive) invocation. */
		}
	}

	NET_MGMT_REGISTER_EVENT_HANDLER(iface_event_handler, EVENT_IFACE_SET,
					event_handler, NULL);
	NET_MGMT_REGISTER_EVENT_HANDLER(ipv4_event_handler, EVENT_IPV4_SET,
					event_handler, NULL);

See :zephyr_file:`include/zephyr/net/net_event.h` for available generic core events that
can be listened to.


Defining a network management procedure
***************************************

You can provide additional management procedures specific to your
stack implementation by defining a handler and registering it with an
associated mgmt_request code.

Management request code are defined in relevant places depending on
the targeted layer or eventually, if l2 is the layer, on the
technology as well. For instance, all IP layer management request code
will be found in the :zephyr_file:`include/zephyr/net/net_event.h` header file. But in case
of an L2 technology, let's say Ethernet, these would be found in
:zephyr_file:`include/zephyr/net/ethernet.h`

You define your handler modeled with this signature:

.. code-block:: c

   static int your_handler(uint32_t mgmt_event, struct net_if *iface,
                           void *data, size_t len);

and then register it with an associated mgmt_request code:

.. code-block:: c

   NET_MGMT_REGISTER_REQUEST_HANDLER(<mgmt_request code>, your_handler);

This new management procedure could then be called by using:

.. code-block:: c

   net_mgmt(<mgmt_request code>, ...);


Signaling a network event
*************************

You can signal a specific network event using the :c:func:`net_mgmt_event_notify`
function and provide the network event code. See
:zephyr_file:`include/zephyr/net/net_mgmt.h` for details. As for the management request
code, event code can be also found on specific L2 technology mgmt headers,
for example :zephyr_file:`include/zephyr/net/ieee802154_mgmt.h` would be the right place if
802.15.4 L2 is the technology one wants to listen to events.

API Reference
*************

.. doxygengroup:: net_mgmt

Part 3

한국어 전문 번역

Network Management

Overview

Network Management API를 사용하면 application과 network-layer code가 IP stack 어느 level에서든 정의된 network routine을 호출하거나 관련 network event notification을 받을 수 있습니다. 예를 들어 Wi-Fi 또는 Bluetooth network interface scan을 요청하거나 interface IP address 변경 notification을 받을 수 있습니다.

NET_MGMT_REGISTER_REQUEST_HANDLER macro로 실제 procedure handler를 등록하고 하나의 net_mgmt() API가 request에 대응하는 handler를 호출합니다. 사용하지 않는 management routine code를 build time에 제거해 memory를 절약하며 procedure마다 별도의 static API를 두지 않습니다.

현재 implementation은 experimental 상태이며 이후 release에서 변경·개선될 수 있습니다.

Requesting a defined procedure

모든 network management request는 net_mgmt(mgmt_request, ...) 형태입니다. mgmt_request bit mask는 target stack layer, net_if object 사용 여부, 요청할 구체적인 management procedure를 나타냅니다. 사용할 수 있는 request는 stack에 구현된 procedure에 따라 달라집니다.

net_mgmt() call은 추가 비용을 피하려고 모두 direct call로 수행됩니다. 이후 release에서 구현 방식이 바뀌더라도 이 함수 사용자는 영향을 받지 않습니다.

Listening to network events

Callback function과 callback을 호출할 event set을 등록하면 network event notification을 받을 수 있습니다. Callback은 layer와 code 조합마다 고유해야 하며 command 부분에는 event mask를 사용합니다.

net_mgmt_add_event_callback()은 runtime callback 등록, net_mgmt_del_event_callback()은 해제에 사용합니다. net_mgmt_init_event_callback() helper는 callback structure initialization을 돕습니다.

NET_MGMT_REGISTER_EVENT_HANDLER macro를 사용하면 compile time에 callback handler를 등록할 수도 있습니다.

발생한 event가 callback event set과 일치하면 실제 event code와 함께 callback이 호출됩니다. 따라서 하나의 callback function으로 여러 event를 처리할 수 있습니다.

다음은 interface event와 IPv4 event에 별도 listener를 등록하고 한 callback에서 실제 event code를 검사하는 예입니다.

/*
 * Set of events to handle.
 * See e.g. include/net/net_event.h for some NET_EVENT_xxx values.
 */
#define EVENT_IFACE_SET (NET_EVENT_IF_xxx | NET_EVENT_IF_yyy)
#define EVENT_IPV4_SET (NET_EVENT_IPV4_xxx | NET_EVENT_IPV4_yyy)

struct net_mgmt_event_callback iface_callback;
struct net_mgmt_event_callback ipv4_callback;

void callback_handler(struct net_mgmt_event_callback *cb,
		      uint32_t mgmt_event,
		      struct net_if *iface)
{
	if (mgmt_event == NET_EVENT_IF_xxx) {
		/* Handle NET_EVENT_IF_xxx */
	} else if (mgmt_event == NET_EVENT_IF_yyy) {
		/* Handle NET_EVENT_IF_yyy */
	} else if (mgmt_event == NET_EVENT_IPV4_xxx) {
		/* Handle NET_EVENT_IPV4_xxx */
	} else if (mgmt_event == NET_EVENT_IPV4_yyy) {
		/* Handle NET_EVENT_IPV4_yyy */
	} else {
		/* Spurious (false positive) invocation. */
	}
}

void register_cb(void)
{
	net_mgmt_init_event_callback(&iface_callback, callback_handler,
				     EVENT_IFACE_SET);
	net_mgmt_init_event_callback(&ipv4_callback, callback_handler,
				     EVENT_IPV4_SET);
	net_mgmt_add_event_callback(&iface_callback);
	net_mgmt_add_event_callback(&ipv4_callback);
}

EVENT_IFACE_SET과 EVENT_IPV4_SET은 layer별 event mask입니다. 두 callback structure를 같은 callback_handler()로 initialize하고 각각 등록합니다. 예상하지 않은 false-positive invocation도 마지막 branch에서 처리합니다.

NET_MGMT_REGISTER_EVENT_HANDLER를 사용해 비슷하게 등록할 수도 있습니다.

/*
 * Set of events to handle.
 */
#define EVENT_IFACE_SET (NET_EVENT_IF_xxx | NET_EVENT_IF_yyy)
#define EVENT_IPV4_SET (NET_EVENT_IPV4_xxx | NET_EVENT_IPV4_yyy)

static void event_handler(uint32_t mgmt_event, struct net_if *iface,
			  void *info, size_t info_length,
			  void *user_data)
{
	if (mgmt_event == NET_EVENT_IF_xxx) {
		/* Handle NET_EVENT_IF_xxx */
	} else if (mgmt_event == NET_EVENT_IF_yyy) {
		/* Handle NET_EVENT_IF_yyy */
	} else if (mgmt_event == NET_EVENT_IPV4_xxx) {
		/* Handle NET_EVENT_IPV4_xxx */
	} else if (mgmt_event == NET_EVENT_IPV4_yyy) {
		/* Handle NET_EVENT_IPV4_yyy */
	} else {
		/* Spurious (false positive) invocation. */
	}
}

NET_MGMT_REGISTER_EVENT_HANDLER(iface_event_handler, EVENT_IFACE_SET,
				event_handler, NULL);
NET_MGMT_REGISTER_EVENT_HANDLER(ipv4_event_handler, EVENT_IPV4_SET,
				event_handler, NULL);

include/zephyr/net/net_event.h에서 listener가 받을 수 있는 generic core event를 확인할 수 있습니다.

Defining a network management procedure

Stack implementation에 특화된 management procedure를 추가하려면 handler를 정의하고 관련 mgmt_request code에 등록합니다.

Management request code는 target layer에 맞는 위치에 정의하며 L2이면 technology별 header에 둘 수도 있습니다. IP-layer code는 include/zephyr/net/net_event.h, Ethernet L2 code는 include/zephyr/net/ethernet.h에서 찾습니다.

Handler signature는 다음과 같습니다.

static int your_handler(uint32_t mgmt_event, struct net_if *iface,
                        void *data, size_t len);

Handler를 mgmt_request code에 등록합니다.

NET_MGMT_REGISTER_REQUEST_HANDLER(<mgmt_request code>, your_handler);

새 management procedure는 다음처럼 호출합니다.

net_mgmt(<mgmt_request code>, ...);

Signaling a network event

net_mgmt_event_notify()와 network event code를 사용해 특정 event를 signal할 수 있습니다. 자세한 API는 include/zephyr/net/net_mgmt.h에 있습니다.

Event code도 L2 technology별 management header에 둘 수 있습니다. 예를 들어 IEEE 802.15.4 L2 event는 include/zephyr/net/ieee802154_mgmt.h에서 찾습니다.

API Reference

Doxygen group net_mgmt가 API reference를 제공합니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.