net/core/link_watch.c
Source file repositories/reference/linux-study-clean/net/core/link_watch.c
File Facts
- System
- Linux kernel
- Corpus path
net/core/link_watch.c- Extension
.c- Size
- 7712 bytes
- Lines
- 331
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/module.hlinux/netdevice.hlinux/if.hnet/sock.hnet/pkt_sched.hlinux/rtnetlink.hlinux/jiffies.hlinux/spinlock.hlinux/workqueue.hlinux/bitops.hlinux/types.hdev.h
Detected Declarations
enum lw_bitsfunction default_operstatefunction rfc2863_policyfunction linkwatch_init_devfunction linkwatch_urgent_eventfunction linkwatch_add_eventfunction linkwatch_schedule_workfunction linkwatch_do_devfunction __linkwatch_run_queuefunction linkwatch_clean_devfunction __linkwatch_sync_devfunction linkwatch_sync_devfunction linkwatch_run_queuefunction linkwatch_eventfunction linkwatch_fire_eventexport linkwatch_fire_event
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Linux network device link state notification
*
* Author:
* Stefan Rompf <sux@loplof.de>
*/
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/if.h>
#include <net/sock.h>
#include <net/pkt_sched.h>
#include <linux/rtnetlink.h>
#include <linux/jiffies.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <linux/bitops.h>
#include <linux/types.h>
#include "dev.h"
enum lw_bits {
LW_URGENT = 0,
};
static unsigned long linkwatch_flags;
static unsigned long linkwatch_nextevent;
static void linkwatch_event(struct work_struct *dummy);
static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
static LIST_HEAD(lweventlist);
static DEFINE_SPINLOCK(lweventlist_lock);
static unsigned int default_operstate(const struct net_device *dev)
{
if (netif_testing(dev))
return IF_OPER_TESTING;
/* Some uppers (DSA) have additional sources for being down, so
* first check whether lower is indeed the source of its down state.
*/
if (!netif_carrier_ok(dev)) {
struct net_device *peer;
int iflink;
/* If called from netdev_run_todo()/linkwatch_sync_dev(),
* dev_net(dev) can be already freed, and RTNL is not held.
*/
if (dev->reg_state <= NETREG_REGISTERED)
iflink = dev_get_iflink(dev);
else
iflink = dev->ifindex;
if (iflink == dev->ifindex)
return IF_OPER_DOWN;
ASSERT_RTNL();
peer = __dev_get_by_index(dev_net(dev), iflink);
if (!peer)
return IF_OPER_DOWN;
return netif_carrier_ok(peer) ? IF_OPER_DOWN :
IF_OPER_LOWERLAYERDOWN;
}
if (netif_dormant(dev))
return IF_OPER_DORMANT;
return IF_OPER_UP;
}
static void rfc2863_policy(struct net_device *dev)
{
unsigned int operstate = default_operstate(dev);
if (operstate == READ_ONCE(dev->operstate))
return;
switch(dev->link_mode) {
case IF_LINK_MODE_TESTING:
if (operstate == IF_OPER_UP)
operstate = IF_OPER_TESTING;
break;
case IF_LINK_MODE_DORMANT:
if (operstate == IF_OPER_UP)
operstate = IF_OPER_DORMANT;
break;
Annotation
- Immediate include surface: `linux/module.h`, `linux/netdevice.h`, `linux/if.h`, `net/sock.h`, `net/pkt_sched.h`, `linux/rtnetlink.h`, `linux/jiffies.h`, `linux/spinlock.h`.
- Detected declarations: `enum lw_bits`, `function default_operstate`, `function rfc2863_policy`, `function linkwatch_init_dev`, `function linkwatch_urgent_event`, `function linkwatch_add_event`, `function linkwatch_schedule_work`, `function linkwatch_do_dev`, `function __linkwatch_run_queue`, `function linkwatch_clean_dev`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.