datapath.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  1. /*
  2. * Copyright (c) 2007-2014 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/genetlink.h>
  49. #include <net/genetlink.h>
  50. #include <net/genetlink.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #include "datapath.h"
  54. #include "flow.h"
  55. #include "flow_table.h"
  56. #include "flow_netlink.h"
  57. #include "vport-internal_dev.h"
  58. #include "vport-netdev.h"
  59. int ovs_net_id __read_mostly;
  60. static struct genl_family dp_packet_genl_family;
  61. static struct genl_family dp_flow_genl_family;
  62. static struct genl_family dp_datapath_genl_family;
  63. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  64. .name = OVS_FLOW_MCGROUP,
  65. };
  66. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  67. .name = OVS_DATAPATH_MCGROUP,
  68. };
  69. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  70. .name = OVS_VPORT_MCGROUP,
  71. };
  72. /* Check if need to build a reply message.
  73. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  74. static bool ovs_must_notify(struct genl_info *info,
  75. const struct genl_multicast_group *grp)
  76. {
  77. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  78. netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
  79. }
  80. static void ovs_notify(struct genl_family *family,
  81. struct sk_buff *skb, struct genl_info *info)
  82. {
  83. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  84. 0, info->nlhdr, GFP_KERNEL);
  85. }
  86. /**
  87. * DOC: Locking:
  88. *
  89. * All writes e.g. Writes to device state (add/remove datapath, port, set
  90. * operations on vports, etc.), Writes to other state (flow table
  91. * modifications, set miscellaneous datapath parameters, etc.) are protected
  92. * by ovs_lock.
  93. *
  94. * Reads are protected by RCU.
  95. *
  96. * There are a few special cases (mostly stats) that have their own
  97. * synchronization but they nest under all of above and don't interact with
  98. * each other.
  99. *
  100. * The RTNL lock nests inside ovs_mutex.
  101. */
  102. static DEFINE_MUTEX(ovs_mutex);
  103. void ovs_lock(void)
  104. {
  105. mutex_lock(&ovs_mutex);
  106. }
  107. void ovs_unlock(void)
  108. {
  109. mutex_unlock(&ovs_mutex);
  110. }
  111. #ifdef CONFIG_LOCKDEP
  112. int lockdep_ovsl_is_held(void)
  113. {
  114. if (debug_locks)
  115. return lockdep_is_held(&ovs_mutex);
  116. else
  117. return 1;
  118. }
  119. #endif
  120. static struct vport *new_vport(const struct vport_parms *);
  121. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  122. const struct dp_upcall_info *);
  123. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  124. const struct dp_upcall_info *);
  125. /* Must be called with rcu_read_lock or ovs_mutex. */
  126. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  127. {
  128. struct datapath *dp = NULL;
  129. struct net_device *dev;
  130. rcu_read_lock();
  131. dev = dev_get_by_index_rcu(net, dp_ifindex);
  132. if (dev) {
  133. struct vport *vport = ovs_internal_dev_get_vport(dev);
  134. if (vport)
  135. dp = vport->dp;
  136. }
  137. rcu_read_unlock();
  138. return dp;
  139. }
  140. /* Must be called with rcu_read_lock or ovs_mutex. */
  141. static const char *ovs_dp_name(const struct datapath *dp)
  142. {
  143. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  144. return vport->ops->get_name(vport);
  145. }
  146. static int get_dpifindex(struct datapath *dp)
  147. {
  148. struct vport *local;
  149. int ifindex;
  150. rcu_read_lock();
  151. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  152. if (local)
  153. ifindex = netdev_vport_priv(local)->dev->ifindex;
  154. else
  155. ifindex = 0;
  156. rcu_read_unlock();
  157. return ifindex;
  158. }
  159. static void destroy_dp_rcu(struct rcu_head *rcu)
  160. {
  161. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  162. free_percpu(dp->stats_percpu);
  163. release_net(ovs_dp_get_net(dp));
  164. kfree(dp->ports);
  165. kfree(dp);
  166. }
  167. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  168. u16 port_no)
  169. {
  170. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  171. }
  172. /* Called with ovs_mutex or RCU read lock. */
  173. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  174. {
  175. struct vport *vport;
  176. struct hlist_head *head;
  177. head = vport_hash_bucket(dp, port_no);
  178. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  179. if (vport->port_no == port_no)
  180. return vport;
  181. }
  182. return NULL;
  183. }
  184. /* Called with ovs_mutex. */
  185. static struct vport *new_vport(const struct vport_parms *parms)
  186. {
  187. struct vport *vport;
  188. vport = ovs_vport_add(parms);
  189. if (!IS_ERR(vport)) {
  190. struct datapath *dp = parms->dp;
  191. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  192. hlist_add_head_rcu(&vport->dp_hash_node, head);
  193. }
  194. return vport;
  195. }
  196. void ovs_dp_detach_port(struct vport *p)
  197. {
  198. ASSERT_OVSL();
  199. /* First drop references to device. */
  200. hlist_del_rcu(&p->dp_hash_node);
  201. /* Then destroy it. */
  202. ovs_vport_del(p);
  203. }
  204. /* Must be called with rcu_read_lock. */
  205. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  206. {
  207. struct datapath *dp = p->dp;
  208. struct sw_flow *flow;
  209. struct dp_stats_percpu *stats;
  210. struct sw_flow_key key;
  211. u64 *stats_counter;
  212. u32 n_mask_hit;
  213. int error;
  214. stats = this_cpu_ptr(dp->stats_percpu);
  215. /* Extract flow from 'skb' into 'key'. */
  216. error = ovs_flow_extract(skb, p->port_no, &key);
  217. if (unlikely(error)) {
  218. kfree_skb(skb);
  219. return;
  220. }
  221. /* Look up flow. */
  222. flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
  223. if (unlikely(!flow)) {
  224. struct dp_upcall_info upcall;
  225. upcall.cmd = OVS_PACKET_CMD_MISS;
  226. upcall.key = &key;
  227. upcall.userdata = NULL;
  228. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  229. ovs_dp_upcall(dp, skb, &upcall);
  230. consume_skb(skb);
  231. stats_counter = &stats->n_missed;
  232. goto out;
  233. }
  234. OVS_CB(skb)->flow = flow;
  235. OVS_CB(skb)->pkt_key = &key;
  236. ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
  237. ovs_execute_actions(dp, skb);
  238. stats_counter = &stats->n_hit;
  239. out:
  240. /* Update datapath statistics. */
  241. u64_stats_update_begin(&stats->syncp);
  242. (*stats_counter)++;
  243. stats->n_mask_hit += n_mask_hit;
  244. u64_stats_update_end(&stats->syncp);
  245. }
  246. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  247. const struct dp_upcall_info *upcall_info)
  248. {
  249. struct dp_stats_percpu *stats;
  250. int err;
  251. if (upcall_info->portid == 0) {
  252. err = -ENOTCONN;
  253. goto err;
  254. }
  255. if (!skb_is_gso(skb))
  256. err = queue_userspace_packet(dp, skb, upcall_info);
  257. else
  258. err = queue_gso_packets(dp, skb, upcall_info);
  259. if (err)
  260. goto err;
  261. return 0;
  262. err:
  263. stats = this_cpu_ptr(dp->stats_percpu);
  264. u64_stats_update_begin(&stats->syncp);
  265. stats->n_lost++;
  266. u64_stats_update_end(&stats->syncp);
  267. return err;
  268. }
  269. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  270. const struct dp_upcall_info *upcall_info)
  271. {
  272. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  273. struct dp_upcall_info later_info;
  274. struct sw_flow_key later_key;
  275. struct sk_buff *segs, *nskb;
  276. int err;
  277. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  278. if (IS_ERR(segs))
  279. return PTR_ERR(segs);
  280. /* Queue all of the segments. */
  281. skb = segs;
  282. do {
  283. err = queue_userspace_packet(dp, skb, upcall_info);
  284. if (err)
  285. break;
  286. if (skb == segs && gso_type & SKB_GSO_UDP) {
  287. /* The initial flow key extracted by ovs_flow_extract()
  288. * in this case is for a first fragment, so we need to
  289. * properly mark later fragments.
  290. */
  291. later_key = *upcall_info->key;
  292. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  293. later_info = *upcall_info;
  294. later_info.key = &later_key;
  295. upcall_info = &later_info;
  296. }
  297. } while ((skb = skb->next));
  298. /* Free all of the segments. */
  299. skb = segs;
  300. do {
  301. nskb = skb->next;
  302. if (err)
  303. kfree_skb(skb);
  304. else
  305. consume_skb(skb);
  306. } while ((skb = nskb));
  307. return err;
  308. }
  309. static size_t key_attr_size(void)
  310. {
  311. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  312. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  313. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  314. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  315. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  316. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  317. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  318. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  319. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  320. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  321. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  322. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  323. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  324. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  325. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  326. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  327. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  328. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  329. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  330. }
  331. static size_t upcall_msg_size(const struct nlattr *userdata,
  332. unsigned int hdrlen)
  333. {
  334. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  335. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  336. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  337. /* OVS_PACKET_ATTR_USERDATA */
  338. if (userdata)
  339. size += NLA_ALIGN(userdata->nla_len);
  340. return size;
  341. }
  342. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  343. const struct dp_upcall_info *upcall_info)
  344. {
  345. struct ovs_header *upcall;
  346. struct sk_buff *nskb = NULL;
  347. struct sk_buff *user_skb; /* to be queued to userspace */
  348. struct nlattr *nla;
  349. struct genl_info info = {
  350. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  351. .snd_portid = upcall_info->portid,
  352. };
  353. size_t len;
  354. unsigned int hlen;
  355. int err, dp_ifindex;
  356. dp_ifindex = get_dpifindex(dp);
  357. if (!dp_ifindex)
  358. return -ENODEV;
  359. if (vlan_tx_tag_present(skb)) {
  360. nskb = skb_clone(skb, GFP_ATOMIC);
  361. if (!nskb)
  362. return -ENOMEM;
  363. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  364. if (!nskb)
  365. return -ENOMEM;
  366. nskb->vlan_tci = 0;
  367. skb = nskb;
  368. }
  369. if (nla_attr_size(skb->len) > USHRT_MAX) {
  370. err = -EFBIG;
  371. goto out;
  372. }
  373. /* Complete checksum if needed */
  374. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  375. (err = skb_checksum_help(skb)))
  376. goto out;
  377. /* Older versions of OVS user space enforce alignment of the last
  378. * Netlink attribute to NLA_ALIGNTO which would require extensive
  379. * padding logic. Only perform zerocopy if padding is not required.
  380. */
  381. if (dp->user_features & OVS_DP_F_UNALIGNED)
  382. hlen = skb_zerocopy_headlen(skb);
  383. else
  384. hlen = skb->len;
  385. len = upcall_msg_size(upcall_info->userdata, hlen);
  386. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  387. if (!user_skb) {
  388. err = -ENOMEM;
  389. goto out;
  390. }
  391. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  392. 0, upcall_info->cmd);
  393. upcall->dp_ifindex = dp_ifindex;
  394. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  395. err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  396. BUG_ON(err);
  397. nla_nest_end(user_skb, nla);
  398. if (upcall_info->userdata)
  399. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  400. nla_len(upcall_info->userdata),
  401. nla_data(upcall_info->userdata));
  402. /* Only reserve room for attribute header, packet data is added
  403. * in skb_zerocopy() */
  404. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  405. err = -ENOBUFS;
  406. goto out;
  407. }
  408. nla->nla_len = nla_attr_size(skb->len);
  409. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  410. if (err)
  411. goto out;
  412. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  413. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  414. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  415. if (plen > 0)
  416. memset(skb_put(user_skb, plen), 0, plen);
  417. }
  418. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  419. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  420. out:
  421. if (err)
  422. skb_tx_error(skb);
  423. kfree_skb(nskb);
  424. return err;
  425. }
  426. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  427. {
  428. struct ovs_header *ovs_header = info->userhdr;
  429. struct nlattr **a = info->attrs;
  430. struct sw_flow_actions *acts;
  431. struct sk_buff *packet;
  432. struct sw_flow *flow;
  433. struct datapath *dp;
  434. struct ethhdr *eth;
  435. int len;
  436. int err;
  437. err = -EINVAL;
  438. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  439. !a[OVS_PACKET_ATTR_ACTIONS])
  440. goto err;
  441. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  442. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  443. err = -ENOMEM;
  444. if (!packet)
  445. goto err;
  446. skb_reserve(packet, NET_IP_ALIGN);
  447. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  448. skb_reset_mac_header(packet);
  449. eth = eth_hdr(packet);
  450. /* Normally, setting the skb 'protocol' field would be handled by a
  451. * call to eth_type_trans(), but it assumes there's a sending
  452. * device, which we may not have. */
  453. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  454. packet->protocol = eth->h_proto;
  455. else
  456. packet->protocol = htons(ETH_P_802_2);
  457. /* Build an sw_flow for sending this packet. */
  458. flow = ovs_flow_alloc();
  459. err = PTR_ERR(flow);
  460. if (IS_ERR(flow))
  461. goto err_kfree_skb;
  462. err = ovs_flow_extract(packet, -1, &flow->key);
  463. if (err)
  464. goto err_flow_free;
  465. err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
  466. if (err)
  467. goto err_flow_free;
  468. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  469. err = PTR_ERR(acts);
  470. if (IS_ERR(acts))
  471. goto err_flow_free;
  472. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  473. &flow->key, 0, &acts);
  474. rcu_assign_pointer(flow->sf_acts, acts);
  475. if (err)
  476. goto err_flow_free;
  477. OVS_CB(packet)->flow = flow;
  478. OVS_CB(packet)->pkt_key = &flow->key;
  479. packet->priority = flow->key.phy.priority;
  480. packet->mark = flow->key.phy.skb_mark;
  481. rcu_read_lock();
  482. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  483. err = -ENODEV;
  484. if (!dp)
  485. goto err_unlock;
  486. local_bh_disable();
  487. err = ovs_execute_actions(dp, packet);
  488. local_bh_enable();
  489. rcu_read_unlock();
  490. ovs_flow_free(flow, false);
  491. return err;
  492. err_unlock:
  493. rcu_read_unlock();
  494. err_flow_free:
  495. ovs_flow_free(flow, false);
  496. err_kfree_skb:
  497. kfree_skb(packet);
  498. err:
  499. return err;
  500. }
  501. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  502. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  503. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  504. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  505. };
  506. static const struct genl_ops dp_packet_genl_ops[] = {
  507. { .cmd = OVS_PACKET_CMD_EXECUTE,
  508. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  509. .policy = packet_policy,
  510. .doit = ovs_packet_cmd_execute
  511. }
  512. };
  513. static struct genl_family dp_packet_genl_family = {
  514. .id = GENL_ID_GENERATE,
  515. .hdrsize = sizeof(struct ovs_header),
  516. .name = OVS_PACKET_FAMILY,
  517. .version = OVS_PACKET_VERSION,
  518. .maxattr = OVS_PACKET_ATTR_MAX,
  519. .netnsok = true,
  520. .parallel_ops = true,
  521. .ops = dp_packet_genl_ops,
  522. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  523. };
  524. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  525. struct ovs_dp_megaflow_stats *mega_stats)
  526. {
  527. int i;
  528. memset(mega_stats, 0, sizeof(*mega_stats));
  529. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  530. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  531. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  532. for_each_possible_cpu(i) {
  533. const struct dp_stats_percpu *percpu_stats;
  534. struct dp_stats_percpu local_stats;
  535. unsigned int start;
  536. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  537. do {
  538. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  539. local_stats = *percpu_stats;
  540. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  541. stats->n_hit += local_stats.n_hit;
  542. stats->n_missed += local_stats.n_missed;
  543. stats->n_lost += local_stats.n_lost;
  544. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  545. }
  546. }
  547. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  548. {
  549. return NLMSG_ALIGN(sizeof(struct ovs_header))
  550. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  551. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  552. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  553. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  554. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  555. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  556. }
  557. /* Called with ovs_mutex or RCU read lock. */
  558. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  559. struct sk_buff *skb, u32 portid,
  560. u32 seq, u32 flags, u8 cmd)
  561. {
  562. const int skb_orig_len = skb->len;
  563. struct nlattr *start;
  564. struct ovs_flow_stats stats;
  565. __be16 tcp_flags;
  566. unsigned long used;
  567. struct ovs_header *ovs_header;
  568. struct nlattr *nla;
  569. int err;
  570. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  571. if (!ovs_header)
  572. return -EMSGSIZE;
  573. ovs_header->dp_ifindex = dp_ifindex;
  574. /* Fill flow key. */
  575. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  576. if (!nla)
  577. goto nla_put_failure;
  578. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  579. if (err)
  580. goto error;
  581. nla_nest_end(skb, nla);
  582. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  583. if (!nla)
  584. goto nla_put_failure;
  585. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  586. if (err)
  587. goto error;
  588. nla_nest_end(skb, nla);
  589. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  590. if (used &&
  591. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  592. goto nla_put_failure;
  593. if (stats.n_packets &&
  594. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  595. goto nla_put_failure;
  596. if ((u8)ntohs(tcp_flags) &&
  597. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  598. goto nla_put_failure;
  599. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  600. * this is the first flow to be dumped into 'skb'. This is unusual for
  601. * Netlink but individual action lists can be longer than
  602. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  603. * The userspace caller can always fetch the actions separately if it
  604. * really wants them. (Most userspace callers in fact don't care.)
  605. *
  606. * This can only fail for dump operations because the skb is always
  607. * properly sized for single flows.
  608. */
  609. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  610. if (start) {
  611. const struct sw_flow_actions *sf_acts;
  612. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  613. err = ovs_nla_put_actions(sf_acts->actions,
  614. sf_acts->actions_len, skb);
  615. if (!err)
  616. nla_nest_end(skb, start);
  617. else {
  618. if (skb_orig_len)
  619. goto error;
  620. nla_nest_cancel(skb, start);
  621. }
  622. } else if (skb_orig_len)
  623. goto nla_put_failure;
  624. return genlmsg_end(skb, ovs_header);
  625. nla_put_failure:
  626. err = -EMSGSIZE;
  627. error:
  628. genlmsg_cancel(skb, ovs_header);
  629. return err;
  630. }
  631. /* May not be called with RCU read lock. */
  632. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  633. struct genl_info *info,
  634. bool always)
  635. {
  636. struct sk_buff *skb;
  637. if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
  638. return NULL;
  639. skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
  640. if (!skb)
  641. return ERR_PTR(-ENOMEM);
  642. return skb;
  643. }
  644. /* Called with ovs_mutex. */
  645. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  646. int dp_ifindex,
  647. struct genl_info *info, u8 cmd,
  648. bool always)
  649. {
  650. struct sk_buff *skb;
  651. int retval;
  652. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
  653. always);
  654. if (IS_ERR_OR_NULL(skb))
  655. return skb;
  656. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  657. info->snd_portid, info->snd_seq, 0,
  658. cmd);
  659. BUG_ON(retval < 0);
  660. return skb;
  661. }
  662. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  663. {
  664. struct nlattr **a = info->attrs;
  665. struct ovs_header *ovs_header = info->userhdr;
  666. struct sw_flow *flow, *new_flow;
  667. struct sw_flow_mask mask;
  668. struct sk_buff *reply;
  669. struct datapath *dp;
  670. struct sw_flow_actions *acts;
  671. struct sw_flow_match match;
  672. int error;
  673. /* Must have key and actions. */
  674. error = -EINVAL;
  675. if (!a[OVS_FLOW_ATTR_KEY])
  676. goto error;
  677. if (!a[OVS_FLOW_ATTR_ACTIONS])
  678. goto error;
  679. /* Most of the time we need to allocate a new flow, do it before
  680. * locking.
  681. */
  682. new_flow = ovs_flow_alloc();
  683. if (IS_ERR(new_flow)) {
  684. error = PTR_ERR(new_flow);
  685. goto error;
  686. }
  687. /* Extract key. */
  688. ovs_match_init(&match, &new_flow->unmasked_key, &mask);
  689. error = ovs_nla_get_match(&match,
  690. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  691. if (error)
  692. goto err_kfree_flow;
  693. ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
  694. /* Validate actions. */
  695. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  696. error = PTR_ERR(acts);
  697. if (IS_ERR(acts))
  698. goto err_kfree_flow;
  699. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  700. 0, &acts);
  701. if (error) {
  702. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  703. goto err_kfree_acts;
  704. }
  705. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  706. if (IS_ERR(reply)) {
  707. error = PTR_ERR(reply);
  708. goto err_kfree_acts;
  709. }
  710. ovs_lock();
  711. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  712. if (unlikely(!dp)) {
  713. error = -ENODEV;
  714. goto err_unlock_ovs;
  715. }
  716. /* Check if this is a duplicate flow */
  717. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
  718. if (likely(!flow)) {
  719. rcu_assign_pointer(new_flow->sf_acts, acts);
  720. /* Put flow in bucket. */
  721. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  722. if (unlikely(error)) {
  723. acts = NULL;
  724. goto err_unlock_ovs;
  725. }
  726. if (unlikely(reply)) {
  727. error = ovs_flow_cmd_fill_info(new_flow,
  728. ovs_header->dp_ifindex,
  729. reply, info->snd_portid,
  730. info->snd_seq, 0,
  731. OVS_FLOW_CMD_NEW);
  732. BUG_ON(error < 0);
  733. }
  734. ovs_unlock();
  735. } else {
  736. struct sw_flow_actions *old_acts;
  737. /* Bail out if we're not allowed to modify an existing flow.
  738. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  739. * because Generic Netlink treats the latter as a dump
  740. * request. We also accept NLM_F_EXCL in case that bug ever
  741. * gets fixed.
  742. */
  743. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  744. | NLM_F_EXCL))) {
  745. error = -EEXIST;
  746. goto err_unlock_ovs;
  747. }
  748. /* The unmasked key has to be the same for flow updates. */
  749. if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
  750. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  751. if (!flow) {
  752. error = -ENOENT;
  753. goto err_unlock_ovs;
  754. }
  755. }
  756. /* Update actions. */
  757. old_acts = ovsl_dereference(flow->sf_acts);
  758. rcu_assign_pointer(flow->sf_acts, acts);
  759. if (unlikely(reply)) {
  760. error = ovs_flow_cmd_fill_info(flow,
  761. ovs_header->dp_ifindex,
  762. reply, info->snd_portid,
  763. info->snd_seq, 0,
  764. OVS_FLOW_CMD_NEW);
  765. BUG_ON(error < 0);
  766. }
  767. ovs_unlock();
  768. ovs_nla_free_flow_actions(old_acts);
  769. ovs_flow_free(new_flow, false);
  770. }
  771. if (reply)
  772. ovs_notify(&dp_flow_genl_family, reply, info);
  773. return 0;
  774. err_unlock_ovs:
  775. ovs_unlock();
  776. kfree_skb(reply);
  777. err_kfree_acts:
  778. kfree(acts);
  779. err_kfree_flow:
  780. ovs_flow_free(new_flow, false);
  781. error:
  782. return error;
  783. }
  784. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  785. {
  786. struct nlattr **a = info->attrs;
  787. struct ovs_header *ovs_header = info->userhdr;
  788. struct sw_flow_key key, masked_key;
  789. struct sw_flow *flow;
  790. struct sw_flow_mask mask;
  791. struct sk_buff *reply = NULL;
  792. struct datapath *dp;
  793. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  794. struct sw_flow_match match;
  795. int error;
  796. /* Extract key. */
  797. error = -EINVAL;
  798. if (!a[OVS_FLOW_ATTR_KEY])
  799. goto error;
  800. ovs_match_init(&match, &key, &mask);
  801. error = ovs_nla_get_match(&match,
  802. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  803. if (error)
  804. goto error;
  805. /* Validate actions. */
  806. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  807. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  808. error = PTR_ERR(acts);
  809. if (IS_ERR(acts))
  810. goto error;
  811. ovs_flow_mask_key(&masked_key, &key, &mask);
  812. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
  813. &masked_key, 0, &acts);
  814. if (error) {
  815. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  816. goto err_kfree_acts;
  817. }
  818. }
  819. /* Can allocate before locking if have acts. */
  820. if (acts) {
  821. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  822. if (IS_ERR(reply)) {
  823. error = PTR_ERR(reply);
  824. goto err_kfree_acts;
  825. }
  826. }
  827. ovs_lock();
  828. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  829. if (unlikely(!dp)) {
  830. error = -ENODEV;
  831. goto err_unlock_ovs;
  832. }
  833. /* Check that the flow exists. */
  834. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  835. if (unlikely(!flow)) {
  836. error = -ENOENT;
  837. goto err_unlock_ovs;
  838. }
  839. /* Update actions, if present. */
  840. if (likely(acts)) {
  841. old_acts = ovsl_dereference(flow->sf_acts);
  842. rcu_assign_pointer(flow->sf_acts, acts);
  843. if (unlikely(reply)) {
  844. error = ovs_flow_cmd_fill_info(flow,
  845. ovs_header->dp_ifindex,
  846. reply, info->snd_portid,
  847. info->snd_seq, 0,
  848. OVS_FLOW_CMD_NEW);
  849. BUG_ON(error < 0);
  850. }
  851. } else {
  852. /* Could not alloc without acts before locking. */
  853. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  854. info, OVS_FLOW_CMD_NEW, false);
  855. if (unlikely(IS_ERR(reply))) {
  856. error = PTR_ERR(reply);
  857. goto err_unlock_ovs;
  858. }
  859. }
  860. /* Clear stats. */
  861. if (a[OVS_FLOW_ATTR_CLEAR])
  862. ovs_flow_stats_clear(flow);
  863. ovs_unlock();
  864. if (reply)
  865. ovs_notify(&dp_flow_genl_family, reply, info);
  866. if (old_acts)
  867. ovs_nla_free_flow_actions(old_acts);
  868. return 0;
  869. err_unlock_ovs:
  870. ovs_unlock();
  871. kfree_skb(reply);
  872. err_kfree_acts:
  873. kfree(acts);
  874. error:
  875. return error;
  876. }
  877. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  878. {
  879. struct nlattr **a = info->attrs;
  880. struct ovs_header *ovs_header = info->userhdr;
  881. struct sw_flow_key key;
  882. struct sk_buff *reply;
  883. struct sw_flow *flow;
  884. struct datapath *dp;
  885. struct sw_flow_match match;
  886. int err;
  887. if (!a[OVS_FLOW_ATTR_KEY]) {
  888. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  889. return -EINVAL;
  890. }
  891. ovs_match_init(&match, &key, NULL);
  892. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  893. if (err)
  894. return err;
  895. ovs_lock();
  896. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  897. if (!dp) {
  898. err = -ENODEV;
  899. goto unlock;
  900. }
  901. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  902. if (!flow) {
  903. err = -ENOENT;
  904. goto unlock;
  905. }
  906. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  907. OVS_FLOW_CMD_NEW, true);
  908. if (IS_ERR(reply)) {
  909. err = PTR_ERR(reply);
  910. goto unlock;
  911. }
  912. ovs_unlock();
  913. return genlmsg_reply(reply, info);
  914. unlock:
  915. ovs_unlock();
  916. return err;
  917. }
  918. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  919. {
  920. struct nlattr **a = info->attrs;
  921. struct ovs_header *ovs_header = info->userhdr;
  922. struct sw_flow_key key;
  923. struct sk_buff *reply;
  924. struct sw_flow *flow;
  925. struct datapath *dp;
  926. struct sw_flow_match match;
  927. int err;
  928. if (likely(a[OVS_FLOW_ATTR_KEY])) {
  929. ovs_match_init(&match, &key, NULL);
  930. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  931. if (unlikely(err))
  932. return err;
  933. }
  934. ovs_lock();
  935. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  936. if (unlikely(!dp)) {
  937. err = -ENODEV;
  938. goto unlock;
  939. }
  940. if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
  941. err = ovs_flow_tbl_flush(&dp->table);
  942. goto unlock;
  943. }
  944. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  945. if (unlikely(!flow)) {
  946. err = -ENOENT;
  947. goto unlock;
  948. }
  949. ovs_flow_tbl_remove(&dp->table, flow);
  950. ovs_unlock();
  951. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  952. info, false);
  953. if (likely(reply)) {
  954. if (likely(!IS_ERR(reply))) {
  955. rcu_read_lock(); /*To keep RCU checker happy. */
  956. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  957. reply, info->snd_portid,
  958. info->snd_seq, 0,
  959. OVS_FLOW_CMD_DEL);
  960. rcu_read_unlock();
  961. BUG_ON(err < 0);
  962. ovs_notify(&dp_flow_genl_family, reply, info);
  963. } else {
  964. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  965. }
  966. }
  967. ovs_flow_free(flow, true);
  968. return 0;
  969. unlock:
  970. ovs_unlock();
  971. return err;
  972. }
  973. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  974. {
  975. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  976. struct table_instance *ti;
  977. struct datapath *dp;
  978. rcu_read_lock();
  979. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  980. if (!dp) {
  981. rcu_read_unlock();
  982. return -ENODEV;
  983. }
  984. ti = rcu_dereference(dp->table.ti);
  985. for (;;) {
  986. struct sw_flow *flow;
  987. u32 bucket, obj;
  988. bucket = cb->args[0];
  989. obj = cb->args[1];
  990. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  991. if (!flow)
  992. break;
  993. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  994. NETLINK_CB(cb->skb).portid,
  995. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  996. OVS_FLOW_CMD_NEW) < 0)
  997. break;
  998. cb->args[0] = bucket;
  999. cb->args[1] = obj;
  1000. }
  1001. rcu_read_unlock();
  1002. return skb->len;
  1003. }
  1004. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1005. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1006. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1007. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1008. };
  1009. static const struct genl_ops dp_flow_genl_ops[] = {
  1010. { .cmd = OVS_FLOW_CMD_NEW,
  1011. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1012. .policy = flow_policy,
  1013. .doit = ovs_flow_cmd_new
  1014. },
  1015. { .cmd = OVS_FLOW_CMD_DEL,
  1016. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1017. .policy = flow_policy,
  1018. .doit = ovs_flow_cmd_del
  1019. },
  1020. { .cmd = OVS_FLOW_CMD_GET,
  1021. .flags = 0, /* OK for unprivileged users. */
  1022. .policy = flow_policy,
  1023. .doit = ovs_flow_cmd_get,
  1024. .dumpit = ovs_flow_cmd_dump
  1025. },
  1026. { .cmd = OVS_FLOW_CMD_SET,
  1027. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1028. .policy = flow_policy,
  1029. .doit = ovs_flow_cmd_set,
  1030. },
  1031. };
  1032. static struct genl_family dp_flow_genl_family = {
  1033. .id = GENL_ID_GENERATE,
  1034. .hdrsize = sizeof(struct ovs_header),
  1035. .name = OVS_FLOW_FAMILY,
  1036. .version = OVS_FLOW_VERSION,
  1037. .maxattr = OVS_FLOW_ATTR_MAX,
  1038. .netnsok = true,
  1039. .parallel_ops = true,
  1040. .ops = dp_flow_genl_ops,
  1041. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1042. .mcgrps = &ovs_dp_flow_multicast_group,
  1043. .n_mcgrps = 1,
  1044. };
  1045. static size_t ovs_dp_cmd_msg_size(void)
  1046. {
  1047. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1048. msgsize += nla_total_size(IFNAMSIZ);
  1049. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1050. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1051. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1052. return msgsize;
  1053. }
  1054. /* Called with ovs_mutex or RCU read lock. */
  1055. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1056. u32 portid, u32 seq, u32 flags, u8 cmd)
  1057. {
  1058. struct ovs_header *ovs_header;
  1059. struct ovs_dp_stats dp_stats;
  1060. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1061. int err;
  1062. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1063. flags, cmd);
  1064. if (!ovs_header)
  1065. goto error;
  1066. ovs_header->dp_ifindex = get_dpifindex(dp);
  1067. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1068. if (err)
  1069. goto nla_put_failure;
  1070. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1071. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1072. &dp_stats))
  1073. goto nla_put_failure;
  1074. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1075. sizeof(struct ovs_dp_megaflow_stats),
  1076. &dp_megaflow_stats))
  1077. goto nla_put_failure;
  1078. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1079. goto nla_put_failure;
  1080. return genlmsg_end(skb, ovs_header);
  1081. nla_put_failure:
  1082. genlmsg_cancel(skb, ovs_header);
  1083. error:
  1084. return -EMSGSIZE;
  1085. }
  1086. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1087. {
  1088. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1089. }
  1090. /* Called with rcu_read_lock or ovs_mutex. */
  1091. static struct datapath *lookup_datapath(struct net *net,
  1092. struct ovs_header *ovs_header,
  1093. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1094. {
  1095. struct datapath *dp;
  1096. if (!a[OVS_DP_ATTR_NAME])
  1097. dp = get_dp(net, ovs_header->dp_ifindex);
  1098. else {
  1099. struct vport *vport;
  1100. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1101. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1102. }
  1103. return dp ? dp : ERR_PTR(-ENODEV);
  1104. }
  1105. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1106. {
  1107. struct datapath *dp;
  1108. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1109. if (IS_ERR(dp))
  1110. return;
  1111. WARN(dp->user_features, "Dropping previously announced user features\n");
  1112. dp->user_features = 0;
  1113. }
  1114. static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
  1115. {
  1116. if (a[OVS_DP_ATTR_USER_FEATURES])
  1117. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1118. }
  1119. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1120. {
  1121. struct nlattr **a = info->attrs;
  1122. struct vport_parms parms;
  1123. struct sk_buff *reply;
  1124. struct datapath *dp;
  1125. struct vport *vport;
  1126. struct ovs_net *ovs_net;
  1127. int err, i;
  1128. err = -EINVAL;
  1129. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1130. goto err;
  1131. reply = ovs_dp_cmd_alloc_info(info);
  1132. if (!reply)
  1133. return -ENOMEM;
  1134. err = -ENOMEM;
  1135. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1136. if (dp == NULL)
  1137. goto err_free_reply;
  1138. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1139. /* Allocate table. */
  1140. err = ovs_flow_tbl_init(&dp->table);
  1141. if (err)
  1142. goto err_free_dp;
  1143. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1144. if (!dp->stats_percpu) {
  1145. err = -ENOMEM;
  1146. goto err_destroy_table;
  1147. }
  1148. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1149. GFP_KERNEL);
  1150. if (!dp->ports) {
  1151. err = -ENOMEM;
  1152. goto err_destroy_percpu;
  1153. }
  1154. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1155. INIT_HLIST_HEAD(&dp->ports[i]);
  1156. /* Set up our datapath device. */
  1157. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1158. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1159. parms.options = NULL;
  1160. parms.dp = dp;
  1161. parms.port_no = OVSP_LOCAL;
  1162. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1163. ovs_dp_change(dp, a);
  1164. /* So far only local changes have been made, now need the lock. */
  1165. ovs_lock();
  1166. vport = new_vport(&parms);
  1167. if (IS_ERR(vport)) {
  1168. err = PTR_ERR(vport);
  1169. if (err == -EBUSY)
  1170. err = -EEXIST;
  1171. if (err == -EEXIST) {
  1172. /* An outdated user space instance that does not understand
  1173. * the concept of user_features has attempted to create a new
  1174. * datapath and is likely to reuse it. Drop all user features.
  1175. */
  1176. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1177. ovs_dp_reset_user_features(skb, info);
  1178. }
  1179. goto err_destroy_ports_array;
  1180. }
  1181. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1182. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1183. BUG_ON(err < 0);
  1184. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1185. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1186. ovs_unlock();
  1187. ovs_notify(&dp_datapath_genl_family, reply, info);
  1188. return 0;
  1189. err_destroy_ports_array:
  1190. ovs_unlock();
  1191. kfree(dp->ports);
  1192. err_destroy_percpu:
  1193. free_percpu(dp->stats_percpu);
  1194. err_destroy_table:
  1195. ovs_flow_tbl_destroy(&dp->table, false);
  1196. err_free_dp:
  1197. release_net(ovs_dp_get_net(dp));
  1198. kfree(dp);
  1199. err_free_reply:
  1200. kfree_skb(reply);
  1201. err:
  1202. return err;
  1203. }
  1204. /* Called with ovs_mutex. */
  1205. static void __dp_destroy(struct datapath *dp)
  1206. {
  1207. int i;
  1208. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1209. struct vport *vport;
  1210. struct hlist_node *n;
  1211. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1212. if (vport->port_no != OVSP_LOCAL)
  1213. ovs_dp_detach_port(vport);
  1214. }
  1215. list_del_rcu(&dp->list_node);
  1216. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1217. * all ports in datapath are destroyed first before freeing datapath.
  1218. */
  1219. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1220. /* RCU destroy the flow table */
  1221. ovs_flow_tbl_destroy(&dp->table, true);
  1222. call_rcu(&dp->rcu, destroy_dp_rcu);
  1223. }
  1224. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1225. {
  1226. struct sk_buff *reply;
  1227. struct datapath *dp;
  1228. int err;
  1229. reply = ovs_dp_cmd_alloc_info(info);
  1230. if (!reply)
  1231. return -ENOMEM;
  1232. ovs_lock();
  1233. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1234. err = PTR_ERR(dp);
  1235. if (IS_ERR(dp))
  1236. goto err_unlock_free;
  1237. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1238. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1239. BUG_ON(err < 0);
  1240. __dp_destroy(dp);
  1241. ovs_unlock();
  1242. ovs_notify(&dp_datapath_genl_family, reply, info);
  1243. return 0;
  1244. err_unlock_free:
  1245. ovs_unlock();
  1246. kfree_skb(reply);
  1247. return err;
  1248. }
  1249. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1250. {
  1251. struct sk_buff *reply;
  1252. struct datapath *dp;
  1253. int err;
  1254. reply = ovs_dp_cmd_alloc_info(info);
  1255. if (!reply)
  1256. return -ENOMEM;
  1257. ovs_lock();
  1258. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1259. err = PTR_ERR(dp);
  1260. if (IS_ERR(dp))
  1261. goto err_unlock_free;
  1262. ovs_dp_change(dp, info->attrs);
  1263. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1264. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1265. BUG_ON(err < 0);
  1266. ovs_unlock();
  1267. ovs_notify(&dp_datapath_genl_family, reply, info);
  1268. return 0;
  1269. err_unlock_free:
  1270. ovs_unlock();
  1271. kfree_skb(reply);
  1272. return err;
  1273. }
  1274. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1275. {
  1276. struct sk_buff *reply;
  1277. struct datapath *dp;
  1278. int err;
  1279. reply = ovs_dp_cmd_alloc_info(info);
  1280. if (!reply)
  1281. return -ENOMEM;
  1282. rcu_read_lock();
  1283. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1284. if (IS_ERR(dp)) {
  1285. err = PTR_ERR(dp);
  1286. goto err_unlock_free;
  1287. }
  1288. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1289. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1290. BUG_ON(err < 0);
  1291. rcu_read_unlock();
  1292. return genlmsg_reply(reply, info);
  1293. err_unlock_free:
  1294. rcu_read_unlock();
  1295. kfree_skb(reply);
  1296. return err;
  1297. }
  1298. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1299. {
  1300. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1301. struct datapath *dp;
  1302. int skip = cb->args[0];
  1303. int i = 0;
  1304. rcu_read_lock();
  1305. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1306. if (i >= skip &&
  1307. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1308. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1309. OVS_DP_CMD_NEW) < 0)
  1310. break;
  1311. i++;
  1312. }
  1313. rcu_read_unlock();
  1314. cb->args[0] = i;
  1315. return skb->len;
  1316. }
  1317. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1318. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1319. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1320. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1321. };
  1322. static const struct genl_ops dp_datapath_genl_ops[] = {
  1323. { .cmd = OVS_DP_CMD_NEW,
  1324. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1325. .policy = datapath_policy,
  1326. .doit = ovs_dp_cmd_new
  1327. },
  1328. { .cmd = OVS_DP_CMD_DEL,
  1329. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1330. .policy = datapath_policy,
  1331. .doit = ovs_dp_cmd_del
  1332. },
  1333. { .cmd = OVS_DP_CMD_GET,
  1334. .flags = 0, /* OK for unprivileged users. */
  1335. .policy = datapath_policy,
  1336. .doit = ovs_dp_cmd_get,
  1337. .dumpit = ovs_dp_cmd_dump
  1338. },
  1339. { .cmd = OVS_DP_CMD_SET,
  1340. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1341. .policy = datapath_policy,
  1342. .doit = ovs_dp_cmd_set,
  1343. },
  1344. };
  1345. static struct genl_family dp_datapath_genl_family = {
  1346. .id = GENL_ID_GENERATE,
  1347. .hdrsize = sizeof(struct ovs_header),
  1348. .name = OVS_DATAPATH_FAMILY,
  1349. .version = OVS_DATAPATH_VERSION,
  1350. .maxattr = OVS_DP_ATTR_MAX,
  1351. .netnsok = true,
  1352. .parallel_ops = true,
  1353. .ops = dp_datapath_genl_ops,
  1354. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1355. .mcgrps = &ovs_dp_datapath_multicast_group,
  1356. .n_mcgrps = 1,
  1357. };
  1358. /* Called with ovs_mutex or RCU read lock. */
  1359. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1360. u32 portid, u32 seq, u32 flags, u8 cmd)
  1361. {
  1362. struct ovs_header *ovs_header;
  1363. struct ovs_vport_stats vport_stats;
  1364. int err;
  1365. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1366. flags, cmd);
  1367. if (!ovs_header)
  1368. return -EMSGSIZE;
  1369. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1370. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1371. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1372. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1373. vport->ops->get_name(vport)))
  1374. goto nla_put_failure;
  1375. ovs_vport_get_stats(vport, &vport_stats);
  1376. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1377. &vport_stats))
  1378. goto nla_put_failure;
  1379. if (ovs_vport_get_upcall_portids(vport, skb))
  1380. goto nla_put_failure;
  1381. err = ovs_vport_get_options(vport, skb);
  1382. if (err == -EMSGSIZE)
  1383. goto error;
  1384. return genlmsg_end(skb, ovs_header);
  1385. nla_put_failure:
  1386. err = -EMSGSIZE;
  1387. error:
  1388. genlmsg_cancel(skb, ovs_header);
  1389. return err;
  1390. }
  1391. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1392. {
  1393. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1394. }
  1395. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1396. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1397. u32 seq, u8 cmd)
  1398. {
  1399. struct sk_buff *skb;
  1400. int retval;
  1401. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1402. if (!skb)
  1403. return ERR_PTR(-ENOMEM);
  1404. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1405. BUG_ON(retval < 0);
  1406. return skb;
  1407. }
  1408. /* Called with ovs_mutex or RCU read lock. */
  1409. static struct vport *lookup_vport(struct net *net,
  1410. struct ovs_header *ovs_header,
  1411. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1412. {
  1413. struct datapath *dp;
  1414. struct vport *vport;
  1415. if (a[OVS_VPORT_ATTR_NAME]) {
  1416. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1417. if (!vport)
  1418. return ERR_PTR(-ENODEV);
  1419. if (ovs_header->dp_ifindex &&
  1420. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1421. return ERR_PTR(-ENODEV);
  1422. return vport;
  1423. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1424. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1425. if (port_no >= DP_MAX_PORTS)
  1426. return ERR_PTR(-EFBIG);
  1427. dp = get_dp(net, ovs_header->dp_ifindex);
  1428. if (!dp)
  1429. return ERR_PTR(-ENODEV);
  1430. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1431. if (!vport)
  1432. return ERR_PTR(-ENODEV);
  1433. return vport;
  1434. } else
  1435. return ERR_PTR(-EINVAL);
  1436. }
  1437. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1438. {
  1439. struct nlattr **a = info->attrs;
  1440. struct ovs_header *ovs_header = info->userhdr;
  1441. struct vport_parms parms;
  1442. struct sk_buff *reply;
  1443. struct vport *vport;
  1444. struct datapath *dp;
  1445. u32 port_no;
  1446. int err;
  1447. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1448. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1449. return -EINVAL;
  1450. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1451. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1452. if (port_no >= DP_MAX_PORTS)
  1453. return -EFBIG;
  1454. reply = ovs_vport_cmd_alloc_info();
  1455. if (!reply)
  1456. return -ENOMEM;
  1457. ovs_lock();
  1458. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1459. err = -ENODEV;
  1460. if (!dp)
  1461. goto exit_unlock_free;
  1462. if (port_no) {
  1463. vport = ovs_vport_ovsl(dp, port_no);
  1464. err = -EBUSY;
  1465. if (vport)
  1466. goto exit_unlock_free;
  1467. } else {
  1468. for (port_no = 1; ; port_no++) {
  1469. if (port_no >= DP_MAX_PORTS) {
  1470. err = -EFBIG;
  1471. goto exit_unlock_free;
  1472. }
  1473. vport = ovs_vport_ovsl(dp, port_no);
  1474. if (!vport)
  1475. break;
  1476. }
  1477. }
  1478. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1479. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1480. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1481. parms.dp = dp;
  1482. parms.port_no = port_no;
  1483. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1484. vport = new_vport(&parms);
  1485. err = PTR_ERR(vport);
  1486. if (IS_ERR(vport))
  1487. goto exit_unlock_free;
  1488. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1489. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1490. BUG_ON(err < 0);
  1491. ovs_unlock();
  1492. ovs_notify(&dp_vport_genl_family, reply, info);
  1493. return 0;
  1494. exit_unlock_free:
  1495. ovs_unlock();
  1496. kfree_skb(reply);
  1497. return err;
  1498. }
  1499. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1500. {
  1501. struct nlattr **a = info->attrs;
  1502. struct sk_buff *reply;
  1503. struct vport *vport;
  1504. int err;
  1505. reply = ovs_vport_cmd_alloc_info();
  1506. if (!reply)
  1507. return -ENOMEM;
  1508. ovs_lock();
  1509. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1510. err = PTR_ERR(vport);
  1511. if (IS_ERR(vport))
  1512. goto exit_unlock_free;
  1513. if (a[OVS_VPORT_ATTR_TYPE] &&
  1514. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1515. err = -EINVAL;
  1516. goto exit_unlock_free;
  1517. }
  1518. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1519. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1520. if (err)
  1521. goto exit_unlock_free;
  1522. }
  1523. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1524. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1525. err = ovs_vport_set_upcall_portids(vport, ids);
  1526. if (err)
  1527. goto exit_unlock_free;
  1528. }
  1529. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1530. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1531. BUG_ON(err < 0);
  1532. ovs_unlock();
  1533. ovs_notify(&dp_vport_genl_family, reply, info);
  1534. return 0;
  1535. exit_unlock_free:
  1536. ovs_unlock();
  1537. kfree_skb(reply);
  1538. return err;
  1539. }
  1540. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1541. {
  1542. struct nlattr **a = info->attrs;
  1543. struct sk_buff *reply;
  1544. struct vport *vport;
  1545. int err;
  1546. reply = ovs_vport_cmd_alloc_info();
  1547. if (!reply)
  1548. return -ENOMEM;
  1549. ovs_lock();
  1550. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1551. err = PTR_ERR(vport);
  1552. if (IS_ERR(vport))
  1553. goto exit_unlock_free;
  1554. if (vport->port_no == OVSP_LOCAL) {
  1555. err = -EINVAL;
  1556. goto exit_unlock_free;
  1557. }
  1558. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1559. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1560. BUG_ON(err < 0);
  1561. ovs_dp_detach_port(vport);
  1562. ovs_unlock();
  1563. ovs_notify(&dp_vport_genl_family, reply, info);
  1564. return 0;
  1565. exit_unlock_free:
  1566. ovs_unlock();
  1567. kfree_skb(reply);
  1568. return err;
  1569. }
  1570. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1571. {
  1572. struct nlattr **a = info->attrs;
  1573. struct ovs_header *ovs_header = info->userhdr;
  1574. struct sk_buff *reply;
  1575. struct vport *vport;
  1576. int err;
  1577. reply = ovs_vport_cmd_alloc_info();
  1578. if (!reply)
  1579. return -ENOMEM;
  1580. rcu_read_lock();
  1581. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1582. err = PTR_ERR(vport);
  1583. if (IS_ERR(vport))
  1584. goto exit_unlock_free;
  1585. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1586. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1587. BUG_ON(err < 0);
  1588. rcu_read_unlock();
  1589. return genlmsg_reply(reply, info);
  1590. exit_unlock_free:
  1591. rcu_read_unlock();
  1592. kfree_skb(reply);
  1593. return err;
  1594. }
  1595. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1596. {
  1597. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1598. struct datapath *dp;
  1599. int bucket = cb->args[0], skip = cb->args[1];
  1600. int i, j = 0;
  1601. rcu_read_lock();
  1602. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1603. if (!dp) {
  1604. rcu_read_unlock();
  1605. return -ENODEV;
  1606. }
  1607. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1608. struct vport *vport;
  1609. j = 0;
  1610. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1611. if (j >= skip &&
  1612. ovs_vport_cmd_fill_info(vport, skb,
  1613. NETLINK_CB(cb->skb).portid,
  1614. cb->nlh->nlmsg_seq,
  1615. NLM_F_MULTI,
  1616. OVS_VPORT_CMD_NEW) < 0)
  1617. goto out;
  1618. j++;
  1619. }
  1620. skip = 0;
  1621. }
  1622. out:
  1623. rcu_read_unlock();
  1624. cb->args[0] = i;
  1625. cb->args[1] = j;
  1626. return skb->len;
  1627. }
  1628. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1629. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1630. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1631. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1632. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1633. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1634. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1635. };
  1636. static const struct genl_ops dp_vport_genl_ops[] = {
  1637. { .cmd = OVS_VPORT_CMD_NEW,
  1638. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1639. .policy = vport_policy,
  1640. .doit = ovs_vport_cmd_new
  1641. },
  1642. { .cmd = OVS_VPORT_CMD_DEL,
  1643. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1644. .policy = vport_policy,
  1645. .doit = ovs_vport_cmd_del
  1646. },
  1647. { .cmd = OVS_VPORT_CMD_GET,
  1648. .flags = 0, /* OK for unprivileged users. */
  1649. .policy = vport_policy,
  1650. .doit = ovs_vport_cmd_get,
  1651. .dumpit = ovs_vport_cmd_dump
  1652. },
  1653. { .cmd = OVS_VPORT_CMD_SET,
  1654. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1655. .policy = vport_policy,
  1656. .doit = ovs_vport_cmd_set,
  1657. },
  1658. };
  1659. struct genl_family dp_vport_genl_family = {
  1660. .id = GENL_ID_GENERATE,
  1661. .hdrsize = sizeof(struct ovs_header),
  1662. .name = OVS_VPORT_FAMILY,
  1663. .version = OVS_VPORT_VERSION,
  1664. .maxattr = OVS_VPORT_ATTR_MAX,
  1665. .netnsok = true,
  1666. .parallel_ops = true,
  1667. .ops = dp_vport_genl_ops,
  1668. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1669. .mcgrps = &ovs_dp_vport_multicast_group,
  1670. .n_mcgrps = 1,
  1671. };
  1672. static struct genl_family * const dp_genl_families[] = {
  1673. &dp_datapath_genl_family,
  1674. &dp_vport_genl_family,
  1675. &dp_flow_genl_family,
  1676. &dp_packet_genl_family,
  1677. };
  1678. static void dp_unregister_genl(int n_families)
  1679. {
  1680. int i;
  1681. for (i = 0; i < n_families; i++)
  1682. genl_unregister_family(dp_genl_families[i]);
  1683. }
  1684. static int dp_register_genl(void)
  1685. {
  1686. int err;
  1687. int i;
  1688. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1689. err = genl_register_family(dp_genl_families[i]);
  1690. if (err)
  1691. goto error;
  1692. }
  1693. return 0;
  1694. error:
  1695. dp_unregister_genl(i);
  1696. return err;
  1697. }
  1698. static int __net_init ovs_init_net(struct net *net)
  1699. {
  1700. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1701. INIT_LIST_HEAD(&ovs_net->dps);
  1702. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1703. return 0;
  1704. }
  1705. static void __net_exit ovs_exit_net(struct net *net)
  1706. {
  1707. struct datapath *dp, *dp_next;
  1708. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1709. ovs_lock();
  1710. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1711. __dp_destroy(dp);
  1712. ovs_unlock();
  1713. cancel_work_sync(&ovs_net->dp_notify_work);
  1714. }
  1715. static struct pernet_operations ovs_net_ops = {
  1716. .init = ovs_init_net,
  1717. .exit = ovs_exit_net,
  1718. .id = &ovs_net_id,
  1719. .size = sizeof(struct ovs_net),
  1720. };
  1721. static int __init dp_init(void)
  1722. {
  1723. int err;
  1724. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1725. pr_info("Open vSwitch switching datapath\n");
  1726. err = ovs_internal_dev_rtnl_link_register();
  1727. if (err)
  1728. goto error;
  1729. err = ovs_flow_init();
  1730. if (err)
  1731. goto error_unreg_rtnl_link;
  1732. err = ovs_vport_init();
  1733. if (err)
  1734. goto error_flow_exit;
  1735. err = register_pernet_device(&ovs_net_ops);
  1736. if (err)
  1737. goto error_vport_exit;
  1738. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1739. if (err)
  1740. goto error_netns_exit;
  1741. err = dp_register_genl();
  1742. if (err < 0)
  1743. goto error_unreg_notifier;
  1744. return 0;
  1745. error_unreg_notifier:
  1746. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1747. error_netns_exit:
  1748. unregister_pernet_device(&ovs_net_ops);
  1749. error_vport_exit:
  1750. ovs_vport_exit();
  1751. error_flow_exit:
  1752. ovs_flow_exit();
  1753. error_unreg_rtnl_link:
  1754. ovs_internal_dev_rtnl_link_unregister();
  1755. error:
  1756. return err;
  1757. }
  1758. static void dp_cleanup(void)
  1759. {
  1760. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1761. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1762. unregister_pernet_device(&ovs_net_ops);
  1763. rcu_barrier();
  1764. ovs_vport_exit();
  1765. ovs_flow_exit();
  1766. ovs_internal_dev_rtnl_link_unregister();
  1767. }
  1768. module_init(dp_init);
  1769. module_exit(dp_cleanup);
  1770. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1771. MODULE_LICENSE("GPL");