datapath.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  1. /*
  2. * Copyright (c) 2007-2012 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <linux/workqueue.h>
  49. #include <net/genetlink.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netns/generic.h>
  52. #include "datapath.h"
  53. #include "flow.h"
  54. #include "vport-internal_dev.h"
  55. /**
  56. * struct ovs_net - Per net-namespace data for ovs.
  57. * @dps: List of datapaths to enable dumping them all out.
  58. * Protected by genl_mutex.
  59. */
  60. struct ovs_net {
  61. struct list_head dps;
  62. };
  63. static int ovs_net_id __read_mostly;
  64. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  65. static void rehash_flow_table(struct work_struct *work);
  66. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  67. /**
  68. * DOC: Locking:
  69. *
  70. * Writes to device state (add/remove datapath, port, set operations on vports,
  71. * etc.) are protected by RTNL.
  72. *
  73. * Writes to other state (flow table modifications, set miscellaneous datapath
  74. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  75. * genl_mutex.
  76. *
  77. * Reads are protected by RCU.
  78. *
  79. * There are a few special cases (mostly stats) that have their own
  80. * synchronization but they nest under all of above and don't interact with
  81. * each other.
  82. */
  83. static struct vport *new_vport(const struct vport_parms *);
  84. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  85. const struct dp_upcall_info *);
  86. static int queue_userspace_packet(struct net *, int dp_ifindex,
  87. struct sk_buff *,
  88. const struct dp_upcall_info *);
  89. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  90. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  91. {
  92. struct datapath *dp = NULL;
  93. struct net_device *dev;
  94. rcu_read_lock();
  95. dev = dev_get_by_index_rcu(net, dp_ifindex);
  96. if (dev) {
  97. struct vport *vport = ovs_internal_dev_get_vport(dev);
  98. if (vport)
  99. dp = vport->dp;
  100. }
  101. rcu_read_unlock();
  102. return dp;
  103. }
  104. /* Must be called with rcu_read_lock or RTNL lock. */
  105. const char *ovs_dp_name(const struct datapath *dp)
  106. {
  107. struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
  108. return vport->ops->get_name(vport);
  109. }
  110. static int get_dpifindex(struct datapath *dp)
  111. {
  112. struct vport *local;
  113. int ifindex;
  114. rcu_read_lock();
  115. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  116. if (local)
  117. ifindex = local->ops->get_ifindex(local);
  118. else
  119. ifindex = 0;
  120. rcu_read_unlock();
  121. return ifindex;
  122. }
  123. static void destroy_dp_rcu(struct rcu_head *rcu)
  124. {
  125. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  126. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  127. free_percpu(dp->stats_percpu);
  128. release_net(ovs_dp_get_net(dp));
  129. kfree(dp->ports);
  130. kfree(dp);
  131. }
  132. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  133. u16 port_no)
  134. {
  135. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  136. }
  137. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  138. {
  139. struct vport *vport;
  140. struct hlist_node *n;
  141. struct hlist_head *head;
  142. head = vport_hash_bucket(dp, port_no);
  143. hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
  144. if (vport->port_no == port_no)
  145. return vport;
  146. }
  147. return NULL;
  148. }
  149. /* Called with RTNL lock and genl_lock. */
  150. static struct vport *new_vport(const struct vport_parms *parms)
  151. {
  152. struct vport *vport;
  153. vport = ovs_vport_add(parms);
  154. if (!IS_ERR(vport)) {
  155. struct datapath *dp = parms->dp;
  156. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  157. hlist_add_head_rcu(&vport->dp_hash_node, head);
  158. }
  159. return vport;
  160. }
  161. /* Called with RTNL lock. */
  162. void ovs_dp_detach_port(struct vport *p)
  163. {
  164. ASSERT_RTNL();
  165. /* First drop references to device. */
  166. hlist_del_rcu(&p->dp_hash_node);
  167. /* Then destroy it. */
  168. ovs_vport_del(p);
  169. }
  170. /* Must be called with rcu_read_lock. */
  171. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  172. {
  173. struct datapath *dp = p->dp;
  174. struct sw_flow *flow;
  175. struct dp_stats_percpu *stats;
  176. struct sw_flow_key key;
  177. u64 *stats_counter;
  178. int error;
  179. int key_len;
  180. stats = this_cpu_ptr(dp->stats_percpu);
  181. /* Extract flow from 'skb' into 'key'. */
  182. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  183. if (unlikely(error)) {
  184. kfree_skb(skb);
  185. return;
  186. }
  187. /* Look up flow. */
  188. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  189. if (unlikely(!flow)) {
  190. struct dp_upcall_info upcall;
  191. upcall.cmd = OVS_PACKET_CMD_MISS;
  192. upcall.key = &key;
  193. upcall.userdata = NULL;
  194. upcall.portid = p->upcall_portid;
  195. ovs_dp_upcall(dp, skb, &upcall);
  196. consume_skb(skb);
  197. stats_counter = &stats->n_missed;
  198. goto out;
  199. }
  200. OVS_CB(skb)->flow = flow;
  201. stats_counter = &stats->n_hit;
  202. ovs_flow_used(OVS_CB(skb)->flow, skb);
  203. ovs_execute_actions(dp, skb);
  204. out:
  205. /* Update datapath statistics. */
  206. u64_stats_update_begin(&stats->sync);
  207. (*stats_counter)++;
  208. u64_stats_update_end(&stats->sync);
  209. }
  210. static struct genl_family dp_packet_genl_family = {
  211. .id = GENL_ID_GENERATE,
  212. .hdrsize = sizeof(struct ovs_header),
  213. .name = OVS_PACKET_FAMILY,
  214. .version = OVS_PACKET_VERSION,
  215. .maxattr = OVS_PACKET_ATTR_MAX,
  216. .netnsok = true
  217. };
  218. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  219. const struct dp_upcall_info *upcall_info)
  220. {
  221. struct dp_stats_percpu *stats;
  222. int dp_ifindex;
  223. int err;
  224. if (upcall_info->portid == 0) {
  225. err = -ENOTCONN;
  226. goto err;
  227. }
  228. dp_ifindex = get_dpifindex(dp);
  229. if (!dp_ifindex) {
  230. err = -ENODEV;
  231. goto err;
  232. }
  233. if (!skb_is_gso(skb))
  234. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  235. else
  236. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  237. if (err)
  238. goto err;
  239. return 0;
  240. err:
  241. stats = this_cpu_ptr(dp->stats_percpu);
  242. u64_stats_update_begin(&stats->sync);
  243. stats->n_lost++;
  244. u64_stats_update_end(&stats->sync);
  245. return err;
  246. }
  247. static int queue_gso_packets(struct net *net, int dp_ifindex,
  248. struct sk_buff *skb,
  249. const struct dp_upcall_info *upcall_info)
  250. {
  251. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  252. struct dp_upcall_info later_info;
  253. struct sw_flow_key later_key;
  254. struct sk_buff *segs, *nskb;
  255. int err;
  256. segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
  257. if (IS_ERR(segs))
  258. return PTR_ERR(segs);
  259. /* Queue all of the segments. */
  260. skb = segs;
  261. do {
  262. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  263. if (err)
  264. break;
  265. if (skb == segs && gso_type & SKB_GSO_UDP) {
  266. /* The initial flow key extracted by ovs_flow_extract()
  267. * in this case is for a first fragment, so we need to
  268. * properly mark later fragments.
  269. */
  270. later_key = *upcall_info->key;
  271. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  272. later_info = *upcall_info;
  273. later_info.key = &later_key;
  274. upcall_info = &later_info;
  275. }
  276. } while ((skb = skb->next));
  277. /* Free all of the segments. */
  278. skb = segs;
  279. do {
  280. nskb = skb->next;
  281. if (err)
  282. kfree_skb(skb);
  283. else
  284. consume_skb(skb);
  285. } while ((skb = nskb));
  286. return err;
  287. }
  288. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  289. struct sk_buff *skb,
  290. const struct dp_upcall_info *upcall_info)
  291. {
  292. struct ovs_header *upcall;
  293. struct sk_buff *nskb = NULL;
  294. struct sk_buff *user_skb; /* to be queued to userspace */
  295. struct nlattr *nla;
  296. unsigned int len;
  297. int err;
  298. if (vlan_tx_tag_present(skb)) {
  299. nskb = skb_clone(skb, GFP_ATOMIC);
  300. if (!nskb)
  301. return -ENOMEM;
  302. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  303. if (!nskb)
  304. return -ENOMEM;
  305. nskb->vlan_tci = 0;
  306. skb = nskb;
  307. }
  308. if (nla_attr_size(skb->len) > USHRT_MAX) {
  309. err = -EFBIG;
  310. goto out;
  311. }
  312. len = sizeof(struct ovs_header);
  313. len += nla_total_size(skb->len);
  314. len += nla_total_size(FLOW_BUFSIZE);
  315. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  316. len += nla_total_size(8);
  317. user_skb = genlmsg_new(len, GFP_ATOMIC);
  318. if (!user_skb) {
  319. err = -ENOMEM;
  320. goto out;
  321. }
  322. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  323. 0, upcall_info->cmd);
  324. upcall->dp_ifindex = dp_ifindex;
  325. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  326. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  327. nla_nest_end(user_skb, nla);
  328. if (upcall_info->userdata)
  329. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  330. nla_get_u64(upcall_info->userdata));
  331. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  332. skb_copy_and_csum_dev(skb, nla_data(nla));
  333. genlmsg_end(user_skb, upcall);
  334. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  335. out:
  336. kfree_skb(nskb);
  337. return err;
  338. }
  339. /* Called with genl_mutex. */
  340. static int flush_flows(struct datapath *dp)
  341. {
  342. struct flow_table *old_table;
  343. struct flow_table *new_table;
  344. old_table = genl_dereference(dp->table);
  345. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  346. if (!new_table)
  347. return -ENOMEM;
  348. rcu_assign_pointer(dp->table, new_table);
  349. ovs_flow_tbl_deferred_destroy(old_table);
  350. return 0;
  351. }
  352. static int validate_actions(const struct nlattr *attr,
  353. const struct sw_flow_key *key, int depth);
  354. static int validate_sample(const struct nlattr *attr,
  355. const struct sw_flow_key *key, int depth)
  356. {
  357. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  358. const struct nlattr *probability, *actions;
  359. const struct nlattr *a;
  360. int rem;
  361. memset(attrs, 0, sizeof(attrs));
  362. nla_for_each_nested(a, attr, rem) {
  363. int type = nla_type(a);
  364. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  365. return -EINVAL;
  366. attrs[type] = a;
  367. }
  368. if (rem)
  369. return -EINVAL;
  370. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  371. if (!probability || nla_len(probability) != sizeof(u32))
  372. return -EINVAL;
  373. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  374. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  375. return -EINVAL;
  376. return validate_actions(actions, key, depth + 1);
  377. }
  378. static int validate_tp_port(const struct sw_flow_key *flow_key)
  379. {
  380. if (flow_key->eth.type == htons(ETH_P_IP)) {
  381. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  382. return 0;
  383. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  384. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  385. return 0;
  386. }
  387. return -EINVAL;
  388. }
  389. static int validate_set(const struct nlattr *a,
  390. const struct sw_flow_key *flow_key)
  391. {
  392. const struct nlattr *ovs_key = nla_data(a);
  393. int key_type = nla_type(ovs_key);
  394. /* There can be only one key in a action */
  395. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  396. return -EINVAL;
  397. if (key_type > OVS_KEY_ATTR_MAX ||
  398. nla_len(ovs_key) != ovs_key_lens[key_type])
  399. return -EINVAL;
  400. switch (key_type) {
  401. const struct ovs_key_ipv4 *ipv4_key;
  402. const struct ovs_key_ipv6 *ipv6_key;
  403. case OVS_KEY_ATTR_PRIORITY:
  404. case OVS_KEY_ATTR_SKB_MARK:
  405. case OVS_KEY_ATTR_ETHERNET:
  406. break;
  407. case OVS_KEY_ATTR_IPV4:
  408. if (flow_key->eth.type != htons(ETH_P_IP))
  409. return -EINVAL;
  410. if (!flow_key->ip.proto)
  411. return -EINVAL;
  412. ipv4_key = nla_data(ovs_key);
  413. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  414. return -EINVAL;
  415. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  416. return -EINVAL;
  417. break;
  418. case OVS_KEY_ATTR_IPV6:
  419. if (flow_key->eth.type != htons(ETH_P_IPV6))
  420. return -EINVAL;
  421. if (!flow_key->ip.proto)
  422. return -EINVAL;
  423. ipv6_key = nla_data(ovs_key);
  424. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  425. return -EINVAL;
  426. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  427. return -EINVAL;
  428. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  429. return -EINVAL;
  430. break;
  431. case OVS_KEY_ATTR_TCP:
  432. if (flow_key->ip.proto != IPPROTO_TCP)
  433. return -EINVAL;
  434. return validate_tp_port(flow_key);
  435. case OVS_KEY_ATTR_UDP:
  436. if (flow_key->ip.proto != IPPROTO_UDP)
  437. return -EINVAL;
  438. return validate_tp_port(flow_key);
  439. default:
  440. return -EINVAL;
  441. }
  442. return 0;
  443. }
  444. static int validate_userspace(const struct nlattr *attr)
  445. {
  446. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  447. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  448. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  449. };
  450. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  451. int error;
  452. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  453. attr, userspace_policy);
  454. if (error)
  455. return error;
  456. if (!a[OVS_USERSPACE_ATTR_PID] ||
  457. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  458. return -EINVAL;
  459. return 0;
  460. }
  461. static int validate_actions(const struct nlattr *attr,
  462. const struct sw_flow_key *key, int depth)
  463. {
  464. const struct nlattr *a;
  465. int rem, err;
  466. if (depth >= SAMPLE_ACTION_DEPTH)
  467. return -EOVERFLOW;
  468. nla_for_each_nested(a, attr, rem) {
  469. /* Expected argument lengths, (u32)-1 for variable length. */
  470. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  471. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  472. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  473. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  474. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  475. [OVS_ACTION_ATTR_SET] = (u32)-1,
  476. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  477. };
  478. const struct ovs_action_push_vlan *vlan;
  479. int type = nla_type(a);
  480. if (type > OVS_ACTION_ATTR_MAX ||
  481. (action_lens[type] != nla_len(a) &&
  482. action_lens[type] != (u32)-1))
  483. return -EINVAL;
  484. switch (type) {
  485. case OVS_ACTION_ATTR_UNSPEC:
  486. return -EINVAL;
  487. case OVS_ACTION_ATTR_USERSPACE:
  488. err = validate_userspace(a);
  489. if (err)
  490. return err;
  491. break;
  492. case OVS_ACTION_ATTR_OUTPUT:
  493. if (nla_get_u32(a) >= DP_MAX_PORTS)
  494. return -EINVAL;
  495. break;
  496. case OVS_ACTION_ATTR_POP_VLAN:
  497. break;
  498. case OVS_ACTION_ATTR_PUSH_VLAN:
  499. vlan = nla_data(a);
  500. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  501. return -EINVAL;
  502. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  503. return -EINVAL;
  504. break;
  505. case OVS_ACTION_ATTR_SET:
  506. err = validate_set(a, key);
  507. if (err)
  508. return err;
  509. break;
  510. case OVS_ACTION_ATTR_SAMPLE:
  511. err = validate_sample(a, key, depth);
  512. if (err)
  513. return err;
  514. break;
  515. default:
  516. return -EINVAL;
  517. }
  518. }
  519. if (rem > 0)
  520. return -EINVAL;
  521. return 0;
  522. }
  523. static void clear_stats(struct sw_flow *flow)
  524. {
  525. flow->used = 0;
  526. flow->tcp_flags = 0;
  527. flow->packet_count = 0;
  528. flow->byte_count = 0;
  529. }
  530. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  531. {
  532. struct ovs_header *ovs_header = info->userhdr;
  533. struct nlattr **a = info->attrs;
  534. struct sw_flow_actions *acts;
  535. struct sk_buff *packet;
  536. struct sw_flow *flow;
  537. struct datapath *dp;
  538. struct ethhdr *eth;
  539. int len;
  540. int err;
  541. int key_len;
  542. err = -EINVAL;
  543. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  544. !a[OVS_PACKET_ATTR_ACTIONS] ||
  545. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  546. goto err;
  547. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  548. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  549. err = -ENOMEM;
  550. if (!packet)
  551. goto err;
  552. skb_reserve(packet, NET_IP_ALIGN);
  553. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  554. skb_reset_mac_header(packet);
  555. eth = eth_hdr(packet);
  556. /* Normally, setting the skb 'protocol' field would be handled by a
  557. * call to eth_type_trans(), but it assumes there's a sending
  558. * device, which we may not have. */
  559. if (ntohs(eth->h_proto) >= 1536)
  560. packet->protocol = eth->h_proto;
  561. else
  562. packet->protocol = htons(ETH_P_802_2);
  563. /* Build an sw_flow for sending this packet. */
  564. flow = ovs_flow_alloc();
  565. err = PTR_ERR(flow);
  566. if (IS_ERR(flow))
  567. goto err_kfree_skb;
  568. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  569. if (err)
  570. goto err_flow_free;
  571. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  572. &flow->key.phy.skb_mark,
  573. &flow->key.phy.in_port,
  574. a[OVS_PACKET_ATTR_KEY]);
  575. if (err)
  576. goto err_flow_free;
  577. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  578. if (err)
  579. goto err_flow_free;
  580. flow->hash = ovs_flow_hash(&flow->key, key_len);
  581. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  582. err = PTR_ERR(acts);
  583. if (IS_ERR(acts))
  584. goto err_flow_free;
  585. rcu_assign_pointer(flow->sf_acts, acts);
  586. OVS_CB(packet)->flow = flow;
  587. packet->priority = flow->key.phy.priority;
  588. packet->mark = flow->key.phy.skb_mark;
  589. rcu_read_lock();
  590. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  591. err = -ENODEV;
  592. if (!dp)
  593. goto err_unlock;
  594. local_bh_disable();
  595. err = ovs_execute_actions(dp, packet);
  596. local_bh_enable();
  597. rcu_read_unlock();
  598. ovs_flow_free(flow);
  599. return err;
  600. err_unlock:
  601. rcu_read_unlock();
  602. err_flow_free:
  603. ovs_flow_free(flow);
  604. err_kfree_skb:
  605. kfree_skb(packet);
  606. err:
  607. return err;
  608. }
  609. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  610. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  611. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  612. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  613. };
  614. static struct genl_ops dp_packet_genl_ops[] = {
  615. { .cmd = OVS_PACKET_CMD_EXECUTE,
  616. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  617. .policy = packet_policy,
  618. .doit = ovs_packet_cmd_execute
  619. }
  620. };
  621. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  622. {
  623. int i;
  624. struct flow_table *table = genl_dereference(dp->table);
  625. stats->n_flows = ovs_flow_tbl_count(table);
  626. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  627. for_each_possible_cpu(i) {
  628. const struct dp_stats_percpu *percpu_stats;
  629. struct dp_stats_percpu local_stats;
  630. unsigned int start;
  631. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  632. do {
  633. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  634. local_stats = *percpu_stats;
  635. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  636. stats->n_hit += local_stats.n_hit;
  637. stats->n_missed += local_stats.n_missed;
  638. stats->n_lost += local_stats.n_lost;
  639. }
  640. }
  641. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  642. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  643. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  644. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  645. };
  646. static struct genl_family dp_flow_genl_family = {
  647. .id = GENL_ID_GENERATE,
  648. .hdrsize = sizeof(struct ovs_header),
  649. .name = OVS_FLOW_FAMILY,
  650. .version = OVS_FLOW_VERSION,
  651. .maxattr = OVS_FLOW_ATTR_MAX,
  652. .netnsok = true
  653. };
  654. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  655. .name = OVS_FLOW_MCGROUP
  656. };
  657. /* Called with genl_lock. */
  658. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  659. struct sk_buff *skb, u32 portid,
  660. u32 seq, u32 flags, u8 cmd)
  661. {
  662. const int skb_orig_len = skb->len;
  663. const struct sw_flow_actions *sf_acts;
  664. struct ovs_flow_stats stats;
  665. struct ovs_header *ovs_header;
  666. struct nlattr *nla;
  667. unsigned long used;
  668. u8 tcp_flags;
  669. int err;
  670. sf_acts = rcu_dereference_protected(flow->sf_acts,
  671. lockdep_genl_is_held());
  672. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  673. if (!ovs_header)
  674. return -EMSGSIZE;
  675. ovs_header->dp_ifindex = get_dpifindex(dp);
  676. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  677. if (!nla)
  678. goto nla_put_failure;
  679. err = ovs_flow_to_nlattrs(&flow->key, skb);
  680. if (err)
  681. goto error;
  682. nla_nest_end(skb, nla);
  683. spin_lock_bh(&flow->lock);
  684. used = flow->used;
  685. stats.n_packets = flow->packet_count;
  686. stats.n_bytes = flow->byte_count;
  687. tcp_flags = flow->tcp_flags;
  688. spin_unlock_bh(&flow->lock);
  689. if (used &&
  690. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  691. goto nla_put_failure;
  692. if (stats.n_packets &&
  693. nla_put(skb, OVS_FLOW_ATTR_STATS,
  694. sizeof(struct ovs_flow_stats), &stats))
  695. goto nla_put_failure;
  696. if (tcp_flags &&
  697. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  698. goto nla_put_failure;
  699. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  700. * this is the first flow to be dumped into 'skb'. This is unusual for
  701. * Netlink but individual action lists can be longer than
  702. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  703. * The userspace caller can always fetch the actions separately if it
  704. * really wants them. (Most userspace callers in fact don't care.)
  705. *
  706. * This can only fail for dump operations because the skb is always
  707. * properly sized for single flows.
  708. */
  709. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  710. sf_acts->actions);
  711. if (err < 0 && skb_orig_len)
  712. goto error;
  713. return genlmsg_end(skb, ovs_header);
  714. nla_put_failure:
  715. err = -EMSGSIZE;
  716. error:
  717. genlmsg_cancel(skb, ovs_header);
  718. return err;
  719. }
  720. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  721. {
  722. const struct sw_flow_actions *sf_acts;
  723. int len;
  724. sf_acts = rcu_dereference_protected(flow->sf_acts,
  725. lockdep_genl_is_held());
  726. /* OVS_FLOW_ATTR_KEY */
  727. len = nla_total_size(FLOW_BUFSIZE);
  728. /* OVS_FLOW_ATTR_ACTIONS */
  729. len += nla_total_size(sf_acts->actions_len);
  730. /* OVS_FLOW_ATTR_STATS */
  731. len += nla_total_size(sizeof(struct ovs_flow_stats));
  732. /* OVS_FLOW_ATTR_TCP_FLAGS */
  733. len += nla_total_size(1);
  734. /* OVS_FLOW_ATTR_USED */
  735. len += nla_total_size(8);
  736. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  737. return genlmsg_new(len, GFP_KERNEL);
  738. }
  739. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  740. struct datapath *dp,
  741. u32 portid, u32 seq, u8 cmd)
  742. {
  743. struct sk_buff *skb;
  744. int retval;
  745. skb = ovs_flow_cmd_alloc_info(flow);
  746. if (!skb)
  747. return ERR_PTR(-ENOMEM);
  748. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  749. BUG_ON(retval < 0);
  750. return skb;
  751. }
  752. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  753. {
  754. struct nlattr **a = info->attrs;
  755. struct ovs_header *ovs_header = info->userhdr;
  756. struct sw_flow_key key;
  757. struct sw_flow *flow;
  758. struct sk_buff *reply;
  759. struct datapath *dp;
  760. struct flow_table *table;
  761. int error;
  762. int key_len;
  763. /* Extract key. */
  764. error = -EINVAL;
  765. if (!a[OVS_FLOW_ATTR_KEY])
  766. goto error;
  767. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  768. if (error)
  769. goto error;
  770. /* Validate actions. */
  771. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  772. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  773. if (error)
  774. goto error;
  775. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  776. error = -EINVAL;
  777. goto error;
  778. }
  779. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  780. error = -ENODEV;
  781. if (!dp)
  782. goto error;
  783. table = genl_dereference(dp->table);
  784. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  785. if (!flow) {
  786. struct sw_flow_actions *acts;
  787. /* Bail out if we're not allowed to create a new flow. */
  788. error = -ENOENT;
  789. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  790. goto error;
  791. /* Expand table, if necessary, to make room. */
  792. if (ovs_flow_tbl_need_to_expand(table)) {
  793. struct flow_table *new_table;
  794. new_table = ovs_flow_tbl_expand(table);
  795. if (!IS_ERR(new_table)) {
  796. rcu_assign_pointer(dp->table, new_table);
  797. ovs_flow_tbl_deferred_destroy(table);
  798. table = genl_dereference(dp->table);
  799. }
  800. }
  801. /* Allocate flow. */
  802. flow = ovs_flow_alloc();
  803. if (IS_ERR(flow)) {
  804. error = PTR_ERR(flow);
  805. goto error;
  806. }
  807. flow->key = key;
  808. clear_stats(flow);
  809. /* Obtain actions. */
  810. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  811. error = PTR_ERR(acts);
  812. if (IS_ERR(acts))
  813. goto error_free_flow;
  814. rcu_assign_pointer(flow->sf_acts, acts);
  815. /* Put flow in bucket. */
  816. flow->hash = ovs_flow_hash(&key, key_len);
  817. ovs_flow_tbl_insert(table, flow);
  818. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  819. info->snd_seq,
  820. OVS_FLOW_CMD_NEW);
  821. } else {
  822. /* We found a matching flow. */
  823. struct sw_flow_actions *old_acts;
  824. struct nlattr *acts_attrs;
  825. /* Bail out if we're not allowed to modify an existing flow.
  826. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  827. * because Generic Netlink treats the latter as a dump
  828. * request. We also accept NLM_F_EXCL in case that bug ever
  829. * gets fixed.
  830. */
  831. error = -EEXIST;
  832. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  833. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  834. goto error;
  835. /* Update actions. */
  836. old_acts = rcu_dereference_protected(flow->sf_acts,
  837. lockdep_genl_is_held());
  838. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  839. if (acts_attrs &&
  840. (old_acts->actions_len != nla_len(acts_attrs) ||
  841. memcmp(old_acts->actions, nla_data(acts_attrs),
  842. old_acts->actions_len))) {
  843. struct sw_flow_actions *new_acts;
  844. new_acts = ovs_flow_actions_alloc(acts_attrs);
  845. error = PTR_ERR(new_acts);
  846. if (IS_ERR(new_acts))
  847. goto error;
  848. rcu_assign_pointer(flow->sf_acts, new_acts);
  849. ovs_flow_deferred_free_acts(old_acts);
  850. }
  851. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  852. info->snd_seq, OVS_FLOW_CMD_NEW);
  853. /* Clear stats. */
  854. if (a[OVS_FLOW_ATTR_CLEAR]) {
  855. spin_lock_bh(&flow->lock);
  856. clear_stats(flow);
  857. spin_unlock_bh(&flow->lock);
  858. }
  859. }
  860. if (!IS_ERR(reply))
  861. genl_notify(reply, genl_info_net(info), info->snd_portid,
  862. ovs_dp_flow_multicast_group.id, info->nlhdr,
  863. GFP_KERNEL);
  864. else
  865. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  866. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  867. return 0;
  868. error_free_flow:
  869. ovs_flow_free(flow);
  870. error:
  871. return error;
  872. }
  873. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  874. {
  875. struct nlattr **a = info->attrs;
  876. struct ovs_header *ovs_header = info->userhdr;
  877. struct sw_flow_key key;
  878. struct sk_buff *reply;
  879. struct sw_flow *flow;
  880. struct datapath *dp;
  881. struct flow_table *table;
  882. int err;
  883. int key_len;
  884. if (!a[OVS_FLOW_ATTR_KEY])
  885. return -EINVAL;
  886. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  887. if (err)
  888. return err;
  889. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  890. if (!dp)
  891. return -ENODEV;
  892. table = genl_dereference(dp->table);
  893. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  894. if (!flow)
  895. return -ENOENT;
  896. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  897. info->snd_seq, OVS_FLOW_CMD_NEW);
  898. if (IS_ERR(reply))
  899. return PTR_ERR(reply);
  900. return genlmsg_reply(reply, info);
  901. }
  902. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  903. {
  904. struct nlattr **a = info->attrs;
  905. struct ovs_header *ovs_header = info->userhdr;
  906. struct sw_flow_key key;
  907. struct sk_buff *reply;
  908. struct sw_flow *flow;
  909. struct datapath *dp;
  910. struct flow_table *table;
  911. int err;
  912. int key_len;
  913. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  914. if (!dp)
  915. return -ENODEV;
  916. if (!a[OVS_FLOW_ATTR_KEY])
  917. return flush_flows(dp);
  918. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  919. if (err)
  920. return err;
  921. table = genl_dereference(dp->table);
  922. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  923. if (!flow)
  924. return -ENOENT;
  925. reply = ovs_flow_cmd_alloc_info(flow);
  926. if (!reply)
  927. return -ENOMEM;
  928. ovs_flow_tbl_remove(table, flow);
  929. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  930. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  931. BUG_ON(err < 0);
  932. ovs_flow_deferred_free(flow);
  933. genl_notify(reply, genl_info_net(info), info->snd_portid,
  934. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  935. return 0;
  936. }
  937. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  938. {
  939. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  940. struct datapath *dp;
  941. struct flow_table *table;
  942. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  943. if (!dp)
  944. return -ENODEV;
  945. table = genl_dereference(dp->table);
  946. for (;;) {
  947. struct sw_flow *flow;
  948. u32 bucket, obj;
  949. bucket = cb->args[0];
  950. obj = cb->args[1];
  951. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  952. if (!flow)
  953. break;
  954. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  955. NETLINK_CB(cb->skb).portid,
  956. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  957. OVS_FLOW_CMD_NEW) < 0)
  958. break;
  959. cb->args[0] = bucket;
  960. cb->args[1] = obj;
  961. }
  962. return skb->len;
  963. }
  964. static struct genl_ops dp_flow_genl_ops[] = {
  965. { .cmd = OVS_FLOW_CMD_NEW,
  966. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  967. .policy = flow_policy,
  968. .doit = ovs_flow_cmd_new_or_set
  969. },
  970. { .cmd = OVS_FLOW_CMD_DEL,
  971. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  972. .policy = flow_policy,
  973. .doit = ovs_flow_cmd_del
  974. },
  975. { .cmd = OVS_FLOW_CMD_GET,
  976. .flags = 0, /* OK for unprivileged users. */
  977. .policy = flow_policy,
  978. .doit = ovs_flow_cmd_get,
  979. .dumpit = ovs_flow_cmd_dump
  980. },
  981. { .cmd = OVS_FLOW_CMD_SET,
  982. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  983. .policy = flow_policy,
  984. .doit = ovs_flow_cmd_new_or_set,
  985. },
  986. };
  987. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  988. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  989. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  990. };
  991. static struct genl_family dp_datapath_genl_family = {
  992. .id = GENL_ID_GENERATE,
  993. .hdrsize = sizeof(struct ovs_header),
  994. .name = OVS_DATAPATH_FAMILY,
  995. .version = OVS_DATAPATH_VERSION,
  996. .maxattr = OVS_DP_ATTR_MAX,
  997. .netnsok = true
  998. };
  999. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  1000. .name = OVS_DATAPATH_MCGROUP
  1001. };
  1002. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1003. u32 portid, u32 seq, u32 flags, u8 cmd)
  1004. {
  1005. struct ovs_header *ovs_header;
  1006. struct ovs_dp_stats dp_stats;
  1007. int err;
  1008. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1009. flags, cmd);
  1010. if (!ovs_header)
  1011. goto error;
  1012. ovs_header->dp_ifindex = get_dpifindex(dp);
  1013. rcu_read_lock();
  1014. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1015. rcu_read_unlock();
  1016. if (err)
  1017. goto nla_put_failure;
  1018. get_dp_stats(dp, &dp_stats);
  1019. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1020. goto nla_put_failure;
  1021. return genlmsg_end(skb, ovs_header);
  1022. nla_put_failure:
  1023. genlmsg_cancel(skb, ovs_header);
  1024. error:
  1025. return -EMSGSIZE;
  1026. }
  1027. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1028. u32 seq, u8 cmd)
  1029. {
  1030. struct sk_buff *skb;
  1031. int retval;
  1032. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1033. if (!skb)
  1034. return ERR_PTR(-ENOMEM);
  1035. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1036. if (retval < 0) {
  1037. kfree_skb(skb);
  1038. return ERR_PTR(retval);
  1039. }
  1040. return skb;
  1041. }
  1042. /* Called with genl_mutex and optionally with RTNL lock also. */
  1043. static struct datapath *lookup_datapath(struct net *net,
  1044. struct ovs_header *ovs_header,
  1045. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1046. {
  1047. struct datapath *dp;
  1048. if (!a[OVS_DP_ATTR_NAME])
  1049. dp = get_dp(net, ovs_header->dp_ifindex);
  1050. else {
  1051. struct vport *vport;
  1052. rcu_read_lock();
  1053. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1054. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1055. rcu_read_unlock();
  1056. }
  1057. return dp ? dp : ERR_PTR(-ENODEV);
  1058. }
  1059. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1060. {
  1061. struct nlattr **a = info->attrs;
  1062. struct vport_parms parms;
  1063. struct sk_buff *reply;
  1064. struct datapath *dp;
  1065. struct vport *vport;
  1066. struct ovs_net *ovs_net;
  1067. int err, i;
  1068. err = -EINVAL;
  1069. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1070. goto err;
  1071. rtnl_lock();
  1072. err = -ENOMEM;
  1073. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1074. if (dp == NULL)
  1075. goto err_unlock_rtnl;
  1076. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1077. /* Allocate table. */
  1078. err = -ENOMEM;
  1079. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1080. if (!dp->table)
  1081. goto err_free_dp;
  1082. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1083. if (!dp->stats_percpu) {
  1084. err = -ENOMEM;
  1085. goto err_destroy_table;
  1086. }
  1087. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1088. GFP_KERNEL);
  1089. if (!dp->ports) {
  1090. err = -ENOMEM;
  1091. goto err_destroy_percpu;
  1092. }
  1093. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1094. INIT_HLIST_HEAD(&dp->ports[i]);
  1095. /* Set up our datapath device. */
  1096. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1097. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1098. parms.options = NULL;
  1099. parms.dp = dp;
  1100. parms.port_no = OVSP_LOCAL;
  1101. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1102. vport = new_vport(&parms);
  1103. if (IS_ERR(vport)) {
  1104. err = PTR_ERR(vport);
  1105. if (err == -EBUSY)
  1106. err = -EEXIST;
  1107. goto err_destroy_ports_array;
  1108. }
  1109. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1110. info->snd_seq, OVS_DP_CMD_NEW);
  1111. err = PTR_ERR(reply);
  1112. if (IS_ERR(reply))
  1113. goto err_destroy_local_port;
  1114. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1115. list_add_tail(&dp->list_node, &ovs_net->dps);
  1116. rtnl_unlock();
  1117. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1118. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1119. GFP_KERNEL);
  1120. return 0;
  1121. err_destroy_local_port:
  1122. ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
  1123. err_destroy_ports_array:
  1124. kfree(dp->ports);
  1125. err_destroy_percpu:
  1126. free_percpu(dp->stats_percpu);
  1127. err_destroy_table:
  1128. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1129. err_free_dp:
  1130. release_net(ovs_dp_get_net(dp));
  1131. kfree(dp);
  1132. err_unlock_rtnl:
  1133. rtnl_unlock();
  1134. err:
  1135. return err;
  1136. }
  1137. /* Called with genl_mutex. */
  1138. static void __dp_destroy(struct datapath *dp)
  1139. {
  1140. int i;
  1141. rtnl_lock();
  1142. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1143. struct vport *vport;
  1144. struct hlist_node *node, *n;
  1145. hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
  1146. if (vport->port_no != OVSP_LOCAL)
  1147. ovs_dp_detach_port(vport);
  1148. }
  1149. list_del(&dp->list_node);
  1150. ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
  1151. /* rtnl_unlock() will wait until all the references to devices that
  1152. * are pending unregistration have been dropped. We do it here to
  1153. * ensure that any internal devices (which contain DP pointers) are
  1154. * fully destroyed before freeing the datapath.
  1155. */
  1156. rtnl_unlock();
  1157. call_rcu(&dp->rcu, destroy_dp_rcu);
  1158. }
  1159. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1160. {
  1161. struct sk_buff *reply;
  1162. struct datapath *dp;
  1163. int err;
  1164. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1165. err = PTR_ERR(dp);
  1166. if (IS_ERR(dp))
  1167. return err;
  1168. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1169. info->snd_seq, OVS_DP_CMD_DEL);
  1170. err = PTR_ERR(reply);
  1171. if (IS_ERR(reply))
  1172. return err;
  1173. __dp_destroy(dp);
  1174. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1175. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1176. GFP_KERNEL);
  1177. return 0;
  1178. }
  1179. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1180. {
  1181. struct sk_buff *reply;
  1182. struct datapath *dp;
  1183. int err;
  1184. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1185. if (IS_ERR(dp))
  1186. return PTR_ERR(dp);
  1187. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1188. info->snd_seq, OVS_DP_CMD_NEW);
  1189. if (IS_ERR(reply)) {
  1190. err = PTR_ERR(reply);
  1191. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1192. ovs_dp_datapath_multicast_group.id, err);
  1193. return 0;
  1194. }
  1195. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1196. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1197. GFP_KERNEL);
  1198. return 0;
  1199. }
  1200. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1201. {
  1202. struct sk_buff *reply;
  1203. struct datapath *dp;
  1204. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1205. if (IS_ERR(dp))
  1206. return PTR_ERR(dp);
  1207. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1208. info->snd_seq, OVS_DP_CMD_NEW);
  1209. if (IS_ERR(reply))
  1210. return PTR_ERR(reply);
  1211. return genlmsg_reply(reply, info);
  1212. }
  1213. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1214. {
  1215. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1216. struct datapath *dp;
  1217. int skip = cb->args[0];
  1218. int i = 0;
  1219. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1220. if (i >= skip &&
  1221. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1222. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1223. OVS_DP_CMD_NEW) < 0)
  1224. break;
  1225. i++;
  1226. }
  1227. cb->args[0] = i;
  1228. return skb->len;
  1229. }
  1230. static struct genl_ops dp_datapath_genl_ops[] = {
  1231. { .cmd = OVS_DP_CMD_NEW,
  1232. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1233. .policy = datapath_policy,
  1234. .doit = ovs_dp_cmd_new
  1235. },
  1236. { .cmd = OVS_DP_CMD_DEL,
  1237. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1238. .policy = datapath_policy,
  1239. .doit = ovs_dp_cmd_del
  1240. },
  1241. { .cmd = OVS_DP_CMD_GET,
  1242. .flags = 0, /* OK for unprivileged users. */
  1243. .policy = datapath_policy,
  1244. .doit = ovs_dp_cmd_get,
  1245. .dumpit = ovs_dp_cmd_dump
  1246. },
  1247. { .cmd = OVS_DP_CMD_SET,
  1248. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1249. .policy = datapath_policy,
  1250. .doit = ovs_dp_cmd_set,
  1251. },
  1252. };
  1253. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1254. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1255. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1256. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1257. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1258. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1259. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1260. };
  1261. static struct genl_family dp_vport_genl_family = {
  1262. .id = GENL_ID_GENERATE,
  1263. .hdrsize = sizeof(struct ovs_header),
  1264. .name = OVS_VPORT_FAMILY,
  1265. .version = OVS_VPORT_VERSION,
  1266. .maxattr = OVS_VPORT_ATTR_MAX,
  1267. .netnsok = true
  1268. };
  1269. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1270. .name = OVS_VPORT_MCGROUP
  1271. };
  1272. /* Called with RTNL lock or RCU read lock. */
  1273. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1274. u32 portid, u32 seq, u32 flags, u8 cmd)
  1275. {
  1276. struct ovs_header *ovs_header;
  1277. struct ovs_vport_stats vport_stats;
  1278. int err;
  1279. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1280. flags, cmd);
  1281. if (!ovs_header)
  1282. return -EMSGSIZE;
  1283. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1284. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1285. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1286. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1287. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1288. goto nla_put_failure;
  1289. ovs_vport_get_stats(vport, &vport_stats);
  1290. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1291. &vport_stats))
  1292. goto nla_put_failure;
  1293. err = ovs_vport_get_options(vport, skb);
  1294. if (err == -EMSGSIZE)
  1295. goto error;
  1296. return genlmsg_end(skb, ovs_header);
  1297. nla_put_failure:
  1298. err = -EMSGSIZE;
  1299. error:
  1300. genlmsg_cancel(skb, ovs_header);
  1301. return err;
  1302. }
  1303. /* Called with RTNL lock or RCU read lock. */
  1304. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1305. u32 seq, u8 cmd)
  1306. {
  1307. struct sk_buff *skb;
  1308. int retval;
  1309. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1310. if (!skb)
  1311. return ERR_PTR(-ENOMEM);
  1312. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1313. if (retval < 0) {
  1314. kfree_skb(skb);
  1315. return ERR_PTR(retval);
  1316. }
  1317. return skb;
  1318. }
  1319. /* Called with RTNL lock or RCU read lock. */
  1320. static struct vport *lookup_vport(struct net *net,
  1321. struct ovs_header *ovs_header,
  1322. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1323. {
  1324. struct datapath *dp;
  1325. struct vport *vport;
  1326. if (a[OVS_VPORT_ATTR_NAME]) {
  1327. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1328. if (!vport)
  1329. return ERR_PTR(-ENODEV);
  1330. if (ovs_header->dp_ifindex &&
  1331. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1332. return ERR_PTR(-ENODEV);
  1333. return vport;
  1334. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1335. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1336. if (port_no >= DP_MAX_PORTS)
  1337. return ERR_PTR(-EFBIG);
  1338. dp = get_dp(net, ovs_header->dp_ifindex);
  1339. if (!dp)
  1340. return ERR_PTR(-ENODEV);
  1341. vport = ovs_vport_rtnl_rcu(dp, port_no);
  1342. if (!vport)
  1343. return ERR_PTR(-ENOENT);
  1344. return vport;
  1345. } else
  1346. return ERR_PTR(-EINVAL);
  1347. }
  1348. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1349. {
  1350. struct nlattr **a = info->attrs;
  1351. struct ovs_header *ovs_header = info->userhdr;
  1352. struct vport_parms parms;
  1353. struct sk_buff *reply;
  1354. struct vport *vport;
  1355. struct datapath *dp;
  1356. u32 port_no;
  1357. int err;
  1358. err = -EINVAL;
  1359. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1360. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1361. goto exit;
  1362. rtnl_lock();
  1363. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1364. err = -ENODEV;
  1365. if (!dp)
  1366. goto exit_unlock;
  1367. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1368. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1369. err = -EFBIG;
  1370. if (port_no >= DP_MAX_PORTS)
  1371. goto exit_unlock;
  1372. vport = ovs_vport_rtnl_rcu(dp, port_no);
  1373. err = -EBUSY;
  1374. if (vport)
  1375. goto exit_unlock;
  1376. } else {
  1377. for (port_no = 1; ; port_no++) {
  1378. if (port_no >= DP_MAX_PORTS) {
  1379. err = -EFBIG;
  1380. goto exit_unlock;
  1381. }
  1382. vport = ovs_vport_rtnl(dp, port_no);
  1383. if (!vport)
  1384. break;
  1385. }
  1386. }
  1387. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1388. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1389. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1390. parms.dp = dp;
  1391. parms.port_no = port_no;
  1392. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1393. vport = new_vport(&parms);
  1394. err = PTR_ERR(vport);
  1395. if (IS_ERR(vport))
  1396. goto exit_unlock;
  1397. err = 0;
  1398. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1399. OVS_VPORT_CMD_NEW);
  1400. if (IS_ERR(reply)) {
  1401. err = PTR_ERR(reply);
  1402. ovs_dp_detach_port(vport);
  1403. goto exit_unlock;
  1404. }
  1405. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1406. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1407. exit_unlock:
  1408. rtnl_unlock();
  1409. exit:
  1410. return err;
  1411. }
  1412. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1413. {
  1414. struct nlattr **a = info->attrs;
  1415. struct sk_buff *reply;
  1416. struct vport *vport;
  1417. int err;
  1418. rtnl_lock();
  1419. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1420. err = PTR_ERR(vport);
  1421. if (IS_ERR(vport))
  1422. goto exit_unlock;
  1423. err = 0;
  1424. if (a[OVS_VPORT_ATTR_TYPE] &&
  1425. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1426. err = -EINVAL;
  1427. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1428. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1429. if (err)
  1430. goto exit_unlock;
  1431. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1432. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1433. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1434. OVS_VPORT_CMD_NEW);
  1435. if (IS_ERR(reply)) {
  1436. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1437. ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
  1438. goto exit_unlock;
  1439. }
  1440. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1441. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1442. exit_unlock:
  1443. rtnl_unlock();
  1444. return err;
  1445. }
  1446. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1447. {
  1448. struct nlattr **a = info->attrs;
  1449. struct sk_buff *reply;
  1450. struct vport *vport;
  1451. int err;
  1452. rtnl_lock();
  1453. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1454. err = PTR_ERR(vport);
  1455. if (IS_ERR(vport))
  1456. goto exit_unlock;
  1457. if (vport->port_no == OVSP_LOCAL) {
  1458. err = -EINVAL;
  1459. goto exit_unlock;
  1460. }
  1461. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1462. OVS_VPORT_CMD_DEL);
  1463. err = PTR_ERR(reply);
  1464. if (IS_ERR(reply))
  1465. goto exit_unlock;
  1466. err = 0;
  1467. ovs_dp_detach_port(vport);
  1468. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1469. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1470. exit_unlock:
  1471. rtnl_unlock();
  1472. return err;
  1473. }
  1474. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1475. {
  1476. struct nlattr **a = info->attrs;
  1477. struct ovs_header *ovs_header = info->userhdr;
  1478. struct sk_buff *reply;
  1479. struct vport *vport;
  1480. int err;
  1481. rcu_read_lock();
  1482. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1483. err = PTR_ERR(vport);
  1484. if (IS_ERR(vport))
  1485. goto exit_unlock;
  1486. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1487. OVS_VPORT_CMD_NEW);
  1488. err = PTR_ERR(reply);
  1489. if (IS_ERR(reply))
  1490. goto exit_unlock;
  1491. rcu_read_unlock();
  1492. return genlmsg_reply(reply, info);
  1493. exit_unlock:
  1494. rcu_read_unlock();
  1495. return err;
  1496. }
  1497. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1498. {
  1499. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1500. struct datapath *dp;
  1501. int bucket = cb->args[0], skip = cb->args[1];
  1502. int i, j = 0;
  1503. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1504. if (!dp)
  1505. return -ENODEV;
  1506. rcu_read_lock();
  1507. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1508. struct vport *vport;
  1509. struct hlist_node *n;
  1510. j = 0;
  1511. hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
  1512. if (j >= skip &&
  1513. ovs_vport_cmd_fill_info(vport, skb,
  1514. NETLINK_CB(cb->skb).portid,
  1515. cb->nlh->nlmsg_seq,
  1516. NLM_F_MULTI,
  1517. OVS_VPORT_CMD_NEW) < 0)
  1518. goto out;
  1519. j++;
  1520. }
  1521. skip = 0;
  1522. }
  1523. out:
  1524. rcu_read_unlock();
  1525. cb->args[0] = i;
  1526. cb->args[1] = j;
  1527. return skb->len;
  1528. }
  1529. static struct genl_ops dp_vport_genl_ops[] = {
  1530. { .cmd = OVS_VPORT_CMD_NEW,
  1531. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1532. .policy = vport_policy,
  1533. .doit = ovs_vport_cmd_new
  1534. },
  1535. { .cmd = OVS_VPORT_CMD_DEL,
  1536. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1537. .policy = vport_policy,
  1538. .doit = ovs_vport_cmd_del
  1539. },
  1540. { .cmd = OVS_VPORT_CMD_GET,
  1541. .flags = 0, /* OK for unprivileged users. */
  1542. .policy = vport_policy,
  1543. .doit = ovs_vport_cmd_get,
  1544. .dumpit = ovs_vport_cmd_dump
  1545. },
  1546. { .cmd = OVS_VPORT_CMD_SET,
  1547. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1548. .policy = vport_policy,
  1549. .doit = ovs_vport_cmd_set,
  1550. },
  1551. };
  1552. struct genl_family_and_ops {
  1553. struct genl_family *family;
  1554. struct genl_ops *ops;
  1555. int n_ops;
  1556. struct genl_multicast_group *group;
  1557. };
  1558. static const struct genl_family_and_ops dp_genl_families[] = {
  1559. { &dp_datapath_genl_family,
  1560. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1561. &ovs_dp_datapath_multicast_group },
  1562. { &dp_vport_genl_family,
  1563. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1564. &ovs_dp_vport_multicast_group },
  1565. { &dp_flow_genl_family,
  1566. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1567. &ovs_dp_flow_multicast_group },
  1568. { &dp_packet_genl_family,
  1569. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1570. NULL },
  1571. };
  1572. static void dp_unregister_genl(int n_families)
  1573. {
  1574. int i;
  1575. for (i = 0; i < n_families; i++)
  1576. genl_unregister_family(dp_genl_families[i].family);
  1577. }
  1578. static int dp_register_genl(void)
  1579. {
  1580. int n_registered;
  1581. int err;
  1582. int i;
  1583. n_registered = 0;
  1584. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1585. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1586. err = genl_register_family_with_ops(f->family, f->ops,
  1587. f->n_ops);
  1588. if (err)
  1589. goto error;
  1590. n_registered++;
  1591. if (f->group) {
  1592. err = genl_register_mc_group(f->family, f->group);
  1593. if (err)
  1594. goto error;
  1595. }
  1596. }
  1597. return 0;
  1598. error:
  1599. dp_unregister_genl(n_registered);
  1600. return err;
  1601. }
  1602. static void rehash_flow_table(struct work_struct *work)
  1603. {
  1604. struct datapath *dp;
  1605. struct net *net;
  1606. genl_lock();
  1607. rtnl_lock();
  1608. for_each_net(net) {
  1609. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1610. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1611. struct flow_table *old_table = genl_dereference(dp->table);
  1612. struct flow_table *new_table;
  1613. new_table = ovs_flow_tbl_rehash(old_table);
  1614. if (!IS_ERR(new_table)) {
  1615. rcu_assign_pointer(dp->table, new_table);
  1616. ovs_flow_tbl_deferred_destroy(old_table);
  1617. }
  1618. }
  1619. }
  1620. rtnl_unlock();
  1621. genl_unlock();
  1622. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1623. }
  1624. static int __net_init ovs_init_net(struct net *net)
  1625. {
  1626. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1627. INIT_LIST_HEAD(&ovs_net->dps);
  1628. return 0;
  1629. }
  1630. static void __net_exit ovs_exit_net(struct net *net)
  1631. {
  1632. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1633. struct datapath *dp, *dp_next;
  1634. genl_lock();
  1635. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1636. __dp_destroy(dp);
  1637. genl_unlock();
  1638. }
  1639. static struct pernet_operations ovs_net_ops = {
  1640. .init = ovs_init_net,
  1641. .exit = ovs_exit_net,
  1642. .id = &ovs_net_id,
  1643. .size = sizeof(struct ovs_net),
  1644. };
  1645. static int __init dp_init(void)
  1646. {
  1647. struct sk_buff *dummy_skb;
  1648. int err;
  1649. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
  1650. pr_info("Open vSwitch switching datapath\n");
  1651. err = ovs_flow_init();
  1652. if (err)
  1653. goto error;
  1654. err = ovs_vport_init();
  1655. if (err)
  1656. goto error_flow_exit;
  1657. err = register_pernet_device(&ovs_net_ops);
  1658. if (err)
  1659. goto error_vport_exit;
  1660. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1661. if (err)
  1662. goto error_netns_exit;
  1663. err = dp_register_genl();
  1664. if (err < 0)
  1665. goto error_unreg_notifier;
  1666. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1667. return 0;
  1668. error_unreg_notifier:
  1669. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1670. error_netns_exit:
  1671. unregister_pernet_device(&ovs_net_ops);
  1672. error_vport_exit:
  1673. ovs_vport_exit();
  1674. error_flow_exit:
  1675. ovs_flow_exit();
  1676. error:
  1677. return err;
  1678. }
  1679. static void dp_cleanup(void)
  1680. {
  1681. cancel_delayed_work_sync(&rehash_flow_wq);
  1682. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1683. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1684. unregister_pernet_device(&ovs_net_ops);
  1685. rcu_barrier();
  1686. ovs_vport_exit();
  1687. ovs_flow_exit();
  1688. }
  1689. module_init(dp_init);
  1690. module_exit(dp_cleanup);
  1691. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1692. MODULE_LICENSE("GPL");