blog.farhan.codes

Farhan's Personal and Professional Blog


Power Management During Driver Development Environment

You can use Power Per Port Switch (PPPS) functionality to power-cycle a USB device during driver development.

Introduction to the Problem

To reduce my hardware footprint, I typically work on device driver development in a VM running on the same multi-use personal system that houses my notes, files, picture backup, etc. This works fine for general development if the kernel crashes often the device is becomes unresponsible without a power-cycle. That leaves me with one of two options:

Read more...

Debugging a running FreeBSD kernel in a VM using kgdb

Using KGDB Remote Debugging for FreeBSD VMs

This guide explains how to debug a running FreeBSD kernel in a VM using kgdb. The default ddb debugger has decent functionality, but unless you somehow know dynamic memory addresses, those capabilities are effectively unusable. Conversely, kdb offers a more robust interface and set of features.

While there is a handbook section on kgdb, it assumes you are running FreeBSD on bare-metal with a physical serial port connected to another serial host, not on modern virtualized infrastructure. In this guide I hope to bridge this gap and make the process I went through easier for the next person. I ran into this problem after trying to set a breakpoint on a variable, only to realize that ddb required me to know exact memory addresses and setting up kgdb for my use-case was not well documented and hope it benefits someone later down the line.

Read more...

Sarf Generator

I’m making available a Arabic word conjugator tool I wrote in TypeScript. It is located here https://sarf.farhan.codes. Aside from the basics, it takes into account some foundational rules of تعللات (special conjugation of certain letters)

There is some stuff its missing, such as repeating letters or edge-cases, but its a good general tool that I use myself when I’m confused. It is also the first project I ever wrote in Typescript.

Read more...

Benchmarking Base32 vs Base64

This is a very rudimentary benchmark of Base32 vs Base64 performance in Golang. This should also serve as an example of Golang’s benchmarking toolchain. Conclusion: Base64 is faster.

This code works by creating 1000 test strings, then performing Base32 and Base64 conversions. Timing is done by Go’s built-in benchmarking tooling, which is part of the testing framework. Here is the code:

package main

import "os"
import "time"
import "testing"
import "math/rand"
import "encoding/base32"
import "encoding/base64"

var strings = []string{}

func setupStrings(n int) []string {
	charset := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	strings := []string{}
	rand.Seed(time.Now().UnixNano())

	for i := 0; i < n; i++ {
		newstring := genRandomString(charset, 32)
		strings = append(strings, newstring)
	}

	return strings
}

func genRandomString(charset string, length int) string {
	b := make([]byte, length)

	for i := range b {
		b[i] = charset[rand.Intn(len(charset))]
	}
	return string(b)
}

func setup() {
	strings = setupStrings(1000)
}

func BenchmarkBase32Run(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, s := range strings {
			_ = base32.StdEncoding.EncodeToString([]byte(s))
		}
	}
}

func BenchmarkBase64Run(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, s := range strings {
			_ = base64.StdEncoding.EncodeToString([]byte(s))
		}
	}
}

func TestMain(m *testing.M) {
	setup()
	code := m.Run()
	os.Exit(code)
}

You will also need a main function, mine is as follows:

Read more...

Let IRC Die

For the sake of progress, let IRC die!

IRC was great, but its continued usage is a relic of an older internet, when storage was expensive and servers were expensive. At this stage, communities should migrate off of IRC to slack-like solutions like Mattermost or Matrix.

IRC served as a workhorse for communication, but it has failed to adapt to modern demands. In particular:

  • Offline sending and retrieval
  • Referenceable conversations (ie, URLs to a conversation)
  • Editing messages
  • Formatted text (including media)
  • True decentralization

Many of these problems can be remediated at the client-level, but not without a cost.

Read more...

Simple dtrace use-case

Very small example of using dtrace, mostly for me to refernece in the future.

As I port this wifi card, I am running into a situation where ifconfig is reporting an error that I do not understand. From previous reading, I know that ifconfig interfaces with the kernel through ioctl. Driver code initially handles ioctl requests by implementing the ic->ic_ioctl handler.

To that end, in my handler I printed the u_long cmd variable with the intention of looking up the value. However, the source has them listed like this in /usr/src/sys/sys/sockio.h.

Read more...

My FreeBSD Development Environment

I’m writing this primarily for myself, but also for others to reference and improve upon.

I run FreeBSD CURRENT in a VM. I use a VM for the inevitable situation where it crashes. I prefer to use Qemu on KVM, almost exclusively because of the ease of using the graphical virt-manager over ssh, and the ease of rebooting.

FreeBSD cannot do crash dumps to VT disks (vtdb0) so I make sure the Disk type is SATA. Also, the disk should be at least 40GBs, or else you won’t be able to dump the kernel core in the event of a crash.

Read more...

Rebuilt This Blog

I rebuilt this blog after a few months of having it down.

Long story short, I ran the blog off of WordPress and self-hosted on my personal server for years. When I moved into a house and migrated off my personal server, I somehow lost the wp_content.sql file, and only that file, somewhere in the mix. An accidental deletion? Who knows. So all posts died. Bummer. Fortunately, I was able to rebuild the content from Wayback Machine (seriously) and Google Cache (yeah, seriously).

Read more...

Unexpected IPv6 Behavior on Linux

Linux has some strange default IPv6 behavior. Here are a few things I noticed…

You can bind to a port on an IPv4 address while all of your tools will report that the port is on IPv6. For example, if your host is 198.51.100.1, you can bind to ::FFFF:198.51.100.1 and all state-checking tools like netstat, ss or lsof will report the listener as on an IPv6-port. You can do this in netcat with nc -6 -l ::FFFF:192.51.100.1.

Read more...

Capturing Input/Output of Another Process in C

In my travels in C programming, I periodically need to run another process and redirect its standard output back to the first process. While it is straight forward to perform, it is not always obvious. This article will explain the process of how this is done in three sections.

  • High Level Overview
  • Explanation of each line
  • Code Sample

High Level Overview

Create a three pipe(2)s for standard input, output and error fork(2) the process The child process runs dup2(2) to over the pipes to redirect the new processes’s standard input, output and error to the pipe. The parent process reads from the pipe(2) descriptors as needed.

Read more...
1 of 4 Next Page