drivers/net/netdevsim/dev.c
Source file repositories/reference/linux-study-clean/drivers/net/netdevsim/dev.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/net/netdevsim/dev.c- Extension
.c- Size
- 53430 bytes
- Lines
- 1909
- Domain
- Driver Families
- Bucket
- drivers/net
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/debugfs.hlinux/device.hlinux/etherdevice.hlinux/hex.hlinux/inet.hlinux/jiffies.hlinux/kernel.hlinux/list.hlinux/mutex.hlinux/random.hlinux/rtnetlink.hlinux/workqueue.hnet/devlink.hnet/ip.hnet/flow_offload.huapi/linux/devlink.huapi/linux/ip.huapi/linux/udp.hnetdevsim.h
Detected Declarations
struct nsim_trap_itemstruct nsim_trap_datastruct nsim_rate_nodeenum nsim_devlink_param_idfunction Copyrightfunction nsim_dev_port_index_to_vf_indexfunction nsim_dev_get_vfsfunction nsim_bus_dev_set_vfsfunction nsim_dev_take_snapshotfunction nsim_dev_take_snapshot_writefunction nsim_dev_trap_fa_cookie_readfunction nsim_dev_trap_fa_cookie_writefunction nsim_bus_dev_max_vfs_readfunction nsim_bus_dev_max_vfs_writefunction nsim_dev_debugfs_initfunction nsim_dev_debugfs_exitfunction nsim_dev_rate_parent_readfunction nsim_dev_tc_bw_debugfs_initfunction nsim_dev_port_debugfs_initfunction nsim_dev_port_debugfs_exitfunction nsim_dev_resources_registerfunction nsim_devlink_param_test2_getfunction nsim_devlink_param_test2_setfunction nsim_devlink_param_test2_get_defaultfunction nsim_devlink_param_test2_reset_defaultfunction nsim_devlink_set_params_init_valuesfunction nsim_devlink_param_load_driverinit_valuesfunction nsim_dev_dummy_region_initfunction nsim_dev_dummy_region_exitfunction nsim_esw_legacy_enablefunction nsim_esw_switchdev_enablefunction nsim_devlink_eswitch_mode_setfunction nsim_devlink_eswitch_mode_getfunction nsim_dev_trap_reportfunction nsim_dev_trap_report_workfunction nsim_dev_traps_initfunction nsim_dev_traps_exitfunction nsim_dev_reload_downfunction nsim_dev_reload_upfunction nsim_dev_info_getfunction nsim_dev_flash_updatefunction nsim_dev_trap_item_lookupfunction nsim_dev_devlink_trap_initfunction nsim_dev_devlink_trap_action_setfunction nsim_dev_devlink_trap_group_setfunction nsim_dev_devlink_trap_policer_setfunction nsim_dev_devlink_trap_policer_counter_getfunction nsim_rate_bytes_to_units
Annotated Snippet
static const struct file_operations nsim_dev_take_snapshot_fops = {
.open = simple_open,
.write = nsim_dev_take_snapshot_write,
.llseek = generic_file_llseek,
.owner = THIS_MODULE,
};
static ssize_t nsim_dev_trap_fa_cookie_read(struct file *file,
char __user *data,
size_t count, loff_t *ppos)
{
struct nsim_dev *nsim_dev = file->private_data;
struct flow_action_cookie *fa_cookie;
unsigned int buf_len;
ssize_t ret;
char *buf;
spin_lock(&nsim_dev->fa_cookie_lock);
fa_cookie = nsim_dev->fa_cookie;
if (!fa_cookie) {
ret = -EINVAL;
goto errout;
}
buf_len = fa_cookie->cookie_len * 2;
buf = kmalloc(buf_len, GFP_ATOMIC);
if (!buf) {
ret = -ENOMEM;
goto errout;
}
bin2hex(buf, fa_cookie->cookie, fa_cookie->cookie_len);
spin_unlock(&nsim_dev->fa_cookie_lock);
ret = simple_read_from_buffer(data, count, ppos, buf, buf_len);
kfree(buf);
return ret;
errout:
spin_unlock(&nsim_dev->fa_cookie_lock);
return ret;
}
static ssize_t nsim_dev_trap_fa_cookie_write(struct file *file,
const char __user *data,
size_t count, loff_t *ppos)
{
struct nsim_dev *nsim_dev = file->private_data;
struct flow_action_cookie *fa_cookie;
size_t cookie_len;
ssize_t ret;
char *buf;
if (*ppos != 0)
return -EINVAL;
cookie_len = (count - 1) / 2;
if ((count - 1) % 2)
return -EINVAL;
buf = memdup_user(data, count);
if (IS_ERR(buf))
return PTR_ERR(buf);
fa_cookie = kmalloc(sizeof(*fa_cookie) + cookie_len,
GFP_KERNEL | __GFP_NOWARN);
if (!fa_cookie) {
ret = -ENOMEM;
goto free_buf;
}
fa_cookie->cookie_len = cookie_len;
ret = hex2bin(fa_cookie->cookie, buf, cookie_len);
if (ret)
goto free_fa_cookie;
kfree(buf);
spin_lock(&nsim_dev->fa_cookie_lock);
kfree(nsim_dev->fa_cookie);
nsim_dev->fa_cookie = fa_cookie;
spin_unlock(&nsim_dev->fa_cookie_lock);
return count;
free_fa_cookie:
kfree(fa_cookie);
free_buf:
kfree(buf);
return ret;
}
static const struct file_operations nsim_dev_trap_fa_cookie_fops = {
Annotation
- Immediate include surface: `linux/debugfs.h`, `linux/device.h`, `linux/etherdevice.h`, `linux/hex.h`, `linux/inet.h`, `linux/jiffies.h`, `linux/kernel.h`, `linux/list.h`.
- Detected declarations: `struct nsim_trap_item`, `struct nsim_trap_data`, `struct nsim_rate_node`, `enum nsim_devlink_param_id`, `function Copyright`, `function nsim_dev_port_index_to_vf_index`, `function nsim_dev_get_vfs`, `function nsim_bus_dev_set_vfs`, `function nsim_dev_take_snapshot`, `function nsim_dev_take_snapshot_write`.
- Atlas domain: Driver Families / drivers/net.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.