OpenStack, Neutron, Open source in general

#openstack

The metadata for a VM are delivered by Nova metadata service. Nova
needs to know the instance ID of a VM to be able to deliver its metadata. Neutron has a special agent (metadata agent) just to add this information in the HTTP header of the metadata request of the VM. Let’s see more in detail how it works…

There are two possible configurations:

Routed networks

Isolated networks

Routed network

In this case the VM is on a network that is connected to a router. A router is implemented in Neutron using a network namespace. There’s a specific agent that handles the routers in Neutron: the L3 agent. In routed network mode the L3 agent is also in charge of spawning the metadata proxy. As the name says, the metadata proxy is just a proxy that forwards the requests to the metadata agent using a Unix domain socket. When a VM sends a metadata request, the request reaches the router, since it’s the default gateway. In the router namespace there’s an iptables rules that redirects the traffic whose destination is the metadata server to a local port 9697.

When the proxy receives a packets it knows 1) the IP of the VM that is sending the request 2) the router ID of the router that it’s connected to the network the VM is on since there’s one proxy for every router. It adds this information (IP of the VM and router ID) in the HTTP header and forwards the request to the metadata agent. The metadata agent uses the router ID to list all the network connected to that router and identifies the one to which the VM belongs. Then it issues a query to the neutron server to get the instance ID of the VM using the IP and the network ID as filters. It adds the instance ID in the HTTP request and forwards the request to Nova. Yuppie!

Isolated networks

When a network is not connected to a router, how can a VM get its metadata? Well, there’s a flag that you can set in the dhcp agent config file: enable_isolated_metadata . If it’s set to True, the dhcp agent will do some magic. Let’s see the datails.

The dhcp agent is the one that is in charge of dhcp. DHCP is not only about assigning IP addresses, there are other options. For example the Option 121 is for setting a static route. That’s exactly what the dhcp agent uses to tell the VM that the next hop to reach the metadata server is the IP of the dhcp port. If you set enable_isolated_metadata to True and you ssh into the VM you’ll see:

The proxy knows the network ID and the IP of the VM (it’s in the request). It adds this info in the HTTP request and forwards it to the metadata agent. As before the metadata agent gets the instance ID of the VM from the neutron server using the network ID and the IP as filters. It adds the instance ID to the request and forwards it to Nova. Yuppie!

Debugging !

If you use devstack, you can just join the screen session

screen -x

and you will see the metadata server in the window named q-meta.You can set breakpoints directly in the code using pdb .

It’s a bit trickier to debug the metadata proxy, since it’s not in the screen session. Here is what In _get_metadata_proxy_callback add the ‘–nodaemonize’ flag in the command line. You can also specify the log directory if you want to access the logs ‘–log-dir=/opt/stack/logs’. To make the dhcp agent restart the proxy do:

Kill the current proxy process. Create a new window in the screen section that you can name q-meta-proxy. Just paste the command line you copied there to start the proxy. Now you can modify the source code and debug the metadata proxy directly. Don’t forget that you need to restart the proxy every time you modify the code. To make the VM send a metadata request you can just ssh into the VM and do: