<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Tawaliou]]></title><description><![CDATA[Software Developer | Love Low Level Eng. | Python, Javascript, C, Linux, I'm learning backend development and system programming.]]></description><link>https://tawaliou.com</link><generator>RSS for Node</generator><lastBuildDate>Sun, 08 Nov 2026 05:29:03 GMT</lastBuildDate><atom:link href="https://tawaliou.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Linux Kernel Labs: Kprobe based tracer (Part 1)
]]></title><description><![CDATA[Linux Kernel Labs/Operating System 2 - assignment 2: Kprobe based tracer, miscdevice, file_operations, kretprobe, hashtable, ioctl]]></description><link>https://tawaliou.com/linux-kernel-labs-kprobe-based-tracer-part-1</link><guid isPermaLink="true">https://tawaliou.com/linux-kernel-labs-kprobe-based-tracer-part-1</guid><category><![CDATA[operating system]]></category><category><![CDATA[linux drivers]]></category><category><![CDATA[miscdevice]]></category><category><![CDATA[kretprobe]]></category><category><![CDATA[kprobe]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Fri, 25 Sep 2026 16:19:07 GMT</pubDate><content:encoded><![CDATA[<p>In this post, I'll describe how I build a simple kprobe-based tracer for the <a href="https://linux-kernel-labs.github.io/refs/heads/master/so2/assign1-kprobe-based-tracer.html">2nd assignment</a> of the Linux Kernel Labs course.</p>
<h1>Assignment</h1>
<p><a href="https://linux-kernel-labs.github.io/refs/heads/master/so2/assign1-kprobe-based-tracer.html">Link</a></p>
<p>We want to build a kernel module named <code>tracer</code> that'll be a kernel operations surveillant. With this surveillant, we aim to intercept calls from processes:</p>
<ul>
<li><p><code>kmalloc</code> and <code>kfree</code> calls</p>
</li>
<li><p><code>schedule</code> calls</p>
</li>
<li><p><code>up</code> and <code>down_interruptible</code> calls</p>
</li>
<li><p><code>mutex_lock</code> and <code>mutex_unlock</code> calls</p>
</li>
</ul>
<p>The surveillant will hold, at the process level, the <strong>number of calls</strong> for each of the above functions.</p>
<p>Some key points:</p>
<ul>
<li><p>for the <code>kmalloc</code> and <code>kfree</code> calls, the total quantity of <strong>allocated</strong> and <strong>deallocated memory</strong> will be shown</p>
</li>
<li><p>we'll use <code>__kmalloc</code> because <code>kmalloc</code> is inline</p>
</li>
<li><p><code>kretprobe</code> will be used for the instrumentation</p>
</li>
<li><p>we'll use the <code>miscdevice</code> interface instead of <code>cdev</code> to define and manage the device</p>
</li>
<li><p><code>/proc/tracer</code> will display the information (stats), and we will interact with the driver through <code>/dev/tracer</code>.</p>
</li>
</ul>
<p>You can look at the link above to get more details about the assignment.</p>
<h1>Step-by-Step</h1>
<p>In the following section, we'll go step by step through the implementation of our instrumentation module.</p>
<p><strong>NB</strong>: We'll first probe all functions except <code>kmalloc</code> and <code>kfree</code>, because those two require some additional work</p>
<p>Before starting, here is the content of the <code>tracer.h</code> file already available for the assignment</p>
<details>
	<summary><strong>tracer.h</strong></summary>
<pre><code class="language-c">/*
 * SO2 kprobe-based tracer header file
 *
 * This is shared with user space.
 */
<p>#ifndef TRACER_H__
#define TRACER_H__ 1</p>
<p>#include 
#ifndef <strong>KERNEL</strong>
#include 
#endif /* <strong>KERNEL</strong> */</p>
<p>#define TRACER_DEV_MINOR 42
#define TRACER_DEV_NAME "tracer"</p>
<p>#define TRACER_ADD_PROCESS	_IOW(_IOC_WRITE, 42, pid_t)
#define TRACER_REMOVE_PROCESS	_IOW(_IOC_WRITE, 43, pid_t)</p>
<p>#endif /* TRACER_H_ */</p>
</code><p><code class="language-c"></code></p></pre><p></p>
</details>

<p>In the <a href="#driver-data-and-ioctl-operations">ioctl section</a>, we'll explain what <code>_IOW</code>, <code>TRACER_ADD_PROCESS</code>, and <code>TRACER_REMOVE_PROCESS</code> are for.</p>
<h2>Module creation</h2>
<p>As we did in the post on <a href="https://tawaliou.com/linux-kernel-labs-kernel-module-and-kernel-api">kernel module</a>, let's set up our module:</p>
<p><em><strong>tracer.c</strong></em></p>
<pre><code class="language-c">#include &lt;linux/init.h&gt;
#include &lt;linux/kernel.h&gt;
#include &lt;linux/module.h&gt;

#include "tracer.h"

MODULE_DESCRIPTION("Kernel operations surveillant: kprobe-based tracer");
MODULE_AUTHOR("Tawaliou ALAO &lt;alaotawaliou@gmail.com&gt;");
MODULE_LICENSE("GPL");

static int tracer_init(void)
{
	pr_info("Misc device registered! Look at /dev/%s\n",
		TRACER_DEV_NAME);
	return 0;
}

static void tracer_exit(void)
{
	pr_info("Misc device %s unregistered!\n", TRACER_DEV_NAME);
}

module_init(tracer_init);
module_exit(tracer_exit);
</code></pre>
<h2>Driver interface definition with <code>miscdevice</code> and <code>file_operations</code></h2>
<h3>Registration</h3>
<p>Now, let's define our driver interface, which will allow us to register our driver and create the <code>/dev/tracer</code> entry. In the last post on <a href="https://tawaliou.com/linux-kernel-labs-character-device-driver-part-1">Character device driver - Part 1</a>, to define our driver interface, we used to:</p>
<ul>
<li><p>set the major and minor numbers,</p>
</li>
<li><p>initialize and add an instance of <code>struct cdev</code> to the kernel,</p>
</li>
<li><p>create an entry for the driver in <code>/dev</code> manually with <code>mknod</code>, or programmatically with <code>device_create</code>,</p>
</li>
<li><p>then define the file operations,</p>
</li>
</ul>
<p>And I said that it's too long, right? Yes, for this assignment, we'll use <code>struct miscdevice</code> instead (Miscellaneous Character Device 😉). <code>miscdevice</code> <strong>will definitely simplify driver creation and save us from all the steps listed above.</strong> In short, it'll do all of these steps for us in the background.</p>
<p>Here is the link to the definition of <a href="https://elixir.bootlin.com/linux/v7.1.4/source/include/linux/miscdevice.h">miscdevice</a></p>
<details>
	<summary><strong>miscdevice.c</strong></summary>
<pre><code class="language-c">struct miscdevice {
	int minor;
	const char *name;
	const struct file_operations *fops;
	struct list_head list;
	struct device *parent;
	struct device *this_device;
	const struct attribute_group **groups;
	const char *nodename;
	umode_t mode;
};
</code></pre>
</details>

<p>So let's define our driver:</p>
<p><em><strong>tracer.c</strong></em></p>
<pre><code class="language-c">#include &lt;linux/miscdevice.h&gt;

static struct miscdevice tracer_miscdevice = { 
	.minor = TRACER_DEV_MINOR,
	.name = TRACER_DEV_NAME,
};
</code></pre>
<p>I don't think I need to provide any additional explanation 😁.</p>
<p>Then, we'll register it with <code>misc_register</code></p>
<pre><code class="language-c">int misc_register(struct miscdevice *misc)
</code></pre>
<p>In our init function, we'll register it</p>
<pre><code class="language-c">static int tracer_init(void)
{
int ret;
	ret = misc_register(&amp;tracer_miscdevice);
	if (ret &lt; 0) {
		pr_err("cannot register miscdev on minor=%d name=%s (err=%d)\n",
		       TRACER_DEV_MINOR, tracer_miscdevice.name, ret);
		return ret;
	}

	pr_info("Misc device registered! Look at /dev/%s\n",
		tracer_miscdevice.name);

	return 0;
}
</code></pre>
<p>Don't forget to check the return value of <code>misc_register</code> because the registration can fail.</p>
<p><strong>You</strong> can notice that we don't set the major number with <code>miscdevice</code>. We don't need to do that because <code>miscdevice</code> has a default major number, <code>MISC_MAJOR</code>, which is set to <code>10</code> and defined in <a href="https://elixir.bootlin.com/linux/v7.1.4/source/include/uapi/linux/major.h#L26">major.h</a></p>
<pre><code class="language-c">#define MISC_MAJOR 10
</code></pre>
<h3>Unregistration</h3>
<p>We unregister the driver with:</p>
<pre><code class="language-c">void misc_deregister(struct miscdevice *misc);
</code></pre>
<p>in our exit function.</p>
<pre><code class="language-c">static void tracer_exit(void)
{
	misc_deregister(&amp;tracer_miscdevice);
	pr_info("Misc device %s unregistered!\n", tracer_miscdevice.name);
}
</code></pre>
<h3>File operations</h3>
<p>For the file operations, we'll just implement the <code>ioctl</code> operations. So, let's define an instance of <code>file_operations</code></p>
<pre><code class="language-c">static const struct file_operations tracer_fops = { 
	.owner = THIS_MODULE,
	.unlocked_ioctl = tracer_ioctl,
};
</code></pre>
<p>Before moving on to <code>ioctl</code>, let's set <code>tracer_fops</code> as the file-operations handler for <code>tracer_miscdevice</code></p>
<pre><code class="language-c">static struct miscdevice tracer_miscdevice = { 
	...
	.fops = &amp;tracer_fops,
};
</code></pre>
<p>Now, we'll define the <code>tracer_ioctl</code> function.</p>
<p><strong>So, what is</strong> <code>IOCTL</code><strong>?</strong></p>
<p><strong>IOCTL</strong>, short for <strong>Input/Output Control</strong>, allows us to perform custom actions with the driver. You know, with the <code>open</code>, <code>read</code>, and <code>write</code> handlers of <code>file_operations</code>, we can only perform a limited set of actions: we can either read data sent from user space to the driver and do something with it, or write data back to user space. Now, imagine we want to do something like:</p>
<ul>
<li><p>check whether some data exists</p>
</li>
<li><p>remove an entry from the driver's list if a flag is set</p>
</li>
</ul>
<p>So, <code>ioctl</code> will help our driver do more than just read or write; it will let us control what the driver can do and how it does it.</p>
<p>In fact, what will happen is that we'll define the <code>ioctl</code> commands (I'll show how to do this in the following lines), and a user-space application will send these commands to the driver with the appropriate arguments.</p>
<p>Our driver will handle two commands:</p>
<ul>
<li><p><code>TRACER_ADD_PROCESS</code>: to add the <code>pid</code> of a process to trace</p>
</li>
<li><p><code>TRACER_REMOVE_PROCESS</code>: to remove a process from the tracing list</p>
</li>
</ul>
<p><strong>IOCTL command</strong></p>
<p>Many macros exist to define an IOCTL command; you can read about them <a href="https://elixir.bootlin.com/linux/v7.1.4/source/include/uapi/asm-generic/ioctl.h">here</a>.</p>
<p>To define <code>read</code> and <code>write</code> commands, we can use the following macros: <code>_IOR</code> and <code>_IOW</code>.</p>
<pre><code class="language-c">// For read operations
#define _IOR(type,nr,size)	_IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))

// For write operations
#define _IOW(type,nr,size)	_IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
</code></pre>
<p>where:</p>
<ul>
<li><p>the first parameter (<code>type</code>) is an arbitrary 8-bit character unique to the driver, for example, <code>'d'</code> or <code>'t'</code>,...</p>
</li>
<li><p>the second parameter (<code>nr</code>) is an incrementing number that uniquely identifies the command</p>
</li>
<li><p>the last parameter (<code>size</code>) is the "type" of the argument passed to the command, such as <code>int</code> or <code>struct my_data</code></p>
</li>
</ul>
<p>So, in our case:</p>
<pre><code class="language-c">#define MAGIC_BYTE 't' 

#define TRACER_ADD_PROCESS _IOW(MAGIC_BYTE, 42, pid_t)
#define TRACER_REMOVE_PROCESS _IOW(MAGIC_BYTE, 43, pid_t)
</code></pre>
<p>Inside <code>tracer_ioctl</code>, which we set as the <code>unlocked_ioctl</code> handler of the <code>file_operations</code> instance above:</p>
<pre><code class="language-c">
static long tracer_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	case TRACER_ADD_PROCESS:
		tracer_add_proc(arg);
		break;

	case TRACER_REMOVE_PROCESS:
		tracer_remove_proc(arg);
		break;
	default:
		return -ENOTTY;
	}

	return 0;
}
</code></pre>
<p>where:</p>
<ul>
<li><p><code>cmd</code>: the command sent from user space (<code>TRACER_ADD_PROCESS</code> and <code>TRACER_REMOVE_PROCESS</code>)</p>
</li>
<li><p><code>arg</code>: the value passed with the command (here, it's the process's <code>pid</code>); it could be more complex data.</p>
</li>
</ul>
<p>So, based on the value of <code>cmd</code>, we know which operations to handle:</p>
<pre><code class="language-plaintext">TRACER_ADD_PROCESS		--&gt; tracer_add_proc
TRACER_REMOVE_PROCESS	--&gt; tracer_remove_proc
</code></pre>
<p>In the next section, after defining our driver data, we'll implement the two handlers for <code>ioctl</code></p>
<h2>Driver data and <code>ioctl</code> operations</h2>
<ul>
<li><p><code>struct tracer_stats</code></p>
</li>
<li><p><code>static struct list_head tracer_head</code>;</p>
</li>
</ul>
<p>We'll store statistics for each process in a structure, and a linked list will hold all added processes.</p>
<h3>struct tracer_stats</h3>
<pre><code class="language-c">struct tracer_stats {
	pid_t tr_pid;		// process pid added
	int tr_alloc;		// number of calls to kmalloc
	int tr_free;  		// number of calls to kfree
	int tr_mem;			// memory allocated with kmalloc
	int tr_mem_free;	// memory freed with kfree
	int tr_sched;		// number of calls to schedule
	int tr_up;			// number of calls to up
	int tr_down;		// number of calls to down_interruptible
	int tr_lock;		// number of calls to mutex_lock
	int tr_unlock;		// number of calls to mutex_unlock

	struct list_head tr_pid_list; // list node reference
};
</code></pre>
<p>Most of the <code>struct tracer_stats</code> fields, except <code>tr_pid</code>, <code>tr_mem</code>, and <code>tr_mem_free</code>, will be incremented each time the traced function is called.</p>
<p>Then, we define our linked-list head</p>
<pre><code class="language-c">static struct list_head tracer_head;
</code></pre>
<p>You can check my article on the kernel linked list <a href="https://tawaliou.com/linux-kernel-labs-kernel-module-and-kernel-api#list-api">here</a>.</p>
<p>We also need to define a lock on the list to avoid race conditions</p>
<pre><code class="language-c">static DEFINE_SPINLOCK(tracer_list_lock);
</code></pre>
<p>Now, there are some helper functions we need to write to work on our list based on the <code>ioctl</code> commands we'll handle. In fact, based on the <code>cmd</code>:</p>
<ul>
<li><p><code>TRACER_ADD_PROCESS</code>: we'll <strong>add</strong> a new process ID to the list</p>
</li>
<li><p><code>TRACER_REMOVE_PROCESS</code>: we'll <strong>remove</strong> a process ID from the list</p>
</li>
</ul>
<h3>Add new process</h3>
<p>First, we need to <code>check</code> whether the PID exists in the list when adding or removing a process.</p>
<p>So, the first function to write is <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/58155db4d34484de260b7d0131552cf652b886b4/1-tracer/tracer.c#L106">tracer_find_proc</a>, which returns a pointer to the <code>tracer_stats</code> instance if the PID to add/remove exists, or <code>NULL</code> otherwise.</p>
<details>
	<summary><strong>tracer_find_proc(pid_t pid)</strong></summary>
<pre><code class="language-c">static struct tracer_stats *tracer_find_proc(pid_t pid)
{
	struct tracer_stats *tr_proc;
	struct list_head *pos;
	unsigned long flags;
<pre><code>spin_lock_irqsave(&amp;tracer_list_lock, flags);
list_for_each (pos, &amp;tracer_head) {
	tr_proc = list_entry(pos, struct tracer_stats, tr_pid_list);
	if (tr_proc-&gt;tr_pid == pid) {
		spin_unlock_irqrestore(&amp;tracer_list_lock, flags);
		return tr_proc;
	}
}
spin_unlock_irqrestore(&amp;tracer_list_lock, flags);

<p>return NULL;
</p></code></pre>
</code><p><code class="language-c">}
</code></p></pre><p></p><p></p>
</details>

<p><strong>NB</strong>: In that code we use</p>
<pre><code class="language-c">spin_lock_irqsave(&amp;tracer_list_lock, flags);
...
...
spin_unlock_irqrestore(&amp;tracer_list_lock, flags);
</code></pre>
<p><code>spin_lock_irqsave</code></p>
<ul>
<li><p>disables kernel preemption</p>
</li>
<li><p>disables local hardware interrupts (<strong>irq</strong>)</p>
</li>
<li><p>save the CPU's current interrupt state into <code>flags</code> (<strong>save</strong>)</p>
</li>
<li><p>acquires the lock</p>
</li>
</ul>
<p><code>spin_lock_irqsave()</code> saves the interrupt flags, and <code>spin_unlock_irqrestore()</code> restores whatever interrupt state existed before.</p>
<p>If we don't use an IRQ lock here, an interrupt that executes <code>kmalloc</code> or <code>kfree</code> could happen (and we need to update the malloc stat if the relevant process is registered in the list) while the CPU is executing <code>proc_tracer_show</code> or an <strong>ioctl command</strong>, causing a race condition.</p>
<p>Then, the <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/58155db4d34484de260b7d0131552cf652b886b4/1-tracer/tracer.c#L165">tracer_add_proc</a> function for adding a process is trivial</p>
<details>
	<summary><strong>tracer_add_proc(pid_t pid)</strong></summary>
<pre><code class="language-c">static void tracer_add_proc(pid_t pid)
{
	struct tracer_stats *tr_proc;
	unsigned long flags;
<pre><code>if (tracer_find_proc(pid))
	return;

<p>tr_proc = tracer_stat_alloc(pid);
if (!tr_proc)
	return;</p>
<p>spin_lock_irqsave(&amp;tracer_list_lock, flags);
list_add(&amp;tr_proc-&gt;tr_pid_list, &amp;tracer_head);
spin_unlock_irqrestore(&amp;tracer_list_lock, flags);
</p></code></pre>
</code><p><code class="language-c">}
</code></p></pre><p></p><p></p>
</details>

<p>And yes, there is the <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/58155db4d34484de260b7d0131552cf652b886b4/1-tracer/tracer.c#L151">tracer_stat_alloc</a> function that allocates memory for a new process's <code>tracer_stats</code> and initializes it to zero.</p>
<h3>Remove an existing process</h3>
<p>The removal implementation is also straightforward: we iterate over the list with <code>list_for_each_safe</code>, then use <code>list_entry</code> to check whether the entry's PID is the one we want to remove by comparing it with <code>pid</code>. We can then use <code>list_del</code> to delete the entry from the list and free its memory with <code>kfree(tr_proc)</code>. Below is the implementation for the <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/58155db4d34484de260b7d0131552cf652b886b4/1-tracer/tracer.c#L182">tracer_remove_proc</a></p>
<details>
	<summary><strong>tracer_remove_proc(pid_t pid)</strong></summary>
<pre><code class="language-c">static void tracer_remove_proc(pid_t pid)
{
	struct list_head *pos;
	struct list_head *n;
	struct tracer_stats *tr_proc;
	unsigned long flags;
<pre><code>spin_lock_irqsave(&amp;tracer_list_lock, flags);
list_for_each_safe (pos, n, &amp;tracer_head) {
	tr_proc = list_entry(pos, struct tracer_stats, tr_pid_list);
	if (tr_proc-&gt;tr_pid == pid) {
		list_del(pos);
		spin_unlock_irqrestore(&amp;tracer_list_lock, flags);
		kfree(tr_proc);
		return;
	}
}
spin_unlock_irqrestore(&amp;tracer_list_lock, flags);
</code></pre>
</code><p><code class="language-c">}
</code></p></pre><p></p>
</details>

<p>As we implement the handler for our <code>ioctl</code> operations, I'll quickly show how an <code>ioctl</code> command can be used from user space to send a command to the driver</p>
<ol>
<li>First, get a file descriptor by opening the driver with <code>int open (const char *file, int flag)</code>, like this:</li>
</ol>
<pre><code class="language-c">int fd = open("/dev/tracer", O_RDONLY); 
</code></pre>
<p>The flag can be <code>O_RDONLY</code>, <code>O_WRONLY</code>, or <code>O_RDWR</code></p>
<ol>
<li>Second, use <code>int ioctl (int fd, unsigned long int request)</code> to send a command:</li>
</ol>
<pre><code class="language-c">int rc = ioctl(fd, TRACER_ADD_PROCESS, pid);

int rc = ioctl(fd, TRACER_REMOVE_PROCESS, pid);
</code></pre>
<p>Easy.</p>
<h2>/proc/tracer entry creation</h2>
<p>OK, it's good to see that we can handle adding and removing PIDs, but to test our driver quickly and manually, we can use the old method: print. At the start of this post, we listed that one requirement of the assignment was to display the stats through <code>cat /proc/tracer</code>.</p>
<p>So, we'll see how to automatically create an entry named <strong>tracer</strong> in the <code>/proc</code> directory, and then we'll iterate over the list to display its content.</p>
<h3>Create an entry in /proc</h3>
<p>In our init function, we'll use <code>proc_create_single(name, mode, parent, show)</code>, like this:</p>
<pre><code class="language-c">static int tracer_init(void)
{
	...
	proc_create_single(TRACER_DEV_NAME, 0, NULL, tracer_proc_show);
	...
}
</code></pre>
<p>where</p>
<ul>
<li><p><code>name</code>: is the path to the file inside <code>/proc</code>—<code>tracer</code> in our case. It could be <code>/tracer/stats/whatever_file</code></p>
</li>
<li><p><code>mode</code>: the permissions/rights (read-only, write, ...)</p>
</li>
<li><p><code>parent</code>: NULL if <code>/proc</code></p>
</li>
<li><p><code>show</code>: a pointer to the function that will be called to render the file's content</p>
</li>
</ul>
<h3>/proc/tracer content with tracer_proc_show</h3>
<p><a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/58155db4d34484de260b7d0131552cf652b886b4/1-tracer/tracer.c#L125">tracer_proc_show</a> below is simple: it iterates over the list with <code>list_for_each</code> and, for each entry (process), displays the content of its attributes.</p>
<details>
	<summary><strong>tracer_proc_show(struct seq_file *m, void *v)</strong></summary>
<pre><code class="language-c">static int tracer_proc_show(struct seq_file *m, void *v)
{
	struct list_head *pos;
	struct tracer_stats *tr_proc;
	unsigned long flags;
<pre><code>seq_printf(m, "%-6s %-10s %-8s %-12s %-12s %-8s %-6s %-8s %-6s %-8s\n",
	   "PID", "kmalloc", "kfree", "kmalloc_mem", "kfree_mem",
	   "sched", "up", "down", "lock", "unlock");

<p>spin_lock_irqsave(&amp;tracer_list_lock, flags);
list_for_each (pos, &amp;tracer_head) {
	tr_proc = list_entry(pos, struct tracer_stats, tr_pid_list);
	seq_printf(
		m,
		"%-6d %-10d %-8d %-12d %-12d %-8d %-6d %-8d %-6d %-8d\n",
		tr_proc-&gt;tr_pid, tr_proc-&gt;tr_alloc, tr_proc-&gt;tr_free,
		tr_proc-&gt;tr_mem, tr_proc-&gt;tr_mem_free,
		tr_proc-&gt;tr_sched, tr_proc-&gt;tr_up, tr_proc-&gt;tr_down,
		tr_proc-&gt;tr_lock, tr_proc-&gt;tr_unlock);
}
spin_unlock_irqrestore(&amp;tracer_list_lock, flags);</p>
<p>return 0;
</p></code></pre>
</code><p><code class="language-c">}
</code></p></pre><p></p><p></p>
</details>

<p>Here are a few details</p>
<ul>
<li><p>Our <code>tracer_proc_show</code> takes a pointer to <code>struct seq_file</code>, which is a buffer for <code>/proc</code></p>
</li>
<li><p>We use <code>seq_printf</code> instead of <code>pr_info</code> to write the formatted text into the <code>struct seq_file *m</code> buffer.</p>
</li>
<li><p><code>%-6s</code> and <code>%-12d</code> are used to set a minimum width of <strong>6</strong> or <strong>12</strong> characters with left alignment (<strong>-</strong>). <strong>s</strong> (string) and <strong>d</strong> (integer) indicate the type of data. With that, we ensure that the data and column headers are well aligned when displayed.</p>
</li>
</ul>
<p>Now, let's talk about the instrumentation itself.</p>
<h2>Instrumentation with kretprobe 1: <code>schedule</code>, <code>up</code>, <code>down_interruptible</code>, <code>mutex_lock</code> and <code>mutex_unlock</code></h2>
<p>So, how does probing a kernel function with a kprobe work? When we register a kprobe (probe handler) on a kernel function, depending on the architecture, the kernel replaces the first byte of the target function's instructions with a breakpoint instruction. When the kernel function is called and the CPU starts executing its instructions and encounters the breakpoint, an exception is triggered. The CPU then switches to execute the probe handler and afterward switches back to the normal flow. That's how <code>kprobe</code> works.</p>
<p>In our case, we'll use <code>kretprobe</code> (<strong>ret</strong> -&gt; <strong>return</strong>): it runs the handler at the function exit.</p>
<p>Now, let's see how to register a handler to probe a kernel function with <code>kretprobe</code>.</p>
<h3>Initialize an instance of <code>struct kretprobe</code></h3>
<pre><code class="language-c">static struct kretprobe schedule_probe = {
	.entry_handler = schedule_probe_entry_handler,
	.maxactive = 32,
	.kp = { .symbol_name = "schedule" }
};
</code></pre>
<p>where:</p>
<ul>
<li><p><code>schedule_probe_entry_handler</code> is our probe handler, which is executed when <code>schedule</code> returns.</p>
</li>
<li><p><code>maxactive = 32</code>: the maximum number of concurrent executions of the probed function that will be tracked</p>
</li>
<li><p><code>.symbol_name = "schedule"</code> is used to specify the function to track or probe</p>
</li>
</ul>
<h3>Register the probe</h3>
<p>In the init function, after registering the miscdevice, we'll use <code>register_kretprobe</code> to register <code>schedule_probe</code>.</p>
<pre><code class="language-c">static int tracer_init(void)
{	
	...
	ret = register_kretprobe(&amp;schedule_probe);
	...
}
</code></pre>
<h3>Implement the handler</h3>
<details>
	<summary><strong>schedule_probe_entry_handler</strong></summary>
<pre><code class="language-c">static int schedule_probe_entry_handler(struct kretprobe_instance *ri,
					struct pt_regs *regs)
{
	struct tracer_stats *tr_proc;
	pid_t running_pid;
<pre><code>running_pid = ri-&gt;task-&gt;pid;
tr_proc = tracer_find_proc(running_pid);
if (tr_proc)
	tr_proc-&gt;tr_sched += 1;

<p>return 0;
</p></code></pre>
</code><p><code class="language-c">}
NOKPROBE_SYMBOL(schedule_probe_entry_handler);
</code></p></pre><p></p><p></p>
</details>

<p><a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/58155db4d34484de260b7d0131552cf652b886b4/1-tracer/tracer.c#L329-L342">schedule_probe_entry_handler</a> is simple; it takes:</p>
<ul>
<li><p>an instance of <code>struct kretprobe_instance</code> as its first argument, which contains a pointer to the process that called the probed function (for example, <code>schedule</code>)</p>
</li>
<li><p>a pointer to a copy of the CPU registers saved at that moment. We'll see later how to get the values of these registers.</p>
</li>
</ul>
<p><strong>NB</strong>: <code>NOKPROBE_SYMBOL</code> prevents our probe handler from being probed by kprobe/kretprobe or traced by other techniques available in the kernel.</p>
<p>So, each time <code>schedule</code> is called by a process and our handler is fired, we go through the <code>kretprobe_instance</code> and check whether the process ID is already on our list, so we can increment <code>tr_sched</code>, the number of <code>schedule</code> calls for this process.</p>
<h3>Probe <code>up</code>, <code>down_interruptible</code>, <code>mutex_lock</code> and <code>mutex_unlock</code></h3>
<p>Basically, we'll do the same thing to probe <code>up</code>, <code>down_interruptible</code>, <code>mutex_lock</code>, and <code>mutex_unlock</code>, except that in the case of <code>mutex_lock</code>, we'll rather probe <code>mutex_lock_nested</code> instead of <code>mutex_lock</code> directly because <code>mutex_lock</code> is a macro that calls <code>mutex_lock_nested</code>.</p>
<p>And what's the problem with the macro? Macros in C are inlined directly at each call site, which means that the assembly code associated with the macro is always pasted directly wherever the macro is called. So, there is no explicit <strong>call to</strong> <code>mutex_lock</code>, but rather <strong>assembly code where</strong> <code>mutex_lock_nested</code> <strong>is called</strong>.</p>
<p>You can go to the <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/main/1-tracer/tracer.c">GitHub repository</a> to see the full implementations.</p>
<h1>Next</h1>
<p>In this article, we learnt:</p>
<ul>
<li><p>A new way to create a character device driver with <strong>miscdevice</strong></p>
</li>
<li><p>How to define the <strong>file_operations</strong> used by drivers</p>
</li>
<li><p>How to interact with the <code>/proc</code> file</p>
</li>
<li><p>How to define a handler to kprobe some kernel functions</p>
</li>
<li><p>An introduction to the famous <code>ioctl</code></p>
</li>
</ul>
<p>In the next article, we'll continue the kprobe-based tracer by handling <code>kmalloc</code> and <code>kfree</code>.</p>
]]></content:encoded></item><item><title><![CDATA[Linux Kernel Labs: Character device driver (Part 1)]]></title><description><![CDATA[Linux Kernel Labs/Operating System 2 - (Week 3- 5): Character device, interrupts, I/O access, deferred work, Kprobe based tracer]]></description><link>https://tawaliou.com/linux-kernel-labs-character-device-driver-part-1</link><guid isPermaLink="true">https://tawaliou.com/linux-kernel-labs-character-device-driver-part-1</guid><category><![CDATA[operating system]]></category><category><![CDATA[linux kernel]]></category><category><![CDATA[device-driver]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Fri, 14 Aug 2026 16:52:25 GMT</pubDate><content:encoded><![CDATA[<h1>Character device drivers</h1>
<p>Another week with the Linux kernel source! This week was tough with the topics and the assignments.</p>
<h1>Lectures</h1>
<p>The lectures of these weeks were mostly about:</p>
<ul>
<li><p><strong>Interrupts:</strong> Events that can alter program execution and can be generated by hardware devices or the CPU.</p>
</li>
<li><p><strong>I/O access:</strong> Operations that allow read/write functionality from/to device ports (=registers).</p>
</li>
<li><p><strong>Deferred work:</strong> Postponing or scheduling for a later time the execution of a routine.</p>
</li>
</ul>
<p>But let's focus on the labs, because yes, they were more interesting and I learned more from them (by practicing).</p>
<h1>Labs</h1>
<h2>Character device drivers</h2>
<p><a href="https://linux-kernel-labs.github.io/refs/heads/master/so2/lab3-device-drivers.html"><strong>Links</strong></a></p>
<p>A character device is a hardware or virtual computer component that sends and receives data sequentially, one <strong>byte</strong> or <strong>character</strong> at a time. The device driver is the kernel module that interacts with this device. Examples of character devices include the keyboard and mouse.</p>
<p>The hardware devices are accessed by the user through special device files. These files are grouped into the <code>/dev</code> directory, and system calls <code>open</code>, <code>read</code>, <code>write</code>, <code>close</code>, <code>lseek</code>, <code>mmap</code> etc. are redirected by the operating system to the device driver associated with the device. Usually we'll write user code (client code) that will allow interaction with them.</p>
<p><strong>NB</strong>: In contrast to character devices, we have block devices where data volume is large and organized into <strong>blocks</strong>, and they also have their own API to interact with them. I've already read that there is a future lab dedicated to them.</p>
<p>As the character device driver supports many operations, let's see how to create a character device driver (=kernel module) and handle these operations.</p>
<p>Following are the common steps (the long way) to set up a character device driver named <code>my_char_dev</code>:</p>
<h3>Set the Major and Minor numbers</h3>
<p><strong>Major</strong> number identifies the device type (we can see it like the group/category of the device), while the <strong>minor</strong> number identifies the device itself. The <strong>major number</strong> can identify the <code>DISK</code> or <code>Serial Port</code> and the <strong>minor</strong> identifies the <code>first disk</code> or the <code>second serial port</code> on which the driver will work.</p>
<p>In the initialization function passed to <code>module_init</code> (refer to my article on <a href="https://tawaliou.com/linux-kernel-labs-kernel-module-and-kernel-api#kernel-modules">modules</a>):</p>
<pre><code class="language-c">#include &lt;linux/cdev.h&gt;

int register_chrdev_region(dev_t first, unsigned int count, char *name);
</code></pre>
<p>In our case:</p>
<pre><code class="language-c">#define MY_MAJOR 42
#define MY_MINOR 0
#define MY_MINOR_COUNT 0
#define MY_DRIVER_NAME "my_char_dev"   

// in the initialization function
register_chrdev_region(MKDEV(MY_MAJOR, MY_MINOR), MY_MINOR_COUNT, MY_DRIVER_NAME);
</code></pre>
<p>The registration can fail and return an error, we need to check that and do the appropriate operations.</p>
<p>I recommend at the same time to unregister the device allocated in the exit so we will not forget to do it. In the exit function passed to <code>module_exit</code> (refer to my article on <a href="https://tawaliou.com/linux-kernel-labs-kernel-module-and-kernel-api#kernel-modules">modules</a>):</p>
<pre><code class="language-c">#include &lt;linux/cdev.h&gt;

void unregister_chrdev_region(dev_t first, unsigned int count);
</code></pre>
<p>In our case:</p>
<pre><code class="language-c">// in the exit function
unregister_chrdev_region(MKDEV(MY_MAJOR,MY_MINOR),MY_MINOR_COUNT);
</code></pre>
<p>NB: If the number of minor devices exceeds <code>MY_MINOR_COUNT</code>, the registration will move to the next major.</p>
<h3>Initialize and add <code>cdev</code> structure to kernel</h3>
<p>Here are the methods we will use to add and initialize our device to the kernel:</p>
<pre><code class="language-c">void cdev_init(struct cdev *cdev, struct file_operations *fops);

int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
</code></pre>
<p>We need the <code>struct cdev</code>. In reality, our driver can manage some data related to each device, like holding some information per device. So we'll define a struct that will have <code>cdev</code> as an attribute with additional data:</p>
<pre><code class="language-c">struct my_cdev_data {
    struct cdev cdev;
    int nb_used;
    pid_t related_pid; 
};

struct my_cdev_data my_dev;
</code></pre>
<p><strong>NB</strong>: We can have more than one minor and we can do <code>struct my_cdev_data my_devs[MY_MINOR_COUNT];</code> to index all the minor devices.</p>
<p>Inside the initialization as always we'll initialize:</p>
<pre><code class="language-c">// inside the initialization function
cdev_init(&amp;my_dev.cdev, &amp;my_fops);
cdev_add(&amp;my_dev.cdev, MKDEV(MY_MAJOR, MY_MINOR), 1);
</code></pre>
<p>In the next section, we'll talk about <code>my_fops</code>.</p>
<p>In the exit function (when the module is unloaded), we will not forget to delete the device before unregistration:</p>
<pre><code class="language-c">void cdev_del(struct cdev *dev);
</code></pre>
<p>So:</p>
<pre><code class="language-c">// in the exit function
cdev_del(&amp;my_dev.cdev);


unregister_chrdev_region(MKDEV(MY_MAJOR,MY_MINOR),MY_MINOR_COUNT);
</code></pre>
<h3>Define file operations with <code>struct file_operations</code></h3>
<p>Above, we said that we can do some actions (<code>syscalls</code>) like <code>open</code>, <code>read</code>, <code>write</code>, <code>close</code> and many more on the devices through our driver, right? Good.</p>
<p><code>struct file_operations</code> will allow us to do that, because it defines some functions that we need to provide for each operation.<br />Refer to this link <a href="https://elixir.bootlin.com/linux/v7.1.5/source/include/linux/fs.h#L1926">struct file_operations</a> to have an overview of these operations.<br />Here I'll just show briefly some of them:</p>
<pre><code class="language-c">struct file_operations {
	struct module *owner;
	...
	loff_t (*llseek) (struct file *, loff_t, int);
	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
	...
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	...
	int (*open) (struct inode *, struct file *);
    int (*release) (struct inode *, struct file *);
	...
	...
};
</code></pre>
<p><code>release</code> is the equivalent of <code>close</code>.<br />When we do a syscall from the user space to interact with our driver like:</p>
<pre><code class="language-c">int fd = open("/dev/my_char_dev", O_RDONLY);
...
read(fd, buffer, size) 
...
</code></pre>
<p>Under the hood, the equivalent operation handlers defined in <code>file_operations</code> are called with the appropriate arguments.</p>
<p>In our case we can do something like:</p>
<pre><code class="language-c">static int my_open (struct inode *inode, struct file *file)
{
 // pr_info("Device opened");
    return 0;
}

static int my_release (struct inode *inode, struct file *file)
{
    // pr_info("Device released");
    return 0;
}

static ssize_t my_read (struct file *file, char __user *buffer, size_t size, loff_t *offset)
{
    // pr_info("Data read");
    return size;
}

static ssize_t my_write (struct file *, const char __user *, size_t, loff_t *)
{
    // pr_info("Data write");
    return size;
}

struct file_operations my_fops = {
    .owner = THIS_MODULE,
    .open = my_open,
    .read = my_read,
    .write = my_write,
    .release = my_release,
    .unlocked_ioctl = my_ioctl
};
</code></pre>
<p>In these functions, you define the operations and how things will be handled. But keep in mind:</p>
<ul>
<li><p>Use <code>copy_from_user</code> and <code>copy_to_user</code> when you want to transfer data between the kernel and user space. For example on <code>char __user *buffer</code> (it's marked <code>__user</code>). So the kernel under the hood can do some security checks like availability and rights to access these memories.</p>
</li>
<li><p><code>unlocked_ioctl</code>: We'll talk later in (the assignment) about this handler and ioctl (Input Output Control), which is really super fun and powerful to use. It allows the device to do custom operations, not only read or write.</p>
</li>
<li><p>In these functions, usually we'll need to manage synchronization to avoid concurrency pitfalls when the same resource (maybe <code>my_dev</code>) is accessed at the same time by two CPUs.</p>
</li>
</ul>
<h3><code>mknod</code> to create <code>/dev/my_char_dev</code></h3>
<p>After loading the driver in the kernel with <code>insmod</code>, we need to create the device. There are many ways to do it: programmatically or manually.<br />For the programmatic way, check <code>device_create</code> and <code>class_create</code>.</p>
<p>Manually, to create an entry for our driver in <code>/dev</code>, we do:</p>
<pre><code class="language-c">//sudo insmod my_char_dev.ko

sudo mknod /dev/my_char_dev c 42 0
</code></pre>
<p>where:</p>
<ul>
<li><p><code>c</code>: is for character device (<code>b</code> for block device)</p>
</li>
<li><p><code>42</code> and <code>0</code>: are respectively the major and the minor</p>
</li>
</ul>
<h3>Optional: Ports registration</h3>
<p>As a character device can interact with hardware devices, there are some functions that allow reading/writing from/to the ports or registers of the hardware device. We can look for <code>inb</code> and <code>outb</code> in <code>#include &lt;asm/io.h&gt;</code>.</p>
<p>Too much? 😭😂, don't worry, in the next post, I'll present my second assignment: Kprobe-based tracer. In this assignment we did tracing on <code>kmalloc</code> and some kernel functions, and we used a new way to define device drivers in a shorter way with operations like <code>open</code>, <code>read</code>, <code>mknod</code>, handled under the hood (not by us) by using <code>struct miscdevice</code>.</p>
<hr />
<p>Here is the complete code of our character device driver with some checks:</p>
<pre><code class="language-c">#include &lt;linux/init.h&gt;
#include &lt;linux/module.h&gt;
#include &lt;linux/fs.h&gt;
#include &lt;linux/cdev.h&gt;
#include &lt;linux/uaccess.h&gt;

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kernel Dev");
MODULE_DESCRIPTION("A minimal character device driver with a single minor device");

#define MY_MAJOR 42
#define MY_MINOR 0
#define MY_DRIVER_NAME "my_char_dev"

/* Single-instance device wrapper */
struct my_cdev_data {
    struct cdev cdev;
    int nb_used;
    pid_t related_pid;
};

/* Single device instance */
static struct my_cdev_data my_dev;

/* File operations */
static int my_open(struct inode *inode, struct file *file)
{
    pr_info("%s: Device opened\n", MY_DRIVER_NAME);
    return 0;
}

static int my_release(struct inode *inode, struct file *file)
{
    pr_info("%s: Device released\n", MY_DRIVER_NAME);
    return 0;
}

static ssize_t my_read(struct file *file, char __user *buffer, size_t size, loff_t *offset)
{
    pr_info("%s: Read operation triggered\n", MY_DRIVER_NAME);
    return 0; /* EOF */
}

static ssize_t my_write(struct file *file, const char __user *buffer, size_t size, loff_t *offset)
{
    pr_info("%s: Write operation triggered (%zu bytes)\n", MY_DRIVER_NAME, size);
    return size;
}

static const struct file_operations my_fops = {
    .owner   = THIS_MODULE,
    .open    = my_open,
    .read    = my_read,
    .write   = my_write,
    .release = my_release,
};

static int __init my_char_init(void)
{
    int ret;
    dev_t dev_num = MKDEV(MY_MAJOR, MY_MINOR);

    /* 1. Reserve 1 device number */
    ret = register_chrdev_region(dev_num, 1, MY_DRIVER_NAME);
    if (ret &lt; 0) {
        pr_err("%s: Failed to register device number (err: %d)\n", MY_DRIVER_NAME, ret);
        return ret;
    }

    /* 2. Initialize and add the single cdev */
    cdev_init(&amp;my_dev.cdev, &amp;my_fops);
    my_dev.cdev.owner = THIS_MODULE;

    ret = cdev_add(&amp;my_dev.cdev, dev_num, 1);
    if (ret &lt; 0) {
        pr_err("%s: Failed to add cdev\n", MY_DRIVER_NAME);
        unregister_chrdev_region(dev_num, 1);
        return ret;
    }

    pr_info("%s: Registered successfully (Major %d, Minor %d)\n",
            MY_DRIVER_NAME, MY_MAJOR, MY_MINOR);
    return 0;
}

static void __exit my_char_exit(void)
{
    /* Clean up cdev and unregister region */
    cdev_del(&amp;my_dev.cdev);
    unregister_chrdev_region(MKDEV(MY_MAJOR, MY_MINOR), 1);

    pr_info("%s: Driver unloaded successfully\n", MY_DRIVER_NAME);
}

module_init(my_char_init);
module_exit(my_char_exit);
</code></pre>
<h2><strong>Next</strong></h2>
<p>The next post will be about assignment 2 <strong>Kprobe based tracer:</strong> the task is to do an instrumentation (count the calls) of functions like <code>kmalloc</code>, <code>kfree</code>, <code>up</code>, <code>down_interruptible</code> in the Linux kernel by using <code>kretprobe</code>. I learnt more about device driver by using <code>miscdevice</code>, <code>ioctl</code> and <code>hashtable</code></p>
<p>By the way, here's my GitHub repo for the assignments: <a href="https://github.com/tawaliou/kernel-labs-OS-2"><strong>kernel-labs-OS-2</strong></a></p>
]]></content:encoded></item><item><title><![CDATA[Linux Kernel Labs: Kernel Module and Kernel  API    ]]></title><description><![CDATA[Linux Kernel Labs/Operating System 2 - (Week 1 - 2): System call, Kernel module, processes, kernel api...]]></description><link>https://tawaliou.com/linux-kernel-labs-kernel-module-and-kernel-api</link><guid isPermaLink="true">https://tawaliou.com/linux-kernel-labs-kernel-module-and-kernel-api</guid><category><![CDATA[operating system]]></category><category><![CDATA[linux kernel]]></category><category><![CDATA[kernel-module]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Thu, 23 Jul 2026 11:32:23 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/7c262178-26fb-4190-b401-6a3c2510f1f4.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Ok, I've started a new course on the Linux kernel. It's mostly labs and assignments with lectures.</p>
<p>The course <a href="https://linux-kernel-labs.github.io/refs/heads/master/so2/index.html#operating-systems-2">Operating System 2</a> or <a href="https://linux-kernel-labs.github.io/refs/heads/master/so2/index.html#operating-systems-2">Linux Kernel Labs</a> is focused on the Linux kernel, on how it works and how to interact with it. It explores major concepts such as processes, filesystems, I/O, and more. Most of the labs and assignments involve building kernel drivers, giving us real hands-on practice by loading them into a running kernel.<br />You can go on the website to get more informations about it:</p>
<ul>
<li><p>Linux Kernel version: 5.10.14</p>
</li>
<li><p>Labs: To discover concepts</p>
</li>
<li><p>Assignments</p>
</li>
<li><p>Machine configuration/Setup: <a href="https://gitlab.cs.pub.ro/so2">https://gitlab.cs.pub.ro/so2</a></p>
</li>
</ul>
<p>In this serie I'll talk mostly about the labs and assignments</p>
<h1>Lectures</h1>
<p>The lectures cover system calls, processes, and interrupts.</p>
<p>It covers how system calls are bound to a number, processes with their virtual memory and the <code>task_struct</code> that represents a process in the Linux kernel, and other topics.</p>
<p>Let's move on to the labs (they were more interesting and less boring, sorry 😅). And if you took a course on <a href="https://pdos.csail.mit.edu/6.1810/2025/index.html">operating systems</a> like the one offered by MIT, there is some common knowledge and patterns you'll quickly notice.<br />You'll feel much less overwhelmed by all the concepts of the Linux kernel.</p>
<h1>Labs</h1>
<h2>Kernel Modules</h2>
<p><a href="https://linux-kernel-labs.github.io/refs/heads/master/labs/kernel_modules.html">Link</a></p>
<p>The tasks in this lab are about creating, loading, and unloading kernel modules.</p>
<p>Kernel modules (<code>.ko</code> files) are compiled source code that can be loaded into a running kernel, so there is no need to stop or restart the OS. They run at the kernel level. Inside kernel modules, we can use the kernel API to do many things like getting information about processes, changing their behavior, allocating memory, and much more.<br />You write them like C code (with some conventions), write a Kbuild file, and that's it.</p>
<p>What's Kbuild ?</p>
<blockquote>
<p><strong>Kbuild</strong> is the primary build system used by the Linux Kernel . It compiles both the core kernel and loadable modules, handling complex dependencies, compiler flags, and architecture-specific configurations across various hardware platforms.</p>
<p><code>Google</code>.</p>
</blockquote>
<p>Here is an example of a kernel module</p>
<p><code>Kbuild</code></p>
<pre><code class="language-shell">obj-m = hello_mod.o
</code></pre>
<p><code>hello_module.c</code></p>
<pre><code class="language-c">#include &lt;linux/module.h&gt;// always required
// export functions like module_init/module_exit
 
#include &lt;linux/init.h&gt;
 
#include &lt;linux/kernel.h&gt; // export basic kernel macros 
// like pr_info/printk, pr_debug

MODULE_DESCRIPTION("Simple module");
MODULE_AUTHOR("Kernel Hacker");
MODULE_LICENSE("GPL"); // Module license and code that can be used. For example: "GPL" unlocks GPL-only kernel symbols.

static int my_hello_init(void)
{
	pr_debug("Hello!\n");
	return 0;
}

static void hello_exit(void)
{
	pr_debug("Goodbye!\n");
}

module_init(my_hello_init); // Will invoke my_hello_init after loading the module

module_exit(hello_exit); // Will invoke hello_exit after unloading 
</code></pre>
<p>And with a command like the following:</p>
<pre><code class="language-shell">make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
</code></pre>
<p>where</p>
<ul>
<li><p><code>-C /lib/modules/$(uname -r)/build</code>: kernel build system directory. Points to headers needed to build modules against the running kernel.</p>
</li>
<li><p><code>M=$(pwd)</code>: your module directory (where your Kbuild sits)</p>
</li>
<li><p><code>modules</code>: target (we're building a module)</p>
</li>
</ul>
<p>So after that we'll have a <code>hello_mod.ko</code></p>
<p>Then:</p>
<ul>
<li><p>load it into the kernel: <code>insmod hello_mod.ko</code> (need root privileges)</p>
</li>
<li><p>unload it from the kernel: <code>rmmod hello_mod</code> (.ko is not required when unloading)</p>
</li>
<li><p>to list loaded modules: <code>lsmod</code></p>
</li>
</ul>
<p>Something to remember, debugging in the kernel cannot be done like in our usual C code with a <code>printf</code>, these functions are not available at the kernel level. But we can read the debug info from the kernel log by using <code>dmesg</code> (display message).<br />You can read more about it on the course, internet and play with it.</p>
<h2>Kernel API</h2>
<p><a href="https://linux-kernel-labs.github.io/refs/heads/master/labs/kernel_api.html">Link</a></p>
<p>The tasks here are about some Kernel API to interact with...the kernel:</p>
<ul>
<li><p>Memory allocation: <code>kmalloc</code></p>
</li>
<li><p>Locking with <strong>mutex</strong> like <code>mutex_lock</code> , <strong>spinlock</strong> like <code>spin_lock</code> or <code>read_lock</code> (for locking when reading) and many more</p>
</li>
<li><p>Atomic methods like <code>atomic_read</code> to read <code>atomic_t</code> variables safely with no race condition</p>
</li>
<li><p>List: doubly linked list (yes the kernel has a dedicated list data structure)</p>
</li>
</ul>
<p>It's the list api that I really like. Why ? The way to use it: a little ambiguous but meaningful.</p>
<h2>List API</h2>
<p>The list data structure of the kernel is a doubly linked list defined in <a href="https://elixir.bootlin.com/linux/v4.19.19/source/include/linux/types.h">include/linux/types.h</a> and its methods are in <a href="https://elixir.bootlin.com/linux/v4.19.19/source/include/linux/list.h">include/linux/list.h</a> .</p>
<p>What's interesting is that it's an independent structure that we can add as an attribute to our own structure to have all the capabilities it offers.<br />Let's assume you have a structure like</p>
<p><code>my_data</code></p>
<pre><code class="language-c">struct my_data {
    u8 access;
    char name[25];
    int num;
};

// and use it like
struct my_data data1;
struct my_data data2;
...
</code></pre>
<p>And you want to have a list of <code>my_data</code>, you just need to:</p>
<ul>
<li><p>add the <code>struct list_head</code> as an attribute to <code>my_data</code></p>
</li>
<li><p>Set a head that will be used to access your list</p>
</li>
</ul>
<pre><code class="language-c">struct my_data {
    u8 access;
    char name[25];
    int num;

    struct list_head my_list; // (1)
};

LIST_HEAD(my_head); // (2)
</code></pre>
<p>Easy.</p>
<p>So as <code>list_head</code> has <code>prev</code> and <code>next</code> as attributes which are respectively pointers to the previous and next data (in our case <code>data1</code>, <code>data2</code>), we can easily access them through <code>my_list</code>.</p>
<p>But did you notice that there is no link between <code>my_list</code> and <code>head</code> 😅 ? and also the way we define the head ? 😅 . A little funny and confusing.</p>
<p>Yes at this point, adding <code>list_head</code> to <code>my_data</code> is like</p>
<blockquote>
<p>we'll have a list of my_data and <code>my_list</code> attribute will be used as the corresponding node of the target data in the list</p>
</blockquote>
<p>And by doing <code>LIST_HEAD(my_head)</code>, <code>LIST_HEAD</code> is a macro that takes a name (here <code>my_head</code>) and internally sets it as a list head, so it's like</p>
<blockquote>
<p>my_head will be the head of a list (of type struct list_head of course) and set its prev to itself</p>
</blockquote>
<p>So at this point, no link between the declarations.</p>
<p>To now connect both or start using your list, you need to use the provided methods that work on the list: we'll see just 3 (there are plenty of them). Some of them are macros, others are regular functions.</p>
<h3>Add element: list_add</h3>
<p>To add an element to the list we use</p>
<pre><code class="language-c">list_add(struct list_head *new, struct list_head *head)
</code></pre>
<p>so something like</p>
<pre><code class="language-c">list_add(&amp;data1.my_list, &amp;head);
</code></pre>
<h3>Access element: list_entry, list_for_each</h3>
<p>we can use the provided macro</p>
<pre><code class="language-c">list_for_each(pos, head);
</code></pre>
<p>that iterates over the list where:</p>
<ul>
<li><p>pos: is a pointer to a <strong>struct list_head</strong> used as a cursor to allow stepping from one element of the node to another</p>
</li>
<li><p>head: is a pointer to the head</p>
</li>
</ul>
<p>Internally it's the usual loop we write to iterate over a list, here is the code of this macro in <code>include/linux/list.h</code></p>
<pre><code class="language-c">/**
 * list_for_each	-	iterate over a list
 * @pos:	the &amp;struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 */

#define list_for_each(pos, head) \
	for (pos = (head)-&gt;next; pos != (head); pos = pos-&gt;next)
</code></pre>
<p>Then inside this macro we'll use</p>
<pre><code class="language-c">list_entry(ptr, type, member); // return a pointer to struct my_data
</code></pre>
<p>That returns the entry at each iteration.<br />where</p>
<ul>
<li><p>ptr: is the cursor defined for <code>list_for_each</code></p>
</li>
<li><p>type: literally the type of struct (<code>struct my_data</code>)</p>
</li>
<li><p>member: the list_head attribute added to the struct</p>
</li>
</ul>
<p>So it's used like this</p>
<pre><code class="language-c">struct list_head *cursor;
struct my_data *temp;

list_for_each(cursor, &amp;head) {
	temp = list_entry(cursor, struct my_data, my_list);
	pr_info("(%d, %s) ", temp-&gt;access, ti-&gt;name);
}
</code></pre>
<p>It can be a little confusing but by doing the various labs you can start to feel it.</p>
<p>There are more macros to interact with the list that you can find in <code>include/linux/list.h</code></p>
<h2>Assignments 0: Kernel API</h2>
<p><a href="https://linux-kernel-labs.github.io/refs/heads/master/so2/assign0-kernel-api.html">Link</a></p>
<p><a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/main/0-list/list.c">Github Link to my solution</a>.</p>
<p>The assignment is about the use of the Kernel API. Here's a short version of the tasks:</p>
<ul>
<li><p>Create a <code>list.ko</code> kernel module that uses the Linux kernel list API to store strings.</p>
</li>
<li><p>Expose a <code>/proc/list/management</code> write-only interface to process <code>addf</code>, <code>adde</code>, <code>delf</code>, and <code>dela</code> commands like <code>echo "addf name" &gt; /proc/list/management</code>).</p>
</li>
<li><p>Allow reading the list contents line-by-line through the read-only <code>/proc/list/preview</code> file through <code>cat /proc/list/preview</code></p>
</li>
<li><p>A skeleton is already provided for the setup of <code>preview</code> and <code>management</code> proc files. This part of the code is worth reading.</p>
</li>
<li><p>A test <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/main/0-list/checker/_checker"><code>check/_checker</code></a> is also provided so we can check if our implementation respects the constraints.</p>
</li>
</ul>
<p>The work is to parse the write commands and implement for each of the four of them a function to handle the operation. In my case I implemented:</p>
<ul>
<li><p><code>list_proc_addf</code> for <code>addf</code>: <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/05c871ec3b0a6ae1b0546f91f84ccc8fd09209d4/0-list/list.c#L57">Github Link</a></p>
</li>
<li><p><code>list_proc_delf</code> for <code>delf</code>: <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/05c871ec3b0a6ae1b0546f91f84ccc8fd09209d4/0-list/list.c#L79">Github Link</a></p>
</li>
<li><p>and do the same for the remaining 2 commands</p>
</li>
<li><p>Then the function to allocate memory for the string and set its value: <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/05c871ec3b0a6ae1b0546f91f84ccc8fd09209d4/0-list/list.c#L43">Github Link</a></p>
</li>
</ul>
<p>If you do the lab on kernel api, it'll be easy.</p>
<p>Here is</p>
<p>Few things we need to keep in mind or understand from this assignment:</p>
<ul>
<li><p>Don't forget to purge (free) the memory you allocate in the module before the exit. In my case I implemented <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/05c871ec3b0a6ae1b0546f91f84ccc8fd09209d4/0-list/list.c#L124"><code>list_proc_purge</code></a> that I called inside the <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/05c871ec3b0a6ae1b0546f91f84ccc8fd09209d4/0-list/list.c#L223"><code>list_exit</code></a> which is used as the module exit entry point <a href="https://github.com/tawaliou/kernel-labs-OS-2/blob/05c871ec3b0a6ae1b0546f91f84ccc8fd09209d4/0-list/list.c#L230"><code>module_exit</code></a>(when we remove the module with rmmod). Otherwise there will be a <strong>memory leak in kernel memory.</strong></p>
</li>
<li><p>Don't forget to use <code>copy_from_user</code> and <code>copy_to_user</code> that respectively copy data (buffer) from user space to kernel space and copy data from kernel space to user space (for security reasons...)</p>
</li>
<li><p>We can use <code>seq_*</code> methods like <code>seq_puts</code>, <code>seq_printf</code> and many more to output data in a <code>/proc</code> file. In our case by doing <code>cat /proc/list/preview</code>. These functions are better for interacting with <code>/proc</code> than <code>printk</code> which outputs data in the kernel log (<code>dmesg</code>)</p>
</li>
<li><p>Look at <a href="https://elixir.bootlin.com/linux/v4.19.19/source/include/linux/proc_fs.h"><code>include/linux/proc_fs.h</code></a> and <a href="https://elixir.bootlin.com/linux/v4.19.19/source/fs/proc/generic.c">fs/proc/generic.c</a> to have a list of functions to interact with <code>/proc</code>, like creating a directory, sub files...</p>
</li>
</ul>
<h2><strong>Next weeks</strong></h2>
<p>The next weeks are about character device drivers, which are kernel modules that allow access to hardware devices, device files, I/O access, interrupts.</p>
<p>By the way, here's my GitHub repo for the assignments: <a href="https://github.com/tawaliou/kernel-labs-OS-2"><strong>kernel-labs-OS-2</strong></a></p>
<img src="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/949d3175-6b16-47d1-947f-19a5c7ca5a0c.png" alt="" style="display:block;margin:0 auto" />

<p>Figure 1: Kernel API assignment tests passed.</p>
<img src="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/2ff4b09f-415d-4845-a31d-bb7f81ec3edf.png" alt="" style="display:block;margin:0 auto" />

<p>Figure 2: <code>addf</code> and <code>adde</code> commands and <code>/proc/list/preview</code> showcase</p>
<img src="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/da88afee-2b7b-4b26-91d9-d2c3dc980c10.png" alt="" style="display:block;margin:0 auto" />

<p>Figure 3: <code>delf</code> and <code>dela</code> showcase</p>
]]></content:encoded></item><item><title><![CDATA[OS Design and Implementation: Recap]]></title><description><![CDATA[MIT 6.1810 Fall2025 Course - Recap]]></description><link>https://tawaliou.com/os-design-and-implementation-recap</link><guid isPermaLink="true">https://tawaliou.com/os-design-and-implementation-recap</guid><category><![CDATA[networking]]></category><category><![CDATA[Linux]]></category><category><![CDATA[systemd]]></category><category><![CDATA[file system]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Mon, 18 May 2026 06:22:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/f34ee422-83cb-4362-aca4-82e94c8e58c1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The course <a href="https://pdos.csail.mit.edu/6.1810/2025/index.html">MIT 6.1810: Operating System Engineering - Fall 2025</a></p>
<p>Let's say I'm at the end of the course, with a few papers still left to read:</p>
<ul>
<li><p><a href="https://pdos.csail.mit.edu/6.1810/2025/readings/containers-vm.pdf">Blending Containers and Virtual Machines: a study of Firecracker and gVisor (2020)</a>: <em>next task</em></p>
</li>
<li><p><a href="https://pdos.csail.mit.edu/6.1810/2025/readings/rcu-decade-later.pdf">Read-Copy Update (RCU) Usage In the Linux Kernel</a>: a scalable high-performance synchronization mechanism implemented in the Linux kernel (<strong>on locking</strong>)</p>
</li>
<li><p><a href="https://pdos.csail.mit.edu/6.1810/2025/readings/bpf-usenix93.pdf">The BSD Packet Filter: A New Architecture for User-level Packet Capture</a>: on eBPF</p>
</li>
<li><p><a href="https://pdos.csail.mit.edu/6.1810/2025/readings/meltdown.pdf">Meltdown: Reading Kernel Memory from User Space</a></p>
</li>
</ul>
<p>I'm wondering if it's the right time to read these papers. There are still many basic concepts in the Linux kernel I don't fully understand. Perhaps I'll return to them later. Now, let me share my thoughts about the courses.</p>
<h2>Course structure</h2>
<h3>Structure</h3>
<p>The MIT course focuses on operating system engineering, exploring how an operating system is constructed—from accessing hardware resources to running user-space applications. As part of the course, a small OS called xv6 is developed. It appears the course was initially created in 2010 or 2006 and has been updated annually.</p>
<p>xv6 is a Unix-like OS, and the course lectures and labs revolve around it. Therefore, we have:</p>
<ul>
<li><p>videos courses,</p>
</li>
<li><p>book: the xv6 book</p>
</li>
<li><p>Labs: coding exercises with tests to verify your solutions</p>
</li>
</ul>
<p>We gain theoretical knowledge reinforced through labs (which are quite challenging 😅).</p>
<p>For each important aspect of the OS, there is a chapter to read and a video to watch. These resources are free, and there is also a lab to complete.</p>
<h3>Duration</h3>
<p>The course typically runs for <strong>4</strong> <strong>months</strong>, from <strong>September</strong> to <strong>December</strong>, during the fall of 2025. Here is the schedule: <a href="https://pdos.csail.mit.edu/6.1810/2025/schedule.html">https://pdos.csail.mit.edu/6.1810/2025/schedule.html</a></p>
<p>I studied the course during nights and weekends from <strong>November</strong> to <strong>May</strong> 2016, which took me <strong>7</strong> <strong>months</strong>. Some lectures and tasks took me more than a week to study or complete, unlike the one-week-per-chapter schedule on their website. So yes, I'm tired, but I love it 😂 😭!</p>
<p>The advantage is that you can progress at your own pace, spending more time on specific topics to ensure you understand them before moving on.</p>
<p>However, after 4 or 5 months, you might start feeling bored and tired. Without classmates, challenging topics like Memory Management can become exhausting.</p>
<h2>My feeling</h2>
<p>I can't deny that I enjoy studying the course. While there are topics I didn't fully grasp (it's unlikely you'll understand everything with just this course), many concepts now seem less daunting. The labs have helped me develop mental models of how OS components are designed and implemented, similar to Linux.</p>
<p>I believe one of the course's main goals is to:</p>
<ul>
<li><p>Introduce students to topics like file systems, system calls, locks, etc, reducing their complexity.</p>
</li>
<li><p>Make Operating Systems and Kernel development less intimidating.</p>
</li>
<li><p>Lay the groundwork for research and deeper knowledge by reading papers on various topics.</p>
</li>
<li><p>Practice writing C code, which is essential for Kernel developers.</p>
</li>
</ul>
<p>Now, when I hear "System Call," I understand it's a function executed in the kernel, tagged with a number that user space code uses to perform actions.</p>
<p>I have blog posts on the course in this series: <a href="https://tawaliou.com/series/operating-system"><strong>Operating System Eng</strong></a></p>
<h2>Next step</h2>
<p>My timeline since 2023:</p>
<ol>
<li><p><strong>Computer Architecture</strong> (<a href="https://www.nand2tetris.org/">From Nand2Tetris</a>)</p>
</li>
<li><p><strong>OS Engineering</strong> (<a href="https://pdos.csail.mit.edu/6.1810/2025/index.html">MIT 6.1810</a>)</p>
</li>
<li><p>??</p>
</li>
</ol>
<p>For the third step, I'm planning to delve into the Linux Kernel. So my target for the next <strong>7</strong> <strong>months</strong> is <a href="https://linux-kernel-labs.github.io/refs/heads/master/">Linux Kernel Teaching</a> (yes another course). Here's a description of the course taken from their website</p>
<blockquote>
<p>This is a collection of lectures and labs Linux kernel topics. The lectures focus on theoretical and Linux kernel exploration.</p>
<p>The labs focus on device drivers topics and they resemble "howto" style documentation. Each topic has two parts:</p>
<ul>
<li><p>a walk-through the topic which contains an overview, the main abstractions, simple examples and pointers to APIs</p>
</li>
<li><p>a hands-on part which contains a few exercises that should be resolved by the student; to focus on the topic at hand, the student is presented with a starting coding skeleton and with in-depth tips on how to solve the exercises</p>
</li>
</ul>
</blockquote>
<p>The lectures and labs focus on the Linux Kernel (5.10.14). I'll spend a month evaluating it to decide whether to continue (I'm currently 99% sure I will).</p>
<p><mark class="bg-yellow-200 dark:bg-yellow-500/30">By 2027, I aim for the Linux Kernel to feel less intimidating. It's a long-term goal, with many open-source projects in this area (like hypervisors and containers) where Kernel knowledge will be beneficial.</mark> The truth is, I'm not sure if I'll achieve this, but I enjoy studying systems, so let's hope I make it one day 🤣. You can read <a href="https://jyn.dev/i-m-just-having-fun/">i'm just having fun</a> by Jynn Nelson (a compiler expert) on having fun while writing code.</p>
<p><strong>So let's take a breath and start this new step.</strong></p>
<p><strong>Links</strong></p>
<ol>
<li><strong>(My blog - Series) Operating system Eng. - My learning path</strong> <a href="https://tawaliou.com/series/operating-system"><strong>Operating System Eng</strong></a></li>
</ol>
<img src="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/ce00005b-7e8c-47ca-8f3a-3b26774122d0.png" alt="" style="display:block;margin:0 auto" />

<ol>
<li><p>The course website: <a href="https://pdos.csail.mit.edu/6.1810/2025/index.html">MIT 6.1810: Operating System Engineering - Fall 2025</a></p>
</li>
<li><p>My Github: <a href="https://github.com/tawaliou/MIT-6.1810Fall2025-xv6">https://github.com/tawaliou/MIT-6.1810Fall2025-xv6</a></p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[OS Design and Implementation: (Week 8-9) - Scheduling; File system]]></title><description><![CDATA[MIT 6.1810 Fall2025 Course - Week 8-9: Scheduling; File system]]></description><link>https://tawaliou.com/os-design-and-implementation-week-8-9-scheduling-file-system</link><guid isPermaLink="true">https://tawaliou.com/os-design-and-implementation-week-8-9-scheduling-file-system</guid><category><![CDATA[Linux]]></category><category><![CDATA[Kernel]]></category><category><![CDATA[networking]]></category><category><![CDATA[file system]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Sat, 16 May 2026 08:35:01 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/3dd5123a-2bbf-4e40-bb5f-14b728aa6e82.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Scheduling: big points</h2>
<p>To execute multiple processes (multiplexing), xv6 switches each CPU from one process to another in the following situations:</p>
<ul>
<li><p>Voluntary switches: Occur when a trap (such as a system call or timer interrupt) happens during the execution of a process.</p>
</li>
<li><p>Involuntary switches: Occur when xv6 forces the CPU to switch if a process takes too long to execute.</p>
</li>
</ul>
<p>However, this approach presents several challenges (as noted in the xv6 book):</p>
<ul>
<li><p>How to switch from one process to another</p>
</li>
<li><p>Forcing switches that are transparent to the user</p>
</li>
<li><p>Managing situations when multiple CPUs attempt to switch and run the same process simultaneously</p>
</li>
<li><p>Handling memory, I/O, and other resources</p>
</li>
<li><p>Keeping track of which process is running on each CPU during switches</p>
</li>
</ul>
<h2>File system</h2>
<p>A file system is a method for an OS to organize and access data on a disk, whether it's a hard disk drive or a solid-state drive. It serves as a hierarchy that enables indirect data access, allowing the OS to perform actions and checks before and after saving or reading data. In xv6 and Unix-like operating systems, this structure is implemented in a specific order.</p>
<ol>
<li><p>File descriptor: small integer representing a kernel-managed object that a process will use to read from/write to a file.</p>
</li>
<li><p>Pathname</p>
</li>
<li><p>Directory</p>
</li>
<li><p>Inode: Data structure that holds info (size, permissions, file type, pointer to the block adresses, ...)</p>
</li>
<li><p>Logging: For crash recovery, this layer allows the OS to store descriptions of write operations before execution, enabling replay or discarding if a crash occurs.</p>
</li>
<li><p>Buffer Cache: LRU data structure that cached and ensure the accessed block of data</p>
</li>
<li><p>Disk (block/sector): unit of stockage of data</p>
</li>
</ol>
<p>Remember that the filesystem treats the disk as a large, continuous array of blocks segmented to store data. There's much more to learn about filesystems, but for now, I'll stick to the fundamentals.</p>
<h2><strong>Labs</strong></h2>
<p>There are two labs in the chapter and I completed the first one</p>
<ol>
<li><p><strong>Large files (completed):</strong> modifies the xv6 file system to support much larger files by adding a doubly-indirect block to inodes, increasing the file limit from 268 blocks to 65,803 blocks. You must update functions like <code>bmap()</code> and <code>itrunc()</code> to correctly allocate, map, and free direct, indirect, and doubly-indirect blocks while preserving the inode size. (<strong>Figure 1 below</strong>)</p>
</li>
<li><p><strong>Symbolic links:</strong> This assignment adds symbolic link support to xv6 by implementing the <code>symlink(target, path)</code> system call, which creates links that reference files by pathname instead of inode. You must introduce a new symbolic link file type, store target paths in link inodes, and modify <code>open()</code> to recursively resolve symbolic links unless the <code>O_NOFOLLOW</code> flag is used, while also detecting cycles to avoid infinite loops.</p>
</li>
</ol>
<img src="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/f011a4c0-19c7-48ec-8e53-8f862c90a19d.png" alt="Figure: Lab File System" style="display:block;margin:0 auto" />

<p>Figure 1: Lab FS - Large Files</p>
<h2>Next week</h2>
<p>Next weeks is about..<strong>. no there is no "next week".</strong></p>
<p>The upcoming chapters primarily focus on reading papers on specific topics such as virtual machines and eBPF.. For now, let's say I'm nearing the end of the course.</p>
<p>By the way, here's my GitHub repo for the lab course: <a href="https://github.com/TawalMc/MIT-6.1810Fall2025-xv6"><strong>https://github.com/TawalMc/MIT-6.1810Fall2025-xv6</strong></a></p>
]]></content:encoded></item><item><title><![CDATA[OS Design and Implementation: (Week 6 - 7) - 
Interrupts & Device Driver; Locks]]></title><description><![CDATA[MIT 6.1810 Fall2025 Course - Week 6-7: Interrupts & Device Driver; Locks]]></description><link>https://tawaliou.com/os-design-and-implementation-week-6-7-interrupts-device-driver-locks</link><guid isPermaLink="true">https://tawaliou.com/os-design-and-implementation-week-6-7-interrupts-device-driver-locks</guid><category><![CDATA[systemprogramming]]></category><category><![CDATA[Linux]]></category><category><![CDATA[Kernel]]></category><category><![CDATA[operating system]]></category><category><![CDATA[interrupts]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Thu, 02 Apr 2026 18:54:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/ece9779f-02d2-4849-b3cf-acc6d38af908.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>xv6 Book; Chapter 6 - 7 - Interrupts &amp; Device Driver; Locks</strong>: <a href="https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf">https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf</a></p>
<p>Chapter 6 and 7 are about Interrupts, device driver and locks.</p>
<h2>Interrupts and Device Driver</h2>
<blockquote>
<p>A driver is the code in an operating system that manages a particular device (network card, mouse, screen...): it configures the device hardware, tells the device to perform operations, handles the resulting interrupts, and interacts with processes using the device</p>
</blockquote>
<p>An interrupt is a type of trap that allows a device to communicate with the operating system.</p>
<p>Here are some details about interrupts and device drivers:</p>
<ul>
<li><p>The CPU does not directly control I/O devices; instead, it communicates with a microcontroller installed on the devices via a control bus.</p>
</li>
<li><p>Memory-mapped I/O is a communication technique where each device is allocated a specific memory space in RAM, allowing the CPU to read or write data.</p>
</li>
<li><p>Polling, or programmed I/O, is another technique where the OS periodically checks if there is data to read from the device.</p>
</li>
<li><p>Interrupt-based I/O is akin to a trap, as discussed in OS Design and Implementation (Week 4) - Trap.</p>
</li>
</ul>
<h2>Locks</h2>
<p>Sure! Everyone is familiar with locks: they enable the OS to safeguard shared resources during use.In this chapter, we explore the internals of locks. Although I don't grasp everything, I gain new insights about them.</p>
<h2><strong>Labs</strong></h2>
<p>There are three labs where we were tasked with building a network driver and adding a custom lock mechanism to xv6. I completed the first one:</p>
<ul>
<li><p><strong>[Device Drivers] Network Interface Card (e1000) driver</strong>: Implement <code>e1000_transmit</code> and <code>e1000_recv</code> in <code>kernel/e1000.c</code> to manage packet flow by manipulating the hardware's transmit and receive descriptor rings via DMA and control registers. You must successfully pass the "txone" and "rxone" tests by correctly handling buffer allocation, hardware status checks, and synchronization. <strong>Completed</strong></p>
</li>
<li><p><strong>[Device Drivers] UDP Receive:</strong> Implement <code>ip_rx</code>, <code>sys_recv</code>, and <code>sys_bind</code> in <code>kernel/net.c</code> to enable UDP packet reception by filtering incoming IP traffic, queuing up to 16 packets per bound port, and delivering them to user processes. You must handle the transition between network and host byte orders using <code>ntohs</code>/<code>ntohl</code> and ensure the system correctly manages packet buffers to pass all <code>nettest</code> verification suites. I implemented <code>ip_rx</code>, <code>sys_bind</code>, i didn't complete the <code>sys_recv</code>.</p>
</li>
<li><p><strong>[Locks] Read-Write Lock:</strong> You must implement a read-write spinlock in xv6 that allows multiple concurrent readers but ensures exclusive access for a single writer. To prevent writer starvation, your lock must implement a priority scheme where any pending writer blocks new readers from acquiring the lock until the writer has finished.</p>
</li>
</ul>
<h2>Next week</h2>
<p>Next weeks is about <strong>Scheduling.</strong></p>
<p>By the way, here's my GitHub repo for the lab course: <a href="https://github.com/TawalMc/MIT-6.1810Fall2025-xv6"><strong>https://github.com/TawalMc/MIT-6.1810Fall2025-xv6</strong></a></p>
<img src="https://cdn.hashnode.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/9afed4bc-5f91-48e9-8bce-8256fc9e284f.png" alt="" style="display:block;margin:0 auto" />]]></content:encoded></item><item><title><![CDATA[OS Design and Implementation: (Week 5) - Page fault]]></title><description><![CDATA[MIT 6.1810 Fall2025 Course - Week 5: Page fault]]></description><link>https://tawaliou.com/os-design-and-implementation-week-5-page-fault</link><guid isPermaLink="true">https://tawaliou.com/os-design-and-implementation-week-5-page-fault</guid><category><![CDATA[Kernel]]></category><category><![CDATA[Linux]]></category><category><![CDATA[systemprogramming]]></category><category><![CDATA[operating system]]></category><category><![CDATA[page-fault]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Fri, 20 Feb 2026 21:47:52 GMT</pubDate><content:encoded><![CDATA[<h2>Lecture</h2>
<p><strong>xv6 Book; Chapter 5 - Page fault</strong>: <a href="https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf">https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf</a></p>
<p>Chapter 5 discusses page faults. A page fault is an exception that occurs when a virtual address is used but either:</p>
<ul>
<li><p>lacks a mapping in the page table (physical memory is not yet allocated),</p>
</li>
<li><p>has an invalid mapping because PTE_V is not set,</p>
</li>
<li><p>or the requested operation (read, write, execute, user, etc.) is not permitted, meaning the appropriate flags (PTE_R, PTE_W, etc.) are not set.</p>
</li>
</ul>
<p>An interesting aspect of page faults is that, when combined with the page table (which maps virtual addresses to physical addresses), they allow the kernel to fully control memory usage, enabling the application of various techniques. For example:</p>
<ul>
<li><p>Copy-on-Write (<strong>CoW</strong>): When executing a fork system call, the kernel shares memory between the parent and child instead of copying the parent's memory to the child, as was done previously. The parent's memory is marked as read-only in the page table and shared with the child. If a write operation occurs, a page fault exception is triggered because the memory is read-only. This alerts the kernel to allocate new memory and copy the shared data before allowing the write operation. This approach ensures that a copy is made only if a write operation occurs, making the fork process faster.</p>
</li>
<li><p><strong>Demand paging</strong>: In this approach, the kernel stores only a portion of the user program's pages in RAM, while the rest remain on disk. The page table entries (PTE) for the pages on disk are marked as invalid. The kernel loads these pages into RAM only when the program requests them, triggering a page fault exception due to their invalid PTEs.</p>
</li>
</ul>
<p>It's as if the kernel intentionally allows this exception and knows how to handle it whenever it occurs.</p>
<h2><strong>Labs</strong></h2>
<p>There is just one lab: Implement copy-on-write (COW) fork() to delay allocating and copying physical memory pages until they are actually needed, if at all. I didn't finish it.</p>
<h2>Next week</h2>
<p>Next weeks is about <strong>Interrupts and Locks.</strong></p>
<p>By the way, here's my GitHub repo for the lab course: <a href="https://github.com/TawalMc/MIT-6.1810Fall2025-xv6"><strong>https://github.com/TawalMc/MIT-6.1810Fall2025-xv6</strong></a></p>
<img src="https://cloudmate-test.s3.us-east-1.amazonaws.com/uploads/covers/5f9bb8f3ff730d5cb8dedb03/581dfde2-0fd5-4059-a1d7-e71f2e7c33de.png" alt="" style="display:block;margin:0 auto" />]]></content:encoded></item><item><title><![CDATA[OS Design and Implementation: (Week 4) - Trap]]></title><description><![CDATA[MIT 6.1810 Fall2025 Course - Week 4: Trap]]></description><link>https://tawaliou.com/os-design-and-implementation-week-4-trap</link><guid isPermaLink="true">https://tawaliou.com/os-design-and-implementation-week-4-trap</guid><category><![CDATA[Kernel]]></category><category><![CDATA[Linux]]></category><category><![CDATA[systemprogramming]]></category><category><![CDATA[operating system]]></category><category><![CDATA[traps]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Tue, 13 Jan 2026 22:25:41 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-lecture">Lecture</h2>
<p><strong>xv6 Book; Chapter 4 - Traps and system calls</strong>: <a target="_blank" href="https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf">https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf</a></p>
<p>The lecture in the 4th week was about <strong>Traps.</strong><br />A trap is an event that causes the CPU to switch from its current task to specific code in the kernel space to handle the event. There are three types of traps:</p>
<ul>
<li><p><strong>syscall:</strong> user space code asking the kernel (through the <strong>ecall</strong> instruction) to perform an action</p>
</li>
<li><p><strong>interrupt:</strong> when a device requests attention, like an I/O operation with a disk</p>
</li>
<li><p><strong>exception:</strong> when something goes wrong in either the kernel or user space.</p>
</li>
</ul>
<p>Anyway, there are common operations that need to be done when a trap occurs:</p>
<ol>
<li><p>Save the current state of the CPU and what's being executed (registers, memory, etc.)</p>
</li>
<li><p>The CPU enters supervisor mode.</p>
</li>
<li><p>The kernel performs appropriate checks (permissions, security, etc.)</p>
</li>
<li><p>Jump to the kernel code (handler) that will handle the trap.</p>
</li>
<li><p>Restore the saved state.</p>
</li>
<li><p>Resume the execution of the running code.</p>
</li>
</ol>
<p>Saying it like this makes it sound easy, but it's more complicated. For example, in the case of Xv6, process state saving is written directly in RISC-V assembly language, and there can be more layers between when the trap occurs and when the process execution resumes.</p>
<p>In the case of a syscall, here is a path in the code:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1768319669107/c5181c27-dbf1-47f9-9cbf-a0282a855920.png" alt class="image--center mx-auto" /></p>
<p>Where <strong>trampoline</strong> is a memory space within the process address space, mapped directly to the same memory space inside the kernel address space, containing the code to enter and exit the kernel.</p>
<p>You can find more details in the book: <a target="_blank" href="https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf">https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf</a></p>
<p>There was a lot of RISC-V assembly code to read at this point 😭</p>
<h2 id="heading-labs">Labs</h2>
<p>There are two labs to complete - <a target="_blank" href="https://pdos.csail.mit.edu/6.1810/2025/labs/traps.html">https://pdos.csail.mit.edu/6.1810/2025/labs/traps.html</a>:</p>
<ol>
<li><p><strong>Backtrace:</strong> A <strong>backtrace</strong>, also known as StackTrace, reconstructs the call history by going through a chain of stack frames. Each frame stores a return address and a pointer to the caller's frame. By starting with the <strong>frame pointer (s0)</strong> and following these links upward, you can identify and print the return address for every function in the current execution path.</p>
</li>
<li><p><strong>Alarm:</strong> You will implement a feature in xv6 that periodically interrupts a process after a set amount of CPU time, serving as a basic foundation for <strong>user-level interrupt and fault handlers</strong>. Your solution is complete once it successfully passes the <code>alarmtest</code> and <code>usertests -q</code> verification suites.</p>
</li>
</ol>
<p>Unlike in week 3, I completed these two labs this time. A big thank you to <a target="_blank" href="https://github.com/Miigon">https://github.com/Miigon</a> for having solutions for some labs of this course, but for the 2020 version "<a target="_blank" href="https://github.com/Miigon/my-xv6-labs-2020">https://github.com/Miigon/my-xv6-labs-2020</a>." It really helped me after spending about 2 weeks working on these labs. My challenge was reading the lectures, the book, and trying to understand the code and explanations on my own, so it wasn't always easy. Having a repository to check when I'm stuck or facing difficulties is really helpful.</p>
<h2 id="heading-next-week">Next week</h2>
<p>Next week is about <strong>Page faults.</strong></p>
<p>By the way, here's my GitHub repo for the lab course: <a target="_blank" href="https://github.com/TawalMc/MIT-6.1810Fall2025-xv6"><strong>https://github.com/TawalMc/MIT-6.1810Fall2025-xv6</strong></a></p>
]]></content:encoded></item><item><title><![CDATA[OS Design and Implementation: (Week 3) - Page tables]]></title><description><![CDATA[MIT 6.1810 Fall2025 Course - Week 3: Page tables]]></description><link>https://tawaliou.com/os-design-and-implementation-week-3-page-tables</link><guid isPermaLink="true">https://tawaliou.com/os-design-and-implementation-week-3-page-tables</guid><category><![CDATA[Kernel]]></category><category><![CDATA[Linux]]></category><category><![CDATA[systemprogramming]]></category><category><![CDATA[operating system]]></category><category><![CDATA[networking]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Wed, 31 Dec 2025 05:52:17 GMT</pubDate><content:encoded><![CDATA[<p>Okay! Not only did I spend more than 3 weeks on it, but I also completed only 2 of the 3 labs about page tables. And yes, it was a tough chapter. Anything related to memory is tough, by the way.</p>
<p>In short, this chapter was about page tables and how the OS creates virtual memory for applications. In fact, the memory used by user applications or that we get when doing</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> a = <span class="hljs-number">25</span>;
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%p"</span>, &amp;a);
</code></pre>
<p>is virtual memory that the OS creates and maps to physical memory.<br />The OS doesn't allow user applications to interact directly with the RAM (this is too dangerous); instead,</p>
<ul>
<li><p>It allocates the same address space of <code>[0; 0x3fffffffff]</code> for each process or application, which contains blocks of memory that are 4096 bits in size.</p>
</li>
<li><p>Then, it maps or associates each address of this virtual memory to a real address in the RAM using what we call a <strong>Page Table Entry</strong>.</p>
</li>
</ul>
<p>There are many other aspects of Virtual Memory, such as access rights to addresses, page size, and the Translation Lookaside Buffer (which acts like an LRU cache for mapping virtual addresses to physical addresses)...</p>
<p>This technique allows the OS to control access to memory and check if a process has the right to read, execute, or write to a memory address, addressing a security concern.</p>
<p>Now, back to the course. It took me some time to understand what Virtual Memory is and how it works, and even more time to complete the labs, which were:</p>
<ol>
<li><p>Map a read-only page containing the PID into the process's virtual memory during creation. The user program then reads the PID directly from that memory address, avoiding the system call.</p>
</li>
<li><p>To help visualize RISC-V page tables, write a function that prints the contents of a page table.</p>
</li>
<li><p>Create megapages by setting valid and read bits in a level-1 PTE and pointing it to a 2MB-aligned physical address.</p>
<p> This reduces page table size and TLB misses, significantly boosting performance for memory-intensive programs. <strong><em>(Not completed)</em></strong>.</p>
</li>
</ol>
<p>And yes, there is an approach called Superpages. Instead of allocating small memory sizes like 4096 bits, the OS allocates larger superpages, such as 2MB. However, it's not straightforward. There are some challenges with its implementation. And guess what? There are two papers to read about it (I swear 😭 It's not just something I imagined about the course):</p>
<ul>
<li><p>Practical, transparent operating system support for superpages [<a target="_blank" href="https://www.usenix.org/conference/osdi-02/practical-transparent-operating-system-support-superpages">https://www.usenix.org/conference/osdi-02/practical-transparent-operating-system-support-superpages</a>] <strong><em>(In reading)</em></strong></p>
</li>
<li><p>A Comprehensive Analysis of Superpage Management Mechanisms and Policies [<a target="_blank" href="https://www.usenix.org/conference/atc20/presentation/zhu-weixi">https://www.usenix.org/conference/atc20/presentation/zhu-weixi</a>] <strong><em>(Not started)</em></strong>.</p>
</li>
</ul>
<p>So, what now? I'll leave it there and start <strong>Week 4</strong>: <strong>Trap and Syscall entry-exit</strong>. Maybe while reading and doing the labs, I can come back to the Superpages lab. I'm not required to understand everything right away 😂  </p>
<p>By the way, here's my GitHub repo for the lab course: <a target="_blank" href="https://github.com/TawalMc/MIT-6.1810Fall2025-xv6">https://github.com/TawalMc/MIT-6.1810Fall2025-xv6</a></p>
]]></content:encoded></item><item><title><![CDATA[Year In Review 2025: The only bad choice is the absence of choice]]></title><description><![CDATA[Reflections on 2025: Embracing decision-making, career growth, learning, and personal insights for the year ahead]]></description><link>https://tawaliou.com/year-in-review-2025-the-only-bad-choice-is-the-absence-of-choice</link><guid isPermaLink="true">https://tawaliou.com/year-in-review-2025-the-only-bad-choice-is-the-absence-of-choice</guid><category><![CDATA[devretro2025]]></category><category><![CDATA[yearinreview]]></category><category><![CDATA[2025]]></category><category><![CDATA[recap]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Mon, 22 Dec 2025 08:07:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1766391884258/8325439b-c9f6-43c5-9186-e95b94368c53.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Here's a brief review of how my 2025 went.<br />Once again, we're at the end of the year, and it's nice to read people's <strong>Year In Review</strong>.<strong>.</strong></p>
<p>I did it last year: <a target="_blank" href="https://tawaldevuniverse.hashnode.dev/dev-retro-2024">Dev Retro 2024</a>, so I’ll do it again.</p>
<p><strong><mark>Let's thank God for keeping us healthy and alive this year.</mark></strong></p>
<h2 id="heading-year-of-decisions">Year of Decisions</h2>
<p>This year was mainly about making decisions. What should I study? What should I do next? What am I interested in? What can I do, and what do I want?<br />If you've read my <a target="_blank" href="https://tawaldevuniverse.hashnode.dev/dev-retro-2024">Year In Review 2024</a> (called Dev Retro 2024), you'll know that I set some goals based on what I knew at that time. Well, I changed some of them this year.</p>
<p>So, I'll talk about my daily job, what I did in my free time, and the decisions I made.</p>
<h3 id="heading-at-work">At work</h3>
<p>Compared to 2024, not much has changed. Just take a look at the section <a target="_blank" href="https://tawaldevuniverse.hashnode.dev/dev-retro-2024#heading-at-work"><strong>At work</strong></a> from my <a target="_blank" href="https://tawaldevuniverse.hashnode.dev/dev-retro-2024">Year in Review of 2024</a>.</p>
<p>I'm still a backend developer working on some services at <a target="_blank" href="https://uptimise.io/">Uptimise</a>, a startup I joined in 2022. We officially launched the product this year, and it's been great working with everyone. This year, I didn't do any major refactoring or big projects. We focused on making the two versions, Uptimise Benin and Uptimise Ivory Coast, more stable. There's still work to do, but we're making progress.</p>
<p>Now, let's talk about my personal projects and what I did in my free time..</p>
<h3 id="heading-networking-cisco-networking-basics">Networking: Cisco Networking Basics</h3>
<p>I'm not good in networking, and everyone knows it. So this year, I decided to change that and learn the basics of networking.<br />From June to July, I took the Cisco Networking Basics course. I worked on it during nights and weekends, and it went well. I really enjoyed it. I learned a lot and now feel less clueless when talking about networking at work, configuring my router, or handling network tasks on my computer..</p>
<p>I wrote a series about it here: <a target="_blank" href="https://tawaldevuniverse.hashnode.dev/series/networking-linux-system-programming">Networking Basics</a></p>
<h3 id="heading-operating-system-engineering">Operating System Engineering</h3>
<p>It's no secret that I'm interested in system engineering and have a background in embedded systems. Currently, I'm a web (backend) developer, but I really want to switch to this part of the industry. Will I do it? I don't know. The only thing I know is that I want it, and I'll try to make it happen. Failure is part of the game, just like success, right?</p>
<p>After taking the Computer Architecture and Organization course (From Nand to Tetris) in 2024, I decided to move to the next step: Operating System Design and Implementation. The goal is to understand OS concepts and contribute to the Linux Kernel.</p>
<p>So, in November, I started the MIT Course on OS Fall 2025 (<a target="_blank" href="https://pdos.csail.mit.edu/6.1810/2025/schedule.html">https://pdos.csail.mit.edu/6.1810/2025/schedule.html</a>). It's 15 weeks of lectures with labs, books, and papers to complete. I'm currently in week 3. Yes, the lessons and labs are not easy to complete, at least for me, but I'm doing my best 😅.</p>
<h3 id="heading-blogging">Blogging</h3>
<p>I restarted blogging in the middle of this year. My goal was to write regularly, aiming for 1 or 2 blog posts per month about my work. It didn't go exactly as planned, but it wasn't bad at all. This year, I wrote 9 articles on Networking and OS. These articles cover what I learned in those courses.</p>
<p>The good thing is that I wrote at my own pace: 1 or 2 articles per month, or more if I could. I wrote a little bit whenever I had time, like writing 10 lines at night or spending an hour on an article on Saturday. The idea is to make it a part of my daily routine..</p>
<h3 id="heading-social-media-post-linkedin-and-twitter">Social Media post: LinkedIn and Twitter</h3>
<p>Some of my friends, <a target="_blank" href="https://bj.linkedin.com/in/trinit%C3%A9-zinsou">Trinité</a> (my Lead), <a target="_blank" href="http://linkedin.com/in/frejus-zotongninou/">Fréjus</a> (our Product Designer), <a target="_blank" href="https://bj.linkedin.com/in/devalade">Alade</a>, and <a target="_blank" href="https://bj.linkedin.com/in/amidou-ta%C3%AFrou-ciss%C3%A9">Cissé</a>, advised me to start posting about my progress on LinkedIn. So, that's what I did. It helps me connect with people who share my interests. Mostly, I share summaries of the articles I wrote on these two networks.<br />My LinkedIn: <a target="_blank" href="http://www.linkedin.com/in/tawaliou-alao-26688614b">Tawaliou ALAO</a><br />My Twitter: <a target="_blank" href="http://www.linkedin.com/in/tawaliou-alao-26688614b">@TawalMc</a></p>
<h3 id="heading-rejection-for-master-application">Rejection for Master Application</h3>
<p>In the section titled <a target="_blank" href="https://tawaldevuniverse.hashnode.dev/dev-retro-2024#heading-rejection-for-master-application">Rejection for Master Application</a> in my Year In Review 2024, I explained that I was rejected for a Master's in Embedded Systems through Campus France and for Electrical and Computer Engineering (ECE) at CMU Rwanda. I decided to reapply for ECE this year. So, yes, I reapplied, and guess what? I got rejected again. Why? My application wasn't outstanding, so I guess that's the truth. Maybe I didn't do something right, or my background doesn't align well with the courses, or my path isn't impressive. Anyway, I don't care anymore 😂. I applied to a few other universities, like King Abdullah University of Science and Technology, and got rejected too 😂..</p>
<p>And some problems arise: Should I apply for Campus France (Licence 3) and do everything to leave the country 😂? Or should I stay and pursue a master's degree here? Should I even pursue a master's degree at all?<br />You know, when you wake up with these kinds of thoughts and questions in your mind, it's hard to stay focused. The combination of the <strong>unknown</strong>, <strong>fear of bad choices</strong>, and <strong>failures</strong> increases indecision.<br />So I did something I often do in situations like this: I asked for advice from elders. It's the same thing: don't fear failure, don't fear making bad choices, fear not making a choice at all..</p>
<p>So I made two decisions:</p>
<ul>
<li><p>Stay. No more applications, at least for a master's abroad.</p>
</li>
<li><p>Go all-in on system programming. Put all my effort into this field. That's why, in the sections above, I mentioned that I started the OS Engineering course by MIT..</p>
</li>
</ul>
<p><strong>Is this a good choice?</strong> <strong>Will it pay off?</strong> <strong>I don’t know.</strong><br />Right now, I mostly care about the process rather than the results.<br />There are also many reasons behind my decision to focus on System Programming. In a future article, I’ll discuss some of these reasons that motivate me to choose this path..</p>
<p>To give you a hint, here are two of them::</p>
<ul>
<li><p>I currently need time, I need to have full control over my free time and the freedom to study system programming.</p>
</li>
<li><p>Try hard and take risks to achieve it. This article by Aaron Francis: <a target="_blank" href="https://aaronfrancis.com/2024/what-if-you-tried-hard-dac139a5">What if you tried hard</a> explained it</p>
</li>
</ul>
<blockquote>
<p>I wonder what would happen if you gave it all you had and even risked embarrassing yourself if it didn't work out.</p>
<p>Aaron Francis (<a target="_blank" href="https://aaronfrancis.com/2024/what-if-you-tried-hard-dac139a5">https://aaronfrancis.com/2024/what-if-you-tried-hard-dac139a5</a>)</p>
</blockquote>
<h2 id="heading-what-for-the-next-2026-2027">What for the next (2026, 2027, …) ?</h2>
<p>To be honest, as usual, I don't have <strong>clear, well-defined plans</strong> that I begin at the start of the year. No, nothing like that. I just have some important goals. I usually start working in this direction from October (3 months before the new year). This helps me adjust based on how things unfold.</p>
<p>So for the next few years:</p>
<ul>
<li><p><strong>Be consistent in blogging</strong>, aiming for at least 1 or 2 articles per month</p>
</li>
<li><p><strong>Get involved in open source projects (System Programming)</strong></p>
</li>
<li><p><strong>Linux Kernel</strong></p>
</li>
<li><p><strong>Read at least 6 papers on Operating System available on:</strong></p>
<ul>
<li><strong>Operating System Design and Implementation (OSDI) by USENIX</strong></li>
</ul>
</li>
</ul>
<h2 id="heading-last-words">Last Words</h2>
<p>A few things to keep in mind:</p>
<ul>
<li><p>Things don’t always go as planned, so it's good to be flexible.</p>
</li>
<li><p>Try hard at whatever interests you. At least give it a shot.</p>
</li>
<li><p>Be patient, seriously.</p>
</li>
<li><p>Avoid comparisons, as they only create frustration and prevent you from focusing on yourself.</p>
</li>
<li><p>Make choices, even if they might seem silly. Make decisions based on what you know, want, care about, and fear. Don’t hold back when you need to decide.</p>
</li>
<li><p>Sometimes, or often, life isn't easy, and that's just how it is.</p>
</li>
<li><p>And have hope.</p>
</li>
</ul>
<p>There are many people I want to think about this year, from my parents, my closest friends, and my girlfriend to my colleagues and elders. But there will be a dedicated blog post for that in the future (I hope).</p>
<p>Share your Year in Review with me. It inspires me..</p>
]]></content:encoded></item><item><title><![CDATA[OS Design and Implementation: (Week 2) - Syscall]]></title><description><![CDATA[MIT 6.1810 Fall2025 Course - Week 2: system calls]]></description><link>https://tawaliou.com/os-design-and-implementation-week-2-syscall</link><guid isPermaLink="true">https://tawaliou.com/os-design-and-implementation-week-2-syscall</guid><category><![CDATA[System Programming]]></category><category><![CDATA[Kernel]]></category><category><![CDATA[Linux]]></category><category><![CDATA[operating system]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Sun, 21 Dec 2025 11:13:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1766315546401/8e61eecc-0375-46b5-8287-ab91a5fba7ae.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>It took me almost three weeks (nights and week-end) to read <a target="_blank" href="https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf">Chapter 2 of the xv6 book: <em>Operating System Organization</em></a>, study the xv6 code (process and system calls), and finish the assignments on system calls.</p>
<p>This chapter covers the organization of the operating system:</p>
<ul>
<li><p>There is a separation between the kernel and user space: user space code does not directly call kernel space code.</p>
</li>
<li><p>Processes are isolated, with each process running as if it has its own dedicated CPU and virtual memory, which is its own memory space (it cannot access memory outside this space without requesting more).</p>
</li>
<li><p>The underlying architecture (CPU) provides a system call mechanism (RISC-V uses the ecall instruction) to switch from user code to kernel code.</p>
</li>
<li><p>The kernel performs checks when a system call is triggered to ensure the operation is allowed and the correct arguments are passed.</p>
</li>
<li><p>Every system call, like read for example, is assigned a number (the read system call number in xv6 is 5) that the kernel uses to identify which system call the user code triggered.</p>
</li>
<li><p>And more... I'll explain "System Call" in more detail in my next blog post.</p>
</li>
</ul>
<p>And the labs? I spent most of my time trying to understand the assignments and figuring out where to start. Here are the labs:</p>
<ul>
<li><p>Implement a system call called “interpose” that limits the use of system calls based on a bitmask passed as an argument to interpose.</p>
</li>
<li><p>Exploit a vulnerability in xv6 that lets a process read data from another process's virtual memory. This lab helps illustrate why kernel code must be well-written.</p>
</li>
</ul>
<p>Currently, I'm getting hands-on experience with GDB for more effective debugging of the C and kernel code.</p>
<p>Next week, I'll start Week 3: Page Tables.</p>
]]></content:encoded></item><item><title><![CDATA[System Programming: OS Design and Implementation]]></title><description><![CDATA[So… here’s an update on my learning path. 2023 – 2024: Building a Modern Computer from First Principles (aka Nand2Tetris). I studied the fundamentals of…]]></description><link>https://tawaliou.com/system-programming-os-design-and-implementation</link><guid isPermaLink="true">https://tawaliou.com/system-programming-os-design-and-implementation</guid><category><![CDATA[operating system]]></category><category><![CDATA[Linux]]></category><category><![CDATA[System Programming]]></category><category><![CDATA[networking]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Mon, 03 Nov 2025 09:47:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1762162746900/5ad3ec0f-3781-4c19-a683-0ec520ea7d7a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-start-the-mit-61810-fall2025-course">Start the MIT 6.1810 Fall2025 Course</h2>
<p>So… here’s an update on my learning path.</p>
<p><strong>2023 – 2024:</strong> <em>Building a Modern Computer from First Principles</em> (aka <strong>Nand2Tetris</strong>). I studied the fundamentals of computer architecture; from logic gates and the CPU to the operating system and compiler.</p>
<p><strong>Mid 2025:</strong> <em>Introduction to Networking Basics.</em></p>
<p><strong>Late 2025 – :</strong></p>
<p>I want to learn more about OS internals, especially the Linux kernel and similar topics. So, some days ago, I started the <strong>MIT 6.1810</strong> course on <em>Design and Implementation of Operating Systems</em>.</p>
<p>The course is based on <strong>xv6</strong>, a simple Unix-like teaching operating system. The goal is to study OS concepts by extending xv6.</p>
<p><strong>What’s good about this course:</strong></p>
<ul>
<li><p>It’s well structured and has a clear schedule. (This year’s session started in September 2025 and is expected to end in December 2025.)</p>
</li>
<li><p>It combines theory with practical labs, so you don’t just read; you build as well.</p>
</li>
</ul>
<p>I’ll spend some nights and weekends working on it, enjoy the process, and share updates about my progress here.</p>
<p>I’m currently working on the Week 1 assignment (<em>Lab: Utilities</em>), where we’re implementing Unix-like utilities using the xv6 API.</p>
<p>By the way, MIT offers really well-structured courses on various topics in Computer Science.</p>
<p><strong>MIT 61.810</strong> Fall 2025 (Operating System Engineering) link:[<a target="_blank" href="https://pdos.csail.mit.edu/6.1810/2025/overview.html">https://pdos.csail.mit.edu/6.1810/2025/overview.html</a>]</p>
<h2 id="heading-update-07-november-2025">Update [07 November 2025]</h2>
<h3 id="heading-lectures">Lectures</h3>
<p>I just finished week 1 of the course. This week, I read <a target="_blank" href="https://pdos.csail.mit.edu/6.1810/2025/xv6/book-riscv-rev5.pdf">Chapter 1: Operating System Interface</a> of the book on <strong>xv6.</strong> This chapter introduces:</p>
<ul>
<li><p>The <strong>xv6</strong> system calls, such as <strong>open</strong>, which "opens" a file by returning a file descriptor, and <strong>fork</strong>, which creates a process.</p>
</li>
<li><p>How processes behave, including how to create a child process and execute a command.</p>
</li>
<li><p>Importantly, what a <strong>file descriptor</strong> is (an entry in a table for each process that lists files opened by the process), which allows a process to read or write.</p>
</li>
<li><p>A method for processes to communicate (Inter-Process Communication): <strong>Pipes</strong>, which use file descriptors.</p>
</li>
</ul>
<p>I also read the chapters on Pointers (Arrays, Characters, Structs) from <strong>The C Programming Language (Second Edition) by <em>Kernighan and Ritchie (K&amp;R)</em></strong> to refresh key concepts about manipulating pointers.</p>
<p>Then there are the assignments or labs.</p>
<h3 id="heading-lab-assignments-unix-utilities">Lab Assignments: Unix utilities</h3>
<p>The labs focused on writing Unix-like utilities. Here are a few, along with brief descriptions::</p>
<ul>
<li><p><strong>sixfive (not so easy):</strong> For each input file, <strong>sixfive</strong> must print all the numbers in the file that are multiples of 5 or 6. Number are a sequence of decimal digits separated by characters in the string " -\r\t\n./,"<em>.</em></p>
</li>
<li><p><strong>memdump (not so easy):</strong> Implement the function memdump(char <em>fmt, char</em> data). <strong>memdump()</strong>'s purpose is to print the contents of the memory pointed to by data in the format described by the fmt argument.</p>
</li>
<li><p><strong>find (not easy):</strong> Write a simple version of the UNIX find program for xv6: find all the files in a directory tree with a specific name.</p>
</li>
<li><p><strong>pingpong (easy and from Fall 2020):</strong> Write a program that uses UNIX system calls to <strong>ping-pong</strong> a byte between two processes over a pair of pipes, one for each direction</p>
</li>
</ul>
<p>These labs were not too difficult and had clear instructions, but I often felt stuck and unsure of what to do. However, after reading <strong>chapter 1</strong> of the <strong>xv6</strong> book multiple times and reviewing the source code of some existing utilities, I completed them. Here are a few things to remember (and I'm not ready to forget them)::</p>
<ol>
<li>When creating a <strong>child</strong> with the <strong>fork</strong> system call, don't forget to use <strong>wait</strong> in the <strong>parent</strong>. This ensures that the parent waits for the child to finish executing. This way, both the <strong>parent</strong> and <strong>child</strong> code will run properly.</li>
</ol>
<ul>
<li><pre><code class="lang-c">  <span class="hljs-keyword">if</span> (fork() == <span class="hljs-number">0</span>) <span class="hljs-comment">// Child</span>
  {
      write(<span class="hljs-number">1</span>, <span class="hljs-string">"hello "</span>, <span class="hljs-number">6</span>);
      <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
  }
  <span class="hljs-keyword">else</span> <span class="hljs-comment">// Parent</span>
  {
      wait(<span class="hljs-number">0</span>); <span class="hljs-comment">// Don't forget to wait here so child can execute</span>
      write(<span class="hljs-number">1</span>, <span class="hljs-string">"world\n"</span>, <span class="hljs-number">6</span>);
  }
</code></pre>
<p>  Forgetting this, make me stuck on the pingpong <strong>task.</strong></p>
</li>
</ul>
<ol start="2">
<li><p>When we define an array of <strong>int</strong> and another of <strong>char</strong>, like <code>int tab_1[5]</code> and <code>char tab_2[5]</code>, we know that <strong>tab_1</strong> and <strong>tab_2</strong> are pointers to the first element ([0]) of the array. Also, <code>tab_1[1]</code> is equivalent to <code>*(tab_1 + 1)</code>, and the same applies to <code>tab_2[1]</code>, which is equivalent to <code>*(tab_2 + 1)</code>..</p>
<ul>
<li><p>But if we assume that the memory address of <strong>tab_1</strong> is <strong>4000</strong> and the address of <strong>tab_2</strong> is <strong>5000</strong>, what will be the address of <code>tab_1[1]</code> and <code>tab_2[2]</code>? The address of <code>tab_1[1]</code> will be <strong>4000 + 4 bytes = 4004</strong>, not <strong>4001</strong>, since <code>sizeof(int) = 4 bytes</code> on most modern 32-bit/64-bit computers..</p>
<p>  Why? Because <strong>tab_1</strong> is a series of contiguous memory addresses that store integers, each with a size of 4 bytes (<code>sizeof(int)</code>). To move from one address or index to the next in the array, you need to step by 4 bytes. So, you move 4 bytes at a time. <code>tab_1[0]</code> occupies the memory blocks from <strong>4000 to 4003</strong>, and <code>tab_1[1]</code> starts at <strong>4004</strong>.</p>
<p>  The same rule applies to <strong>tab_2</strong>, but here <code>sizeof(char)</code> <strong>\= 1</strong>, so the address of <code>tab_2[1]</code> will be <strong>5000 + 1 byte = 5001</strong>.</p>
<p>  The rule is simple: <strong>New Address = Start Address + (N * sizeof(Type))</strong></p>
</li>
<li><p>But here's another question: when we use <code>*(tab_1 + 1)</code> or <code>tab_1[1]</code>, how does the compiler know to add <strong>N * sizeof(Type)</strong> to the initial address? Well, this is done at compile time when the compiler generates the assembly code. It uses a table called the Symbol Table, which holds information about variables, functions, and other elements in our code. This allows it to check the data type and perform the appropriate calculations..</p>
</li>
</ul>
</li>
</ol>
<p>From <strong>Gemini</strong></p>
<blockquote>
<p>When the compiler reads your source code, it builds an internal data structure called the <strong>Symbol Table</strong>. This table tracks every identifier (variable, function, type, etc.) in your program and records all its key attributes, including its data type.</p>
<p><strong>For a pointer variable declared as</strong> <code>int *p</code><strong>; the Symbol Table records that</strong> <code>p</code> <strong>is a pointer to an</strong> <code>int</code><strong>.</strong></p>
</blockquote>
<p><strong>Done for now, see you next time!</strong></p>
<p>It's good to write the utility and get back into C to remember these concepts before diving into OS.</p>
<p>I'm starting Week 2 of the course.</p>
<p>By the way, here's my GitHub repo for the lab course: <a target="_blank" href="https://github.com/TawalMc/MIT-6.1810Fall2025-xv6">https://github.com/TawalMc/MIT-6.1810Fall2025-xv6</a></p>
]]></content:encoded></item><item><title><![CDATA[Networking Programming: getaddrinfo]]></title><description><![CDATA[I was reading about network programming and sockets with Beej’s Guide to Networking Programming, and we ended up writing a simplified version of the…]]></description><link>https://tawaliou.com/networking-programming-getaddrinfo</link><guid isPermaLink="true">https://tawaliou.com/networking-programming-getaddrinfo</guid><category><![CDATA[networking]]></category><category><![CDATA[Linux]]></category><category><![CDATA[System Programming]]></category><category><![CDATA[socket]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Wed, 15 Oct 2025 09:44:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1760520923083/971b37b2-dd91-4f21-8b8f-eaaf651341f3.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I was reading about network programming and sockets with <a target="_blank" href="https://beej.us/guide/bgnet/html/split/">Beej’s Guide to Networking Programming</a>, and we ended up writing a simplified version of the <code>nslookup</code> program in C. This program takes a domain name and finds the IPs associated with it by querying the DNS. <em>nslookup does more than that</em>, but for us, it's impressive to create something similar. After that, I tried to build the same thing in Python and laughed at the result: just 3 lines of code.</p>
<p>So, I'll show you the C code first, followed by the Python code.</p>
<p><code>showip.c</code></p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> _POSIX_C_SOURCE 200112L</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/types.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/socket.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;netdb.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;arpa/inet.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;netinet/in.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/socket.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> <span class="hljs-keyword">const</span> *argv[])</span>
</span>{
    <span class="hljs-keyword">int</span> status;
    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">addrinfo</span> <span class="hljs-title">hints</span>, *<span class="hljs-title">res</span>, *<span class="hljs-title">p</span>;</span>
    <span class="hljs-keyword">char</span> ipstr[INET6_ADDRSTRLEN];

    <span class="hljs-keyword">if</span> (argc != <span class="hljs-number">2</span>)
    {
        <span class="hljs-built_in">fprintf</span>(<span class="hljs-built_in">stderr</span>, <span class="hljs-string">"usage: showip hostname\n"</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    }

    <span class="hljs-built_in">memset</span>(&amp;hints, <span class="hljs-number">0</span>, <span class="hljs-keyword">sizeof</span> hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    <span class="hljs-keyword">if</span> ((status = getaddrinfo(argv[<span class="hljs-number">1</span>], <span class="hljs-string">"80"</span>, &amp;hints, &amp;res)) != <span class="hljs-number">0</span>)
    {
        <span class="hljs-built_in">fprintf</span>(<span class="hljs-built_in">stderr</span>, <span class="hljs-string">"gai error: %s\n"</span>, gai_strerror(status));
        <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;
    }

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"IP addressess for %s: \n\n"</span>, argv[<span class="hljs-number">1</span>]);

    <span class="hljs-keyword">for</span> (p = res; p != <span class="hljs-literal">NULL</span>; p = p-&gt;ai_next)
    {
        <span class="hljs-keyword">void</span> *addr;
        <span class="hljs-keyword">char</span> *ipver;
        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in</span> *<span class="hljs-title">ipv4</span>;</span>
        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in6</span> *<span class="hljs-title">ipv6</span>;</span>

        <span class="hljs-keyword">if</span> (p-&gt;ai_family == AF_INET)
        {
            ipv4 = (struct sockaddr_in *)p-&gt;ai_addr;
            addr = &amp;(ipv4-&gt;sin_addr);
            ipver = <span class="hljs-string">"IPv4"</span>;
        }
        <span class="hljs-keyword">else</span>
        {
            ipv6 = (struct sockaddr_in6 *)p-&gt;ai_addr;
            addr = &amp;(ipv6-&gt;sin6_addr);
            ipver = <span class="hljs-string">"IPv6"</span>;
        }

        inet_ntop(p-&gt;ai_family, addr, ipstr, <span class="hljs-keyword">sizeof</span> ipstr);
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">" %s: %s\n"</span>, ipver, ipstr);
    }

    freeaddrinfo(res);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>And now the <code>showip.py</code></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> socket
<span class="hljs-keyword">import</span> sys
<span class="hljs-keyword">from</span> socket <span class="hljs-keyword">import</span> AddressFamily

FAMILY_TYPE = {<span class="hljs-string">"AF_INET"</span>: <span class="hljs-string">"IPv4"</span>, <span class="hljs-string">"AF_INET6"</span>: <span class="hljs-string">"IPv6"</span>}
PORT = <span class="hljs-string">'80'</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span>
    host = sys.argv[<span class="hljs-number">1</span>]
    <span class="hljs-keyword">try</span>:
        host_info = socket.getaddrinfo(host,  <span class="hljs-comment"># hostname</span>
                                       PORT,  <span class="hljs-comment"># hostname port</span>
                                       socket.AF_UNSPEC,  <span class="hljs-comment"># IPv4/IPv6</span>
                                       socket.SOCK_STREAM <span class="hljs-comment"># TCP,</span>
                                       flags=socket.AI_CANONNAME
                                       )
        <span class="hljs-keyword">for</span> info <span class="hljs-keyword">in</span> host_info:
            family = AddressFamily(info[<span class="hljs-number">0</span>]).name
            text = <span class="hljs-string">f"<span class="hljs-subst">{FAMILY_TYPE[family]}</span>: <span class="hljs-subst">{info[<span class="hljs-number">4</span>][<span class="hljs-number">0</span>]}</span> --&gt; <span class="hljs-subst">{info[<span class="hljs-number">3</span>]}</span>"</span>
            print(text)
    <span class="hljs-keyword">except</span> socket.gaierror:
        print(<span class="hljs-string">f"Hostname <span class="hljs-subst">{host}</span> could not be resolved."</span>, file=sys.stderr)
        <span class="hljs-keyword">return</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    <span class="hljs-keyword">if</span> len(sys.argv) != <span class="hljs-number">2</span>:
        <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">"usage: showip hostname"</span>)
    main()
</code></pre>
<p>When we run the Python code, we get this:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># [(&lt;AddressFamily.AF_INET: 2&gt;, &lt;SocketKind.SOCK_STREAM: 1&gt;, 6, 'hashnode.com', ('66.33.60.194', 8081)), (&lt;AddressFamily.AF_INET: 2&gt;, &lt;SocketKind.SOCK_STREAM: 1&gt;, 6, '', ('76.76.21.22', 8081))]</span>
$ IPv4: 66.33.60.194 --&gt;  hashnode.com
  IPv4: 76.76.21.22 --&gt;
</code></pre>
<p>We can see that even though the Python code is shorter, it uses a similar API to the C code. This is because Python simply wraps the <code>socket</code> API provided in C, which helps reduce some of the developer's workload. The key part is using <code>getaddrinfo</code> (get address info), which is defined like this::</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getaddrinfo</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *node,
                <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *service,
                <span class="hljs-keyword">const</span> struct addrinfo *hints,
                struct addrinfo **res)</span></span>;
</code></pre>
<pre><code class="lang-python">socket.getaddrinfo(host, port, family=<span class="hljs-number">0</span>, type=<span class="hljs-number">0</span>, proto=<span class="hljs-number">0</span>, flags=<span class="hljs-number">0</span>)

<span class="hljs-comment"># and returned value</span>
<span class="hljs-comment"># list if of tuples (family, type, proto, canonname, sockaddr)</span>
</code></pre>
<p>You can already see the similarity.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>C</strong></td><td><strong>Python</strong></td><td><strong>Description</strong></td><td><strong>Example</strong></td></tr>
</thead>
<tbody>
<tr>
<td><code>node</code></td><td><code>host</code></td><td><mark>Hostname</mark> or IP address</td><td><code>hashnode.com</code></td></tr>
<tr>
<td><code>service</code></td><td><code>port</code></td><td>The <mark>service type</mark> (“http”,…) or <mark>Port number</mark></td><td><code>80</code></td></tr>
<tr>
<td><code>hints</code> (structure)</td><td><code>family</code>, <code>type</code>, <code>proto</code></td><td><mark>Address family</mark> (IPv4/v6, …); <mark>Socket type</mark> (stream, datagram, …); <mark>Protocol</mark> (TCP, UDP, …)</td><td>family: <code>AF_INET</code> (IPv4), <code>AF_INET6</code> (IPv6), <code>AF_UNSPEC</code>(unspecified) type: <code>SOCK_STREAM</code> proto: <code>0</code> (any), if set to 0 and type to SOCK_STREAM the system will pick TCP</td></tr>
<tr>
<td><code>res</code> (Linked List)</td><td>list of (<code>family</code>, <code>type</code>, <code>proto</code>, <code>canonname</code>, <code>sockaddr</code>)</td><td>family, type, proto are the same as decrbed above; <mark>Canonical name</mark>: the true host name by the DNS (will read about it) sockaddr is a tuple of IP and Port</td></tr>
</tbody>
</table>
</div><p>In the C code, the result is stored in a linked list, and the head's address is passed to <code>getaddrinfo</code>. This is why we iterate over <code>res</code> and <code>res-&gt;ai_next</code>. In Python, the library takes care of this iteration, so we just go through the list of tuples until the end.</p>
<p>Why a list of IPs? Because a hostname can have multiple IPs (both IPv4 and IPv6) associated with it.</p>
<p>Good, right? Yeah, but I wondered how Python's <code>getaddrinfo</code> is implemented behind the scenes, so I asked ChatGPT, and it gave me this: <a target="_blank" href="https://chatgpt.com/s/t_68eba17bdb4481919241ce01a7890c5b">https://chatgpt.com/s/t_68eba17bdb4481919241ce01a7890c5b</a></p>
<p>You can read it to get some ideas (I didn't understand everything).</p>
<p><strong>Done for now, see you next time!</strong></p>
<p>It's great to see the work Python has already done for developers and how things operate behind the scenes with C. I'm currently learning about network programming, so there might be a future article on sockets or similar topics.</p>
]]></content:encoded></item><item><title><![CDATA[Learning Networkings Basics - Extension of a LAN]]></title><description><![CDATA[How I Expanded My Basic LAN Setup]]></description><link>https://tawaliou.com/learning-networkings-basics-extension-of-a-lan</link><guid isPermaLink="true">https://tawaliou.com/learning-networkings-basics-extension-of-a-lan</guid><category><![CDATA[networking]]></category><category><![CDATA[System Programming]]></category><category><![CDATA[Linux]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Tue, 16 Sep 2025 22:37:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1758059657502/c1c54acc-7502-4850-8bb4-07b38eb5480f.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Three months ago, I began exploring networking with the Cisco Networking Basics course, and it was fun. Then, just a month later, I encountered a simple networking task, and I was glad I took the course. Not because the task was complex, but because I understood each step and knew why I was doing what I did. Let me provide some background.</p>
<p>I used to subscribe to a monthly unlimited internet package from a popular Mobile Network Operator in my country, which is also an ISP (Internet Service Provider). I won't mention the company's name, so I'll call it OPERATOR. In my opinion, it was expensive and slow. It cost 15,000 XOF, about $27 per month, and once you used 25GB, your internet speed dropped from 2MB to 1MB. After using the first 25GB, the rest of the month was a struggle: the speed was slow, and you could barely browse websites other than YouTube. Under these conditions, taking online courses was tough. So, after some recommendations from friends, I decided to switch to another Mobile Network Operator that offers fiber optic for faster internet.</p>
<blockquote>
<p><strong>TL;DR:</strong> I'm really satisfied right now. I hope it stays this way.</p>
</blockquote>
<p>Now, here's the problem: the location where the fiber optic and main router were installed doesn't let my phone connect to the Wi-Fi when I'm in my room. The solution is to extend the network. The good news is that we have a second router at home. So, I need to connect the second router to the first one and place it near my room.</p>
<p>I did it, and it was easy. I just followed a few steps and asked chat.gpt if I had any doubts. I used the second router as a switch, so now I have one local network.</p>
<p>Let's name the router that comes with the optic fiber (the one provided by the ISP) <code>MAIN_ROUTER</code>, and the second one <code>SECOND_ROUTER</code>. Here are the steps and reasons:</p>
<ul>
<li><p>Get the <code>MAIN_ROUTER</code>'s configuration info:</p>
<ul>
<li><p>Access the admin page: The admin ID, password, and IP are on the back of the router.</p>
</li>
<li><p>Write down information like:</p>
<ul>
<li><p>Gateway (router address): This will be used by a host as the Destination IP Address for transmission outside of the LAN.</p>
</li>
<li><p>DNS server: It's important to save the correct address; otherwise, even if you have an internet connection, you can't reach websites.</p>
</li>
<li><p>IP range, subnet mask.</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>Connect the <code>SECOND_ROUTER</code> to the first one through the LAN port (usually orange or yellow).</p>
</li>
<li><p>Configure the <code>SECOND_ROUTER</code> by:</p>
<ul>
<li><p>Change its mode to <mark>Bridge</mark> or <mark>Access Point</mark>, or select an option other than acting as a Router if available.</p>
</li>
<li><p>Entering the same configuration info listed above on its admin page.</p>
</li>
<li><p>Disabling its DHCP Server to ensure that the <code>MAIN_ROUTER</code> is the only one assigning IP addresses to hosts.</p>
</li>
<li><p>Giving it a static address outside the IP range of the <code>MAIN_ROUTER</code> but in the same subnet. For example, if the IP range of <code>MAIN_ROUTER</code> is <code>192.168.1.100</code> - <code>192.168.1.254</code> (and the subnet is <code>192.168.1.1/24</code>), you can set the <code>SECOND_ROUTER</code> IP address to <code>192.168.1.2</code>.<code>68.1.2</code>.</p>
</li>
</ul>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1758060181747/d38364cd-7b41-4407-bb03-e1acbe73abc1.jpeg" alt="LAN ports on Router" class="image--center mx-auto" /></p>
<p>It's not really that complicated, but I like it.</p>
]]></content:encoded></item><item><title><![CDATA[Learning Networkings Basics - Next]]></title><description><![CDATA[After almost two months, I finished the Networking Basics course provided by the CISCO Networking Academy. I started the course not only because I lacked…]]></description><link>https://tawaliou.com/learning-networkings-basics-next</link><guid isPermaLink="true">https://tawaliou.com/learning-networkings-basics-next</guid><category><![CDATA[networking]]></category><category><![CDATA[Linux]]></category><category><![CDATA[systemprogramming]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Fri, 25 Jul 2025 11:05:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1753440398443/39c1695a-2ba1-4929-8ac3-bf1fb2713802.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>After almost two months, I finished the <a target="_blank" href="https://www.netacad.com/courses/networking-basics?courseLang=en-US">Networking Basics</a> course provided by the CISCO Networking Academy.</p>
<p>I started the course not only because I lacked knowledge in networking but also because I believe networking is an important part of the IT field. Now that I have completed the course, I am more comfortable with terms like subnet, broadcast domain, DNS resolver, and others when reading articles or documentation for tools I use daily.</p>
<p>Here are some things I appreciated about the course:</p>
<ul>
<li><p>Well-written chapters with clear use cases and illustrations</p>
</li>
<li><p>Short videos with good explanations and visuals, each lasting a maximum of 8 minutes <em>(I prefer text over video when studying)</em></p>
</li>
<li><p>Practice chapters using CISCO Packet Tracer</p>
</li>
<li><p>Short and long exams after each chapter, featuring multiple-choice questions to assess learners' knowledge</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752308218828/8e03c077-4f31-4411-84ba-4a17760e64dc.png" alt class="image--center mx-auto" /></p>
<p>I really appreciate the course's structure and highly recommend it.</p>
<p>What's next? I'm continuing with network programming because there are still things I need and want to learn in this area. I started with Beej's networking guide.</p>
]]></content:encoded></item><item><title><![CDATA[Learning Networkings Basics - 4]]></title><description><![CDATA[Routing inside and outside a LAN]]></description><link>https://tawaliou.com/learning-networkings-basics-4</link><guid isPermaLink="true">https://tawaliou.com/learning-networkings-basics-4</guid><category><![CDATA[Linux]]></category><category><![CDATA[networking]]></category><category><![CDATA[systemprogramming]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Thu, 17 Jul 2025 17:25:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1752773097079/59a39d0c-492a-40b2-93fa-c5125c2ddec7.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this new blog post, I'll explain what I learned about how data is transmitted within and outside a LAN on Layer 2 of the TCP/IP model. It's important to remember that on Layer 2, a host can send data to other hosts if it knows their IP addresses.</p>
<p>To illustrate this, let's use this network schema as an example.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752706780789/4a7db267-d418-46fc-9207-9c105d39e7c5.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-transmission-inside-a-lan">Transmission inside a LAN</h2>
<p>Let’s say <strong>Host 1</strong> wants to send data or packets to <strong>Host 2</strong>. Both hosts are in the same LAN. There are two possible scenarios: either Host 1 already knows Host 2's MAC address, or Host 1 only knows Host 2's IP address but not the MAC address.</p>
<h3 id="heading-mac-addr-is-known">MAC Addr is known</h3>
<p>In this case, Host 1's ARP table already contains the IP and MAC addresses of Host 2, making the process straightforward.</p>
<p>In fact:</p>
<ol>
<li><p>Host 1 will put its IP and MAC addresses in the source address sections, then use Host 2's IP and MAC addresses as the destination and send the packet to switch S1.</p>
</li>
<li><p>At switch S1, after it de-encapsulates the frame, it will check its MAC Address Table to see if there is a port associated with the destination MAC. If yes, it will forward the frame to this port. If not, it will broadcast the message to all ports except the sending port. In this last case, the frame will be processed only by the host with the corresponding MAC address; others will discard it.</p>
</li>
</ol>
<p>You can get more information about this case in this article I wrote on <a target="_blank" href="https://tawaldevuniverse.hashnode.dev/learning-networking-basics-2">Ethernet and Switch</a>.</p>
<h3 id="heading-mac-addr-is-not-known">MAC Addr is not known</h3>
<p>Host 1's ARP table doesn’t contain the IP and MAC addresses of Host 2. Therefore, before sending the packets, Host 1 will try to discover the MAC address of Host 2 with an ARP request. ARP stands for <strong>Address Resolution Protocol.</strong> So, what happens is an intermediate step where:</p>
<ol>
<li><p>Host 1 will send a Broadcast MAC addr frame with the IP address of Host 2.</p>
</li>
<li><p>Switch S1 will forward it to all ports (except the sending one), and each of the hosts connected to the switch will compare the IP addr in the frame to their own IP.</p>
</li>
<li><p>The corresponding host (Host 2) will be the only one to reply and will send back its MAC addr.</p>
</li>
<li><p>Host 1 will then store the MAC addr in its ARP Table and will set the destination MAC addr of the frame with this new value.</p>
</li>
<li><p>Then the packets will be sent following the steps in the case of <strong>MAC Addr is known (above)</strong>.</p>
</li>
</ol>
<p><strong>Note: Routers</strong> do not forward broadcast domain packets, so <strong>Host A and B</strong> will not receive these packets.</p>
<p>Here is the Ethernet Frame that will be sent (this is a simplified version)</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752561216182/fcc0c075-43ff-406d-8423-9a40bb506074.png" alt class="image--center mx-auto" /></p>
<p>Following text summarise what’s said in this paragraph.</p>
<blockquote>
<p>The method that a host uses to send messages to a destination on a remote network differs from the way a host sends messages on the same local network. When a host needs to send a message to another host located on the same network, it will forward the message directly. A host will use ARP to discover the MAC address of the destination host. The IPv4 packet contains the destination IPv4 address and encapsulates the packet into a frame containing the MAC address of the destination and forwards it out.</p>
</blockquote>
<p><em>from cisco</em></p>
<h2 id="heading-transmission-outside-a-lan">Transmission outside a LAN</h2>
<p>The transmission of a packet outside a LAN is done at Layer 2 of the <strong>TCP/IP</strong> model; the Internet Layer. So we need the IP of the destination host. At this layer, we necessarily need a <strong>Router</strong> to route the traffic (transmit the packet) outside the LAN. So what happens?</p>
<p>Let's suppose that <strong>Host 1</strong> wants to send a packet to <strong>Host B</strong>.</p>
<ul>
<li><p>The Destination MAC addr will be the Default Gateway MAC addr in the LAN: <strong>XXXX</strong>, then</p>
</li>
<li><p>The Router will:</p>
<ul>
<li><p>get the frame,</p>
</li>
<li><p>de-encapsulate it,</p>
</li>
<li><p>evaluate the best path by which to send the packet,</p>
</li>
<li><p>replace the DEST MAC Addr with the next Router MAC Addr,</p>
</li>
<li><p>replace the SRC MAC Addr with its MAC Addr in the new LAN: <strong>QQQQ</strong>,</p>
</li>
<li><p>send the packet.</p>
</li>
<li><p>The next router does the same thing, but if it’s the last router and the packet is already in the DEST LAN, it puts its MAC Addr in the LAN as SRC MAC addr.</p>
</li>
</ul>
</li>
</ul>
<p>As the last router and the destination host are in the same LAN, the steps in <strong>“Transmission inside a LAN”</strong> will be executed to either get and set the destination MAC addr of the packet or send the packet if the destination MAC addr is already known.</p>
<p>The following text summarizes what's said in this paragraph.</p>
<blockquote>
<p>When a host needs to send a message to a remote network, it must use the router. The host includes the IP address of the destination host within the packet just like before. However, when it encapsulates the packet into a frame, it uses the MAC address of the router as the destination for the frame. In this way, the router will receive and accept the frame based on the MAC address.</p>
</blockquote>
<p><em>from cisco</em></p>
<p>The picture below shows changes in the packet sent from a LAN to another.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752709245124/5455155a-8e44-4246-8b47-5365cc95e4ce.png" alt class="image--center mx-auto" /></p>
<p>Magic, right? I really like how networking is designed, and I wonder how the field developed like that.</p>
]]></content:encoded></item><item><title><![CDATA[Learning Networking Basics - 3]]></title><description><![CDATA[Continuing my study of networking basics, I learned about the Router, which is part of the Network Layer (Layer 3) of the OSI Model and the Internet Layer…]]></description><link>https://tawaliou.com/learning-networking-basics-3</link><guid isPermaLink="true">https://tawaliou.com/learning-networking-basics-3</guid><category><![CDATA[networking]]></category><category><![CDATA[systemprogramming]]></category><category><![CDATA[Linux]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Thu, 03 Jul 2025 00:11:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1752773066420/4516ff67-c8a4-4490-965b-f70bf54a1985.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Continuing my study of networking basics, I learned about the <strong>Router</strong>, which is part of the <strong>Network Layer (Layer 3)</strong> of the <strong>OSI Model</strong> and the <strong>Internet Layer (Layer 2)</strong> of <strong>TCP/IP.</strong> I'll keep it brief and share some interesting things I learned about DHCP, routing, ARP, and generally how packets move from a LAN to a remote LAN.</p>
<h2 id="heading-dhcp">DHCP</h2>
<p>DHCP stands for <strong>Dynamic Host Configuration Protocol</strong> and allows hosts to be dynamically assigned IP addresses (IPv4/IPv6). So, you’re not required to know the range of addresses available for hosts in the network, and even if the host restarts, it will automatically get an IP. To have DHCP configured on the host, you need a DHCP Server from which the host will get its IP <em>dynamically.</em> The following are the steps that occur when DHCP is configured (in four steps):</p>
<ol>
<li><p>Host (which is a <strong>DHCP Client</strong>) sends a <strong>broadcast</strong> (255.255.255.255) message <strong>«DHCP DISCOVER»</strong> to the <strong>DHCP server</strong> asking for an IP.</p>
</li>
<li><p>The DHCP server responds back with a <strong>unicast</strong> or <strong>broadcast</strong> message, a <strong>«DHCP OFFER»</strong> that contains an IP + Subnet mask (like 192.168.11.2 + 255.255.255.0) for the new host.</p>
</li>
<li><p>The host responds with a <strong>broadcast</strong> message, a <strong>«DHCP REQUEST»</strong> asking for confirmation to use the IP sent.</p>
</li>
<li><p>DHCP confirms with an <strong>ACKNOWLEDGMENT,</strong> a unicast/broadcast message <strong>«DHCP ACK»</strong> sent back that confirms to the host that it can use the IP sent above.</p>
</li>
</ol>
<p>Next, here's a diagram that shows the flow when a DHCP Client request an IP</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751180796587/25a23a21-3e71-49d7-ba2a-97bf5e0ca738.png" alt class="image--center mx-auto" /></p>
<p>Two additional interesting facts:</p>
<ul>
<li><p><em>Your</em> <strong><em>Router</em></strong> <em>(modem) can act like a DHCP Server and provide IPs dynamically to hosts that are connected to it.</em></p>
</li>
<li><p><em>Normally, you can manually configure your devices (phone, computer) to request IPs dynamically, but usually these devices are set by default to use DHCP to automatically request an IP.</em></p>
</li>
</ul>
<p>Now, if you want to implement the DHCP Protocol in C/Linux yourself, you can follow this post, <a target="_blank" href="https://www.linkedin.com/pulse/understanding-dhcp-socket-functions-david-zhu-sfgvc/?trackingId=c6sWj1mDQEmBFtRb3DmmKg%3D%3D"><strong>Understanding DHCP with Socket Functions</strong></a> by <a target="_blank" href="https://www.linkedin.com/in/david-zhu-3a68a855/">David Zhu</a> (LinkedIn), who publishes good and in-depth content on Networking.</p>
<p>So briefly, this is what happens when our phone/computer connects to Wi-Fi and automatically gets an IP address without us needing to do any configurations.</p>
<p>Now, let’s talk a little bit about routers.</p>
<h2 id="heading-router">Router</h2>
<p>Let’s list some properties of a Router:</p>
<ul>
<li><p>A router allows communication between different LANs or with a remote network. So a host in the LAN <strong>192.168.2.5/24</strong> can send a message to a host in the LAN <strong>192.168.3.4/24</strong>.</p>
</li>
<li><p>A router can act as a DHCP Server for hosts in a LAN and be a DHCP Client within an Internet Service Provider (<strong>ISP</strong>) to get an IP for internet communication.</p>
</li>
<li><p>The <strong>Default Gateway</strong> is the router’s IP in a LAN. So each LAN has its Default Gateway within the router.</p>
</li>
<li><p>Has an ARP (Address Resolution Protocol) Table: <strong>{MAC Addr, IPv4 Addr}</strong></p>
</li>
<li><p>Has a Routing table: <strong>{Port, Network IPv4 Addr, Type of connection}</strong> and best paths to reach a network</p>
</li>
</ul>
<p>Here is an example of a router with its address.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751399332286/52350fcb-b7ae-4c9d-9068-634122a2a386.jpeg" alt class="image--center mx-auto" /></p>
<p>That is it.</p>
<p>There is a lot to say about Router and DHCP, but I think now we can understand a little bit about them with this information. I am open to any suggestions regarding improvements to what is said in this article.</p>
<p>And in the next article, I’ll write about: <strong>How a host sends data to a host in the same LAN and in a remote LAN</strong> (yes, we’ll talk about Router again).</p>
]]></content:encoded></item><item><title><![CDATA[Learning Networking Basics - 2]]></title><description><![CDATA[Continuing my learning on networking basics, I learned about the Ethernet Switch, which is part of the physical layer of the OSI Model and the Network…]]></description><link>https://tawaliou.com/learning-networking-basics-2</link><guid isPermaLink="true">https://tawaliou.com/learning-networking-basics-2</guid><category><![CDATA[networking]]></category><category><![CDATA[systemprogramming]]></category><category><![CDATA[Linux]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Mon, 16 Jun 2025 05:17:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1752773033346/f9eac597-791f-470d-a341-8f95c08d64a8.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Continuing my learning on networking basics, I learned about the <strong>Ethernet Switch</strong>, which is part of the physical layer of the <strong>OSI Model</strong> and the Network Access Layer of <strong>TCP/IP.</strong> I also discovered some interesting details about how the Ethernet switch forwards packets.</p>
<h2 id="heading-ethernet-switch">Ethernet switch</h2>
<h3 id="heading-ethernet-frame">Ethernet Frame</h3>
<p>The Ethernet switch, or simply switch, lets devices connect within a LAN. Its job is to forward or transmit packets (also known as <strong>Ethernet Frames</strong>) from a source device to a destination device using the MAC address of the <strong>Network Interface Card (NIC)</strong> in each device. A packet is just a sequence of bits (0s and 1s).</p>
<p>In the image below, you can see an example of a packet.</p>
<p><img src="https://huseyinpala.site/wp-content/uploads/2023/04/ethernet-frame-format.png" alt="Picture 1- Ethernet Frame Yapısı – Huseyin Pala" class="image--center mx-auto" /></p>
<p>So, we might ask, in a LAN, <strong>how does the switch know the device's MAC address to determine where to forward a packet</strong>? Let's look at this picture of a simple LAN.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1749679778822/976f2017-7679-476f-b183-a4484ab6ac1c.jpeg" alt class="image--center mx-auto" /></p>
<p>My initial thought was that when a device connects to the LAN, it sends its NIC's MAC address to the switch, like a registration. This way, the switch would have the MAC addresses of all devices connected to the LAN, and when it receives a packet, it could use the destination MAC address in the packet to send it to the correct device.</p>
<p><strong>But that's not exactly how it works</strong>: In reality, the NIC in devices doesn't know if it's connected to a switch, so it doesn't need to send its MAC address. Also, the protocol must be designed to work independently of specific devices like the switch..</p>
<h3 id="heading-mac-address-table">MAC Address Table</h3>
<p>The switch has a MAC address table where it stores device MAC addresses along with the port each device is connected to.</p>
<p>When a packet is sent:</p>
<ol>
<li><p>The switch first checks if the <strong>Source Address</strong> is already in its MAC address table.</p>
</li>
<li><p>If it is, and the <strong>Destination Address</strong> is in the table, it forwards the packet to the port where the destination device is connected. If the <strong>Destination Address</strong> is not in the table, it broadcasts the packet to all ports except the source port.</p>
</li>
<li><p>If the Source Address is not in the table, the switch adds it with the corresponding port, then proceeds as in step 2, either broadcasting or directly forwarding the packet.</p>
</li>
<li><p>When a device connected to the switch receives the packet, it processes the packet if the Destination Address matches its MAC address; otherwise, it discards the packet.</p>
</li>
</ol>
<p>Here is a short illustration of this process: that.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1749970586734/156aedd1-c872-4097-95fa-60a4681d5042.png" alt="Ethern frame foward politic by switch" class="image--center mx-auto" /></p>
<p>As we can see, it's a "simple" method to send packets and save MAC addresses simultaneously, using fewer resources. The table updates each time a new packet is sent.</p>
<p>Let me share another thing I learned about IPv4..</p>
<h2 id="heading-ipv4-network-host">IPv4 (Network + Host)</h2>
<p>In <strong>IPv4</strong>, there are <strong>Network</strong> and <strong>Host</strong> parts. The network part, also known as the <strong>Subnet</strong>, indicates which network the device is part of, while the host part identifies the specific device. Refer to the following image.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1749972485360/890a606a-f9ee-4355-a7c9-ef55447304ea.png" alt="IPv4: Network + Host" class="image--center mx-auto" /></p>
<p>Here are a few things to understand:</p>
<ul>
<li><p>When we have an IPv4 address <strong>192.168.5.123/24</strong>, the <strong>24</strong> indicates that the first 24 bits (<strong>192.168.5.0</strong>) represent the network address.</p>
</li>
<li><p>The subnet mask, <strong>255.255.255.0</strong>, serves the same purpose as the <strong>24</strong>: it identifies the network. So, if we have these two pieces of information: <strong>192.168.5.123</strong> and <strong>255.255.255.0</strong>, it means that <strong>192.168.5.0</strong> is the network address.</p>
</li>
</ul>
<p>To conclude, there can be a broadcast address like <strong>192.168.5.255</strong>, which acts as a sub-group for devices with the same address. If a packet is sent to this address <strong>192.168.5.255</strong>, all devices with this broadcast address will receive the packet.</p>
<p>Now, on Linux, when we run <code>ifconfig -a</code> and see a display like the following, we understand what the second line means.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1749994746836/b73dda91-efd9-4908-8289-7e7d38f13b06.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>There are many things I read, use, and see, but I don’t really understand their meanings. It's good to have a little bit of knowledge about them, like the whole world of networking.</p>
]]></content:encoded></item><item><title><![CDATA[Learning Networking Basics - 1]]></title><description><![CDATA[LAN ? Why started a course ?]]></description><link>https://tawaliou.com/learning-networking-basics-1</link><guid isPermaLink="true">https://tawaliou.com/learning-networking-basics-1</guid><category><![CDATA[networking]]></category><category><![CDATA[Linux]]></category><category><![CDATA[ip address]]></category><category><![CDATA[router]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Tue, 03 Jun 2025 17:08:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1752772995859/7b9256fe-3a68-44be-a966-1d3cfa752042.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>And I decided to learn networking fundamentals</p>
<h2 id="heading-why">Why ?</h2>
<p>When I was working at <a target="_blank" href="https://youpilab.com/">YoupiLab</a> as embedded software developer, my CEO told me multiple times that I need to learn networking basics that its a great part in IT. Now that I’m working as backend developer at <a target="_blank" href="https://uptimise.io/">Uptimise</a>, my Lead told me that if I want to advance in the field I need to be comfortable with Networking.</p>
<p>In fact, I don’t know too much about networking besides the fact that I can access my React application locally on <strong>localhost</strong> (<strong>127.0.0.1</strong>) and that generally, I need the <strong>IP address</strong> + <strong>Port</strong> to access an application, like <strong>127.0.0.1:3000</strong> for a React app 😅 (shame).</p>
<p>As I was interested in system programming, I thought, why not learn networking programming (system) at the same time? So I started this month with the <a target="_blank" href="https://www.netacad.com/courses/networking-basics?courseLang=en-US">Networking Basics</a> by Cisco Academy.</p>
<h2 id="heading-first-steps">First Steps</h2>
<p>The course seems good, with around 17 chapters, and the first 4 are about generalities (<em>I’m at chapter 5</em>). It provides good information to conceptualize what networking is, with the support of Cisco Packet Tracer to design networks.</p>
<p>These insights made me realize some things I did in the past but didn’t really understand because I searched “How to” on Google. It’s not bad, but now, I’m gradually understanding why some things are done in certain ways.</p>
<p>For example:</p>
<ul>
<li><p>On a router (even a wireless router), there is a built-in switch that allows devices to connect to the local network (LAN).</p>
</li>
<li><p>The ports usually colored in yellow or orange on a router are for LAN, and the usually blue port (one) is for the internet or Wide Area Network (WAN). That’s why when we want a device to be wired to the router, we connect it to the yellow ports through these (you know) cables with multiple colors.</p>
</li>
</ul>
<p>Basics right ? Yes, that’s why the course is named so.</p>
<p>There is an analogy that I get at this point that helps me remember all of these terms (IP address, LAN, WLAN, Gateway, Subnet Mask, Router, ISP, …):</p>
<blockquote>
<p>The IP addresses for the end devices can range from 192.168.0.2 - 192.168.0.254. <mark>Each device will get a unique IP address in the same network</mark>.</p>
<p>The <mark>subnet mask</mark> is used to <mark>differentiate the host</mark> and <mark>the network ID portion of the IP address</mark>. You can relate the <mark>IP address to your street address</mark>. The <mark>subnet mask defines the length of the street name</mark>. The network part of the address is your street, 192.168.0. <mark>The house number is the host port of the IP address</mark>. For the <mark>IP address 192.168.0.2</mark>, the <mark>house number is 2</mark> and the <mark>street is 192.168.0</mark>. If there is more than one house on the same street, for example, house number 3, will have an address 192.168.0.3. The maximum number of houses on this street is 253, ranging from 2 to 254.</p>
<p><mark>The default gateway is analogous to the street intersection</mark>. The traffic from the 192.168.0 street has to <mark>exit through the intersection to another street</mark>. Another street is another network. In this network, <mark>default gateway is the wireless router</mark> that directs the traffic from the local network to the cable modem, and the traffic is then sent to the ISP.</p>
<p>From Networking Basics by CISCO Academy.</p>
</blockquote>
<p>I understand that the <strong>default gateway</strong> that we read when doing Router config is the Router IP address, simple !</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>The first 4 chapters may seem boring because the general info is already known, but from chapter 5, it starts to get really technical. As Linux networking is built on top of this knowledge, I hope at the end of the course to be able to understand more about Linux networking.</p>
]]></content:encoded></item><item><title><![CDATA[[Monthly Update] February 2025]]></title><description><![CDATA[Sys Prog, Coding Challenges, Go, Container, Linux Internal, February 2025, I built a Linux utility in Go, learnt about how container are built (in Linux…]]></description><link>https://tawaliou.com/monthly-update-february-2025</link><guid isPermaLink="true">https://tawaliou.com/monthly-update-february-2025</guid><category><![CDATA[systemprogramming]]></category><category><![CDATA[Docker]]></category><category><![CDATA[Linux]]></category><dc:creator><![CDATA[Tawaliou ALAO]]></dc:creator><pubDate>Fri, 16 May 2025 11:57:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1741079377997/7387d16f-1019-4427-9019-8a9ec2662e47.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Sys Prog, Coding Challenges, Go, Container, Linux Internal,</p>
<p>February 2025, I built a Linux utility in Go, learnt about how container are built (in Linux environment) and planning to learn about Linux Driver (Kernel Module). Here are some details.</p>
<h2 id="heading-sys-prog-coding-challenges-go">Sys Prog, Coding Challenges, Go</h2>
<p>I try <a target="_blank" href="https://codingchallenges.fyi/challenges/challenge-wc">Build your Own wc tool</a> by The Coding challenges in Go. The challenge is to rewrite in the langage of your choice from scratch the word counter utility found in Linux environment. And you have the freedom to expand the features. wc prints the number of lines, words, characters, bytes of file(s). You can read more details in the links I paste above.</p>
<p>In my case, I built it, and add some sugar features:</p>
<ul>
<li><p>handle multiple files: I use goroutines so the process of multiple files with the same size will took approximatively the amount of time of on file of the same size (good !)</p>
</li>
<li><p>nice output: in my case the output is like</p>
</li>
</ul>
<pre><code class="lang-typescript"> l: <span class="hljs-number">111</span>, w:<span class="hljs-number">100</span>, m: <span class="hljs-number">200</span>, c: <span class="hljs-number">200</span> text.txt
</code></pre>
<ul>
<li>I’ll add(I forget this feature) to get argument from standard input and not only from files.</li>
</ul>
<p>While doing this project, I learnt more about</p>
<ul>
<li><p><a target="_blank" href="https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/">unicodes, characters sets</a> and how Go handle them <a target="_blank" href="https://go.dev/blog/strings">here</a>: Go characters are known as Rune(😏)</p>
</li>
<li><p>newline vs number of lines</p>
</li>
</ul>
<h2 id="heading-container-under-the-hood">Container: under the hood</h2>
<p>I try to understand how containers like Docker are made and try to build a container from scratch. And even if I didn’t go to too far, I have learnt many things. And Yes: <strong>Linux</strong> is awesome, incredible as OS.</p>
<p>A container in Linux env, is based on: namespaces and cgroups. Namespaces help to have process in isolation and <strong>cgroups</strong> to put limit on ressources (memory, cpu, storage...) used by process</p>
<p>Here are the namespaces under linux:</p>
<ul>
<li><p><strong>Mount (mnt)</strong>: for mount points and provide a complete filesystems to the process which is now different of the host filesystems. <strong>That’s why you’ve a complete filesystems (root, dev, home with access) when you’re inside a container.</strong></p>
</li>
<li><p><strong>Process ID (pid)</strong>: a parent process with an ID can see other process but its children only see process from the same parent. <strong>That’s why when you’re inside a container you don’t get PID from applications/process outside the container.</strong></p>
</li>
<li><p><strong>Network</strong> <strong>(net):</strong> this namespace virtualize the network stack. <strong>You remember Docker Network ? Yeah this is the namespace that provide this capabilities</strong>: having your own network isolation inside on the host</p>
</li>
<li><p><strong>User</strong> <strong>ID</strong> (user): This namespace provide you everything related to user and their management: access/privilege, groups. <strong>You remember when creating a Docker image you can specify the user with USER command and create group ?</strong></p>
</li>
<li><p><strong>Inter-process Communication</strong> (System V IPC)</p>
</li>
<li><p><strong>UTS</strong> (Unix Time-Sharing, hostname): with this you have can hostname, DNS for specific process</p>
</li>
</ul>
<p>Here are some details about <a target="_blank" href="https://en.wikipedia.org/wiki/Linux_namespaces">Linux Namespaces</a> and <a target="_blank" href="https://en.wikipedia.org/wiki/Cgroups">Linux cgroups</a>.</p>
<p><a target="_blank" href="https://www.lizrice.com">Liz Rice</a> give many talks on how to build a simple and lightweight <a target="_blank" href="https://www.youtube.com/watch?v=8fi7uSYlOdc">container system from scratch in Go</a></p>
<p>I don’t know but, I think it’s the main reason why Docker is not avalable on Windows without WSL.</p>
<p>Right, hope you learn something from this articles, I’ll be back for sure.</p>
]]></content:encoded></item></channel></rss>